blob: 0bc9412c656a8e02a9bdaed5130c2dd9e67762d9 [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
Douglas Gregor926df6c2011-06-11 01:09:30 +0000109bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
110 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
151 Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
152 << Overridden->getMethodFamily();
153 }
154
155 return false;
156}
157
John McCallf85e1932011-06-15 23:02:42 +0000158/// \brief Check a method declaration for compatibility with the Objective-C
159/// ARC conventions.
160static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
161 ObjCMethodFamily family = method->getMethodFamily();
162 switch (family) {
163 case OMF_None:
164 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000165 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000166 case OMF_retain:
167 case OMF_release:
168 case OMF_autorelease:
169 case OMF_retainCount:
170 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000171 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000172 return false;
173
174 case OMF_init:
175 // If the method doesn't obey the init rules, don't bother annotating it.
176 if (S.checkInitMethod(method, QualType()))
177 return true;
178
179 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
180 S.Context));
181
182 // Don't add a second copy of this attribute, but otherwise don't
183 // let it be suppressed.
184 if (method->hasAttr<NSReturnsRetainedAttr>())
185 return false;
186 break;
187
188 case OMF_alloc:
189 case OMF_copy:
190 case OMF_mutableCopy:
191 case OMF_new:
192 if (method->hasAttr<NSReturnsRetainedAttr>() ||
193 method->hasAttr<NSReturnsNotRetainedAttr>() ||
194 method->hasAttr<NSReturnsAutoreleasedAttr>())
195 return false;
196 break;
197 }
198
199 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
200 S.Context));
201 return false;
202}
203
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000204static void DiagnoseObjCImplementedDeprecations(Sema &S,
205 NamedDecl *ND,
206 SourceLocation ImplLoc,
207 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000208 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000209 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000210 if (select == 0)
211 S.Diag(ND->getLocation(), diag::note_method_declared_at);
212 else
213 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
214 }
215}
216
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000217/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
218/// pool.
219void Sema::AddAnyMethodToGlobalPool(Decl *D) {
220 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
221
222 // If we don't have a valid method decl, simply return.
223 if (!MDecl)
224 return;
225 if (MDecl->isInstanceMethod())
226 AddInstanceMethodToGlobalPool(MDecl, true);
227 else
228 AddFactoryMethodToGlobalPool(MDecl, true);
229}
230
Steve Naroffebf64432009-02-28 16:59:13 +0000231/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000232/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000233void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000234 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000235 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Steve Naroff394f3f42008-07-25 17:57:26 +0000237 // If we don't have a valid method decl, simply return.
238 if (!MDecl)
239 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000240
Chris Lattner4d391482007-12-12 07:09:47 +0000241 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000242 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000243 PushFunctionScope();
244
Chris Lattner4d391482007-12-12 07:09:47 +0000245 // Create Decl objects for each parameter, entrring them in the scope for
246 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000247
248 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000249 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Daniel Dunbar451318c2008-08-26 06:07:48 +0000251 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
252 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000253
Chris Lattner8123a952008-04-10 02:22:51 +0000254 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000255 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000256 E = MDecl->param_end(); PI != E; ++PI) {
257 ParmVarDecl *Param = (*PI);
258 if (!Param->isInvalidDecl() &&
259 RequireCompleteType(Param->getLocation(), Param->getType(),
260 diag::err_typecheck_decl_incomplete_type))
261 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000262 if ((*PI)->getIdentifier())
263 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000264 }
John McCallf85e1932011-06-15 23:02:42 +0000265
266 // In ARC, disallow definition of retain/release/autorelease/retainCount
267 if (getLangOptions().ObjCAutoRefCount) {
268 switch (MDecl->getMethodFamily()) {
269 case OMF_retain:
270 case OMF_retainCount:
271 case OMF_release:
272 case OMF_autorelease:
273 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
274 << MDecl->getSelector();
275 break;
276
277 case OMF_None:
278 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000279 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000280 case OMF_alloc:
281 case OMF_init:
282 case OMF_mutableCopy:
283 case OMF_copy:
284 case OMF_new:
285 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000286 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000287 break;
288 }
289 }
290
Nico Weber9a1ecf02011-08-22 17:25:57 +0000291 // Warn on deprecated methods under -Wdeprecated-implementations,
292 // and prepare for warning on missing super calls.
293 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000294 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000295 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000296 DiagnoseObjCImplementedDeprecations(*this,
297 dyn_cast<NamedDecl>(IMD),
298 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000299
Nico Weber80cb6e62011-08-28 22:35:17 +0000300 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000301 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
302 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
303 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000304 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000305 ObjCShouldCallSuperDealloc =
306 !Context.getLangOptions().ObjCAutoRefCount &&
307 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000308 ObjCShouldCallSuperFinalize =
309 !Context.getLangOptions().ObjCAutoRefCount &&
310 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000311 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000312 }
Chris Lattner4d391482007-12-12 07:09:47 +0000313}
314
John McCalld226f652010-08-21 09:40:31 +0000315Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000316ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
317 IdentifierInfo *ClassName, SourceLocation ClassLoc,
318 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000319 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000320 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000321 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000322 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner4d391482007-12-12 07:09:47 +0000324 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000325 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000326 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000327
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000328 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000329 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000330 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000333 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
334 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000335 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000336 if (!IDecl->isForwardDecl()) {
337 IDecl->setInvalidDecl();
338 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
339 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000340
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000341 // Return the previous class interface.
342 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000343 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000344 } else {
345 IDecl->setLocation(AtInterfaceLoc);
346 IDecl->setForwardDecl(false);
347 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000348 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000349 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000350 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000351
352 // Since this ObjCInterfaceDecl was created by a forward declaration,
353 // we now add it to the DeclContext since it wasn't added before
354 // (see ActOnForwardClassDeclaration).
355 IDecl->setLexicalDeclContext(CurContext);
356 CurContext->addDecl(IDecl);
357
358 if (AttrList)
359 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000360 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000361 } else {
362 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
363 ClassName, ClassLoc);
364 if (AttrList)
365 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
366
367 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner4d391482007-12-12 07:09:47 +0000370 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000371 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000372 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
373 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000374
375 if (!PrevDecl) {
376 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000377 TypoCorrection Corrected = CorrectTypo(
378 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
379 NULL, NULL, false, CTC_NoKeywords);
380 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000381 Diag(SuperLoc, diag::err_undef_superclass_suggest)
382 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000383 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
384 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000385 }
386 }
387
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000388 if (PrevDecl == IDecl) {
389 Diag(SuperLoc, diag::err_recursive_superclass)
390 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
391 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000392 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000393 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000394 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000395
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000396 // Diagnose classes that inherit from deprecated classes.
397 if (SuperClassDecl)
398 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000400 if (PrevDecl && SuperClassDecl == 0) {
401 // The previous declaration was not a class decl. Check if we have a
402 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000403 if (const TypedefNameDecl *TDecl =
404 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000405 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000406 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000407 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
408 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000409 }
410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000412 // This handles the following case:
413 //
414 // typedef int SuperClass;
415 // @interface MyClass : SuperClass {} @end
416 //
417 if (!SuperClassDecl) {
418 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
419 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000420 }
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Richard Smith162e1c12011-04-15 14:24:37 +0000423 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000424 if (!SuperClassDecl)
425 Diag(SuperLoc, diag::err_undef_superclass)
426 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000427 else if (SuperClassDecl->isForwardDecl()) {
428 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000429 << SuperClassDecl->getDeclName() << ClassName
430 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000431 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
432 SuperClassDecl = 0;
433 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000434 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000435 IDecl->setSuperClass(SuperClassDecl);
436 IDecl->setSuperClassLoc(SuperLoc);
437 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000438 }
Chris Lattner4d391482007-12-12 07:09:47 +0000439 } else { // we have a root class.
440 IDecl->setLocEnd(ClassLoc);
441 }
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Sebastian Redl0b17c612010-08-13 00:28:03 +0000443 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000444 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000445 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000446 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000447 IDecl->setLocEnd(EndProtoLoc);
448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Anders Carlsson15281452008-11-04 16:57:32 +0000450 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000451 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000452}
453
454/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000455/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000456Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
457 IdentifierInfo *AliasName,
458 SourceLocation AliasLocation,
459 IdentifierInfo *ClassName,
460 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000461 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000462 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000463 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000464 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000465 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000466 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000467 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000468 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000469 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000470 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000471 }
472 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000473 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000474 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000475 if (const TypedefNameDecl *TDecl =
476 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000477 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000478 if (T->isObjCObjectType()) {
479 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000480 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000481 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000482 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000483 }
484 }
485 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000486 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
487 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000488 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000489 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000490 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000491 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000494 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000495 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000496 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Anders Carlsson15281452008-11-04 16:57:32 +0000498 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000499 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000500
John McCalld226f652010-08-21 09:40:31 +0000501 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000502}
503
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000504bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000505 IdentifierInfo *PName,
506 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000507 const ObjCList<ObjCProtocolDecl> &PList) {
508
509 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000510 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
511 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000512 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
513 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000514 if (PDecl->getIdentifier() == PName) {
515 Diag(Ploc, diag::err_protocol_has_circular_dependency);
516 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000517 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000518 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000519 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
520 PDecl->getLocation(), PDecl->getReferencedProtocols()))
521 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000522 }
523 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000524 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000525}
526
John McCalld226f652010-08-21 09:40:31 +0000527Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000528Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
529 IdentifierInfo *ProtocolName,
530 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000531 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000532 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000533 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000534 SourceLocation EndProtoLoc,
535 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000536 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000537 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000538 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000539 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000540 if (PDecl) {
541 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000542 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000543 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000544 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000545 // Just return the protocol we already had.
546 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000547 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000548 }
Steve Naroff61d68522009-03-05 15:22:01 +0000549 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000550 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000551 err = CheckForwardProtocolDeclarationForCircularDependency(
552 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Steve Narofff11b5082008-08-13 16:39:22 +0000554 // Make sure the cached decl gets a valid start location.
555 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000556 PDecl->setForwardDecl(false);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000557 // Since this ObjCProtocolDecl was created by a forward declaration,
558 // we now add it to the DeclContext since it wasn't added before
559 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000560 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000561 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000562 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000563 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000564 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000565 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000566 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000567 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000568 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000569 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000570 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000571 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000572 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000573 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
574 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000575 PDecl->setLocEnd(EndProtoLoc);
576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
578 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000579 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000580}
581
582/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000583/// issues an error if they are not declared. It returns list of
584/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000585void
Chris Lattnere13b9592008-07-26 04:03:38 +0000586Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000587 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000588 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000589 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000590 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000591 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
592 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000593 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000594 TypoCorrection Corrected = CorrectTypo(
595 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
596 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
597 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000598 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000599 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000600 Diag(PDecl->getLocation(), diag::note_previous_decl)
601 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000602 }
603 }
604
605 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000606 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000607 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000608 continue;
609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000611 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000612
613 // If this is a forward declaration and we are supposed to warn in this
614 // case, do it.
615 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000616 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000617 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000618 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000619 }
620}
621
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000622/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000623/// a class method in its extension.
624///
Mike Stump1eb44332009-09-09 15:08:12 +0000625void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000626 ObjCInterfaceDecl *ID) {
627 if (!ID)
628 return; // Possibly due to previous error
629
630 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000631 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
632 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000633 ObjCMethodDecl *MD = *i;
634 MethodMap[MD->getSelector()] = MD;
635 }
636
637 if (MethodMap.empty())
638 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000639 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
640 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000641 ObjCMethodDecl *Method = *i;
642 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
643 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
644 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
645 << Method->getDeclName();
646 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
647 }
648 }
649}
650
Chris Lattner58fe03b2009-04-12 08:43:13 +0000651/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000652Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000653Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000654 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000655 unsigned NumElts,
656 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000657 SmallVector<ObjCProtocolDecl*, 32> Protocols;
658 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Chris Lattner4d391482007-12-12 07:09:47 +0000660 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000661 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000662 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000663 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000664 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000665 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000666 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000667 PushOnScopeChains(PDecl, TUScope, false);
668 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000669 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000670 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000671 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000672 if (!isNew)
673 PDecl->setChangedSinceDeserialization(true);
674 }
Chris Lattner4d391482007-12-12 07:09:47 +0000675 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000676 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000677 }
Mike Stump1eb44332009-09-09 15:08:12 +0000678
679 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000680 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000681 Protocols.data(), Protocols.size(),
682 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000683 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000684 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000685 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000686}
687
John McCalld226f652010-08-21 09:40:31 +0000688Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000689ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
690 IdentifierInfo *ClassName, SourceLocation ClassLoc,
691 IdentifierInfo *CategoryName,
692 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000693 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000694 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000695 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000696 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000697 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000698 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000699
700 /// Check that class of this category is already completely declared.
701 if (!IDecl || IDecl->isForwardDecl()) {
702 // Create an invalid ObjCCategoryDecl to serve as context for
703 // the enclosing method declarations. We mark the decl invalid
704 // to make it clear that this isn't a valid AST.
705 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000706 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000707 CDecl->setInvalidDecl();
708 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000709 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000710 }
711
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000712 if (!CategoryName && IDecl->getImplementation()) {
713 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
714 Diag(IDecl->getImplementation()->getLocation(),
715 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000716 }
717
Fariborz Jahanian25760612010-02-15 21:55:26 +0000718 if (CategoryName) {
719 /// Check for duplicate interface declaration for this category
720 ObjCCategoryDecl *CDeclChain;
721 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
722 CDeclChain = CDeclChain->getNextClassCategory()) {
723 if (CDeclChain->getIdentifier() == CategoryName) {
724 // Class extensions can be declared multiple times.
725 Diag(CategoryLoc, diag::warn_dup_category_def)
726 << ClassName << CategoryName;
727 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
728 break;
729 }
Chris Lattner70f19542009-02-16 21:26:43 +0000730 }
731 }
Chris Lattner70f19542009-02-16 21:26:43 +0000732
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000733 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
734 ClassLoc, CategoryLoc, CategoryName, IDecl);
735 // FIXME: PushOnScopeChains?
736 CurContext->addDecl(CDecl);
737
738 // If the interface is deprecated, warn about it.
739 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
740
Chris Lattner4d391482007-12-12 07:09:47 +0000741 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000742 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000743 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000744 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000745 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000746 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000747 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Anders Carlsson15281452008-11-04 16:57:32 +0000750 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000751 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000752}
753
754/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000755/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000756/// object.
John McCalld226f652010-08-21 09:40:31 +0000757Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000758 SourceLocation AtCatImplLoc,
759 IdentifierInfo *ClassName, SourceLocation ClassLoc,
760 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000761 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000762 ObjCCategoryDecl *CatIDecl = 0;
763 if (IDecl) {
764 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
765 if (!CatIDecl) {
766 // Category @implementation with no corresponding @interface.
767 // Create and install one.
768 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000769 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000770 CatName, IDecl);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000771 }
772 }
773
Mike Stump1eb44332009-09-09 15:08:12 +0000774 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000775 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
776 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000777 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000778 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000779 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000780 CDecl->setInvalidDecl();
781 }
Chris Lattner4d391482007-12-12 07:09:47 +0000782
Douglas Gregord0434102009-01-09 00:49:46 +0000783 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000784 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000785
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000786 /// Check that CatName, category name, is not used in another implementation.
787 if (CatIDecl) {
788 if (CatIDecl->getImplementation()) {
789 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
790 << CatName;
791 Diag(CatIDecl->getImplementation()->getLocation(),
792 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000793 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000794 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000795 // Warn on implementating category of deprecated class under
796 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000797 DiagnoseObjCImplementedDeprecations(*this,
798 dyn_cast<NamedDecl>(IDecl),
799 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000800 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Anders Carlsson15281452008-11-04 16:57:32 +0000803 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000804 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000805}
806
John McCalld226f652010-08-21 09:40:31 +0000807Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000808 SourceLocation AtClassImplLoc,
809 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000810 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000811 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000812 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000813 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000814 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000815 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
816 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000818 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000819 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000820 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
821 // If this is a forward declaration of an interface, warn.
822 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000823 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000824 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000825 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000826 } else {
827 // We did not find anything with the name ClassName; try to correct for
828 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000829 TypoCorrection Corrected = CorrectTypo(
830 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
831 NULL, NULL, false, CTC_NoKeywords);
832 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000833 // Suggest the (potentially) correct interface name. However, put the
834 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000835 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000836 // provide a code-modification hint or use the typo name for recovery,
837 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000838 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000839 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000840 << ClassName << CorrectedName;
841 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
842 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000843 IDecl = 0;
844 } else {
845 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
846 }
Chris Lattner4d391482007-12-12 07:09:47 +0000847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Chris Lattner4d391482007-12-12 07:09:47 +0000849 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000850 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000851 if (SuperClassname) {
852 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000853 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
854 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000855 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000856 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
857 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000858 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000860 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000861 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000862 Diag(SuperClassLoc, diag::err_undef_superclass)
863 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000864 else if (IDecl && IDecl->getSuperClass() != SDecl) {
865 // This implementation and its interface do not have the same
866 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000867 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000868 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000869 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000870 }
871 }
872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Chris Lattner4d391482007-12-12 07:09:47 +0000874 if (!IDecl) {
875 // Legacy case of @implementation with no corresponding @interface.
876 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000877
Mike Stump390b4cc2009-05-16 07:39:55 +0000878 // FIXME: Do we support attributes on the @implementation? If so we should
879 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000880 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000881 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000882 IDecl->setSuperClass(SDecl);
883 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000884
885 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000886 } else {
887 // Mark the interface as being completed, even if it was just as
888 // @class ....;
889 // declaration; the user cannot reopen it.
890 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
893 ObjCImplementationDecl* IMPDecl =
894 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000895 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Anders Carlsson15281452008-11-04 16:57:32 +0000897 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000898 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Chris Lattner4d391482007-12-12 07:09:47 +0000900 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000901 if (IDecl->getImplementation()) {
902 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000903 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000904 Diag(IDecl->getImplementation()->getLocation(),
905 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000906 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000907 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000908 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000909 // Warn on implementating deprecated class under
910 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000911 DiagnoseObjCImplementedDeprecations(*this,
912 dyn_cast<NamedDecl>(IDecl),
913 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000914 }
John McCalld226f652010-08-21 09:40:31 +0000915 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000916}
917
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
919 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000920 SourceLocation RBrace) {
921 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000922 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000923 if (!IDecl)
924 return;
925 /// Check case of non-existing @interface decl.
926 /// (legacy objective-c @implementation decl without an @interface decl).
927 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000928 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000929 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000930 // Add ivar's to class's DeclContext.
931 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000932 ivars[i]->setLexicalDeclContext(ImpDecl);
933 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000934 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000935 }
936
Chris Lattner4d391482007-12-12 07:09:47 +0000937 return;
938 }
939 // If implementation has empty ivar list, just return.
940 if (numIvars == 0)
941 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Chris Lattner4d391482007-12-12 07:09:47 +0000943 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000944 if (LangOpts.ObjCNonFragileABI2) {
945 if (ImpDecl->getSuperClass())
946 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
947 for (unsigned i = 0; i < numIvars; i++) {
948 ObjCIvarDecl* ImplIvar = ivars[i];
949 if (const ObjCIvarDecl *ClsIvar =
950 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
951 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
952 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
953 continue;
954 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000955 // Instance ivar to Implementation's DeclContext.
956 ImplIvar->setLexicalDeclContext(ImpDecl);
957 IDecl->makeDeclVisibleInContext(ImplIvar, false);
958 ImpDecl->addDecl(ImplIvar);
959 }
960 return;
961 }
Chris Lattner4d391482007-12-12 07:09:47 +0000962 // Check interface's Ivar list against those in the implementation.
963 // names and types must match.
964 //
Chris Lattner4d391482007-12-12 07:09:47 +0000965 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000966 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000967 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
968 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000969 ObjCIvarDecl* ImplIvar = ivars[j++];
970 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000971 assert (ImplIvar && "missing implementation ivar");
972 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Steve Naroffca331292009-03-03 14:49:36 +0000974 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000975 if (Context.getCanonicalType(ImplIvar->getType()) !=
976 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000977 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000978 << ImplIvar->getIdentifier()
979 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000980 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000981 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
982 Expr *ImplBitWidth = ImplIvar->getBitWidth();
983 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000984 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
985 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000986 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
987 << ImplIvar->getIdentifier();
988 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990 }
Steve Naroffca331292009-03-03 14:49:36 +0000991 // Make sure the names are identical.
992 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000993 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000994 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000995 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000996 }
997 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000998 }
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattner609e4c72007-12-12 18:11:49 +00001000 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001001 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001002 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001003 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001004}
1005
Steve Naroff3c2eb662008-02-10 21:38:56 +00001006void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001007 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001008 // No point warning no definition of method which is 'unavailable'.
1009 if (method->hasAttr<UnavailableAttr>())
1010 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001011 if (!IncompleteImpl) {
1012 Diag(ImpLoc, diag::warn_incomplete_impl);
1013 IncompleteImpl = true;
1014 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001015 if (DiagID == diag::warn_unimplemented_protocol_method)
1016 Diag(ImpLoc, DiagID) << method->getDeclName();
1017 else
1018 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001019}
1020
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001021/// Determines if type B can be substituted for type A. Returns true if we can
1022/// guarantee that anything that the user will do to an object of type A can
1023/// also be done to an object of type B. This is trivially true if the two
1024/// types are the same, or if B is a subclass of A. It becomes more complex
1025/// in cases where protocols are involved.
1026///
1027/// Object types in Objective-C describe the minimum requirements for an
1028/// object, rather than providing a complete description of a type. For
1029/// example, if A is a subclass of B, then B* may refer to an instance of A.
1030/// The principle of substitutability means that we may use an instance of A
1031/// anywhere that we may use an instance of B - it will implement all of the
1032/// ivars of B and all of the methods of B.
1033///
1034/// This substitutability is important when type checking methods, because
1035/// the implementation may have stricter type definitions than the interface.
1036/// The interface specifies minimum requirements, but the implementation may
1037/// have more accurate ones. For example, a method may privately accept
1038/// instances of B, but only publish that it accepts instances of A. Any
1039/// object passed to it will be type checked against B, and so will implicitly
1040/// by a valid A*. Similarly, a method may return a subclass of the class that
1041/// it is declared as returning.
1042///
1043/// This is most important when considering subclassing. A method in a
1044/// subclass must accept any object as an argument that its superclass's
1045/// implementation accepts. It may, however, accept a more general type
1046/// without breaking substitutability (i.e. you can still use the subclass
1047/// anywhere that you can use the superclass, but not vice versa). The
1048/// converse requirement applies to return types: the return type for a
1049/// subclass method must be a valid object of the kind that the superclass
1050/// advertises, but it may be specified more accurately. This avoids the need
1051/// for explicit down-casting by callers.
1052///
1053/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001054static bool isObjCTypeSubstitutable(ASTContext &Context,
1055 const ObjCObjectPointerType *A,
1056 const ObjCObjectPointerType *B,
1057 bool rejectId) {
1058 // Reject a protocol-unqualified id.
1059 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001060
1061 // If B is a qualified id, then A must also be a qualified id and it must
1062 // implement all of the protocols in B. It may not be a qualified class.
1063 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1064 // stricter definition so it is not substitutable for id<A>.
1065 if (B->isObjCQualifiedIdType()) {
1066 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001067 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1068 QualType(B,0),
1069 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001070 }
1071
1072 /*
1073 // id is a special type that bypasses type checking completely. We want a
1074 // warning when it is used in one place but not another.
1075 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1076
1077
1078 // If B is a qualified id, then A must also be a qualified id (which it isn't
1079 // if we've got this far)
1080 if (B->isObjCQualifiedIdType()) return false;
1081 */
1082
1083 // Now we know that A and B are (potentially-qualified) class types. The
1084 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001085 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001086}
1087
John McCall10302c02010-10-28 02:34:38 +00001088static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1089 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1090}
1091
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001092static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001093 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001094 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001095 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001096 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001097 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001098 if (IsProtocolMethodDecl &&
1099 (MethodDecl->getObjCDeclQualifier() !=
1100 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001101 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001102 S.Diag(MethodImpl->getLocation(),
1103 (IsOverridingMode ?
1104 diag::warn_conflicting_overriding_ret_type_modifiers
1105 : diag::warn_conflicting_ret_type_modifiers))
1106 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001107 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1108 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1109 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1110 }
1111 else
1112 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001113 }
1114
John McCall10302c02010-10-28 02:34:38 +00001115 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001116 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001117 return true;
1118 if (!Warn)
1119 return false;
John McCall10302c02010-10-28 02:34:38 +00001120
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001121 unsigned DiagID =
1122 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1123 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001124
1125 // Mismatches between ObjC pointers go into a different warning
1126 // category, and sometimes they're even completely whitelisted.
1127 if (const ObjCObjectPointerType *ImplPtrTy =
1128 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1129 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001130 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001131 // Allow non-matching return types as long as they don't violate
1132 // the principle of substitutability. Specifically, we permit
1133 // return types that are subclasses of the declared return type,
1134 // or that are more-qualified versions of the declared type.
1135 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001136 return false;
John McCall10302c02010-10-28 02:34:38 +00001137
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001138 DiagID =
1139 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1140 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001141 }
1142 }
1143
1144 S.Diag(MethodImpl->getLocation(), DiagID)
1145 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001146 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001147 << MethodImpl->getResultType()
1148 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001149 S.Diag(MethodDecl->getLocation(),
1150 IsOverridingMode ? diag::note_previous_declaration
1151 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001152 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001153 return false;
John McCall10302c02010-10-28 02:34:38 +00001154}
1155
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001156static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001157 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001158 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001159 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001160 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001161 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001162 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001163 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001164 if (IsProtocolMethodDecl &&
1165 (ImplVar->getObjCDeclQualifier() !=
1166 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001167 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001168 if (IsOverridingMode)
1169 S.Diag(ImplVar->getLocation(),
1170 diag::warn_conflicting_overriding_param_modifiers)
1171 << getTypeRange(ImplVar->getTypeSourceInfo())
1172 << MethodImpl->getDeclName();
1173 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001174 diag::warn_conflicting_param_modifiers)
1175 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001176 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001177 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1178 << getTypeRange(IfaceVar->getTypeSourceInfo());
1179 }
1180 else
1181 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001182 }
1183
John McCall10302c02010-10-28 02:34:38 +00001184 QualType ImplTy = ImplVar->getType();
1185 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001186
John McCall10302c02010-10-28 02:34:38 +00001187 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001188 return true;
1189
1190 if (!Warn)
1191 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001192 unsigned DiagID =
1193 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1194 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001195
1196 // Mismatches between ObjC pointers go into a different warning
1197 // category, and sometimes they're even completely whitelisted.
1198 if (const ObjCObjectPointerType *ImplPtrTy =
1199 ImplTy->getAs<ObjCObjectPointerType>()) {
1200 if (const ObjCObjectPointerType *IfacePtrTy =
1201 IfaceTy->getAs<ObjCObjectPointerType>()) {
1202 // Allow non-matching argument types as long as they don't
1203 // violate the principle of substitutability. Specifically, the
1204 // implementation must accept any objects that the superclass
1205 // accepts, however it may also accept others.
1206 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001207 return false;
John McCall10302c02010-10-28 02:34:38 +00001208
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001209 DiagID =
1210 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1211 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001212 }
1213 }
1214
1215 S.Diag(ImplVar->getLocation(), DiagID)
1216 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001217 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1218 S.Diag(IfaceVar->getLocation(),
1219 (IsOverridingMode ? diag::note_previous_declaration
1220 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001221 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001222 return false;
John McCall10302c02010-10-28 02:34:38 +00001223}
John McCallf85e1932011-06-15 23:02:42 +00001224
1225/// In ARC, check whether the conventional meanings of the two methods
1226/// match. If they don't, it's a hard error.
1227static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1228 ObjCMethodDecl *decl) {
1229 ObjCMethodFamily implFamily = impl->getMethodFamily();
1230 ObjCMethodFamily declFamily = decl->getMethodFamily();
1231 if (implFamily == declFamily) return false;
1232
1233 // Since conventions are sorted by selector, the only possibility is
1234 // that the types differ enough to cause one selector or the other
1235 // to fall out of the family.
1236 assert(implFamily == OMF_None || declFamily == OMF_None);
1237
1238 // No further diagnostics required on invalid declarations.
1239 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1240
1241 const ObjCMethodDecl *unmatched = impl;
1242 ObjCMethodFamily family = declFamily;
1243 unsigned errorID = diag::err_arc_lost_method_convention;
1244 unsigned noteID = diag::note_arc_lost_method_convention;
1245 if (declFamily == OMF_None) {
1246 unmatched = decl;
1247 family = implFamily;
1248 errorID = diag::err_arc_gained_method_convention;
1249 noteID = diag::note_arc_gained_method_convention;
1250 }
1251
1252 // Indexes into a %select clause in the diagnostic.
1253 enum FamilySelector {
1254 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1255 };
1256 FamilySelector familySelector = FamilySelector();
1257
1258 switch (family) {
1259 case OMF_None: llvm_unreachable("logic error, no method convention");
1260 case OMF_retain:
1261 case OMF_release:
1262 case OMF_autorelease:
1263 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001264 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001265 case OMF_retainCount:
1266 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001267 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001268 // Mismatches for these methods don't change ownership
1269 // conventions, so we don't care.
1270 return false;
1271
1272 case OMF_init: familySelector = F_init; break;
1273 case OMF_alloc: familySelector = F_alloc; break;
1274 case OMF_copy: familySelector = F_copy; break;
1275 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1276 case OMF_new: familySelector = F_new; break;
1277 }
1278
1279 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1280 ReasonSelector reasonSelector;
1281
1282 // The only reason these methods don't fall within their families is
1283 // due to unusual result types.
1284 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1285 reasonSelector = R_UnrelatedReturn;
1286 } else {
1287 reasonSelector = R_NonObjectReturn;
1288 }
1289
1290 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1291 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1292
1293 return true;
1294}
John McCall10302c02010-10-28 02:34:38 +00001295
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001296void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001297 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001298 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001299 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001300 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001301 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001302 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1303 return;
1304
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001305 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001306 IsProtocolMethodDecl, IsOverridingMode,
1307 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Chris Lattner3aff9192009-04-11 19:58:42 +00001309 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001310 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001311 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001312 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1313 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001314 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001315
Fariborz Jahanian21121902011-08-08 18:03:17 +00001316 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001317 if (IsOverridingMode)
1318 Diag(ImpMethodDecl->getLocation(),
1319 diag::warn_conflicting_overriding_variadic);
1320 else
1321 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001322 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001323 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001324}
1325
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001326/// WarnExactTypedMethods - This routine issues a warning if method
1327/// implementation declaration matches exactly that of its declaration.
1328void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1329 ObjCMethodDecl *MethodDecl,
1330 bool IsProtocolMethodDecl) {
1331 // don't issue warning when protocol method is optional because primary
1332 // class is not required to implement it and it is safe for protocol
1333 // to implement it.
1334 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1335 return;
1336 // don't issue warning when primary class's method is
1337 // depecated/unavailable.
1338 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1339 MethodDecl->hasAttr<DeprecatedAttr>())
1340 return;
1341
1342 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1343 IsProtocolMethodDecl, false, false);
1344 if (match)
1345 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1346 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1347 IM != EM; ++IM, ++IF) {
1348 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1349 *IM, *IF,
1350 IsProtocolMethodDecl, false, false);
1351 if (!match)
1352 break;
1353 }
1354 if (match)
1355 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001356 if (match)
1357 match = !(MethodDecl->isClassMethod() &&
1358 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001359
1360 if (match) {
1361 Diag(ImpMethodDecl->getLocation(),
1362 diag::warn_category_method_impl_match);
1363 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1364 }
1365}
1366
Mike Stump390b4cc2009-05-16 07:39:55 +00001367/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1368/// improve the efficiency of selector lookups and type checking by associating
1369/// with each protocol / interface / category the flattened instance tables. If
1370/// we used an immutable set to keep the table then it wouldn't add significant
1371/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001372
Steve Naroffefe7f362008-02-08 22:06:17 +00001373/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001374/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001375void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1376 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001377 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001378 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001379 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001380 ObjCContainerDecl *CDecl) {
1381 ObjCInterfaceDecl *IDecl;
1382 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1383 IDecl = C->getClassInterface();
1384 else
1385 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1386 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1387
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001388 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001389 ObjCInterfaceDecl *NSIDecl = 0;
1390 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001391 // check to see if class implements forwardInvocation method and objects
1392 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001393 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001394 // Under such conditions, which means that every method possible is
1395 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001396 // found" warnings.
1397 // FIXME: Use a general GetUnarySelector method for this.
1398 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1399 Selector fISelector = Context.Selectors.getSelector(1, &II);
1400 if (InsMap.count(fISelector))
1401 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1402 // need be implemented in the implementation.
1403 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1404 }
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001406 // If a method lookup fails locally we still need to look and see if
1407 // the method was implemented by a base class or an inherited
1408 // protocol. This lookup is slow, but occurs rarely in correct code
1409 // and otherwise would terminate in a warning.
1410
Chris Lattner4d391482007-12-12 07:09:47 +00001411 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001412 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001413 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001414 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001415 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001416 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001417 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001418 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001419 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001420 // Ugly, but necessary. Method declared in protcol might have
1421 // have been synthesized due to a property declared in the class which
1422 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001423 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001424 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001425 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001426 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001427 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1428 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001429 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001430 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001431 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1432 << PDecl->getDeclName();
1433 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001434 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001435 }
1436 }
Chris Lattner4d391482007-12-12 07:09:47 +00001437 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001438 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001439 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001440 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001442 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1443 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001444 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001445 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001446 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001447 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001448 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001449 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1450 PDecl->getDeclName();
1451 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001452 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001453 }
Chris Lattner780f3292008-07-21 21:32:27 +00001454 // Check on this protocols's referenced protocols, recursively.
1455 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1456 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001457 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001458}
1459
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001460/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001461/// or protocol against those declared in their implementations.
1462///
1463void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1464 const llvm::DenseSet<Selector> &ClsMap,
1465 llvm::DenseSet<Selector> &InsMapSeen,
1466 llvm::DenseSet<Selector> &ClsMapSeen,
1467 ObjCImplDecl* IMPDecl,
1468 ObjCContainerDecl* CDecl,
1469 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001470 bool ImmediateClass,
1471 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001472 // Check and see if instance methods in class interface have been
1473 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001474 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1475 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001476 if (InsMapSeen.count((*I)->getSelector()))
1477 continue;
1478 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001479 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001480 !InsMap.count((*I)->getSelector())) {
1481 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001482 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1483 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001484 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001485 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001486 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001487 IMPDecl->getInstanceMethod((*I)->getSelector());
1488 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1489 "Expected to find the method through lookup as well");
1490 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001491 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001492 if (ImpMethodDecl) {
1493 if (!WarnExactMatch)
1494 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1495 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001496 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001497 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1498 isa<ObjCProtocolDecl>(CDecl));
1499 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001500 }
1501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001503 // Check and see if class methods in class interface have been
1504 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001505 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001506 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001507 if (ClsMapSeen.count((*I)->getSelector()))
1508 continue;
1509 ClsMapSeen.insert((*I)->getSelector());
1510 if (!ClsMap.count((*I)->getSelector())) {
1511 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001512 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1513 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001514 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001515 ObjCMethodDecl *ImpMethodDecl =
1516 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001517 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1518 "Expected to find the method through lookup as well");
1519 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001520 if (!WarnExactMatch)
1521 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1522 isa<ObjCProtocolDecl>(CDecl));
1523 else
1524 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1525 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001526 }
1527 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001528
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001529 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001530 // Also methods in class extensions need be looked at next.
1531 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1532 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1533 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1534 IMPDecl,
1535 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001536 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001537
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001538 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001539 for (ObjCInterfaceDecl::all_protocol_iterator
1540 PI = I->all_referenced_protocol_begin(),
1541 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001542 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1543 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001544 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001545
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001546 // FIXME. For now, we are not checking for extact match of methods
1547 // in category implementation and its primary class's super class.
1548 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001549 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001550 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001551 I->getSuperClass(), IncompleteImpl, false);
1552 }
1553}
1554
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001555/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1556/// category matches with those implemented in its primary class and
1557/// warns each time an exact match is found.
1558void Sema::CheckCategoryVsClassMethodMatches(
1559 ObjCCategoryImplDecl *CatIMPDecl) {
1560 llvm::DenseSet<Selector> InsMap, ClsMap;
1561
1562 for (ObjCImplementationDecl::instmeth_iterator
1563 I = CatIMPDecl->instmeth_begin(),
1564 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1565 InsMap.insert((*I)->getSelector());
1566
1567 for (ObjCImplementationDecl::classmeth_iterator
1568 I = CatIMPDecl->classmeth_begin(),
1569 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1570 ClsMap.insert((*I)->getSelector());
1571 if (InsMap.empty() && ClsMap.empty())
1572 return;
1573
1574 // Get category's primary class.
1575 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1576 if (!CatDecl)
1577 return;
1578 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1579 if (!IDecl)
1580 return;
1581 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1582 bool IncompleteImpl = false;
1583 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1584 CatIMPDecl, IDecl,
1585 IncompleteImpl, false, true /*WarnExactMatch*/);
1586}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001587
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001588void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001589 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001590 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001591 llvm::DenseSet<Selector> InsMap;
1592 // Check and see if instance methods in class interface have been
1593 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001594 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001595 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001596 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001598 // Check and see if properties declared in the interface have either 1)
1599 // an implementation or 2) there is a @synthesize/@dynamic implementation
1600 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001601 if (isa<ObjCInterfaceDecl>(CDecl) &&
1602 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001603 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001604
Chris Lattner4d391482007-12-12 07:09:47 +00001605 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001606 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001607 I = IMPDecl->classmeth_begin(),
1608 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001609 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001611 // Check for type conflict of methods declared in a class/protocol and
1612 // its implementation; if any.
1613 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001614 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1615 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001616 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001617
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001618 // check all methods implemented in category against those declared
1619 // in its primary class.
1620 if (ObjCCategoryImplDecl *CatDecl =
1621 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1622 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Chris Lattner4d391482007-12-12 07:09:47 +00001624 // Check the protocol list for unimplemented methods in the @implementation
1625 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001626 // Check and see if class methods in class interface have been
1627 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Chris Lattnercddc8882009-03-01 00:56:52 +00001629 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001630 for (ObjCInterfaceDecl::all_protocol_iterator
1631 PI = I->all_referenced_protocol_begin(),
1632 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001633 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001634 InsMap, ClsMap, I);
1635 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001636 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1637 Categories; Categories = Categories->getNextClassExtension())
1638 ImplMethodsVsClassMethods(S, IMPDecl,
1639 const_cast<ObjCCategoryDecl*>(Categories),
1640 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001641 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001642 // For extended class, unimplemented methods in its protocols will
1643 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001644 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001645 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1646 E = C->protocol_end(); PI != E; ++PI)
1647 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001648 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001649 // Report unimplemented properties in the category as well.
1650 // When reporting on missing setter/getters, do not report when
1651 // setter/getter is implemented in category's primary class
1652 // implementation.
1653 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1654 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1655 for (ObjCImplementationDecl::instmeth_iterator
1656 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1657 InsMap.insert((*I)->getSelector());
1658 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001659 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001660 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001661 } else
1662 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001663}
1664
Mike Stump1eb44332009-09-09 15:08:12 +00001665/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001666Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001667Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001668 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001669 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001670 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001671 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001672 for (unsigned i = 0; i != NumElts; ++i) {
1673 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001674 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001675 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001676 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001677 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001678 // Maybe we will complain about the shadowed template parameter.
1679 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1680 // Just pretend that we didn't see the previous declaration.
1681 PrevDecl = 0;
1682 }
1683
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001684 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001685 // GCC apparently allows the following idiom:
1686 //
1687 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1688 // @class XCElementToggler;
1689 //
Mike Stump1eb44332009-09-09 15:08:12 +00001690 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001691 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001692 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001693 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001694 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001695 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001696 // a forward class declaration matching a typedef name of a class refers
1697 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001698 if (const ObjCObjectType *OI =
1699 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1700 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001701 }
Chris Lattner4d391482007-12-12 07:09:47 +00001702 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001703 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1704 if (!IDecl) { // Not already seen? Make a forward decl.
1705 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1706 IdentList[i], IdentLocs[i], true);
1707
1708 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1709 // the current DeclContext. This prevents clients that walk DeclContext
1710 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1711 // declared later (if at all). We also take care to explicitly make
1712 // sure this declaration is visible for name lookup.
1713 PushOnScopeChains(IDecl, TUScope, false);
1714 CurContext->makeDeclVisibleInContext(IDecl, true);
1715 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001716 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1717 IDecl, IdentLocs[i]);
1718 CurContext->addDecl(CDecl);
1719 CheckObjCDeclScope(CDecl);
1720 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001721 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001722
1723 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001724}
1725
John McCall0f4c4c42011-06-16 01:15:19 +00001726static bool tryMatchRecordTypes(ASTContext &Context,
1727 Sema::MethodMatchStrategy strategy,
1728 const Type *left, const Type *right);
1729
John McCallf85e1932011-06-15 23:02:42 +00001730static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1731 QualType leftQT, QualType rightQT) {
1732 const Type *left =
1733 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1734 const Type *right =
1735 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1736
1737 if (left == right) return true;
1738
1739 // If we're doing a strict match, the types have to match exactly.
1740 if (strategy == Sema::MMS_strict) return false;
1741
1742 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1743
1744 // Otherwise, use this absurdly complicated algorithm to try to
1745 // validate the basic, low-level compatibility of the two types.
1746
1747 // As a minimum, require the sizes and alignments to match.
1748 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1749 return false;
1750
1751 // Consider all the kinds of non-dependent canonical types:
1752 // - functions and arrays aren't possible as return and parameter types
1753
1754 // - vector types of equal size can be arbitrarily mixed
1755 if (isa<VectorType>(left)) return isa<VectorType>(right);
1756 if (isa<VectorType>(right)) return false;
1757
1758 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001759 // - structs, unions, and Objective-C objects must match more-or-less
1760 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001761 // - everything else should be a scalar
1762 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001763 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001764
1765 // Make scalars agree in kind, except count bools as chars.
1766 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1767 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1768 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1769 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1770
1771 // Note that data member pointers and function member pointers don't
1772 // intermix because of the size differences.
1773
1774 return (leftSK == rightSK);
1775}
Chris Lattner4d391482007-12-12 07:09:47 +00001776
John McCall0f4c4c42011-06-16 01:15:19 +00001777static bool tryMatchRecordTypes(ASTContext &Context,
1778 Sema::MethodMatchStrategy strategy,
1779 const Type *lt, const Type *rt) {
1780 assert(lt && rt && lt != rt);
1781
1782 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1783 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1784 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1785
1786 // Require union-hood to match.
1787 if (left->isUnion() != right->isUnion()) return false;
1788
1789 // Require an exact match if either is non-POD.
1790 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1791 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1792 return false;
1793
1794 // Require size and alignment to match.
1795 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1796
1797 // Require fields to match.
1798 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1799 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1800 for (; li != le && ri != re; ++li, ++ri) {
1801 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1802 return false;
1803 }
1804 return (li == le && ri == re);
1805}
1806
Chris Lattner4d391482007-12-12 07:09:47 +00001807/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1808/// returns true, or false, accordingly.
1809/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001810bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1811 const ObjCMethodDecl *right,
1812 MethodMatchStrategy strategy) {
1813 if (!matchTypes(Context, strategy,
1814 left->getResultType(), right->getResultType()))
1815 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001816
John McCallf85e1932011-06-15 23:02:42 +00001817 if (getLangOptions().ObjCAutoRefCount &&
1818 (left->hasAttr<NSReturnsRetainedAttr>()
1819 != right->hasAttr<NSReturnsRetainedAttr>() ||
1820 left->hasAttr<NSConsumesSelfAttr>()
1821 != right->hasAttr<NSConsumesSelfAttr>()))
1822 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001823
John McCallf85e1932011-06-15 23:02:42 +00001824 ObjCMethodDecl::param_iterator
1825 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001826
John McCallf85e1932011-06-15 23:02:42 +00001827 for (; li != le; ++li, ++ri) {
1828 assert(ri != right->param_end() && "Param mismatch");
1829 ParmVarDecl *lparm = *li, *rparm = *ri;
1830
1831 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1832 return false;
1833
1834 if (getLangOptions().ObjCAutoRefCount &&
1835 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1836 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001837 }
1838 return true;
1839}
1840
Sebastian Redldb9d2142010-08-02 23:18:59 +00001841/// \brief Read the contents of the method pool for a given selector from
1842/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001843///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001844/// This routine should only be called once, when the method pool has no entry
1845/// for this selector.
1846Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001847 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001848 assert(MethodPool.find(Sel) == MethodPool.end() &&
1849 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001850
1851 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001852 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Sebastian Redldb9d2142010-08-02 23:18:59 +00001854 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001855}
1856
Sebastian Redldb9d2142010-08-02 23:18:59 +00001857void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1858 bool instance) {
1859 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1860 if (Pos == MethodPool.end()) {
1861 if (ExternalSource)
1862 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001863 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001864 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1865 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001866 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001867 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001868 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001869 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001870 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001871 Entry.Method = Method;
1872 Entry.Next = 0;
1873 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Chris Lattnerb25df352009-03-04 05:16:45 +00001876 // We've seen a method with this name, see if we have already seen this type
1877 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001878 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1879 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1880
1881 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001882 ObjCMethodDecl *PrevObjCMethod = List->Method;
1883 PrevObjCMethod->setDefined(impl);
1884 // If a method is deprecated, push it in the global pool.
1885 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001886 if (Method->isDeprecated()) {
1887 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001888 List->Method = Method;
1889 }
1890 // If new method is unavailable, push it into global pool
1891 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001892 if (Method->isUnavailable()) {
1893 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001894 List->Method = Method;
1895 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001896 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001897 }
John McCallf85e1932011-06-15 23:02:42 +00001898 }
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Chris Lattnerb25df352009-03-04 05:16:45 +00001900 // We have a new signature for an existing method - add it.
1901 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001902 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1903 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001904}
1905
John McCallf85e1932011-06-15 23:02:42 +00001906/// Determines if this is an "acceptable" loose mismatch in the global
1907/// method pool. This exists mostly as a hack to get around certain
1908/// global mismatches which we can't afford to make warnings / errors.
1909/// Really, what we want is a way to take a method out of the global
1910/// method pool.
1911static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1912 ObjCMethodDecl *other) {
1913 if (!chosen->isInstanceMethod())
1914 return false;
1915
1916 Selector sel = chosen->getSelector();
1917 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1918 return false;
1919
1920 // Don't complain about mismatches for -length if the method we
1921 // chose has an integral result type.
1922 return (chosen->getResultType()->isIntegerType());
1923}
1924
Sebastian Redldb9d2142010-08-02 23:18:59 +00001925ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001926 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001927 bool warn, bool instance) {
1928 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1929 if (Pos == MethodPool.end()) {
1930 if (ExternalSource)
1931 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001932 else
1933 return 0;
1934 }
1935
Sebastian Redldb9d2142010-08-02 23:18:59 +00001936 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Sebastian Redldb9d2142010-08-02 23:18:59 +00001938 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001939 bool issueDiagnostic = false, issueError = false;
1940
1941 // We support a warning which complains about *any* difference in
1942 // method signature.
1943 bool strictSelectorMatch =
1944 (receiverIdOrClass && warn &&
1945 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1946 R.getBegin()) !=
1947 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001948 if (strictSelectorMatch)
1949 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001950 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1951 MMS_strict)) {
1952 issueDiagnostic = true;
1953 break;
1954 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001955 }
1956
John McCallf85e1932011-06-15 23:02:42 +00001957 // If we didn't see any strict differences, we won't see any loose
1958 // differences. In ARC, however, we also need to check for loose
1959 // mismatches, because most of them are errors.
1960 if (!strictSelectorMatch ||
1961 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001962 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001963 // This checks if the methods differ in type mismatch.
1964 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1965 MMS_loose) &&
1966 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1967 issueDiagnostic = true;
1968 if (getLangOptions().ObjCAutoRefCount)
1969 issueError = true;
1970 break;
1971 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001972 }
1973
John McCallf85e1932011-06-15 23:02:42 +00001974 if (issueDiagnostic) {
1975 if (issueError)
1976 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1977 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001978 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1979 else
1980 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001981
1982 Diag(MethList.Method->getLocStart(),
1983 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001984 << MethList.Method->getSourceRange();
1985 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1986 Diag(Next->Method->getLocStart(), diag::note_also_found)
1987 << Next->Method->getSourceRange();
1988 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001989 }
1990 return MethList.Method;
1991}
1992
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001993ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001994 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1995 if (Pos == MethodPool.end())
1996 return 0;
1997
1998 GlobalMethods &Methods = Pos->second;
1999
2000 if (Methods.first.Method && Methods.first.Method->isDefined())
2001 return Methods.first.Method;
2002 if (Methods.second.Method && Methods.second.Method->isDefined())
2003 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002004 return 0;
2005}
2006
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002007/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2008/// identical selector names in current and its super classes and issues
2009/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002010void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2011 ObjCMethodDecl *Method,
2012 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002013 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2014 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002016 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002017 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002018 SD->lookupMethod(Method->getSelector(), IsInstance);
2019 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002020 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002021 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002022 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002023 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2024 E = Method->param_end();
2025 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2026 for (; ParamI != E; ++ParamI, ++PrevI) {
2027 // Number of parameters are the same and is guaranteed by selector match.
2028 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2029 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2030 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002031 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002032 // respective argument type in the super class method, issue warning;
2033 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002034 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002035 << T1 << T2;
2036 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2037 return;
2038 }
2039 }
2040 ID = SD;
2041 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002042}
2043
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002044/// DiagnoseDuplicateIvars -
2045/// Check for duplicate ivars in the entire class at the start of
2046/// @implementation. This becomes necesssary because class extension can
2047/// add ivars to a class in random order which will not be known until
2048/// class's @implementation is seen.
2049void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2050 ObjCInterfaceDecl *SID) {
2051 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2052 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2053 ObjCIvarDecl* Ivar = (*IVI);
2054 if (Ivar->isInvalidDecl())
2055 continue;
2056 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2057 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2058 if (prevIvar) {
2059 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2060 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2061 Ivar->setInvalidDecl();
2062 }
2063 }
2064 }
2065}
2066
Steve Naroffa56f6162007-12-18 01:30:32 +00002067// Note: For class/category implemenations, allMethods/allProperties is
2068// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002069void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002070 Decl **allMethods, unsigned allNum,
2071 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002072 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002073
2074 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002075 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002076 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2077 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002078
Mike Stump1eb44332009-09-09 15:08:12 +00002079 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002080 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2081 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002082 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002083
Ted Kremenek782f2f52010-01-07 01:20:12 +00002084 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2085 // FIXME: This is wrong. We shouldn't be pretending that there is
2086 // an '@end' in the declaration.
2087 SourceLocation L = ClassDecl->getLocation();
2088 AtEnd.setBegin(L);
2089 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002090 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002091 }
2092
Steve Naroff0701bbb2009-01-08 17:28:14 +00002093 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2094 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2095 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2096
Chris Lattner4d391482007-12-12 07:09:47 +00002097 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002098 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002099 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002100
2101 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002102 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002103 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002104 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002105 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002106 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002107 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002108 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002109 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002110 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002111 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002112 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002113 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002114 InsMap[Method->getSelector()] = Method;
2115 /// The following allows us to typecheck messages to "id".
2116 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002117 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002118 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002119 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002120 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002121 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002122 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002123 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002124 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002125 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002126 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002127 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002128 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002129 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002130 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002131 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002132 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002133 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002134 /// The following allows us to typecheck messages to "Class".
2135 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002136 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002137 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002138 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002139 }
2140 }
2141 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002142 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002143 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002144 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002145 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002146 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002147 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002148 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002149 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002150 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002151
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002152 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002153 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002154 if (C->IsClassExtension()) {
2155 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2156 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002157 }
Chris Lattner4d391482007-12-12 07:09:47 +00002158 }
Steve Naroff09c47192009-01-09 15:36:25 +00002159 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002160 if (CDecl->getIdentifier())
2161 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2162 // user-defined setter/getter. It also synthesizes setter/getter methods
2163 // and adds them to the DeclContext and global method pools.
2164 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2165 E = CDecl->prop_end();
2166 I != E; ++I)
2167 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002168 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002169 }
2170 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002171 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002172 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002173 // Any property declared in a class extension might have user
2174 // declared setter or getter in current class extension or one
2175 // of the other class extensions. Mark them as synthesized as
2176 // property will be synthesized when property with same name is
2177 // seen in the @implementation.
2178 for (const ObjCCategoryDecl *ClsExtDecl =
2179 IDecl->getFirstClassExtension();
2180 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2181 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2182 E = ClsExtDecl->prop_end(); I != E; ++I) {
2183 ObjCPropertyDecl *Property = (*I);
2184 // Skip over properties declared @dynamic
2185 if (const ObjCPropertyImplDecl *PIDecl
2186 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2187 if (PIDecl->getPropertyImplementation()
2188 == ObjCPropertyImplDecl::Dynamic)
2189 continue;
2190
2191 for (const ObjCCategoryDecl *CExtDecl =
2192 IDecl->getFirstClassExtension();
2193 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2194 if (ObjCMethodDecl *GetterMethod =
2195 CExtDecl->getInstanceMethod(Property->getGetterName()))
2196 GetterMethod->setSynthesized(true);
2197 if (!Property->isReadOnly())
2198 if (ObjCMethodDecl *SetterMethod =
2199 CExtDecl->getInstanceMethod(Property->getSetterName()))
2200 SetterMethod->setSynthesized(true);
2201 }
2202 }
2203 }
2204
Ted Kremenekc32647d2010-12-23 21:35:43 +00002205 if (LangOpts.ObjCDefaultSynthProperties &&
2206 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002207 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002208 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002209 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002210 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002211
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002212 if (LangOpts.ObjCNonFragileABI2)
2213 while (IDecl->getSuperClass()) {
2214 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2215 IDecl = IDecl->getSuperClass();
2216 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002217 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002218 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002219 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002220 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002221 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Chris Lattner4d391482007-12-12 07:09:47 +00002223 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002224 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002225 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002226 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002227 Categories; Categories = Categories->getNextClassCategory()) {
2228 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002229 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002230 break;
2231 }
2232 }
2233 }
2234 }
Chris Lattner682bf922009-03-29 16:50:03 +00002235 if (isInterfaceDeclKind) {
2236 // Reject invalid vardecls.
2237 for (unsigned i = 0; i != tuvNum; i++) {
2238 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2239 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2240 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002241 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002242 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002243 }
Chris Lattner682bf922009-03-29 16:50:03 +00002244 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002245 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002246 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002247}
2248
2249
2250/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2251/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002252static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002253CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002254 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002255}
2256
Ted Kremenek422bae72010-04-18 04:59:38 +00002257static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002258bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002259 // The 'ibaction' attribute is allowed on method definitions because of
2260 // how the IBAction macro is used on both method declarations and definitions.
2261 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002262 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2263 if ((*i)->getKind() != attr::IBAction)
2264 return true;
2265 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002266}
2267
Douglas Gregor926df6c2011-06-11 01:09:30 +00002268/// \brief Check whether the declared result type of the given Objective-C
2269/// method declaration is compatible with the method's class.
2270///
2271static bool
2272CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2273 ObjCInterfaceDecl *CurrentClass) {
2274 QualType ResultType = Method->getResultType();
2275 SourceRange ResultTypeRange;
2276 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2277 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2278
2279 // If an Objective-C method inherits its related result type, then its
2280 // declared result type must be compatible with its own class type. The
2281 // declared result type is compatible if:
2282 if (const ObjCObjectPointerType *ResultObjectType
2283 = ResultType->getAs<ObjCObjectPointerType>()) {
2284 // - it is id or qualified id, or
2285 if (ResultObjectType->isObjCIdType() ||
2286 ResultObjectType->isObjCQualifiedIdType())
2287 return false;
2288
2289 if (CurrentClass) {
2290 if (ObjCInterfaceDecl *ResultClass
2291 = ResultObjectType->getInterfaceDecl()) {
2292 // - it is the same as the method's class type, or
2293 if (CurrentClass == ResultClass)
2294 return false;
2295
2296 // - it is a superclass of the method's class type
2297 if (ResultClass->isSuperClassOf(CurrentClass))
2298 return false;
2299 }
2300 }
2301 }
2302
2303 return true;
2304}
2305
John McCall6c2c2502011-07-22 02:45:48 +00002306namespace {
2307/// A helper class for searching for methods which a particular method
2308/// overrides.
2309class OverrideSearch {
2310 Sema &S;
2311 ObjCMethodDecl *Method;
2312 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2313 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2314 bool Recursive;
2315
2316public:
2317 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2318 Selector selector = method->getSelector();
2319
2320 // Bypass this search if we've never seen an instance/class method
2321 // with this selector before.
2322 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2323 if (it == S.MethodPool.end()) {
2324 if (!S.ExternalSource) return;
2325 it = S.ReadMethodPool(selector);
2326 }
2327 ObjCMethodList &list =
2328 method->isInstanceMethod() ? it->second.first : it->second.second;
2329 if (!list.Method) return;
2330
2331 ObjCContainerDecl *container
2332 = cast<ObjCContainerDecl>(method->getDeclContext());
2333
2334 // Prevent the search from reaching this container again. This is
2335 // important with categories, which override methods from the
2336 // interface and each other.
2337 Searched.insert(container);
2338 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002339 }
John McCall6c2c2502011-07-22 02:45:48 +00002340
2341 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2342 iterator begin() const { return Overridden.begin(); }
2343 iterator end() const { return Overridden.end(); }
2344
2345private:
2346 void searchFromContainer(ObjCContainerDecl *container) {
2347 if (container->isInvalidDecl()) return;
2348
2349 switch (container->getDeclKind()) {
2350#define OBJCCONTAINER(type, base) \
2351 case Decl::type: \
2352 searchFrom(cast<type##Decl>(container)); \
2353 break;
2354#define ABSTRACT_DECL(expansion)
2355#define DECL(type, base) \
2356 case Decl::type:
2357#include "clang/AST/DeclNodes.inc"
2358 llvm_unreachable("not an ObjC container!");
2359 }
2360 }
2361
2362 void searchFrom(ObjCProtocolDecl *protocol) {
2363 // A method in a protocol declaration overrides declarations from
2364 // referenced ("parent") protocols.
2365 search(protocol->getReferencedProtocols());
2366 }
2367
2368 void searchFrom(ObjCCategoryDecl *category) {
2369 // A method in a category declaration overrides declarations from
2370 // the main class and from protocols the category references.
2371 search(category->getClassInterface());
2372 search(category->getReferencedProtocols());
2373 }
2374
2375 void searchFrom(ObjCCategoryImplDecl *impl) {
2376 // A method in a category definition that has a category
2377 // declaration overrides declarations from the category
2378 // declaration.
2379 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2380 search(category);
2381
2382 // Otherwise it overrides declarations from the class.
2383 } else {
2384 search(impl->getClassInterface());
2385 }
2386 }
2387
2388 void searchFrom(ObjCInterfaceDecl *iface) {
2389 // A method in a class declaration overrides declarations from
2390
2391 // - categories,
2392 for (ObjCCategoryDecl *category = iface->getCategoryList();
2393 category; category = category->getNextClassCategory())
2394 search(category);
2395
2396 // - the super class, and
2397 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2398 search(super);
2399
2400 // - any referenced protocols.
2401 search(iface->getReferencedProtocols());
2402 }
2403
2404 void searchFrom(ObjCImplementationDecl *impl) {
2405 // A method in a class implementation overrides declarations from
2406 // the class interface.
2407 search(impl->getClassInterface());
2408 }
2409
2410
2411 void search(const ObjCProtocolList &protocols) {
2412 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2413 i != e; ++i)
2414 search(*i);
2415 }
2416
2417 void search(ObjCContainerDecl *container) {
2418 // Abort if we've already searched this container.
2419 if (!Searched.insert(container)) return;
2420
2421 // Check for a method in this container which matches this selector.
2422 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2423 Method->isInstanceMethod());
2424
2425 // If we find one, record it and bail out.
2426 if (meth) {
2427 Overridden.insert(meth);
2428 return;
2429 }
2430
2431 // Otherwise, search for methods that a hypothetical method here
2432 // would have overridden.
2433
2434 // Note that we're now in a recursive case.
2435 Recursive = true;
2436
2437 searchFromContainer(container);
2438 }
2439};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002440}
2441
John McCalld226f652010-08-21 09:40:31 +00002442Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002443 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002444 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002445 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002446 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002447 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002448 Selector Sel,
2449 // optional arguments. The number of types/arguments is obtained
2450 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002451 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002452 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002453 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002454 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002455 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002456 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002457 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002458 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002459 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002460 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2461 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002462 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002463
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002464 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002465 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002466 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Steve Naroffccef3712009-02-20 22:59:16 +00002468 // Methods cannot return interface types. All ObjC objects are
2469 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002470 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002471 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2472 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002473 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002474 }
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002475 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002476 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002477 Diag(MethodLoc, diag::warn_missing_method_return_type)
2478 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002479 }
Mike Stump1eb44332009-09-09 15:08:12 +00002480
2481 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002482 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002483 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002484 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002485 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002486 /*isSynthesized=*/false,
2487 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002488 MethodDeclKind == tok::objc_optional
2489 ? ObjCMethodDecl::Optional
2490 : ObjCMethodDecl::Required,
2491 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002492
Chris Lattner5f9e2722011-07-23 10:55:15 +00002493 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002494
Chris Lattner7db638d2009-04-11 19:42:43 +00002495 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002496 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002497 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Chris Lattnere294d3f2009-04-11 18:57:04 +00002499 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002500 ArgType = Context.getObjCIdType();
2501 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002502 } else {
John McCall58e46772009-10-23 21:48:59 +00002503 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002504 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002505 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002506 }
Mike Stump1eb44332009-09-09 15:08:12 +00002507
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002508 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2509 LookupOrdinaryName, ForRedeclaration);
2510 LookupName(R, S);
2511 if (R.isSingleResult()) {
2512 NamedDecl *PrevDecl = R.getFoundDecl();
2513 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002514 Diag(ArgInfo[i].NameLoc,
2515 (MethodDefinition ? diag::warn_method_param_redefinition
2516 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002517 << ArgInfo[i].Name;
2518 Diag(PrevDecl->getLocation(),
2519 diag::note_previous_declaration);
2520 }
2521 }
2522
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002523 SourceLocation StartLoc = DI
2524 ? DI->getTypeLoc().getBeginLoc()
2525 : ArgInfo[i].NameLoc;
2526
John McCall81ef3e62011-04-23 02:46:06 +00002527 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2528 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2529 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002530
John McCall70798862011-05-02 00:30:12 +00002531 Param->setObjCMethodScopeInfo(i);
2532
Chris Lattner0ed844b2008-04-04 06:12:32 +00002533 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002534 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002535
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002536 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002537 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002538
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002539 S->AddDecl(Param);
2540 IdResolver.AddDecl(Param);
2541
Chris Lattner0ed844b2008-04-04 06:12:32 +00002542 Params.push_back(Param);
2543 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002544
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002545 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002546 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002547 QualType ArgType = Param->getType();
2548 if (ArgType.isNull())
2549 ArgType = Context.getObjCIdType();
2550 else
2551 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002552 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002553 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002554 Diag(Param->getLocation(),
2555 diag::err_object_cannot_be_passed_returned_by_value)
2556 << 1 << ArgType;
2557 Param->setInvalidDecl();
2558 }
2559 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002560
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002561 Params.push_back(Param);
2562 }
2563
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002564 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2565 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002566 ObjCMethod->setObjCDeclQualifier(
2567 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002568
2569 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002570 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002571
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002572 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002573 const ObjCMethodDecl *PrevMethod = 0;
2574 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002575 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002576 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2577 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002578 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002579 PrevMethod = ImpDecl->getClassMethod(Sel);
2580 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002581 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002582
Sean Huntcf807c42010-08-18 23:23:40 +00002583 if (ObjCMethod->hasAttrs() &&
2584 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002585 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002586 } else {
2587 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002588 }
John McCall6c2c2502011-07-22 02:45:48 +00002589
Chris Lattner4d391482007-12-12 07:09:47 +00002590 if (PrevMethod) {
2591 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002592 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002593 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002594 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002595 }
John McCall54abf7d2009-11-04 02:18:39 +00002596
Douglas Gregor926df6c2011-06-11 01:09:30 +00002597 // If this Objective-C method does not have a related result type, but we
2598 // are allowed to infer related result types, try to do so based on the
2599 // method family.
2600 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2601 if (!CurrentClass) {
2602 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2603 CurrentClass = Cat->getClassInterface();
2604 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2605 CurrentClass = Impl->getClassInterface();
2606 else if (ObjCCategoryImplDecl *CatImpl
2607 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2608 CurrentClass = CatImpl->getClassInterface();
2609 }
John McCall6c2c2502011-07-22 02:45:48 +00002610
2611 bool isRelatedResultTypeCompatible =
2612 (getLangOptions().ObjCInferRelatedResultType &&
2613 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass));
2614
2615 // Search for overridden methods and merge information down from them.
2616 OverrideSearch overrides(*this, ObjCMethod);
2617 for (OverrideSearch::iterator
2618 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2619 ObjCMethodDecl *overridden = *i;
2620
2621 // Propagate down the 'related result type' bit from overridden methods.
2622 if (isRelatedResultTypeCompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002623 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002624
2625 // Then merge the declarations.
2626 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002627
2628 // Check for overriding methods
2629 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2630 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2631 WarnConflictingTypedMethods(ObjCMethod, overridden,
2632 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2633 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002634 }
2635
John McCallf85e1932011-06-15 23:02:42 +00002636 bool ARCError = false;
2637 if (getLangOptions().ObjCAutoRefCount)
2638 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2639
John McCall6c2c2502011-07-22 02:45:48 +00002640 if (!ARCError && isRelatedResultTypeCompatible &&
2641 !ObjCMethod->hasRelatedResultType()) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002642 bool InferRelatedResultType = false;
2643 switch (ObjCMethod->getMethodFamily()) {
2644 case OMF_None:
2645 case OMF_copy:
2646 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002647 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002648 case OMF_mutableCopy:
2649 case OMF_release:
2650 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002651 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002652 break;
2653
2654 case OMF_alloc:
2655 case OMF_new:
2656 InferRelatedResultType = ObjCMethod->isClassMethod();
2657 break;
2658
2659 case OMF_init:
2660 case OMF_autorelease:
2661 case OMF_retain:
2662 case OMF_self:
2663 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2664 break;
2665 }
2666
John McCall6c2c2502011-07-22 02:45:48 +00002667 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002668 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002669 }
2670
John McCalld226f652010-08-21 09:40:31 +00002671 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002672}
2673
Chris Lattnercc98eac2008-12-17 07:13:27 +00002674bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002675 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002676 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002677 // Following is also an error. But it is caused by a missing @end
2678 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002679 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2680 return false;
2681 }
2682
Anders Carlsson15281452008-11-04 16:57:32 +00002683 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2684 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002685
Anders Carlsson15281452008-11-04 16:57:32 +00002686 return true;
2687}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002688
Chris Lattnercc98eac2008-12-17 07:13:27 +00002689/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2690/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002691void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002692 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002693 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002694 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002695 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002696 if (!Class) {
2697 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2698 return;
2699 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002700 if (LangOpts.ObjCNonFragileABI) {
2701 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2702 return;
2703 }
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Chris Lattnercc98eac2008-12-17 07:13:27 +00002705 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002706 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002707 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002708 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002709 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002710 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002711 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002712 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2713 /*FIXME: StartL=*/ID->getLocation(),
2714 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002715 ID->getIdentifier(), ID->getType(),
2716 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002717 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002718 }
Mike Stump1eb44332009-09-09 15:08:12 +00002719
Chris Lattnercc98eac2008-12-17 07:13:27 +00002720 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002721 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002722 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002723 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002724 if (getLangOptions().CPlusPlus)
2725 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002726 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002727 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002728 }
2729}
2730
Douglas Gregor160b5632010-04-26 17:32:49 +00002731/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002732VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2733 SourceLocation StartLoc,
2734 SourceLocation IdLoc,
2735 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002736 bool Invalid) {
2737 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2738 // duration shall not be qualified by an address-space qualifier."
2739 // Since all parameters have automatic store duration, they can not have
2740 // an address space.
2741 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002742 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002743 Invalid = true;
2744 }
2745
2746 // An @catch parameter must be an unqualified object pointer type;
2747 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2748 if (Invalid) {
2749 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002750 } else if (T->isDependentType()) {
2751 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002752 } else if (!T->isObjCObjectPointerType()) {
2753 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002754 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002755 } else if (T->isObjCQualifiedIdType()) {
2756 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002757 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002758 }
2759
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002760 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2761 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002762 New->setExceptionVariable(true);
2763
Douglas Gregor160b5632010-04-26 17:32:49 +00002764 if (Invalid)
2765 New->setInvalidDecl();
2766 return New;
2767}
2768
John McCalld226f652010-08-21 09:40:31 +00002769Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002770 const DeclSpec &DS = D.getDeclSpec();
2771
2772 // We allow the "register" storage class on exception variables because
2773 // GCC did, but we drop it completely. Any other storage class is an error.
2774 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2775 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2776 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2777 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2778 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2779 << DS.getStorageClassSpec();
2780 }
2781 if (D.getDeclSpec().isThreadSpecified())
2782 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2783 D.getMutableDeclSpec().ClearStorageClassSpecs();
2784
2785 DiagnoseFunctionSpecifiers(D);
2786
2787 // Check that there are no default arguments inside the type of this
2788 // exception object (C++ only).
2789 if (getLangOptions().CPlusPlus)
2790 CheckExtraCXXDefaultArguments(D);
2791
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002792 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002793 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002794
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002795 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2796 D.getSourceRange().getBegin(),
2797 D.getIdentifierLoc(),
2798 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002799 D.isInvalidType());
2800
2801 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2802 if (D.getCXXScopeSpec().isSet()) {
2803 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2804 << D.getCXXScopeSpec().getRange();
2805 New->setInvalidDecl();
2806 }
2807
2808 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002809 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002810 if (D.getIdentifier())
2811 IdResolver.AddDecl(New);
2812
2813 ProcessDeclAttributes(S, New, D);
2814
2815 if (New->hasAttr<BlocksAttr>())
2816 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002817 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002818}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002819
2820/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002821/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002822void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002823 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002824 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2825 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002826 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002827 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002828 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002829 }
2830}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002831
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002832void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002833 // Load referenced selectors from the external source.
2834 if (ExternalSource) {
2835 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2836 ExternalSource->ReadReferencedSelectors(Sels);
2837 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2838 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2839 }
2840
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002841 // Warning will be issued only when selector table is
2842 // generated (which means there is at lease one implementation
2843 // in the TU). This is to match gcc's behavior.
2844 if (ReferencedSelectors.empty() ||
2845 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002846 return;
2847 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2848 ReferencedSelectors.begin(),
2849 E = ReferencedSelectors.end(); S != E; ++S) {
2850 Selector Sel = (*S).first;
2851 if (!LookupImplementedMethodInGlobalPool(Sel))
2852 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2853 }
2854 return;
2855}