blob: 8dfdeb4b6075fedda9ac6b82b971742872edec11 [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
Douglas Gregore97179c2011-09-08 01:46:34 +0000151 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
152 Diag(Overridden->getLocation(),
153 diag::note_related_result_type_overridden_family)
154 << Family;
155 else
156 Diag(Overridden->getLocation(),
157 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000158 }
159
160 return false;
161}
162
John McCallf85e1932011-06-15 23:02:42 +0000163/// \brief Check a method declaration for compatibility with the Objective-C
164/// ARC conventions.
165static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
166 ObjCMethodFamily family = method->getMethodFamily();
167 switch (family) {
168 case OMF_None:
169 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000170 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000171 case OMF_retain:
172 case OMF_release:
173 case OMF_autorelease:
174 case OMF_retainCount:
175 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000176 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000177 return false;
178
179 case OMF_init:
180 // If the method doesn't obey the init rules, don't bother annotating it.
181 if (S.checkInitMethod(method, QualType()))
182 return true;
183
184 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
185 S.Context));
186
187 // Don't add a second copy of this attribute, but otherwise don't
188 // let it be suppressed.
189 if (method->hasAttr<NSReturnsRetainedAttr>())
190 return false;
191 break;
192
193 case OMF_alloc:
194 case OMF_copy:
195 case OMF_mutableCopy:
196 case OMF_new:
197 if (method->hasAttr<NSReturnsRetainedAttr>() ||
198 method->hasAttr<NSReturnsNotRetainedAttr>() ||
199 method->hasAttr<NSReturnsAutoreleasedAttr>())
200 return false;
201 break;
202 }
203
204 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
205 S.Context));
206 return false;
207}
208
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000209static void DiagnoseObjCImplementedDeprecations(Sema &S,
210 NamedDecl *ND,
211 SourceLocation ImplLoc,
212 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000213 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000214 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000215 if (select == 0)
216 S.Diag(ND->getLocation(), diag::note_method_declared_at);
217 else
218 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
219 }
220}
221
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000222/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
223/// pool.
224void Sema::AddAnyMethodToGlobalPool(Decl *D) {
225 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
226
227 // If we don't have a valid method decl, simply return.
228 if (!MDecl)
229 return;
230 if (MDecl->isInstanceMethod())
231 AddInstanceMethodToGlobalPool(MDecl, true);
232 else
233 AddFactoryMethodToGlobalPool(MDecl, true);
234}
235
Steve Naroffebf64432009-02-28 16:59:13 +0000236/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000237/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000238void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000239 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000240 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Steve Naroff394f3f42008-07-25 17:57:26 +0000242 // If we don't have a valid method decl, simply return.
243 if (!MDecl)
244 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000245
Chris Lattner4d391482007-12-12 07:09:47 +0000246 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000247 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000248 PushFunctionScope();
249
Chris Lattner4d391482007-12-12 07:09:47 +0000250 // Create Decl objects for each parameter, entrring them in the scope for
251 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000252
253 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000254 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Daniel Dunbar451318c2008-08-26 06:07:48 +0000256 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
257 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000258
Chris Lattner8123a952008-04-10 02:22:51 +0000259 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000260 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000261 E = MDecl->param_end(); PI != E; ++PI) {
262 ParmVarDecl *Param = (*PI);
263 if (!Param->isInvalidDecl() &&
264 RequireCompleteType(Param->getLocation(), Param->getType(),
265 diag::err_typecheck_decl_incomplete_type))
266 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000267 if ((*PI)->getIdentifier())
268 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000269 }
John McCallf85e1932011-06-15 23:02:42 +0000270
271 // In ARC, disallow definition of retain/release/autorelease/retainCount
272 if (getLangOptions().ObjCAutoRefCount) {
273 switch (MDecl->getMethodFamily()) {
274 case OMF_retain:
275 case OMF_retainCount:
276 case OMF_release:
277 case OMF_autorelease:
278 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
279 << MDecl->getSelector();
280 break;
281
282 case OMF_None:
283 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000284 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000285 case OMF_alloc:
286 case OMF_init:
287 case OMF_mutableCopy:
288 case OMF_copy:
289 case OMF_new:
290 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000291 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000292 break;
293 }
294 }
295
Nico Weber9a1ecf02011-08-22 17:25:57 +0000296 // Warn on deprecated methods under -Wdeprecated-implementations,
297 // and prepare for warning on missing super calls.
298 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000299 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000300 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000301 DiagnoseObjCImplementedDeprecations(*this,
302 dyn_cast<NamedDecl>(IMD),
303 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000304
Nico Weber80cb6e62011-08-28 22:35:17 +0000305 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000306 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
307 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
308 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000309 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000310 ObjCShouldCallSuperDealloc =
311 !Context.getLangOptions().ObjCAutoRefCount &&
312 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000313 ObjCShouldCallSuperFinalize =
314 !Context.getLangOptions().ObjCAutoRefCount &&
315 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000316 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000317 }
Chris Lattner4d391482007-12-12 07:09:47 +0000318}
319
John McCalld226f652010-08-21 09:40:31 +0000320Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000321ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
322 IdentifierInfo *ClassName, SourceLocation ClassLoc,
323 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000324 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000325 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000326 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000327 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Chris Lattner4d391482007-12-12 07:09:47 +0000329 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000330 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000331 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000332
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000333 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000334 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000335 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000338 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
339 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000340 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000341 if (!IDecl->isForwardDecl()) {
342 IDecl->setInvalidDecl();
343 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
344 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000345
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000346 // Return the previous class interface.
347 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000348 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000349 } else {
350 IDecl->setLocation(AtInterfaceLoc);
351 IDecl->setForwardDecl(false);
352 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000353 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000354 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000355 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000356
357 // Since this ObjCInterfaceDecl was created by a forward declaration,
358 // we now add it to the DeclContext since it wasn't added before
359 // (see ActOnForwardClassDeclaration).
360 IDecl->setLexicalDeclContext(CurContext);
361 CurContext->addDecl(IDecl);
362
363 if (AttrList)
364 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000365 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000366 } else {
367 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
368 ClassName, ClassLoc);
369 if (AttrList)
370 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
371
372 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner4d391482007-12-12 07:09:47 +0000375 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000376 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000377 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
378 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000379
380 if (!PrevDecl) {
381 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000382 TypoCorrection Corrected = CorrectTypo(
383 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
384 NULL, NULL, false, CTC_NoKeywords);
385 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000386 Diag(SuperLoc, diag::err_undef_superclass_suggest)
387 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000388 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
389 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000390 }
391 }
392
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000393 if (PrevDecl == IDecl) {
394 Diag(SuperLoc, diag::err_recursive_superclass)
395 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
396 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000397 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000398 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000399 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000400
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000401 // Diagnose classes that inherit from deprecated classes.
402 if (SuperClassDecl)
403 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000405 if (PrevDecl && SuperClassDecl == 0) {
406 // The previous declaration was not a class decl. Check if we have a
407 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000408 if (const TypedefNameDecl *TDecl =
409 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000410 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000411 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000412 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
413 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000414 }
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000417 // This handles the following case:
418 //
419 // typedef int SuperClass;
420 // @interface MyClass : SuperClass {} @end
421 //
422 if (!SuperClassDecl) {
423 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
424 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000425 }
426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Richard Smith162e1c12011-04-15 14:24:37 +0000428 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000429 if (!SuperClassDecl)
430 Diag(SuperLoc, diag::err_undef_superclass)
431 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000432 else if (SuperClassDecl->isForwardDecl()) {
433 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000434 << SuperClassDecl->getDeclName() << ClassName
435 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000436 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
437 SuperClassDecl = 0;
438 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000439 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000440 IDecl->setSuperClass(SuperClassDecl);
441 IDecl->setSuperClassLoc(SuperLoc);
442 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000443 }
Chris Lattner4d391482007-12-12 07:09:47 +0000444 } else { // we have a root class.
445 IDecl->setLocEnd(ClassLoc);
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Sebastian Redl0b17c612010-08-13 00:28:03 +0000448 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000449 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000450 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000451 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000452 IDecl->setLocEnd(EndProtoLoc);
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Anders Carlsson15281452008-11-04 16:57:32 +0000455 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000456 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000457}
458
459/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000460/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000461Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
462 IdentifierInfo *AliasName,
463 SourceLocation AliasLocation,
464 IdentifierInfo *ClassName,
465 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000466 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000467 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000468 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000469 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000470 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000471 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000472 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000473 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000474 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000475 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000476 }
477 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000478 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000479 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000480 if (const TypedefNameDecl *TDecl =
481 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000482 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000483 if (T->isObjCObjectType()) {
484 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000485 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000486 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000487 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000488 }
489 }
490 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000491 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
492 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000493 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000494 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000495 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000496 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000497 }
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000499 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000500 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000501 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Anders Carlsson15281452008-11-04 16:57:32 +0000503 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000504 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000505
John McCalld226f652010-08-21 09:40:31 +0000506 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000507}
508
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000509bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000510 IdentifierInfo *PName,
511 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000512 const ObjCList<ObjCProtocolDecl> &PList) {
513
514 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000515 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
516 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000517 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
518 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000519 if (PDecl->getIdentifier() == PName) {
520 Diag(Ploc, diag::err_protocol_has_circular_dependency);
521 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000522 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000523 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000524 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
525 PDecl->getLocation(), PDecl->getReferencedProtocols()))
526 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000527 }
528 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000529 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000530}
531
John McCalld226f652010-08-21 09:40:31 +0000532Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000533Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
534 IdentifierInfo *ProtocolName,
535 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000536 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000537 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000538 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000539 SourceLocation EndProtoLoc,
540 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000541 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000542 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000543 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000544 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000545 if (PDecl) {
546 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000547 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000548 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000549 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000550 // Just return the protocol we already had.
551 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000552 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000553 }
Steve Naroff61d68522009-03-05 15:22:01 +0000554 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000555 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000556 err = CheckForwardProtocolDeclarationForCircularDependency(
557 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Steve Narofff11b5082008-08-13 16:39:22 +0000559 // Make sure the cached decl gets a valid start location.
560 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000561 PDecl->setForwardDecl(false);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000562 // Since this ObjCProtocolDecl was created by a forward declaration,
563 // we now add it to the DeclContext since it wasn't added before
564 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000565 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000566 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000567 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000568 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000569 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000570 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000571 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000572 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000573 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000574 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000575 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000576 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000577 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000578 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
579 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000580 PDecl->setLocEnd(EndProtoLoc);
581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000584 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000585}
586
587/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000588/// issues an error if they are not declared. It returns list of
589/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000590void
Chris Lattnere13b9592008-07-26 04:03:38 +0000591Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000592 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000593 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000594 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000595 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000596 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
597 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000598 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000599 TypoCorrection Corrected = CorrectTypo(
600 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
601 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
602 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000603 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000604 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000605 Diag(PDecl->getLocation(), diag::note_previous_decl)
606 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000607 }
608 }
609
610 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000611 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000612 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000613 continue;
614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000616 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000617
618 // If this is a forward declaration and we are supposed to warn in this
619 // case, do it.
620 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000621 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000622 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000623 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000624 }
625}
626
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000627/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000628/// a class method in its extension.
629///
Mike Stump1eb44332009-09-09 15:08:12 +0000630void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000631 ObjCInterfaceDecl *ID) {
632 if (!ID)
633 return; // Possibly due to previous error
634
635 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000636 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
637 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000638 ObjCMethodDecl *MD = *i;
639 MethodMap[MD->getSelector()] = MD;
640 }
641
642 if (MethodMap.empty())
643 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000644 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
645 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000646 ObjCMethodDecl *Method = *i;
647 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
648 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
649 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
650 << Method->getDeclName();
651 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
652 }
653 }
654}
655
Chris Lattner58fe03b2009-04-12 08:43:13 +0000656/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000657Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000658Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000659 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000660 unsigned NumElts,
661 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000662 SmallVector<ObjCProtocolDecl*, 32> Protocols;
663 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Chris Lattner4d391482007-12-12 07:09:47 +0000665 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000666 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000667 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000668 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000669 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000670 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000671 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000672 PushOnScopeChains(PDecl, TUScope, false);
673 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000674 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000675 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000676 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000677 if (!isNew)
678 PDecl->setChangedSinceDeserialization(true);
679 }
Chris Lattner4d391482007-12-12 07:09:47 +0000680 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000681 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000682 }
Mike Stump1eb44332009-09-09 15:08:12 +0000683
684 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000685 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000686 Protocols.data(), Protocols.size(),
687 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000688 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000689 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000690 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000691}
692
John McCalld226f652010-08-21 09:40:31 +0000693Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000694ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
695 IdentifierInfo *ClassName, SourceLocation ClassLoc,
696 IdentifierInfo *CategoryName,
697 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000698 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000699 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000700 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000701 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000702 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000703 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000704
705 /// Check that class of this category is already completely declared.
706 if (!IDecl || IDecl->isForwardDecl()) {
707 // Create an invalid ObjCCategoryDecl to serve as context for
708 // the enclosing method declarations. We mark the decl invalid
709 // to make it clear that this isn't a valid AST.
710 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000711 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000712 CDecl->setInvalidDecl();
713 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000714 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000715 }
716
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000717 if (!CategoryName && IDecl->getImplementation()) {
718 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
719 Diag(IDecl->getImplementation()->getLocation(),
720 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000721 }
722
Fariborz Jahanian25760612010-02-15 21:55:26 +0000723 if (CategoryName) {
724 /// Check for duplicate interface declaration for this category
725 ObjCCategoryDecl *CDeclChain;
726 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
727 CDeclChain = CDeclChain->getNextClassCategory()) {
728 if (CDeclChain->getIdentifier() == CategoryName) {
729 // Class extensions can be declared multiple times.
730 Diag(CategoryLoc, diag::warn_dup_category_def)
731 << ClassName << CategoryName;
732 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
733 break;
734 }
Chris Lattner70f19542009-02-16 21:26:43 +0000735 }
736 }
Chris Lattner70f19542009-02-16 21:26:43 +0000737
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000738 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
739 ClassLoc, CategoryLoc, CategoryName, IDecl);
740 // FIXME: PushOnScopeChains?
741 CurContext->addDecl(CDecl);
742
743 // If the interface is deprecated, warn about it.
744 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
745
Chris Lattner4d391482007-12-12 07:09:47 +0000746 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000747 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000748 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000749 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000750 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000751 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000752 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Anders Carlsson15281452008-11-04 16:57:32 +0000755 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000756 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000757}
758
759/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000760/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000761/// object.
John McCalld226f652010-08-21 09:40:31 +0000762Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000763 SourceLocation AtCatImplLoc,
764 IdentifierInfo *ClassName, SourceLocation ClassLoc,
765 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000766 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000767 ObjCCategoryDecl *CatIDecl = 0;
768 if (IDecl) {
769 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
770 if (!CatIDecl) {
771 // Category @implementation with no corresponding @interface.
772 // Create and install one.
773 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000774 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000775 CatName, IDecl);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000776 }
777 }
778
Mike Stump1eb44332009-09-09 15:08:12 +0000779 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000780 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
781 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000782 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000783 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000784 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000785 CDecl->setInvalidDecl();
786 }
Chris Lattner4d391482007-12-12 07:09:47 +0000787
Douglas Gregord0434102009-01-09 00:49:46 +0000788 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000789 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000790
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000791 /// Check that CatName, category name, is not used in another implementation.
792 if (CatIDecl) {
793 if (CatIDecl->getImplementation()) {
794 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
795 << CatName;
796 Diag(CatIDecl->getImplementation()->getLocation(),
797 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000798 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000799 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000800 // Warn on implementating category of deprecated class under
801 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000802 DiagnoseObjCImplementedDeprecations(*this,
803 dyn_cast<NamedDecl>(IDecl),
804 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000805 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Anders Carlsson15281452008-11-04 16:57:32 +0000808 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000809 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000810}
811
John McCalld226f652010-08-21 09:40:31 +0000812Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000813 SourceLocation AtClassImplLoc,
814 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000815 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000816 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000817 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000818 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000819 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000820 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
821 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000822 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000823 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000824 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000825 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
826 // If this is a forward declaration of an interface, warn.
827 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000828 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000829 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000830 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000831 } else {
832 // We did not find anything with the name ClassName; try to correct for
833 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000834 TypoCorrection Corrected = CorrectTypo(
835 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
836 NULL, NULL, false, CTC_NoKeywords);
837 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000838 // Suggest the (potentially) correct interface name. However, put the
839 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000840 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000841 // provide a code-modification hint or use the typo name for recovery,
842 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000843 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000844 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000845 << ClassName << CorrectedName;
846 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
847 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000848 IDecl = 0;
849 } else {
850 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
851 }
Chris Lattner4d391482007-12-12 07:09:47 +0000852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner4d391482007-12-12 07:09:47 +0000854 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000855 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000856 if (SuperClassname) {
857 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000858 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
859 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000860 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000861 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
862 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000863 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000864 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000865 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000866 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000867 Diag(SuperClassLoc, diag::err_undef_superclass)
868 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000869 else if (IDecl && IDecl->getSuperClass() != SDecl) {
870 // This implementation and its interface do not have the same
871 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000872 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000873 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000874 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000875 }
876 }
877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattner4d391482007-12-12 07:09:47 +0000879 if (!IDecl) {
880 // Legacy case of @implementation with no corresponding @interface.
881 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000882
Mike Stump390b4cc2009-05-16 07:39:55 +0000883 // FIXME: Do we support attributes on the @implementation? If so we should
884 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000885 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000886 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000887 IDecl->setSuperClass(SDecl);
888 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000889
890 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000891 } else {
892 // Mark the interface as being completed, even if it was just as
893 // @class ....;
894 // declaration; the user cannot reopen it.
895 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
898 ObjCImplementationDecl* IMPDecl =
899 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000900 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Anders Carlsson15281452008-11-04 16:57:32 +0000902 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000903 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Chris Lattner4d391482007-12-12 07:09:47 +0000905 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000906 if (IDecl->getImplementation()) {
907 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000908 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000909 Diag(IDecl->getImplementation()->getLocation(),
910 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000911 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000912 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000913 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000914 // Warn on implementating deprecated class under
915 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000916 DiagnoseObjCImplementedDeprecations(*this,
917 dyn_cast<NamedDecl>(IDecl),
918 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000919 }
John McCalld226f652010-08-21 09:40:31 +0000920 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000921}
922
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000923void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
924 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000925 SourceLocation RBrace) {
926 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000927 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000928 if (!IDecl)
929 return;
930 /// Check case of non-existing @interface decl.
931 /// (legacy objective-c @implementation decl without an @interface decl).
932 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000933 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000934 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000935 // Add ivar's to class's DeclContext.
936 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000937 ivars[i]->setLexicalDeclContext(ImpDecl);
938 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000939 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000940 }
941
Chris Lattner4d391482007-12-12 07:09:47 +0000942 return;
943 }
944 // If implementation has empty ivar list, just return.
945 if (numIvars == 0)
946 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Chris Lattner4d391482007-12-12 07:09:47 +0000948 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000949 if (LangOpts.ObjCNonFragileABI2) {
950 if (ImpDecl->getSuperClass())
951 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
952 for (unsigned i = 0; i < numIvars; i++) {
953 ObjCIvarDecl* ImplIvar = ivars[i];
954 if (const ObjCIvarDecl *ClsIvar =
955 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
956 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
957 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
958 continue;
959 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000960 // Instance ivar to Implementation's DeclContext.
961 ImplIvar->setLexicalDeclContext(ImpDecl);
962 IDecl->makeDeclVisibleInContext(ImplIvar, false);
963 ImpDecl->addDecl(ImplIvar);
964 }
965 return;
966 }
Chris Lattner4d391482007-12-12 07:09:47 +0000967 // Check interface's Ivar list against those in the implementation.
968 // names and types must match.
969 //
Chris Lattner4d391482007-12-12 07:09:47 +0000970 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000971 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000972 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
973 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974 ObjCIvarDecl* ImplIvar = ivars[j++];
975 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000976 assert (ImplIvar && "missing implementation ivar");
977 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Steve Naroffca331292009-03-03 14:49:36 +0000979 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000980 if (Context.getCanonicalType(ImplIvar->getType()) !=
981 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000982 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000983 << ImplIvar->getIdentifier()
984 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000985 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000986 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
987 Expr *ImplBitWidth = ImplIvar->getBitWidth();
988 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000989 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
990 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000991 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
992 << ImplIvar->getIdentifier();
993 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995 }
Steve Naroffca331292009-03-03 14:49:36 +0000996 // Make sure the names are identical.
997 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000998 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000999 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001000 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001001 }
1002 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Chris Lattner609e4c72007-12-12 18:11:49 +00001005 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001006 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001007 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001008 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001009}
1010
Steve Naroff3c2eb662008-02-10 21:38:56 +00001011void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001012 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001013 // No point warning no definition of method which is 'unavailable'.
1014 if (method->hasAttr<UnavailableAttr>())
1015 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001016 if (!IncompleteImpl) {
1017 Diag(ImpLoc, diag::warn_incomplete_impl);
1018 IncompleteImpl = true;
1019 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001020 if (DiagID == diag::warn_unimplemented_protocol_method)
1021 Diag(ImpLoc, DiagID) << method->getDeclName();
1022 else
1023 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001024}
1025
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001026/// Determines if type B can be substituted for type A. Returns true if we can
1027/// guarantee that anything that the user will do to an object of type A can
1028/// also be done to an object of type B. This is trivially true if the two
1029/// types are the same, or if B is a subclass of A. It becomes more complex
1030/// in cases where protocols are involved.
1031///
1032/// Object types in Objective-C describe the minimum requirements for an
1033/// object, rather than providing a complete description of a type. For
1034/// example, if A is a subclass of B, then B* may refer to an instance of A.
1035/// The principle of substitutability means that we may use an instance of A
1036/// anywhere that we may use an instance of B - it will implement all of the
1037/// ivars of B and all of the methods of B.
1038///
1039/// This substitutability is important when type checking methods, because
1040/// the implementation may have stricter type definitions than the interface.
1041/// The interface specifies minimum requirements, but the implementation may
1042/// have more accurate ones. For example, a method may privately accept
1043/// instances of B, but only publish that it accepts instances of A. Any
1044/// object passed to it will be type checked against B, and so will implicitly
1045/// by a valid A*. Similarly, a method may return a subclass of the class that
1046/// it is declared as returning.
1047///
1048/// This is most important when considering subclassing. A method in a
1049/// subclass must accept any object as an argument that its superclass's
1050/// implementation accepts. It may, however, accept a more general type
1051/// without breaking substitutability (i.e. you can still use the subclass
1052/// anywhere that you can use the superclass, but not vice versa). The
1053/// converse requirement applies to return types: the return type for a
1054/// subclass method must be a valid object of the kind that the superclass
1055/// advertises, but it may be specified more accurately. This avoids the need
1056/// for explicit down-casting by callers.
1057///
1058/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001059static bool isObjCTypeSubstitutable(ASTContext &Context,
1060 const ObjCObjectPointerType *A,
1061 const ObjCObjectPointerType *B,
1062 bool rejectId) {
1063 // Reject a protocol-unqualified id.
1064 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001065
1066 // If B is a qualified id, then A must also be a qualified id and it must
1067 // implement all of the protocols in B. It may not be a qualified class.
1068 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1069 // stricter definition so it is not substitutable for id<A>.
1070 if (B->isObjCQualifiedIdType()) {
1071 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001072 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1073 QualType(B,0),
1074 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001075 }
1076
1077 /*
1078 // id is a special type that bypasses type checking completely. We want a
1079 // warning when it is used in one place but not another.
1080 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1081
1082
1083 // If B is a qualified id, then A must also be a qualified id (which it isn't
1084 // if we've got this far)
1085 if (B->isObjCQualifiedIdType()) return false;
1086 */
1087
1088 // Now we know that A and B are (potentially-qualified) class types. The
1089 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001090 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001091}
1092
John McCall10302c02010-10-28 02:34:38 +00001093static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1094 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1095}
1096
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001097static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001098 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001099 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001100 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001101 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001102 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001103 if (IsProtocolMethodDecl &&
1104 (MethodDecl->getObjCDeclQualifier() !=
1105 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001106 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001107 S.Diag(MethodImpl->getLocation(),
1108 (IsOverridingMode ?
1109 diag::warn_conflicting_overriding_ret_type_modifiers
1110 : diag::warn_conflicting_ret_type_modifiers))
1111 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001112 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1113 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1114 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1115 }
1116 else
1117 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001118 }
1119
John McCall10302c02010-10-28 02:34:38 +00001120 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001121 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001122 return true;
1123 if (!Warn)
1124 return false;
John McCall10302c02010-10-28 02:34:38 +00001125
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001126 unsigned DiagID =
1127 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1128 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001129
1130 // Mismatches between ObjC pointers go into a different warning
1131 // category, and sometimes they're even completely whitelisted.
1132 if (const ObjCObjectPointerType *ImplPtrTy =
1133 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1134 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001135 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001136 // Allow non-matching return types as long as they don't violate
1137 // the principle of substitutability. Specifically, we permit
1138 // return types that are subclasses of the declared return type,
1139 // or that are more-qualified versions of the declared type.
1140 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001141 return false;
John McCall10302c02010-10-28 02:34:38 +00001142
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001143 DiagID =
1144 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1145 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001146 }
1147 }
1148
1149 S.Diag(MethodImpl->getLocation(), DiagID)
1150 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001151 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001152 << MethodImpl->getResultType()
1153 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001154 S.Diag(MethodDecl->getLocation(),
1155 IsOverridingMode ? diag::note_previous_declaration
1156 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001157 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001158 return false;
John McCall10302c02010-10-28 02:34:38 +00001159}
1160
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001161static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001162 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001163 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001164 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001165 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001166 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001167 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001168 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001169 if (IsProtocolMethodDecl &&
1170 (ImplVar->getObjCDeclQualifier() !=
1171 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001172 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001173 if (IsOverridingMode)
1174 S.Diag(ImplVar->getLocation(),
1175 diag::warn_conflicting_overriding_param_modifiers)
1176 << getTypeRange(ImplVar->getTypeSourceInfo())
1177 << MethodImpl->getDeclName();
1178 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001179 diag::warn_conflicting_param_modifiers)
1180 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001181 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001182 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1183 << getTypeRange(IfaceVar->getTypeSourceInfo());
1184 }
1185 else
1186 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001187 }
1188
John McCall10302c02010-10-28 02:34:38 +00001189 QualType ImplTy = ImplVar->getType();
1190 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001191
John McCall10302c02010-10-28 02:34:38 +00001192 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001193 return true;
1194
1195 if (!Warn)
1196 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001197 unsigned DiagID =
1198 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1199 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001200
1201 // Mismatches between ObjC pointers go into a different warning
1202 // category, and sometimes they're even completely whitelisted.
1203 if (const ObjCObjectPointerType *ImplPtrTy =
1204 ImplTy->getAs<ObjCObjectPointerType>()) {
1205 if (const ObjCObjectPointerType *IfacePtrTy =
1206 IfaceTy->getAs<ObjCObjectPointerType>()) {
1207 // Allow non-matching argument types as long as they don't
1208 // violate the principle of substitutability. Specifically, the
1209 // implementation must accept any objects that the superclass
1210 // accepts, however it may also accept others.
1211 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001212 return false;
John McCall10302c02010-10-28 02:34:38 +00001213
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001214 DiagID =
1215 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1216 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001217 }
1218 }
1219
1220 S.Diag(ImplVar->getLocation(), DiagID)
1221 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001222 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1223 S.Diag(IfaceVar->getLocation(),
1224 (IsOverridingMode ? diag::note_previous_declaration
1225 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001226 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001227 return false;
John McCall10302c02010-10-28 02:34:38 +00001228}
John McCallf85e1932011-06-15 23:02:42 +00001229
1230/// In ARC, check whether the conventional meanings of the two methods
1231/// match. If they don't, it's a hard error.
1232static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1233 ObjCMethodDecl *decl) {
1234 ObjCMethodFamily implFamily = impl->getMethodFamily();
1235 ObjCMethodFamily declFamily = decl->getMethodFamily();
1236 if (implFamily == declFamily) return false;
1237
1238 // Since conventions are sorted by selector, the only possibility is
1239 // that the types differ enough to cause one selector or the other
1240 // to fall out of the family.
1241 assert(implFamily == OMF_None || declFamily == OMF_None);
1242
1243 // No further diagnostics required on invalid declarations.
1244 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1245
1246 const ObjCMethodDecl *unmatched = impl;
1247 ObjCMethodFamily family = declFamily;
1248 unsigned errorID = diag::err_arc_lost_method_convention;
1249 unsigned noteID = diag::note_arc_lost_method_convention;
1250 if (declFamily == OMF_None) {
1251 unmatched = decl;
1252 family = implFamily;
1253 errorID = diag::err_arc_gained_method_convention;
1254 noteID = diag::note_arc_gained_method_convention;
1255 }
1256
1257 // Indexes into a %select clause in the diagnostic.
1258 enum FamilySelector {
1259 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1260 };
1261 FamilySelector familySelector = FamilySelector();
1262
1263 switch (family) {
1264 case OMF_None: llvm_unreachable("logic error, no method convention");
1265 case OMF_retain:
1266 case OMF_release:
1267 case OMF_autorelease:
1268 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001269 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001270 case OMF_retainCount:
1271 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001272 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001273 // Mismatches for these methods don't change ownership
1274 // conventions, so we don't care.
1275 return false;
1276
1277 case OMF_init: familySelector = F_init; break;
1278 case OMF_alloc: familySelector = F_alloc; break;
1279 case OMF_copy: familySelector = F_copy; break;
1280 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1281 case OMF_new: familySelector = F_new; break;
1282 }
1283
1284 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1285 ReasonSelector reasonSelector;
1286
1287 // The only reason these methods don't fall within their families is
1288 // due to unusual result types.
1289 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1290 reasonSelector = R_UnrelatedReturn;
1291 } else {
1292 reasonSelector = R_NonObjectReturn;
1293 }
1294
1295 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1296 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1297
1298 return true;
1299}
John McCall10302c02010-10-28 02:34:38 +00001300
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001301void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001302 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001303 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001304 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001305 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001306 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001307 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1308 return;
1309
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001310 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001311 IsProtocolMethodDecl, IsOverridingMode,
1312 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Chris Lattner3aff9192009-04-11 19:58:42 +00001314 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001315 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001316 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001317 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1318 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001319 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001320
Fariborz Jahanian21121902011-08-08 18:03:17 +00001321 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001322 if (IsOverridingMode)
1323 Diag(ImpMethodDecl->getLocation(),
1324 diag::warn_conflicting_overriding_variadic);
1325 else
1326 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001327 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001328 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001329}
1330
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001331/// WarnExactTypedMethods - This routine issues a warning if method
1332/// implementation declaration matches exactly that of its declaration.
1333void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1334 ObjCMethodDecl *MethodDecl,
1335 bool IsProtocolMethodDecl) {
1336 // don't issue warning when protocol method is optional because primary
1337 // class is not required to implement it and it is safe for protocol
1338 // to implement it.
1339 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1340 return;
1341 // don't issue warning when primary class's method is
1342 // depecated/unavailable.
1343 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1344 MethodDecl->hasAttr<DeprecatedAttr>())
1345 return;
1346
1347 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1348 IsProtocolMethodDecl, false, false);
1349 if (match)
1350 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1351 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1352 IM != EM; ++IM, ++IF) {
1353 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1354 *IM, *IF,
1355 IsProtocolMethodDecl, false, false);
1356 if (!match)
1357 break;
1358 }
1359 if (match)
1360 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001361 if (match)
1362 match = !(MethodDecl->isClassMethod() &&
1363 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001364
1365 if (match) {
1366 Diag(ImpMethodDecl->getLocation(),
1367 diag::warn_category_method_impl_match);
1368 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1369 }
1370}
1371
Mike Stump390b4cc2009-05-16 07:39:55 +00001372/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1373/// improve the efficiency of selector lookups and type checking by associating
1374/// with each protocol / interface / category the flattened instance tables. If
1375/// we used an immutable set to keep the table then it wouldn't add significant
1376/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001377
Steve Naroffefe7f362008-02-08 22:06:17 +00001378/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001379/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001380void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1381 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001382 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001383 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001384 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001385 ObjCContainerDecl *CDecl) {
1386 ObjCInterfaceDecl *IDecl;
1387 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1388 IDecl = C->getClassInterface();
1389 else
1390 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1391 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1392
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001393 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001394 ObjCInterfaceDecl *NSIDecl = 0;
1395 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001396 // check to see if class implements forwardInvocation method and objects
1397 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001398 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001399 // Under such conditions, which means that every method possible is
1400 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001401 // found" warnings.
1402 // FIXME: Use a general GetUnarySelector method for this.
1403 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1404 Selector fISelector = Context.Selectors.getSelector(1, &II);
1405 if (InsMap.count(fISelector))
1406 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1407 // need be implemented in the implementation.
1408 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001411 // If a method lookup fails locally we still need to look and see if
1412 // the method was implemented by a base class or an inherited
1413 // protocol. This lookup is slow, but occurs rarely in correct code
1414 // and otherwise would terminate in a warning.
1415
Chris Lattner4d391482007-12-12 07:09:47 +00001416 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001417 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001418 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001419 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001420 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001421 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001422 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001423 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001424 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001425 // Ugly, but necessary. Method declared in protcol might have
1426 // have been synthesized due to a property declared in the class which
1427 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001428 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001429 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001430 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001431 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001432 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1433 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001434 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001435 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001436 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1437 << PDecl->getDeclName();
1438 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001439 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001440 }
1441 }
Chris Lattner4d391482007-12-12 07:09:47 +00001442 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001443 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001444 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001445 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001446 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001447 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1448 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001449 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001450 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001451 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001452 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001453 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001454 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1455 PDecl->getDeclName();
1456 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001457 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001458 }
Chris Lattner780f3292008-07-21 21:32:27 +00001459 // Check on this protocols's referenced protocols, recursively.
1460 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1461 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001462 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001463}
1464
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001465/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001466/// or protocol against those declared in their implementations.
1467///
1468void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1469 const llvm::DenseSet<Selector> &ClsMap,
1470 llvm::DenseSet<Selector> &InsMapSeen,
1471 llvm::DenseSet<Selector> &ClsMapSeen,
1472 ObjCImplDecl* IMPDecl,
1473 ObjCContainerDecl* CDecl,
1474 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001475 bool ImmediateClass,
1476 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001477 // Check and see if instance methods in class interface have been
1478 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001479 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1480 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001481 if (InsMapSeen.count((*I)->getSelector()))
1482 continue;
1483 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001484 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001485 !InsMap.count((*I)->getSelector())) {
1486 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001487 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1488 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001489 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001490 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001491 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001492 IMPDecl->getInstanceMethod((*I)->getSelector());
1493 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1494 "Expected to find the method through lookup as well");
1495 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001496 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001497 if (ImpMethodDecl) {
1498 if (!WarnExactMatch)
1499 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1500 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001501 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001502 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1503 isa<ObjCProtocolDecl>(CDecl));
1504 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001505 }
1506 }
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001508 // Check and see if class methods in class interface have been
1509 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001510 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001511 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001512 if (ClsMapSeen.count((*I)->getSelector()))
1513 continue;
1514 ClsMapSeen.insert((*I)->getSelector());
1515 if (!ClsMap.count((*I)->getSelector())) {
1516 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001517 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1518 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001519 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001520 ObjCMethodDecl *ImpMethodDecl =
1521 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001522 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1523 "Expected to find the method through lookup as well");
1524 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001525 if (!WarnExactMatch)
1526 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1527 isa<ObjCProtocolDecl>(CDecl));
1528 else
1529 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1530 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001531 }
1532 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001533
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001534 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001535 // Also methods in class extensions need be looked at next.
1536 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1537 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1538 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1539 IMPDecl,
1540 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001541 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001542
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001543 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001544 for (ObjCInterfaceDecl::all_protocol_iterator
1545 PI = I->all_referenced_protocol_begin(),
1546 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001547 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1548 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001549 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001550
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001551 // FIXME. For now, we are not checking for extact match of methods
1552 // in category implementation and its primary class's super class.
1553 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001554 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001555 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001556 I->getSuperClass(), IncompleteImpl, false);
1557 }
1558}
1559
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001560/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1561/// category matches with those implemented in its primary class and
1562/// warns each time an exact match is found.
1563void Sema::CheckCategoryVsClassMethodMatches(
1564 ObjCCategoryImplDecl *CatIMPDecl) {
1565 llvm::DenseSet<Selector> InsMap, ClsMap;
1566
1567 for (ObjCImplementationDecl::instmeth_iterator
1568 I = CatIMPDecl->instmeth_begin(),
1569 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1570 InsMap.insert((*I)->getSelector());
1571
1572 for (ObjCImplementationDecl::classmeth_iterator
1573 I = CatIMPDecl->classmeth_begin(),
1574 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1575 ClsMap.insert((*I)->getSelector());
1576 if (InsMap.empty() && ClsMap.empty())
1577 return;
1578
1579 // Get category's primary class.
1580 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1581 if (!CatDecl)
1582 return;
1583 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1584 if (!IDecl)
1585 return;
1586 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1587 bool IncompleteImpl = false;
1588 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1589 CatIMPDecl, IDecl,
1590 IncompleteImpl, false, true /*WarnExactMatch*/);
1591}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001592
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001593void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001594 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001595 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001596 llvm::DenseSet<Selector> InsMap;
1597 // Check and see if instance methods in class interface have been
1598 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001599 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001600 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001601 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001603 // Check and see if properties declared in the interface have either 1)
1604 // an implementation or 2) there is a @synthesize/@dynamic implementation
1605 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001606 if (isa<ObjCInterfaceDecl>(CDecl) &&
1607 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001608 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001609
Chris Lattner4d391482007-12-12 07:09:47 +00001610 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001611 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001612 I = IMPDecl->classmeth_begin(),
1613 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001614 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001616 // Check for type conflict of methods declared in a class/protocol and
1617 // its implementation; if any.
1618 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001619 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1620 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001621 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001622
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001623 // check all methods implemented in category against those declared
1624 // in its primary class.
1625 if (ObjCCategoryImplDecl *CatDecl =
1626 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1627 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Chris Lattner4d391482007-12-12 07:09:47 +00001629 // Check the protocol list for unimplemented methods in the @implementation
1630 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001631 // Check and see if class methods in class interface have been
1632 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Chris Lattnercddc8882009-03-01 00:56:52 +00001634 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001635 for (ObjCInterfaceDecl::all_protocol_iterator
1636 PI = I->all_referenced_protocol_begin(),
1637 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001638 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001639 InsMap, ClsMap, I);
1640 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001641 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1642 Categories; Categories = Categories->getNextClassExtension())
1643 ImplMethodsVsClassMethods(S, IMPDecl,
1644 const_cast<ObjCCategoryDecl*>(Categories),
1645 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001646 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001647 // For extended class, unimplemented methods in its protocols will
1648 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001649 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001650 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1651 E = C->protocol_end(); PI != E; ++PI)
1652 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001653 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001654 // Report unimplemented properties in the category as well.
1655 // When reporting on missing setter/getters, do not report when
1656 // setter/getter is implemented in category's primary class
1657 // implementation.
1658 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1659 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1660 for (ObjCImplementationDecl::instmeth_iterator
1661 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1662 InsMap.insert((*I)->getSelector());
1663 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001664 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001665 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001666 } else
1667 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001668}
1669
Mike Stump1eb44332009-09-09 15:08:12 +00001670/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001671Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001672Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001673 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001674 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001675 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001676 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001677 for (unsigned i = 0; i != NumElts; ++i) {
1678 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001679 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001680 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001681 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001682 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001683 // Maybe we will complain about the shadowed template parameter.
1684 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1685 // Just pretend that we didn't see the previous declaration.
1686 PrevDecl = 0;
1687 }
1688
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001689 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001690 // GCC apparently allows the following idiom:
1691 //
1692 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1693 // @class XCElementToggler;
1694 //
Mike Stump1eb44332009-09-09 15:08:12 +00001695 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001696 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001697 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001698 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001699 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001700 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001701 // a forward class declaration matching a typedef name of a class refers
1702 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001703 if (const ObjCObjectType *OI =
1704 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1705 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001706 }
Chris Lattner4d391482007-12-12 07:09:47 +00001707 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001708 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1709 if (!IDecl) { // Not already seen? Make a forward decl.
1710 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1711 IdentList[i], IdentLocs[i], true);
1712
1713 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1714 // the current DeclContext. This prevents clients that walk DeclContext
1715 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1716 // declared later (if at all). We also take care to explicitly make
1717 // sure this declaration is visible for name lookup.
1718 PushOnScopeChains(IDecl, TUScope, false);
1719 CurContext->makeDeclVisibleInContext(IDecl, true);
1720 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001721 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1722 IDecl, IdentLocs[i]);
1723 CurContext->addDecl(CDecl);
1724 CheckObjCDeclScope(CDecl);
1725 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001726 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001727
1728 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001729}
1730
John McCall0f4c4c42011-06-16 01:15:19 +00001731static bool tryMatchRecordTypes(ASTContext &Context,
1732 Sema::MethodMatchStrategy strategy,
1733 const Type *left, const Type *right);
1734
John McCallf85e1932011-06-15 23:02:42 +00001735static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1736 QualType leftQT, QualType rightQT) {
1737 const Type *left =
1738 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1739 const Type *right =
1740 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1741
1742 if (left == right) return true;
1743
1744 // If we're doing a strict match, the types have to match exactly.
1745 if (strategy == Sema::MMS_strict) return false;
1746
1747 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1748
1749 // Otherwise, use this absurdly complicated algorithm to try to
1750 // validate the basic, low-level compatibility of the two types.
1751
1752 // As a minimum, require the sizes and alignments to match.
1753 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1754 return false;
1755
1756 // Consider all the kinds of non-dependent canonical types:
1757 // - functions and arrays aren't possible as return and parameter types
1758
1759 // - vector types of equal size can be arbitrarily mixed
1760 if (isa<VectorType>(left)) return isa<VectorType>(right);
1761 if (isa<VectorType>(right)) return false;
1762
1763 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001764 // - structs, unions, and Objective-C objects must match more-or-less
1765 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001766 // - everything else should be a scalar
1767 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001768 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001769
1770 // Make scalars agree in kind, except count bools as chars.
1771 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1772 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1773 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1774 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1775
1776 // Note that data member pointers and function member pointers don't
1777 // intermix because of the size differences.
1778
1779 return (leftSK == rightSK);
1780}
Chris Lattner4d391482007-12-12 07:09:47 +00001781
John McCall0f4c4c42011-06-16 01:15:19 +00001782static bool tryMatchRecordTypes(ASTContext &Context,
1783 Sema::MethodMatchStrategy strategy,
1784 const Type *lt, const Type *rt) {
1785 assert(lt && rt && lt != rt);
1786
1787 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1788 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1789 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1790
1791 // Require union-hood to match.
1792 if (left->isUnion() != right->isUnion()) return false;
1793
1794 // Require an exact match if either is non-POD.
1795 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1796 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1797 return false;
1798
1799 // Require size and alignment to match.
1800 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1801
1802 // Require fields to match.
1803 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1804 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1805 for (; li != le && ri != re; ++li, ++ri) {
1806 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1807 return false;
1808 }
1809 return (li == le && ri == re);
1810}
1811
Chris Lattner4d391482007-12-12 07:09:47 +00001812/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1813/// returns true, or false, accordingly.
1814/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001815bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1816 const ObjCMethodDecl *right,
1817 MethodMatchStrategy strategy) {
1818 if (!matchTypes(Context, strategy,
1819 left->getResultType(), right->getResultType()))
1820 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001821
John McCallf85e1932011-06-15 23:02:42 +00001822 if (getLangOptions().ObjCAutoRefCount &&
1823 (left->hasAttr<NSReturnsRetainedAttr>()
1824 != right->hasAttr<NSReturnsRetainedAttr>() ||
1825 left->hasAttr<NSConsumesSelfAttr>()
1826 != right->hasAttr<NSConsumesSelfAttr>()))
1827 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001828
John McCallf85e1932011-06-15 23:02:42 +00001829 ObjCMethodDecl::param_iterator
1830 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001831
John McCallf85e1932011-06-15 23:02:42 +00001832 for (; li != le; ++li, ++ri) {
1833 assert(ri != right->param_end() && "Param mismatch");
1834 ParmVarDecl *lparm = *li, *rparm = *ri;
1835
1836 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1837 return false;
1838
1839 if (getLangOptions().ObjCAutoRefCount &&
1840 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1841 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001842 }
1843 return true;
1844}
1845
Sebastian Redldb9d2142010-08-02 23:18:59 +00001846/// \brief Read the contents of the method pool for a given selector from
1847/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001848///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001849/// This routine should only be called once, when the method pool has no entry
1850/// for this selector.
1851Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001852 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001853 assert(MethodPool.find(Sel) == MethodPool.end() &&
1854 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001855
1856 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001857 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Sebastian Redldb9d2142010-08-02 23:18:59 +00001859 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001860}
1861
Sebastian Redldb9d2142010-08-02 23:18:59 +00001862void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1863 bool instance) {
1864 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1865 if (Pos == MethodPool.end()) {
1866 if (ExternalSource)
1867 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001868 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001869 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1870 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001871 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001872 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001873 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001874 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001875 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001876 Entry.Method = Method;
1877 Entry.Next = 0;
1878 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001879 }
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Chris Lattnerb25df352009-03-04 05:16:45 +00001881 // We've seen a method with this name, see if we have already seen this type
1882 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001883 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1884 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1885
1886 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001887 ObjCMethodDecl *PrevObjCMethod = List->Method;
1888 PrevObjCMethod->setDefined(impl);
1889 // If a method is deprecated, push it in the global pool.
1890 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001891 if (Method->isDeprecated()) {
1892 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001893 List->Method = Method;
1894 }
1895 // If new method is unavailable, push it into global pool
1896 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001897 if (Method->isUnavailable()) {
1898 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001899 List->Method = Method;
1900 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001901 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001902 }
John McCallf85e1932011-06-15 23:02:42 +00001903 }
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Chris Lattnerb25df352009-03-04 05:16:45 +00001905 // We have a new signature for an existing method - add it.
1906 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001907 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1908 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001909}
1910
John McCallf85e1932011-06-15 23:02:42 +00001911/// Determines if this is an "acceptable" loose mismatch in the global
1912/// method pool. This exists mostly as a hack to get around certain
1913/// global mismatches which we can't afford to make warnings / errors.
1914/// Really, what we want is a way to take a method out of the global
1915/// method pool.
1916static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1917 ObjCMethodDecl *other) {
1918 if (!chosen->isInstanceMethod())
1919 return false;
1920
1921 Selector sel = chosen->getSelector();
1922 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1923 return false;
1924
1925 // Don't complain about mismatches for -length if the method we
1926 // chose has an integral result type.
1927 return (chosen->getResultType()->isIntegerType());
1928}
1929
Sebastian Redldb9d2142010-08-02 23:18:59 +00001930ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001931 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001932 bool warn, bool instance) {
1933 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1934 if (Pos == MethodPool.end()) {
1935 if (ExternalSource)
1936 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001937 else
1938 return 0;
1939 }
1940
Sebastian Redldb9d2142010-08-02 23:18:59 +00001941 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Sebastian Redldb9d2142010-08-02 23:18:59 +00001943 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001944 bool issueDiagnostic = false, issueError = false;
1945
1946 // We support a warning which complains about *any* difference in
1947 // method signature.
1948 bool strictSelectorMatch =
1949 (receiverIdOrClass && warn &&
1950 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1951 R.getBegin()) !=
1952 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001953 if (strictSelectorMatch)
1954 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001955 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1956 MMS_strict)) {
1957 issueDiagnostic = true;
1958 break;
1959 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001960 }
1961
John McCallf85e1932011-06-15 23:02:42 +00001962 // If we didn't see any strict differences, we won't see any loose
1963 // differences. In ARC, however, we also need to check for loose
1964 // mismatches, because most of them are errors.
1965 if (!strictSelectorMatch ||
1966 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001967 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001968 // This checks if the methods differ in type mismatch.
1969 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1970 MMS_loose) &&
1971 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1972 issueDiagnostic = true;
1973 if (getLangOptions().ObjCAutoRefCount)
1974 issueError = true;
1975 break;
1976 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001977 }
1978
John McCallf85e1932011-06-15 23:02:42 +00001979 if (issueDiagnostic) {
1980 if (issueError)
1981 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1982 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001983 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1984 else
1985 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001986
1987 Diag(MethList.Method->getLocStart(),
1988 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001989 << MethList.Method->getSourceRange();
1990 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1991 Diag(Next->Method->getLocStart(), diag::note_also_found)
1992 << Next->Method->getSourceRange();
1993 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001994 }
1995 return MethList.Method;
1996}
1997
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001998ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001999 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2000 if (Pos == MethodPool.end())
2001 return 0;
2002
2003 GlobalMethods &Methods = Pos->second;
2004
2005 if (Methods.first.Method && Methods.first.Method->isDefined())
2006 return Methods.first.Method;
2007 if (Methods.second.Method && Methods.second.Method->isDefined())
2008 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002009 return 0;
2010}
2011
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002012/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2013/// identical selector names in current and its super classes and issues
2014/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002015void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2016 ObjCMethodDecl *Method,
2017 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002018 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2019 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002021 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002022 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002023 SD->lookupMethod(Method->getSelector(), IsInstance);
2024 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002025 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002026 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002027 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002028 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2029 E = Method->param_end();
2030 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2031 for (; ParamI != E; ++ParamI, ++PrevI) {
2032 // Number of parameters are the same and is guaranteed by selector match.
2033 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2034 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2035 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002036 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002037 // respective argument type in the super class method, issue warning;
2038 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002039 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002040 << T1 << T2;
2041 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2042 return;
2043 }
2044 }
2045 ID = SD;
2046 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002047}
2048
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002049/// DiagnoseDuplicateIvars -
2050/// Check for duplicate ivars in the entire class at the start of
2051/// @implementation. This becomes necesssary because class extension can
2052/// add ivars to a class in random order which will not be known until
2053/// class's @implementation is seen.
2054void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2055 ObjCInterfaceDecl *SID) {
2056 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2057 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2058 ObjCIvarDecl* Ivar = (*IVI);
2059 if (Ivar->isInvalidDecl())
2060 continue;
2061 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2062 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2063 if (prevIvar) {
2064 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2065 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2066 Ivar->setInvalidDecl();
2067 }
2068 }
2069 }
2070}
2071
Steve Naroffa56f6162007-12-18 01:30:32 +00002072// Note: For class/category implemenations, allMethods/allProperties is
2073// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002074void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002075 Decl **allMethods, unsigned allNum,
2076 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002077 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002078
2079 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002080 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002081 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2082 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002083
Mike Stump1eb44332009-09-09 15:08:12 +00002084 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002085 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2086 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002087 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002088
Ted Kremenek782f2f52010-01-07 01:20:12 +00002089 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2090 // FIXME: This is wrong. We shouldn't be pretending that there is
2091 // an '@end' in the declaration.
2092 SourceLocation L = ClassDecl->getLocation();
2093 AtEnd.setBegin(L);
2094 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002095 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002096 }
2097
Steve Naroff0701bbb2009-01-08 17:28:14 +00002098 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2099 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2100 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2101
Chris Lattner4d391482007-12-12 07:09:47 +00002102 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002103 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002104 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002105
2106 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002107 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002108 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002109 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002110 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002111 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002112 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002113 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002114 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002115 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002116 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002117 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002118 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002119 InsMap[Method->getSelector()] = Method;
2120 /// The following allows us to typecheck messages to "id".
2121 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002122 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002123 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002124 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002125 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002126 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002127 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002128 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002129 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002130 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002131 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002132 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002133 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002134 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002135 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002136 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002137 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002138 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002139 /// The following allows us to typecheck messages to "Class".
2140 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002141 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002142 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002143 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002144 }
2145 }
2146 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002147 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002148 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002149 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002150 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002151 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002152 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002153 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002154 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002155 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002156
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002157 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002158 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002159 if (C->IsClassExtension()) {
2160 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2161 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002162 }
Chris Lattner4d391482007-12-12 07:09:47 +00002163 }
Steve Naroff09c47192009-01-09 15:36:25 +00002164 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002165 if (CDecl->getIdentifier())
2166 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2167 // user-defined setter/getter. It also synthesizes setter/getter methods
2168 // and adds them to the DeclContext and global method pools.
2169 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2170 E = CDecl->prop_end();
2171 I != E; ++I)
2172 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002173 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002174 }
2175 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002176 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002177 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002178 // Any property declared in a class extension might have user
2179 // declared setter or getter in current class extension or one
2180 // of the other class extensions. Mark them as synthesized as
2181 // property will be synthesized when property with same name is
2182 // seen in the @implementation.
2183 for (const ObjCCategoryDecl *ClsExtDecl =
2184 IDecl->getFirstClassExtension();
2185 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2186 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2187 E = ClsExtDecl->prop_end(); I != E; ++I) {
2188 ObjCPropertyDecl *Property = (*I);
2189 // Skip over properties declared @dynamic
2190 if (const ObjCPropertyImplDecl *PIDecl
2191 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2192 if (PIDecl->getPropertyImplementation()
2193 == ObjCPropertyImplDecl::Dynamic)
2194 continue;
2195
2196 for (const ObjCCategoryDecl *CExtDecl =
2197 IDecl->getFirstClassExtension();
2198 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2199 if (ObjCMethodDecl *GetterMethod =
2200 CExtDecl->getInstanceMethod(Property->getGetterName()))
2201 GetterMethod->setSynthesized(true);
2202 if (!Property->isReadOnly())
2203 if (ObjCMethodDecl *SetterMethod =
2204 CExtDecl->getInstanceMethod(Property->getSetterName()))
2205 SetterMethod->setSynthesized(true);
2206 }
2207 }
2208 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002209 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002210 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002211 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002212
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002213 if (LangOpts.ObjCNonFragileABI2)
2214 while (IDecl->getSuperClass()) {
2215 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2216 IDecl = IDecl->getSuperClass();
2217 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002218 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002219 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002220 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002221 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002222 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Chris Lattner4d391482007-12-12 07:09:47 +00002224 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002225 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002226 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002227 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002228 Categories; Categories = Categories->getNextClassCategory()) {
2229 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002230 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002231 break;
2232 }
2233 }
2234 }
2235 }
Chris Lattner682bf922009-03-29 16:50:03 +00002236 if (isInterfaceDeclKind) {
2237 // Reject invalid vardecls.
2238 for (unsigned i = 0; i != tuvNum; i++) {
2239 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2240 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2241 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002242 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002243 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002244 }
Chris Lattner682bf922009-03-29 16:50:03 +00002245 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002246 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002247 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002248}
2249
2250
2251/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2252/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002253static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002254CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002255 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002256}
2257
Ted Kremenek422bae72010-04-18 04:59:38 +00002258static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002259bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002260 // The 'ibaction' attribute is allowed on method definitions because of
2261 // how the IBAction macro is used on both method declarations and definitions.
2262 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002263 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2264 if ((*i)->getKind() != attr::IBAction)
2265 return true;
2266 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002267}
2268
Douglas Gregore97179c2011-09-08 01:46:34 +00002269namespace {
2270 /// \brief Describes the compatibility of a result type with its method.
2271 enum ResultTypeCompatibilityKind {
2272 RTC_Compatible,
2273 RTC_Incompatible,
2274 RTC_Unknown
2275 };
2276}
2277
Douglas Gregor926df6c2011-06-11 01:09:30 +00002278/// \brief Check whether the declared result type of the given Objective-C
2279/// method declaration is compatible with the method's class.
2280///
Douglas Gregore97179c2011-09-08 01:46:34 +00002281static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002282CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2283 ObjCInterfaceDecl *CurrentClass) {
2284 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002285
2286 // If an Objective-C method inherits its related result type, then its
2287 // declared result type must be compatible with its own class type. The
2288 // declared result type is compatible if:
2289 if (const ObjCObjectPointerType *ResultObjectType
2290 = ResultType->getAs<ObjCObjectPointerType>()) {
2291 // - it is id or qualified id, or
2292 if (ResultObjectType->isObjCIdType() ||
2293 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002294 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002295
2296 if (CurrentClass) {
2297 if (ObjCInterfaceDecl *ResultClass
2298 = ResultObjectType->getInterfaceDecl()) {
2299 // - it is the same as the method's class type, or
2300 if (CurrentClass == ResultClass)
Douglas Gregore97179c2011-09-08 01:46:34 +00002301 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002302
2303 // - it is a superclass of the method's class type
2304 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002305 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002306 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002307 } else {
2308 // Any Objective-C pointer type might be acceptable for a protocol
2309 // method; we just don't know.
2310 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002311 }
2312 }
2313
Douglas Gregore97179c2011-09-08 01:46:34 +00002314 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002315}
2316
John McCall6c2c2502011-07-22 02:45:48 +00002317namespace {
2318/// A helper class for searching for methods which a particular method
2319/// overrides.
2320class OverrideSearch {
2321 Sema &S;
2322 ObjCMethodDecl *Method;
2323 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2324 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2325 bool Recursive;
2326
2327public:
2328 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2329 Selector selector = method->getSelector();
2330
2331 // Bypass this search if we've never seen an instance/class method
2332 // with this selector before.
2333 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2334 if (it == S.MethodPool.end()) {
2335 if (!S.ExternalSource) return;
2336 it = S.ReadMethodPool(selector);
2337 }
2338 ObjCMethodList &list =
2339 method->isInstanceMethod() ? it->second.first : it->second.second;
2340 if (!list.Method) return;
2341
2342 ObjCContainerDecl *container
2343 = cast<ObjCContainerDecl>(method->getDeclContext());
2344
2345 // Prevent the search from reaching this container again. This is
2346 // important with categories, which override methods from the
2347 // interface and each other.
2348 Searched.insert(container);
2349 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002350 }
John McCall6c2c2502011-07-22 02:45:48 +00002351
2352 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2353 iterator begin() const { return Overridden.begin(); }
2354 iterator end() const { return Overridden.end(); }
2355
2356private:
2357 void searchFromContainer(ObjCContainerDecl *container) {
2358 if (container->isInvalidDecl()) return;
2359
2360 switch (container->getDeclKind()) {
2361#define OBJCCONTAINER(type, base) \
2362 case Decl::type: \
2363 searchFrom(cast<type##Decl>(container)); \
2364 break;
2365#define ABSTRACT_DECL(expansion)
2366#define DECL(type, base) \
2367 case Decl::type:
2368#include "clang/AST/DeclNodes.inc"
2369 llvm_unreachable("not an ObjC container!");
2370 }
2371 }
2372
2373 void searchFrom(ObjCProtocolDecl *protocol) {
2374 // A method in a protocol declaration overrides declarations from
2375 // referenced ("parent") protocols.
2376 search(protocol->getReferencedProtocols());
2377 }
2378
2379 void searchFrom(ObjCCategoryDecl *category) {
2380 // A method in a category declaration overrides declarations from
2381 // the main class and from protocols the category references.
2382 search(category->getClassInterface());
2383 search(category->getReferencedProtocols());
2384 }
2385
2386 void searchFrom(ObjCCategoryImplDecl *impl) {
2387 // A method in a category definition that has a category
2388 // declaration overrides declarations from the category
2389 // declaration.
2390 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2391 search(category);
2392
2393 // Otherwise it overrides declarations from the class.
2394 } else {
2395 search(impl->getClassInterface());
2396 }
2397 }
2398
2399 void searchFrom(ObjCInterfaceDecl *iface) {
2400 // A method in a class declaration overrides declarations from
2401
2402 // - categories,
2403 for (ObjCCategoryDecl *category = iface->getCategoryList();
2404 category; category = category->getNextClassCategory())
2405 search(category);
2406
2407 // - the super class, and
2408 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2409 search(super);
2410
2411 // - any referenced protocols.
2412 search(iface->getReferencedProtocols());
2413 }
2414
2415 void searchFrom(ObjCImplementationDecl *impl) {
2416 // A method in a class implementation overrides declarations from
2417 // the class interface.
2418 search(impl->getClassInterface());
2419 }
2420
2421
2422 void search(const ObjCProtocolList &protocols) {
2423 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2424 i != e; ++i)
2425 search(*i);
2426 }
2427
2428 void search(ObjCContainerDecl *container) {
2429 // Abort if we've already searched this container.
2430 if (!Searched.insert(container)) return;
2431
2432 // Check for a method in this container which matches this selector.
2433 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2434 Method->isInstanceMethod());
2435
2436 // If we find one, record it and bail out.
2437 if (meth) {
2438 Overridden.insert(meth);
2439 return;
2440 }
2441
2442 // Otherwise, search for methods that a hypothetical method here
2443 // would have overridden.
2444
2445 // Note that we're now in a recursive case.
2446 Recursive = true;
2447
2448 searchFromContainer(container);
2449 }
2450};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002451}
2452
John McCalld226f652010-08-21 09:40:31 +00002453Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002454 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002455 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002456 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002457 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002458 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002459 Selector Sel,
2460 // optional arguments. The number of types/arguments is obtained
2461 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002462 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002463 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002464 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002465 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002466 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002467 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002468 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002469 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002470 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002471 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2472 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002473 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Douglas Gregore97179c2011-09-08 01:46:34 +00002475 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002476 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002477 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002478 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002479
Steve Naroffccef3712009-02-20 22:59:16 +00002480 // Methods cannot return interface types. All ObjC objects are
2481 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002482 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002483 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2484 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002485 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002486 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002487
2488 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002489 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002490 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002491 Diag(MethodLoc, diag::warn_missing_method_return_type)
2492 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002493 }
Mike Stump1eb44332009-09-09 15:08:12 +00002494
2495 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002496 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002497 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002498 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002499 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002500 /*isSynthesized=*/false,
2501 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002502 MethodDeclKind == tok::objc_optional
2503 ? ObjCMethodDecl::Optional
2504 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002505 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002506
Chris Lattner5f9e2722011-07-23 10:55:15 +00002507 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002508
Chris Lattner7db638d2009-04-11 19:42:43 +00002509 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002510 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002511 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002512
Chris Lattnere294d3f2009-04-11 18:57:04 +00002513 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002514 ArgType = Context.getObjCIdType();
2515 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002516 } else {
John McCall58e46772009-10-23 21:48:59 +00002517 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002518 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002519 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002520 }
Mike Stump1eb44332009-09-09 15:08:12 +00002521
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002522 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2523 LookupOrdinaryName, ForRedeclaration);
2524 LookupName(R, S);
2525 if (R.isSingleResult()) {
2526 NamedDecl *PrevDecl = R.getFoundDecl();
2527 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002528 Diag(ArgInfo[i].NameLoc,
2529 (MethodDefinition ? diag::warn_method_param_redefinition
2530 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002531 << ArgInfo[i].Name;
2532 Diag(PrevDecl->getLocation(),
2533 diag::note_previous_declaration);
2534 }
2535 }
2536
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002537 SourceLocation StartLoc = DI
2538 ? DI->getTypeLoc().getBeginLoc()
2539 : ArgInfo[i].NameLoc;
2540
John McCall81ef3e62011-04-23 02:46:06 +00002541 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2542 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2543 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002544
John McCall70798862011-05-02 00:30:12 +00002545 Param->setObjCMethodScopeInfo(i);
2546
Chris Lattner0ed844b2008-04-04 06:12:32 +00002547 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002548 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002550 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002551 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002552
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002553 S->AddDecl(Param);
2554 IdResolver.AddDecl(Param);
2555
Chris Lattner0ed844b2008-04-04 06:12:32 +00002556 Params.push_back(Param);
2557 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002558
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002559 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002560 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002561 QualType ArgType = Param->getType();
2562 if (ArgType.isNull())
2563 ArgType = Context.getObjCIdType();
2564 else
2565 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002566 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002567 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002568 Diag(Param->getLocation(),
2569 diag::err_object_cannot_be_passed_returned_by_value)
2570 << 1 << ArgType;
2571 Param->setInvalidDecl();
2572 }
2573 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002574
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002575 Params.push_back(Param);
2576 }
2577
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002578 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2579 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002580 ObjCMethod->setObjCDeclQualifier(
2581 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002582
2583 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002584 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002586 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002587 const ObjCMethodDecl *PrevMethod = 0;
2588 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002589 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002590 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2591 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002592 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002593 PrevMethod = ImpDecl->getClassMethod(Sel);
2594 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002595 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002596
Sean Huntcf807c42010-08-18 23:23:40 +00002597 if (ObjCMethod->hasAttrs() &&
2598 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002599 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002600 } else {
2601 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002602 }
John McCall6c2c2502011-07-22 02:45:48 +00002603
Chris Lattner4d391482007-12-12 07:09:47 +00002604 if (PrevMethod) {
2605 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002606 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002607 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002608 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002609 }
John McCall54abf7d2009-11-04 02:18:39 +00002610
Douglas Gregor926df6c2011-06-11 01:09:30 +00002611 // If this Objective-C method does not have a related result type, but we
2612 // are allowed to infer related result types, try to do so based on the
2613 // method family.
2614 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2615 if (!CurrentClass) {
2616 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2617 CurrentClass = Cat->getClassInterface();
2618 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2619 CurrentClass = Impl->getClassInterface();
2620 else if (ObjCCategoryImplDecl *CatImpl
2621 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2622 CurrentClass = CatImpl->getClassInterface();
2623 }
John McCall6c2c2502011-07-22 02:45:48 +00002624
Douglas Gregore97179c2011-09-08 01:46:34 +00002625 ResultTypeCompatibilityKind RTC
2626 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002627
2628 // Search for overridden methods and merge information down from them.
2629 OverrideSearch overrides(*this, ObjCMethod);
2630 for (OverrideSearch::iterator
2631 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2632 ObjCMethodDecl *overridden = *i;
2633
2634 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002635 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002636 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002637
2638 // Then merge the declarations.
2639 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002640
2641 // Check for overriding methods
2642 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2643 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2644 WarnConflictingTypedMethods(ObjCMethod, overridden,
2645 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2646 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002647 }
2648
John McCallf85e1932011-06-15 23:02:42 +00002649 bool ARCError = false;
2650 if (getLangOptions().ObjCAutoRefCount)
2651 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2652
Douglas Gregore97179c2011-09-08 01:46:34 +00002653 // Infer the related result type when possible.
2654 if (!ARCError && RTC == RTC_Compatible &&
2655 !ObjCMethod->hasRelatedResultType() &&
2656 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002657 bool InferRelatedResultType = false;
2658 switch (ObjCMethod->getMethodFamily()) {
2659 case OMF_None:
2660 case OMF_copy:
2661 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002662 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002663 case OMF_mutableCopy:
2664 case OMF_release:
2665 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002666 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002667 break;
2668
2669 case OMF_alloc:
2670 case OMF_new:
2671 InferRelatedResultType = ObjCMethod->isClassMethod();
2672 break;
2673
2674 case OMF_init:
2675 case OMF_autorelease:
2676 case OMF_retain:
2677 case OMF_self:
2678 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2679 break;
2680 }
2681
John McCall6c2c2502011-07-22 02:45:48 +00002682 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002683 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002684 }
2685
John McCalld226f652010-08-21 09:40:31 +00002686 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002687}
2688
Chris Lattnercc98eac2008-12-17 07:13:27 +00002689bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002690 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002691 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002692 // Following is also an error. But it is caused by a missing @end
2693 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002694 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2695 return false;
2696 }
2697
Anders Carlsson15281452008-11-04 16:57:32 +00002698 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2699 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002700
Anders Carlsson15281452008-11-04 16:57:32 +00002701 return true;
2702}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002703
Chris Lattnercc98eac2008-12-17 07:13:27 +00002704/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2705/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002706void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002707 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002708 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002709 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002710 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002711 if (!Class) {
2712 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2713 return;
2714 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002715 if (LangOpts.ObjCNonFragileABI) {
2716 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2717 return;
2718 }
Mike Stump1eb44332009-09-09 15:08:12 +00002719
Chris Lattnercc98eac2008-12-17 07:13:27 +00002720 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002721 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002722 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002723 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002724 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002725 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002726 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002727 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2728 /*FIXME: StartL=*/ID->getLocation(),
2729 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002730 ID->getIdentifier(), ID->getType(),
2731 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002732 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002733 }
Mike Stump1eb44332009-09-09 15:08:12 +00002734
Chris Lattnercc98eac2008-12-17 07:13:27 +00002735 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002736 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002737 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002738 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002739 if (getLangOptions().CPlusPlus)
2740 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002741 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002742 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002743 }
2744}
2745
Douglas Gregor160b5632010-04-26 17:32:49 +00002746/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002747VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2748 SourceLocation StartLoc,
2749 SourceLocation IdLoc,
2750 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002751 bool Invalid) {
2752 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2753 // duration shall not be qualified by an address-space qualifier."
2754 // Since all parameters have automatic store duration, they can not have
2755 // an address space.
2756 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002757 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002758 Invalid = true;
2759 }
2760
2761 // An @catch parameter must be an unqualified object pointer type;
2762 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2763 if (Invalid) {
2764 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002765 } else if (T->isDependentType()) {
2766 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002767 } else if (!T->isObjCObjectPointerType()) {
2768 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002769 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002770 } else if (T->isObjCQualifiedIdType()) {
2771 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002772 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002773 }
2774
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002775 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2776 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002777 New->setExceptionVariable(true);
2778
Douglas Gregor160b5632010-04-26 17:32:49 +00002779 if (Invalid)
2780 New->setInvalidDecl();
2781 return New;
2782}
2783
John McCalld226f652010-08-21 09:40:31 +00002784Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002785 const DeclSpec &DS = D.getDeclSpec();
2786
2787 // We allow the "register" storage class on exception variables because
2788 // GCC did, but we drop it completely. Any other storage class is an error.
2789 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2790 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2791 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2792 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2793 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2794 << DS.getStorageClassSpec();
2795 }
2796 if (D.getDeclSpec().isThreadSpecified())
2797 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2798 D.getMutableDeclSpec().ClearStorageClassSpecs();
2799
2800 DiagnoseFunctionSpecifiers(D);
2801
2802 // Check that there are no default arguments inside the type of this
2803 // exception object (C++ only).
2804 if (getLangOptions().CPlusPlus)
2805 CheckExtraCXXDefaultArguments(D);
2806
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002807 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002808 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002809
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002810 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2811 D.getSourceRange().getBegin(),
2812 D.getIdentifierLoc(),
2813 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002814 D.isInvalidType());
2815
2816 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2817 if (D.getCXXScopeSpec().isSet()) {
2818 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2819 << D.getCXXScopeSpec().getRange();
2820 New->setInvalidDecl();
2821 }
2822
2823 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002824 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002825 if (D.getIdentifier())
2826 IdResolver.AddDecl(New);
2827
2828 ProcessDeclAttributes(S, New, D);
2829
2830 if (New->hasAttr<BlocksAttr>())
2831 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002832 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002833}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002834
2835/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002836/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002837void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002838 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002839 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2840 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002841 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002842 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002843 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002844 }
2845}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002846
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002847void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002848 // Load referenced selectors from the external source.
2849 if (ExternalSource) {
2850 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2851 ExternalSource->ReadReferencedSelectors(Sels);
2852 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2853 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2854 }
2855
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002856 // Warning will be issued only when selector table is
2857 // generated (which means there is at lease one implementation
2858 // in the TU). This is to match gcc's behavior.
2859 if (ReferencedSelectors.empty() ||
2860 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002861 return;
2862 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2863 ReferencedSelectors.begin(),
2864 E = ReferencedSelectors.end(); S != E; ++S) {
2865 Selector Sel = (*S).first;
2866 if (!LookupImplementedMethodInGlobalPool(Sel))
2867 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2868 }
2869 return;
2870}