blob: 35e32d4ad9d350add0ecd5a63d1e4140a1f558ab [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000026#include "llvm/ADT/DenseSet.h"
27
Chris Lattner4d391482007-12-12 07:09:47 +000028using namespace clang;
29
John McCallf85e1932011-06-15 23:02:42 +000030/// Check whether the given method, which must be in the 'init'
31/// family, is a valid member of that family.
32///
33/// \param receiverTypeIfCall - if null, check this as if declaring it;
34/// if non-null, check this as if making a call to it with the given
35/// receiver type
36///
37/// \return true to indicate that there was an error and appropriate
38/// actions were taken
39bool Sema::checkInitMethod(ObjCMethodDecl *method,
40 QualType receiverTypeIfCall) {
41 if (method->isInvalidDecl()) return true;
42
43 // This castAs is safe: methods that don't return an object
44 // pointer won't be inferred as inits and will reject an explicit
45 // objc_method_family(init).
46
47 // We ignore protocols here. Should we? What about Class?
48
49 const ObjCObjectType *result = method->getResultType()
50 ->castAs<ObjCObjectPointerType>()->getObjectType();
51
52 if (result->isObjCId()) {
53 return false;
54 } else if (result->isObjCClass()) {
55 // fall through: always an error
56 } else {
57 ObjCInterfaceDecl *resultClass = result->getInterface();
58 assert(resultClass && "unexpected object type!");
59
60 // It's okay for the result type to still be a forward declaration
61 // if we're checking an interface declaration.
62 if (resultClass->isForwardDecl()) {
63 if (receiverTypeIfCall.isNull() &&
64 !isa<ObjCImplementationDecl>(method->getDeclContext()))
65 return false;
66
67 // Otherwise, we try to compare class types.
68 } else {
69 // If this method was declared in a protocol, we can't check
70 // anything unless we have a receiver type that's an interface.
71 const ObjCInterfaceDecl *receiverClass = 0;
72 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
73 if (receiverTypeIfCall.isNull())
74 return false;
75
76 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
77 ->getInterfaceDecl();
78
79 // This can be null for calls to e.g. id<Foo>.
80 if (!receiverClass) return false;
81 } else {
82 receiverClass = method->getClassInterface();
83 assert(receiverClass && "method not associated with a class!");
84 }
85
86 // If either class is a subclass of the other, it's fine.
87 if (receiverClass->isSuperClassOf(resultClass) ||
88 resultClass->isSuperClassOf(receiverClass))
89 return false;
90 }
91 }
92
93 SourceLocation loc = method->getLocation();
94
95 // If we're in a system header, and this is not a call, just make
96 // the method unusable.
97 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
98 method->addAttr(new (Context) UnavailableAttr(loc, Context,
99 "init method returns a type unrelated to its receiver type"));
100 return true;
101 }
102
103 // Otherwise, it's an error.
104 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
105 method->setInvalidDecl();
106 return true;
107}
108
Douglas Gregor926df6c2011-06-11 01:09:30 +0000109bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
110 const ObjCMethodDecl *Overridden,
111 bool IsImplementation) {
112 if (Overridden->hasRelatedResultType() &&
113 !NewMethod->hasRelatedResultType()) {
114 // This can only happen when the method follows a naming convention that
115 // implies a related result type, and the original (overridden) method has
116 // a suitable return type, but the new (overriding) method does not have
117 // a suitable return type.
118 QualType ResultType = NewMethod->getResultType();
119 SourceRange ResultTypeRange;
120 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000121 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000122 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
123
124 // Figure out which class this method is part of, if any.
125 ObjCInterfaceDecl *CurrentClass
126 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
127 if (!CurrentClass) {
128 DeclContext *DC = NewMethod->getDeclContext();
129 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
130 CurrentClass = Cat->getClassInterface();
131 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
132 CurrentClass = Impl->getClassInterface();
133 else if (ObjCCategoryImplDecl *CatImpl
134 = dyn_cast<ObjCCategoryImplDecl>(DC))
135 CurrentClass = CatImpl->getClassInterface();
136 }
137
138 if (CurrentClass) {
139 Diag(NewMethod->getLocation(),
140 diag::warn_related_result_type_compatibility_class)
141 << Context.getObjCInterfaceType(CurrentClass)
142 << ResultType
143 << ResultTypeRange;
144 } else {
145 Diag(NewMethod->getLocation(),
146 diag::warn_related_result_type_compatibility_protocol)
147 << ResultType
148 << ResultTypeRange;
149 }
150
151 Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
152 << Overridden->getMethodFamily();
153 }
154
155 return false;
156}
157
John McCallf85e1932011-06-15 23:02:42 +0000158/// \brief Check a method declaration for compatibility with the Objective-C
159/// ARC conventions.
160static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
161 ObjCMethodFamily family = method->getMethodFamily();
162 switch (family) {
163 case OMF_None:
164 case OMF_dealloc:
165 case OMF_retain:
166 case OMF_release:
167 case OMF_autorelease:
168 case OMF_retainCount:
169 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000170 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000171 return false;
172
173 case OMF_init:
174 // If the method doesn't obey the init rules, don't bother annotating it.
175 if (S.checkInitMethod(method, QualType()))
176 return true;
177
178 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
179 S.Context));
180
181 // Don't add a second copy of this attribute, but otherwise don't
182 // let it be suppressed.
183 if (method->hasAttr<NSReturnsRetainedAttr>())
184 return false;
185 break;
186
187 case OMF_alloc:
188 case OMF_copy:
189 case OMF_mutableCopy:
190 case OMF_new:
191 if (method->hasAttr<NSReturnsRetainedAttr>() ||
192 method->hasAttr<NSReturnsNotRetainedAttr>() ||
193 method->hasAttr<NSReturnsAutoreleasedAttr>())
194 return false;
195 break;
196 }
197
198 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
199 S.Context));
200 return false;
201}
202
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000203static void DiagnoseObjCImplementedDeprecations(Sema &S,
204 NamedDecl *ND,
205 SourceLocation ImplLoc,
206 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000207 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000208 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000209 if (select == 0)
210 S.Diag(ND->getLocation(), diag::note_method_declared_at);
211 else
212 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
213 }
214}
215
Steve Naroffebf64432009-02-28 16:59:13 +0000216/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000217/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000218void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000219 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000220 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Steve Naroff394f3f42008-07-25 17:57:26 +0000222 // If we don't have a valid method decl, simply return.
223 if (!MDecl)
224 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000225
226 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000227 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000228 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000229 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000230 AddFactoryMethodToGlobalPool(MDecl, true);
231
Chris Lattner4d391482007-12-12 07:09:47 +0000232 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000233 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000234 PushFunctionScope();
235
Chris Lattner4d391482007-12-12 07:09:47 +0000236 // Create Decl objects for each parameter, entrring them in the scope for
237 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000238
239 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000240 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Daniel Dunbar451318c2008-08-26 06:07:48 +0000242 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
243 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000244
Chris Lattner8123a952008-04-10 02:22:51 +0000245 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000246 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000247 E = MDecl->param_end(); PI != E; ++PI) {
248 ParmVarDecl *Param = (*PI);
249 if (!Param->isInvalidDecl() &&
250 RequireCompleteType(Param->getLocation(), Param->getType(),
251 diag::err_typecheck_decl_incomplete_type))
252 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000253 if ((*PI)->getIdentifier())
254 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000255 }
John McCallf85e1932011-06-15 23:02:42 +0000256
257 // In ARC, disallow definition of retain/release/autorelease/retainCount
258 if (getLangOptions().ObjCAutoRefCount) {
259 switch (MDecl->getMethodFamily()) {
260 case OMF_retain:
261 case OMF_retainCount:
262 case OMF_release:
263 case OMF_autorelease:
264 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
265 << MDecl->getSelector();
266 break;
267
268 case OMF_None:
269 case OMF_dealloc:
270 case OMF_alloc:
271 case OMF_init:
272 case OMF_mutableCopy:
273 case OMF_copy:
274 case OMF_new:
275 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000276 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000277 break;
278 }
279 }
280
Nico Weber9a1ecf02011-08-22 17:25:57 +0000281 // Warn on deprecated methods under -Wdeprecated-implementations,
282 // and prepare for warning on missing super calls.
283 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000284 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000285 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000286 DiagnoseObjCImplementedDeprecations(*this,
287 dyn_cast<NamedDecl>(IMD),
288 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000289
290 // If this is "dealloc", set some bit here.
291 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
292 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
293 // Only do this if the current class actually has a superclass.
294 if (IC->getSuperClass())
295 ObjCShouldCallSuperDealloc = MDecl->getMethodFamily() == OMF_dealloc;
296 }
Chris Lattner4d391482007-12-12 07:09:47 +0000297}
298
John McCalld226f652010-08-21 09:40:31 +0000299Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000300ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
301 IdentifierInfo *ClassName, SourceLocation ClassLoc,
302 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000303 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000304 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000305 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000306 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner4d391482007-12-12 07:09:47 +0000308 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000309 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000310 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000311
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000312 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000313 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000314 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000317 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
318 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000319 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000320 if (!IDecl->isForwardDecl()) {
321 IDecl->setInvalidDecl();
322 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
323 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000324
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000325 // Return the previous class interface.
326 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000327 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000328 } else {
329 IDecl->setLocation(AtInterfaceLoc);
330 IDecl->setForwardDecl(false);
331 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000332 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000333 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000334 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000335
336 // Since this ObjCInterfaceDecl was created by a forward declaration,
337 // we now add it to the DeclContext since it wasn't added before
338 // (see ActOnForwardClassDeclaration).
339 IDecl->setLexicalDeclContext(CurContext);
340 CurContext->addDecl(IDecl);
341
342 if (AttrList)
343 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000344 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000345 } else {
346 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
347 ClassName, ClassLoc);
348 if (AttrList)
349 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
350
351 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattner4d391482007-12-12 07:09:47 +0000354 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000355 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000356 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
357 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000358
359 if (!PrevDecl) {
360 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000361 TypoCorrection Corrected = CorrectTypo(
362 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
363 NULL, NULL, false, CTC_NoKeywords);
364 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000365 Diag(SuperLoc, diag::err_undef_superclass_suggest)
366 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000367 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
368 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000369 }
370 }
371
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000372 if (PrevDecl == IDecl) {
373 Diag(SuperLoc, diag::err_recursive_superclass)
374 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
375 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000376 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000377 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000378 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000379
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000380 // Diagnose classes that inherit from deprecated classes.
381 if (SuperClassDecl)
382 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000384 if (PrevDecl && SuperClassDecl == 0) {
385 // The previous declaration was not a class decl. Check if we have a
386 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000387 if (const TypedefNameDecl *TDecl =
388 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000389 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000390 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000391 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
392 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000393 }
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000396 // This handles the following case:
397 //
398 // typedef int SuperClass;
399 // @interface MyClass : SuperClass {} @end
400 //
401 if (!SuperClassDecl) {
402 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
403 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000404 }
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Richard Smith162e1c12011-04-15 14:24:37 +0000407 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000408 if (!SuperClassDecl)
409 Diag(SuperLoc, diag::err_undef_superclass)
410 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000411 else if (SuperClassDecl->isForwardDecl()) {
412 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000413 << SuperClassDecl->getDeclName() << ClassName
414 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000415 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
416 SuperClassDecl = 0;
417 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000418 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000419 IDecl->setSuperClass(SuperClassDecl);
420 IDecl->setSuperClassLoc(SuperLoc);
421 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000422 }
Chris Lattner4d391482007-12-12 07:09:47 +0000423 } else { // we have a root class.
424 IDecl->setLocEnd(ClassLoc);
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Sebastian Redl0b17c612010-08-13 00:28:03 +0000427 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000428 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000429 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000430 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000431 IDecl->setLocEnd(EndProtoLoc);
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Anders Carlsson15281452008-11-04 16:57:32 +0000434 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000435 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000436}
437
438/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000439/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000440Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
441 IdentifierInfo *AliasName,
442 SourceLocation AliasLocation,
443 IdentifierInfo *ClassName,
444 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000445 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000446 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000447 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000448 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000449 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000450 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000451 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000452 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000453 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000454 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000455 }
456 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000457 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000458 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000459 if (const TypedefNameDecl *TDecl =
460 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000461 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000462 if (T->isObjCObjectType()) {
463 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000464 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000465 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000466 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000467 }
468 }
469 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000470 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
471 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000472 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000473 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000474 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000475 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000478 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000479 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000480 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Anders Carlsson15281452008-11-04 16:57:32 +0000482 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000483 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000484
John McCalld226f652010-08-21 09:40:31 +0000485 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000486}
487
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000488bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000489 IdentifierInfo *PName,
490 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000491 const ObjCList<ObjCProtocolDecl> &PList) {
492
493 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000494 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
495 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000496 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
497 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000498 if (PDecl->getIdentifier() == PName) {
499 Diag(Ploc, diag::err_protocol_has_circular_dependency);
500 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000501 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000502 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000503 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
504 PDecl->getLocation(), PDecl->getReferencedProtocols()))
505 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000506 }
507 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000508 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000509}
510
John McCalld226f652010-08-21 09:40:31 +0000511Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000512Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
513 IdentifierInfo *ProtocolName,
514 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000515 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000516 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000517 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000518 SourceLocation EndProtoLoc,
519 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000520 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000521 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000522 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000523 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000524 if (PDecl) {
525 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000526 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000527 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000528 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000529 // Just return the protocol we already had.
530 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000531 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000532 }
Steve Naroff61d68522009-03-05 15:22:01 +0000533 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000534 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000535 err = CheckForwardProtocolDeclarationForCircularDependency(
536 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Steve Narofff11b5082008-08-13 16:39:22 +0000538 // Make sure the cached decl gets a valid start location.
539 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000540 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000541 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000542 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000543 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000544 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000546 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000547 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000548 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000549 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000550 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000551 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000552 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000553 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000554 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
555 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000556 PDecl->setLocEnd(EndProtoLoc);
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
559 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000560 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000561}
562
563/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000564/// issues an error if they are not declared. It returns list of
565/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000566void
Chris Lattnere13b9592008-07-26 04:03:38 +0000567Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000568 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000569 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000570 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000571 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000572 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
573 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000574 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000575 TypoCorrection Corrected = CorrectTypo(
576 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
577 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
578 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000579 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000580 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000581 Diag(PDecl->getLocation(), diag::note_previous_decl)
582 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000583 }
584 }
585
586 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000587 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000588 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000589 continue;
590 }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000592 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000593
594 // If this is a forward declaration and we are supposed to warn in this
595 // case, do it.
596 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000597 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000598 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000599 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000600 }
601}
602
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000603/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000604/// a class method in its extension.
605///
Mike Stump1eb44332009-09-09 15:08:12 +0000606void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000607 ObjCInterfaceDecl *ID) {
608 if (!ID)
609 return; // Possibly due to previous error
610
611 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000612 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
613 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000614 ObjCMethodDecl *MD = *i;
615 MethodMap[MD->getSelector()] = MD;
616 }
617
618 if (MethodMap.empty())
619 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000620 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
621 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000622 ObjCMethodDecl *Method = *i;
623 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
624 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
625 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
626 << Method->getDeclName();
627 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
628 }
629 }
630}
631
Chris Lattner58fe03b2009-04-12 08:43:13 +0000632/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000633Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000634Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000635 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000636 unsigned NumElts,
637 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000638 SmallVector<ObjCProtocolDecl*, 32> Protocols;
639 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Chris Lattner4d391482007-12-12 07:09:47 +0000641 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000642 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000643 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000644 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000645 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000646 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000647 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000648 PushOnScopeChains(PDecl, TUScope, false);
649 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000650 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000651 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000652 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000653 if (!isNew)
654 PDecl->setChangedSinceDeserialization(true);
655 }
Chris Lattner4d391482007-12-12 07:09:47 +0000656 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000657 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
660 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000661 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000662 Protocols.data(), Protocols.size(),
663 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000664 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000665 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000666 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000667}
668
John McCalld226f652010-08-21 09:40:31 +0000669Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000670ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
671 IdentifierInfo *ClassName, SourceLocation ClassLoc,
672 IdentifierInfo *CategoryName,
673 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000674 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000675 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000676 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000677 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000678 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000679 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000680
681 /// Check that class of this category is already completely declared.
682 if (!IDecl || IDecl->isForwardDecl()) {
683 // Create an invalid ObjCCategoryDecl to serve as context for
684 // the enclosing method declarations. We mark the decl invalid
685 // to make it clear that this isn't a valid AST.
686 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
687 ClassLoc, CategoryLoc, CategoryName);
688 CDecl->setInvalidDecl();
689 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000690 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000691 }
692
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000693 if (!CategoryName && IDecl->getImplementation()) {
694 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
695 Diag(IDecl->getImplementation()->getLocation(),
696 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000697 }
698
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000699 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
700 ClassLoc, CategoryLoc, CategoryName);
701 // FIXME: PushOnScopeChains?
702 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000703
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000704 CDecl->setClassInterface(IDecl);
705 // Insert class extension to the list of class's categories.
706 if (!CategoryName)
707 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Chris Lattner16b34b42009-02-16 21:30:01 +0000709 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000710 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000711
Fariborz Jahanian25760612010-02-15 21:55:26 +0000712 if (CategoryName) {
713 /// Check for duplicate interface declaration for this category
714 ObjCCategoryDecl *CDeclChain;
715 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
716 CDeclChain = CDeclChain->getNextClassCategory()) {
717 if (CDeclChain->getIdentifier() == CategoryName) {
718 // Class extensions can be declared multiple times.
719 Diag(CategoryLoc, diag::warn_dup_category_def)
720 << ClassName << CategoryName;
721 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
722 break;
723 }
Chris Lattner70f19542009-02-16 21:26:43 +0000724 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000725 if (!CDeclChain)
726 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000727 }
Chris Lattner70f19542009-02-16 21:26:43 +0000728
Chris Lattner4d391482007-12-12 07:09:47 +0000729 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000730 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000731 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000732 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000733 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000734 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000735 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Anders Carlsson15281452008-11-04 16:57:32 +0000738 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000739 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000740}
741
742/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000743/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000744/// object.
John McCalld226f652010-08-21 09:40:31 +0000745Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000746 SourceLocation AtCatImplLoc,
747 IdentifierInfo *ClassName, SourceLocation ClassLoc,
748 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000749 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000750 ObjCCategoryDecl *CatIDecl = 0;
751 if (IDecl) {
752 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
753 if (!CatIDecl) {
754 // Category @implementation with no corresponding @interface.
755 // Create and install one.
756 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000757 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000758 CatName);
759 CatIDecl->setClassInterface(IDecl);
760 CatIDecl->insertNextClassCategory();
761 }
762 }
763
Mike Stump1eb44332009-09-09 15:08:12 +0000764 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000765 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
766 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000767 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000768 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000769 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000770 CDecl->setInvalidDecl();
771 }
Chris Lattner4d391482007-12-12 07:09:47 +0000772
Douglas Gregord0434102009-01-09 00:49:46 +0000773 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000774 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000775
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000776 /// Check that CatName, category name, is not used in another implementation.
777 if (CatIDecl) {
778 if (CatIDecl->getImplementation()) {
779 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
780 << CatName;
781 Diag(CatIDecl->getImplementation()->getLocation(),
782 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000783 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000784 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000785 // Warn on implementating category of deprecated class under
786 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000787 DiagnoseObjCImplementedDeprecations(*this,
788 dyn_cast<NamedDecl>(IDecl),
789 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000790 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Anders Carlsson15281452008-11-04 16:57:32 +0000793 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000794 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000795}
796
John McCalld226f652010-08-21 09:40:31 +0000797Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000798 SourceLocation AtClassImplLoc,
799 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000800 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000801 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000802 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000803 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000804 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000805 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
806 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000808 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000809 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000810 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
811 // If this is a forward declaration of an interface, warn.
812 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000813 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000814 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000815 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000816 } else {
817 // We did not find anything with the name ClassName; try to correct for
818 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000819 TypoCorrection Corrected = CorrectTypo(
820 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
821 NULL, NULL, false, CTC_NoKeywords);
822 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000823 // Suggest the (potentially) correct interface name. However, put the
824 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000825 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000826 // provide a code-modification hint or use the typo name for recovery,
827 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000828 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000829 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000830 << ClassName << CorrectedName;
831 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
832 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000833 IDecl = 0;
834 } else {
835 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
836 }
Chris Lattner4d391482007-12-12 07:09:47 +0000837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Chris Lattner4d391482007-12-12 07:09:47 +0000839 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000840 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000841 if (SuperClassname) {
842 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000843 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
844 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000845 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000846 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
847 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000848 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000849 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000850 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000851 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000852 Diag(SuperClassLoc, diag::err_undef_superclass)
853 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000854 else if (IDecl && IDecl->getSuperClass() != SDecl) {
855 // This implementation and its interface do not have the same
856 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000857 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000858 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000859 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000860 }
861 }
862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Chris Lattner4d391482007-12-12 07:09:47 +0000864 if (!IDecl) {
865 // Legacy case of @implementation with no corresponding @interface.
866 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000867
Mike Stump390b4cc2009-05-16 07:39:55 +0000868 // FIXME: Do we support attributes on the @implementation? If so we should
869 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000870 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000871 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000872 IDecl->setSuperClass(SDecl);
873 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000874
875 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000876 } else {
877 // Mark the interface as being completed, even if it was just as
878 // @class ....;
879 // declaration; the user cannot reopen it.
880 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
883 ObjCImplementationDecl* IMPDecl =
884 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000885 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Anders Carlsson15281452008-11-04 16:57:32 +0000887 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000888 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Chris Lattner4d391482007-12-12 07:09:47 +0000890 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000891 if (IDecl->getImplementation()) {
892 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000893 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000894 Diag(IDecl->getImplementation()->getLocation(),
895 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000896 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000897 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000898 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000899 // Warn on implementating deprecated class under
900 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000901 DiagnoseObjCImplementedDeprecations(*this,
902 dyn_cast<NamedDecl>(IDecl),
903 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000904 }
John McCalld226f652010-08-21 09:40:31 +0000905 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000906}
907
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000908void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
909 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000910 SourceLocation RBrace) {
911 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000912 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000913 if (!IDecl)
914 return;
915 /// Check case of non-existing @interface decl.
916 /// (legacy objective-c @implementation decl without an @interface decl).
917 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000918 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000919 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000920 // Add ivar's to class's DeclContext.
921 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000922 ivars[i]->setLexicalDeclContext(ImpDecl);
923 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000924 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000925 }
926
Chris Lattner4d391482007-12-12 07:09:47 +0000927 return;
928 }
929 // If implementation has empty ivar list, just return.
930 if (numIvars == 0)
931 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Chris Lattner4d391482007-12-12 07:09:47 +0000933 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000934 if (LangOpts.ObjCNonFragileABI2) {
935 if (ImpDecl->getSuperClass())
936 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
937 for (unsigned i = 0; i < numIvars; i++) {
938 ObjCIvarDecl* ImplIvar = ivars[i];
939 if (const ObjCIvarDecl *ClsIvar =
940 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
941 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
942 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
943 continue;
944 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000945 // Instance ivar to Implementation's DeclContext.
946 ImplIvar->setLexicalDeclContext(ImpDecl);
947 IDecl->makeDeclVisibleInContext(ImplIvar, false);
948 ImpDecl->addDecl(ImplIvar);
949 }
950 return;
951 }
Chris Lattner4d391482007-12-12 07:09:47 +0000952 // Check interface's Ivar list against those in the implementation.
953 // names and types must match.
954 //
Chris Lattner4d391482007-12-12 07:09:47 +0000955 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000956 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000957 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
958 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000959 ObjCIvarDecl* ImplIvar = ivars[j++];
960 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000961 assert (ImplIvar && "missing implementation ivar");
962 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Steve Naroffca331292009-03-03 14:49:36 +0000964 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000965 if (Context.getCanonicalType(ImplIvar->getType()) !=
966 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000967 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000968 << ImplIvar->getIdentifier()
969 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000970 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000971 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
972 Expr *ImplBitWidth = ImplIvar->getBitWidth();
973 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000974 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
975 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000976 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
977 << ImplIvar->getIdentifier();
978 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980 }
Steve Naroffca331292009-03-03 14:49:36 +0000981 // Make sure the names are identical.
982 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000983 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000984 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000985 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000986 }
987 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Chris Lattner609e4c72007-12-12 18:11:49 +0000990 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000991 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000992 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000993 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000994}
995
Steve Naroff3c2eb662008-02-10 21:38:56 +0000996void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000997 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +0000998 // No point warning no definition of method which is 'unavailable'.
999 if (method->hasAttr<UnavailableAttr>())
1000 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001001 if (!IncompleteImpl) {
1002 Diag(ImpLoc, diag::warn_incomplete_impl);
1003 IncompleteImpl = true;
1004 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001005 if (DiagID == diag::warn_unimplemented_protocol_method)
1006 Diag(ImpLoc, DiagID) << method->getDeclName();
1007 else
1008 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001009}
1010
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001011/// Determines if type B can be substituted for type A. Returns true if we can
1012/// guarantee that anything that the user will do to an object of type A can
1013/// also be done to an object of type B. This is trivially true if the two
1014/// types are the same, or if B is a subclass of A. It becomes more complex
1015/// in cases where protocols are involved.
1016///
1017/// Object types in Objective-C describe the minimum requirements for an
1018/// object, rather than providing a complete description of a type. For
1019/// example, if A is a subclass of B, then B* may refer to an instance of A.
1020/// The principle of substitutability means that we may use an instance of A
1021/// anywhere that we may use an instance of B - it will implement all of the
1022/// ivars of B and all of the methods of B.
1023///
1024/// This substitutability is important when type checking methods, because
1025/// the implementation may have stricter type definitions than the interface.
1026/// The interface specifies minimum requirements, but the implementation may
1027/// have more accurate ones. For example, a method may privately accept
1028/// instances of B, but only publish that it accepts instances of A. Any
1029/// object passed to it will be type checked against B, and so will implicitly
1030/// by a valid A*. Similarly, a method may return a subclass of the class that
1031/// it is declared as returning.
1032///
1033/// This is most important when considering subclassing. A method in a
1034/// subclass must accept any object as an argument that its superclass's
1035/// implementation accepts. It may, however, accept a more general type
1036/// without breaking substitutability (i.e. you can still use the subclass
1037/// anywhere that you can use the superclass, but not vice versa). The
1038/// converse requirement applies to return types: the return type for a
1039/// subclass method must be a valid object of the kind that the superclass
1040/// advertises, but it may be specified more accurately. This avoids the need
1041/// for explicit down-casting by callers.
1042///
1043/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001044static bool isObjCTypeSubstitutable(ASTContext &Context,
1045 const ObjCObjectPointerType *A,
1046 const ObjCObjectPointerType *B,
1047 bool rejectId) {
1048 // Reject a protocol-unqualified id.
1049 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001050
1051 // If B is a qualified id, then A must also be a qualified id and it must
1052 // implement all of the protocols in B. It may not be a qualified class.
1053 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1054 // stricter definition so it is not substitutable for id<A>.
1055 if (B->isObjCQualifiedIdType()) {
1056 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001057 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1058 QualType(B,0),
1059 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001060 }
1061
1062 /*
1063 // id is a special type that bypasses type checking completely. We want a
1064 // warning when it is used in one place but not another.
1065 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1066
1067
1068 // If B is a qualified id, then A must also be a qualified id (which it isn't
1069 // if we've got this far)
1070 if (B->isObjCQualifiedIdType()) return false;
1071 */
1072
1073 // Now we know that A and B are (potentially-qualified) class types. The
1074 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001075 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001076}
1077
John McCall10302c02010-10-28 02:34:38 +00001078static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1079 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1080}
1081
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001082static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001083 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001084 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001085 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001086 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001087 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001088 if (IsProtocolMethodDecl &&
1089 (MethodDecl->getObjCDeclQualifier() !=
1090 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001091 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001092 S.Diag(MethodImpl->getLocation(),
1093 (IsOverridingMode ?
1094 diag::warn_conflicting_overriding_ret_type_modifiers
1095 : diag::warn_conflicting_ret_type_modifiers))
1096 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001097 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1098 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1099 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1100 }
1101 else
1102 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001103 }
1104
John McCall10302c02010-10-28 02:34:38 +00001105 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001106 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001107 return true;
1108 if (!Warn)
1109 return false;
John McCall10302c02010-10-28 02:34:38 +00001110
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001111 unsigned DiagID =
1112 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1113 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001114
1115 // Mismatches between ObjC pointers go into a different warning
1116 // category, and sometimes they're even completely whitelisted.
1117 if (const ObjCObjectPointerType *ImplPtrTy =
1118 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1119 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001120 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001121 // Allow non-matching return types as long as they don't violate
1122 // the principle of substitutability. Specifically, we permit
1123 // return types that are subclasses of the declared return type,
1124 // or that are more-qualified versions of the declared type.
1125 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001126 return false;
John McCall10302c02010-10-28 02:34:38 +00001127
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001128 DiagID =
1129 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1130 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001131 }
1132 }
1133
1134 S.Diag(MethodImpl->getLocation(), DiagID)
1135 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001136 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001137 << MethodImpl->getResultType()
1138 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001139 S.Diag(MethodDecl->getLocation(),
1140 IsOverridingMode ? diag::note_previous_declaration
1141 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001142 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001143 return false;
John McCall10302c02010-10-28 02:34:38 +00001144}
1145
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001146static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001147 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001148 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001149 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001150 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001151 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001152 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001153 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001154 if (IsProtocolMethodDecl &&
1155 (ImplVar->getObjCDeclQualifier() !=
1156 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001157 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001158 if (IsOverridingMode)
1159 S.Diag(ImplVar->getLocation(),
1160 diag::warn_conflicting_overriding_param_modifiers)
1161 << getTypeRange(ImplVar->getTypeSourceInfo())
1162 << MethodImpl->getDeclName();
1163 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001164 diag::warn_conflicting_param_modifiers)
1165 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001166 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001167 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1168 << getTypeRange(IfaceVar->getTypeSourceInfo());
1169 }
1170 else
1171 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001172 }
1173
John McCall10302c02010-10-28 02:34:38 +00001174 QualType ImplTy = ImplVar->getType();
1175 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001176
John McCall10302c02010-10-28 02:34:38 +00001177 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001178 return true;
1179
1180 if (!Warn)
1181 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001182 unsigned DiagID =
1183 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1184 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001185
1186 // Mismatches between ObjC pointers go into a different warning
1187 // category, and sometimes they're even completely whitelisted.
1188 if (const ObjCObjectPointerType *ImplPtrTy =
1189 ImplTy->getAs<ObjCObjectPointerType>()) {
1190 if (const ObjCObjectPointerType *IfacePtrTy =
1191 IfaceTy->getAs<ObjCObjectPointerType>()) {
1192 // Allow non-matching argument types as long as they don't
1193 // violate the principle of substitutability. Specifically, the
1194 // implementation must accept any objects that the superclass
1195 // accepts, however it may also accept others.
1196 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001197 return false;
John McCall10302c02010-10-28 02:34:38 +00001198
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001199 DiagID =
1200 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1201 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001202 }
1203 }
1204
1205 S.Diag(ImplVar->getLocation(), DiagID)
1206 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001207 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1208 S.Diag(IfaceVar->getLocation(),
1209 (IsOverridingMode ? diag::note_previous_declaration
1210 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001211 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001212 return false;
John McCall10302c02010-10-28 02:34:38 +00001213}
John McCallf85e1932011-06-15 23:02:42 +00001214
1215/// In ARC, check whether the conventional meanings of the two methods
1216/// match. If they don't, it's a hard error.
1217static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1218 ObjCMethodDecl *decl) {
1219 ObjCMethodFamily implFamily = impl->getMethodFamily();
1220 ObjCMethodFamily declFamily = decl->getMethodFamily();
1221 if (implFamily == declFamily) return false;
1222
1223 // Since conventions are sorted by selector, the only possibility is
1224 // that the types differ enough to cause one selector or the other
1225 // to fall out of the family.
1226 assert(implFamily == OMF_None || declFamily == OMF_None);
1227
1228 // No further diagnostics required on invalid declarations.
1229 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1230
1231 const ObjCMethodDecl *unmatched = impl;
1232 ObjCMethodFamily family = declFamily;
1233 unsigned errorID = diag::err_arc_lost_method_convention;
1234 unsigned noteID = diag::note_arc_lost_method_convention;
1235 if (declFamily == OMF_None) {
1236 unmatched = decl;
1237 family = implFamily;
1238 errorID = diag::err_arc_gained_method_convention;
1239 noteID = diag::note_arc_gained_method_convention;
1240 }
1241
1242 // Indexes into a %select clause in the diagnostic.
1243 enum FamilySelector {
1244 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1245 };
1246 FamilySelector familySelector = FamilySelector();
1247
1248 switch (family) {
1249 case OMF_None: llvm_unreachable("logic error, no method convention");
1250 case OMF_retain:
1251 case OMF_release:
1252 case OMF_autorelease:
1253 case OMF_dealloc:
1254 case OMF_retainCount:
1255 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001256 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001257 // Mismatches for these methods don't change ownership
1258 // conventions, so we don't care.
1259 return false;
1260
1261 case OMF_init: familySelector = F_init; break;
1262 case OMF_alloc: familySelector = F_alloc; break;
1263 case OMF_copy: familySelector = F_copy; break;
1264 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1265 case OMF_new: familySelector = F_new; break;
1266 }
1267
1268 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1269 ReasonSelector reasonSelector;
1270
1271 // The only reason these methods don't fall within their families is
1272 // due to unusual result types.
1273 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1274 reasonSelector = R_UnrelatedReturn;
1275 } else {
1276 reasonSelector = R_NonObjectReturn;
1277 }
1278
1279 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1280 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1281
1282 return true;
1283}
John McCall10302c02010-10-28 02:34:38 +00001284
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001285void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001286 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001287 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001288 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001289 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001290 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001291 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1292 return;
1293
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001294 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001295 IsProtocolMethodDecl, IsOverridingMode,
1296 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Chris Lattner3aff9192009-04-11 19:58:42 +00001298 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001299 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001300 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001301 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1302 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001303 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001304
Fariborz Jahanian21121902011-08-08 18:03:17 +00001305 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001306 if (IsOverridingMode)
1307 Diag(ImpMethodDecl->getLocation(),
1308 diag::warn_conflicting_overriding_variadic);
1309 else
1310 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001311 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001312 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001313}
1314
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001315/// WarnExactTypedMethods - This routine issues a warning if method
1316/// implementation declaration matches exactly that of its declaration.
1317void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1318 ObjCMethodDecl *MethodDecl,
1319 bool IsProtocolMethodDecl) {
1320 // don't issue warning when protocol method is optional because primary
1321 // class is not required to implement it and it is safe for protocol
1322 // to implement it.
1323 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1324 return;
1325 // don't issue warning when primary class's method is
1326 // depecated/unavailable.
1327 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1328 MethodDecl->hasAttr<DeprecatedAttr>())
1329 return;
1330
1331 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1332 IsProtocolMethodDecl, false, false);
1333 if (match)
1334 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1335 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1336 IM != EM; ++IM, ++IF) {
1337 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1338 *IM, *IF,
1339 IsProtocolMethodDecl, false, false);
1340 if (!match)
1341 break;
1342 }
1343 if (match)
1344 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001345 if (match)
1346 match = !(MethodDecl->isClassMethod() &&
1347 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001348
1349 if (match) {
1350 Diag(ImpMethodDecl->getLocation(),
1351 diag::warn_category_method_impl_match);
1352 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1353 }
1354}
1355
Mike Stump390b4cc2009-05-16 07:39:55 +00001356/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1357/// improve the efficiency of selector lookups and type checking by associating
1358/// with each protocol / interface / category the flattened instance tables. If
1359/// we used an immutable set to keep the table then it wouldn't add significant
1360/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001361
Steve Naroffefe7f362008-02-08 22:06:17 +00001362/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001363/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001364void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1365 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001366 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001367 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001368 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001369 ObjCContainerDecl *CDecl) {
1370 ObjCInterfaceDecl *IDecl;
1371 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1372 IDecl = C->getClassInterface();
1373 else
1374 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1375 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1376
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001377 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001378 ObjCInterfaceDecl *NSIDecl = 0;
1379 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001380 // check to see if class implements forwardInvocation method and objects
1381 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001382 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001383 // Under such conditions, which means that every method possible is
1384 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001385 // found" warnings.
1386 // FIXME: Use a general GetUnarySelector method for this.
1387 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1388 Selector fISelector = Context.Selectors.getSelector(1, &II);
1389 if (InsMap.count(fISelector))
1390 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1391 // need be implemented in the implementation.
1392 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1393 }
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001395 // If a method lookup fails locally we still need to look and see if
1396 // the method was implemented by a base class or an inherited
1397 // protocol. This lookup is slow, but occurs rarely in correct code
1398 // and otherwise would terminate in a warning.
1399
Chris Lattner4d391482007-12-12 07:09:47 +00001400 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001401 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001402 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001403 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001404 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001405 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001406 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001407 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001408 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001409 // Ugly, but necessary. Method declared in protcol might have
1410 // have been synthesized due to a property declared in the class which
1411 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001412 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001413 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001414 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001415 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001416 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1417 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001418 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001419 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001420 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1421 << PDecl->getDeclName();
1422 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001423 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001424 }
1425 }
Chris Lattner4d391482007-12-12 07:09:47 +00001426 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001427 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001428 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001429 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001430 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001431 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1432 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001433 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001434 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001435 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001436 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001437 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001438 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1439 PDecl->getDeclName();
1440 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001441 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001442 }
Chris Lattner780f3292008-07-21 21:32:27 +00001443 // Check on this protocols's referenced protocols, recursively.
1444 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1445 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001446 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001447}
1448
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001449/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001450/// or protocol against those declared in their implementations.
1451///
1452void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1453 const llvm::DenseSet<Selector> &ClsMap,
1454 llvm::DenseSet<Selector> &InsMapSeen,
1455 llvm::DenseSet<Selector> &ClsMapSeen,
1456 ObjCImplDecl* IMPDecl,
1457 ObjCContainerDecl* CDecl,
1458 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001459 bool ImmediateClass,
1460 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001461 // Check and see if instance methods in class interface have been
1462 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001463 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1464 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001465 if (InsMapSeen.count((*I)->getSelector()))
1466 continue;
1467 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001468 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001469 !InsMap.count((*I)->getSelector())) {
1470 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001471 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1472 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001473 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001474 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001475 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001476 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001477 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001478 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001479 assert(MethodDecl &&
1480 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001481 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001482 if (ImpMethodDecl) {
1483 if (!WarnExactMatch)
1484 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1485 isa<ObjCProtocolDecl>(CDecl));
1486 else
1487 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1488 isa<ObjCProtocolDecl>(CDecl));
1489 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001490 }
1491 }
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001493 // Check and see if class methods in class interface have been
1494 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001495 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001496 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001497 if (ClsMapSeen.count((*I)->getSelector()))
1498 continue;
1499 ClsMapSeen.insert((*I)->getSelector());
1500 if (!ClsMap.count((*I)->getSelector())) {
1501 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001502 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1503 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001504 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001505 ObjCMethodDecl *ImpMethodDecl =
1506 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001507 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001508 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001509 if (!WarnExactMatch)
1510 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1511 isa<ObjCProtocolDecl>(CDecl));
1512 else
1513 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1514 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001515 }
1516 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001517
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001518 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001519 // Also methods in class extensions need be looked at next.
1520 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1521 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1522 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1523 IMPDecl,
1524 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001525 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001526
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001527 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001528 for (ObjCInterfaceDecl::all_protocol_iterator
1529 PI = I->all_referenced_protocol_begin(),
1530 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001531 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1532 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001533 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001534
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001535 // FIXME. For now, we are not checking for extact match of methods
1536 // in category implementation and its primary class's super class.
1537 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001538 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001539 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001540 I->getSuperClass(), IncompleteImpl, false);
1541 }
1542}
1543
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001544/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1545/// category matches with those implemented in its primary class and
1546/// warns each time an exact match is found.
1547void Sema::CheckCategoryVsClassMethodMatches(
1548 ObjCCategoryImplDecl *CatIMPDecl) {
1549 llvm::DenseSet<Selector> InsMap, ClsMap;
1550
1551 for (ObjCImplementationDecl::instmeth_iterator
1552 I = CatIMPDecl->instmeth_begin(),
1553 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1554 InsMap.insert((*I)->getSelector());
1555
1556 for (ObjCImplementationDecl::classmeth_iterator
1557 I = CatIMPDecl->classmeth_begin(),
1558 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1559 ClsMap.insert((*I)->getSelector());
1560 if (InsMap.empty() && ClsMap.empty())
1561 return;
1562
1563 // Get category's primary class.
1564 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1565 if (!CatDecl)
1566 return;
1567 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1568 if (!IDecl)
1569 return;
1570 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1571 bool IncompleteImpl = false;
1572 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1573 CatIMPDecl, IDecl,
1574 IncompleteImpl, false, true /*WarnExactMatch*/);
1575}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001576
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001577void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001578 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001579 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001580 llvm::DenseSet<Selector> InsMap;
1581 // Check and see if instance methods in class interface have been
1582 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001583 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001584 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001585 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001587 // Check and see if properties declared in the interface have either 1)
1588 // an implementation or 2) there is a @synthesize/@dynamic implementation
1589 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001590 if (isa<ObjCInterfaceDecl>(CDecl) &&
1591 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001592 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001593
Chris Lattner4d391482007-12-12 07:09:47 +00001594 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001595 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001596 I = IMPDecl->classmeth_begin(),
1597 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001598 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001600 // Check for type conflict of methods declared in a class/protocol and
1601 // its implementation; if any.
1602 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001603 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1604 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001605 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001606
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001607 // check all methods implemented in category against those declared
1608 // in its primary class.
1609 if (ObjCCategoryImplDecl *CatDecl =
1610 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1611 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Chris Lattner4d391482007-12-12 07:09:47 +00001613 // Check the protocol list for unimplemented methods in the @implementation
1614 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001615 // Check and see if class methods in class interface have been
1616 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Chris Lattnercddc8882009-03-01 00:56:52 +00001618 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001619 for (ObjCInterfaceDecl::all_protocol_iterator
1620 PI = I->all_referenced_protocol_begin(),
1621 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001622 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001623 InsMap, ClsMap, I);
1624 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001625 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1626 Categories; Categories = Categories->getNextClassExtension())
1627 ImplMethodsVsClassMethods(S, IMPDecl,
1628 const_cast<ObjCCategoryDecl*>(Categories),
1629 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001630 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001631 // For extended class, unimplemented methods in its protocols will
1632 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001633 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001634 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1635 E = C->protocol_end(); PI != E; ++PI)
1636 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001637 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001638 // Report unimplemented properties in the category as well.
1639 // When reporting on missing setter/getters, do not report when
1640 // setter/getter is implemented in category's primary class
1641 // implementation.
1642 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1643 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1644 for (ObjCImplementationDecl::instmeth_iterator
1645 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1646 InsMap.insert((*I)->getSelector());
1647 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001648 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001649 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001650 } else
1651 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001652}
1653
Mike Stump1eb44332009-09-09 15:08:12 +00001654/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001655Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001656Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001657 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001658 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001659 unsigned NumElts) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001660 SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Chris Lattner4d391482007-12-12 07:09:47 +00001662 for (unsigned i = 0; i != NumElts; ++i) {
1663 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001664 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001665 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001666 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001667 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001668 // Maybe we will complain about the shadowed template parameter.
1669 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1670 // Just pretend that we didn't see the previous declaration.
1671 PrevDecl = 0;
1672 }
1673
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001674 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001675 // GCC apparently allows the following idiom:
1676 //
1677 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1678 // @class XCElementToggler;
1679 //
Mike Stump1eb44332009-09-09 15:08:12 +00001680 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001681 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001682 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001683 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001684 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001685 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001686 // a forward class declaration matching a typedef name of a class refers
1687 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001688 if (const ObjCObjectType *OI =
1689 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1690 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001691 }
Chris Lattner4d391482007-12-12 07:09:47 +00001692 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001693 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1694 if (!IDecl) { // Not already seen? Make a forward decl.
1695 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1696 IdentList[i], IdentLocs[i], true);
1697
1698 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1699 // the current DeclContext. This prevents clients that walk DeclContext
1700 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1701 // declared later (if at all). We also take care to explicitly make
1702 // sure this declaration is visible for name lookup.
1703 PushOnScopeChains(IDecl, TUScope, false);
1704 CurContext->makeDeclVisibleInContext(IDecl, true);
1705 }
Chris Lattner4d391482007-12-12 07:09:47 +00001706
1707 Interfaces.push_back(IDecl);
1708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Ted Kremenek321c22f2009-11-18 00:28:11 +00001710 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001711 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001712 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001713 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001714 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001715 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001716 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001717}
1718
John McCall0f4c4c42011-06-16 01:15:19 +00001719static bool tryMatchRecordTypes(ASTContext &Context,
1720 Sema::MethodMatchStrategy strategy,
1721 const Type *left, const Type *right);
1722
John McCallf85e1932011-06-15 23:02:42 +00001723static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1724 QualType leftQT, QualType rightQT) {
1725 const Type *left =
1726 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1727 const Type *right =
1728 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1729
1730 if (left == right) return true;
1731
1732 // If we're doing a strict match, the types have to match exactly.
1733 if (strategy == Sema::MMS_strict) return false;
1734
1735 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1736
1737 // Otherwise, use this absurdly complicated algorithm to try to
1738 // validate the basic, low-level compatibility of the two types.
1739
1740 // As a minimum, require the sizes and alignments to match.
1741 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1742 return false;
1743
1744 // Consider all the kinds of non-dependent canonical types:
1745 // - functions and arrays aren't possible as return and parameter types
1746
1747 // - vector types of equal size can be arbitrarily mixed
1748 if (isa<VectorType>(left)) return isa<VectorType>(right);
1749 if (isa<VectorType>(right)) return false;
1750
1751 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001752 // - structs, unions, and Objective-C objects must match more-or-less
1753 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001754 // - everything else should be a scalar
1755 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001756 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001757
1758 // Make scalars agree in kind, except count bools as chars.
1759 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1760 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1761 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1762 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1763
1764 // Note that data member pointers and function member pointers don't
1765 // intermix because of the size differences.
1766
1767 return (leftSK == rightSK);
1768}
Chris Lattner4d391482007-12-12 07:09:47 +00001769
John McCall0f4c4c42011-06-16 01:15:19 +00001770static bool tryMatchRecordTypes(ASTContext &Context,
1771 Sema::MethodMatchStrategy strategy,
1772 const Type *lt, const Type *rt) {
1773 assert(lt && rt && lt != rt);
1774
1775 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1776 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1777 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1778
1779 // Require union-hood to match.
1780 if (left->isUnion() != right->isUnion()) return false;
1781
1782 // Require an exact match if either is non-POD.
1783 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1784 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1785 return false;
1786
1787 // Require size and alignment to match.
1788 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1789
1790 // Require fields to match.
1791 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1792 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1793 for (; li != le && ri != re; ++li, ++ri) {
1794 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1795 return false;
1796 }
1797 return (li == le && ri == re);
1798}
1799
Chris Lattner4d391482007-12-12 07:09:47 +00001800/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1801/// returns true, or false, accordingly.
1802/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001803bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1804 const ObjCMethodDecl *right,
1805 MethodMatchStrategy strategy) {
1806 if (!matchTypes(Context, strategy,
1807 left->getResultType(), right->getResultType()))
1808 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001809
John McCallf85e1932011-06-15 23:02:42 +00001810 if (getLangOptions().ObjCAutoRefCount &&
1811 (left->hasAttr<NSReturnsRetainedAttr>()
1812 != right->hasAttr<NSReturnsRetainedAttr>() ||
1813 left->hasAttr<NSConsumesSelfAttr>()
1814 != right->hasAttr<NSConsumesSelfAttr>()))
1815 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001816
John McCallf85e1932011-06-15 23:02:42 +00001817 ObjCMethodDecl::param_iterator
1818 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001819
John McCallf85e1932011-06-15 23:02:42 +00001820 for (; li != le; ++li, ++ri) {
1821 assert(ri != right->param_end() && "Param mismatch");
1822 ParmVarDecl *lparm = *li, *rparm = *ri;
1823
1824 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1825 return false;
1826
1827 if (getLangOptions().ObjCAutoRefCount &&
1828 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1829 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001830 }
1831 return true;
1832}
1833
Sebastian Redldb9d2142010-08-02 23:18:59 +00001834/// \brief Read the contents of the method pool for a given selector from
1835/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001836///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001837/// This routine should only be called once, when the method pool has no entry
1838/// for this selector.
1839Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001840 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001841 assert(MethodPool.find(Sel) == MethodPool.end() &&
1842 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001843
1844 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001845 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Sebastian Redldb9d2142010-08-02 23:18:59 +00001847 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001848}
1849
Sebastian Redldb9d2142010-08-02 23:18:59 +00001850void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1851 bool instance) {
1852 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1853 if (Pos == MethodPool.end()) {
1854 if (ExternalSource)
1855 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001856 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001857 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1858 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001859 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001860 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001861 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001862 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001863 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001864 Entry.Method = Method;
1865 Entry.Next = 0;
1866 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001867 }
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Chris Lattnerb25df352009-03-04 05:16:45 +00001869 // We've seen a method with this name, see if we have already seen this type
1870 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001871 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1872 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1873
1874 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001875 ObjCMethodDecl *PrevObjCMethod = List->Method;
1876 PrevObjCMethod->setDefined(impl);
1877 // If a method is deprecated, push it in the global pool.
1878 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001879 if (Method->isDeprecated()) {
1880 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001881 List->Method = Method;
1882 }
1883 // If new method is unavailable, push it into global pool
1884 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001885 if (Method->isUnavailable()) {
1886 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001887 List->Method = Method;
1888 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001889 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001890 }
John McCallf85e1932011-06-15 23:02:42 +00001891 }
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Chris Lattnerb25df352009-03-04 05:16:45 +00001893 // We have a new signature for an existing method - add it.
1894 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001895 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1896 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001897}
1898
John McCallf85e1932011-06-15 23:02:42 +00001899/// Determines if this is an "acceptable" loose mismatch in the global
1900/// method pool. This exists mostly as a hack to get around certain
1901/// global mismatches which we can't afford to make warnings / errors.
1902/// Really, what we want is a way to take a method out of the global
1903/// method pool.
1904static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1905 ObjCMethodDecl *other) {
1906 if (!chosen->isInstanceMethod())
1907 return false;
1908
1909 Selector sel = chosen->getSelector();
1910 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1911 return false;
1912
1913 // Don't complain about mismatches for -length if the method we
1914 // chose has an integral result type.
1915 return (chosen->getResultType()->isIntegerType());
1916}
1917
Sebastian Redldb9d2142010-08-02 23:18:59 +00001918ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001919 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001920 bool warn, bool instance) {
1921 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1922 if (Pos == MethodPool.end()) {
1923 if (ExternalSource)
1924 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001925 else
1926 return 0;
1927 }
1928
Sebastian Redldb9d2142010-08-02 23:18:59 +00001929 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Sebastian Redldb9d2142010-08-02 23:18:59 +00001931 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001932 bool issueDiagnostic = false, issueError = false;
1933
1934 // We support a warning which complains about *any* difference in
1935 // method signature.
1936 bool strictSelectorMatch =
1937 (receiverIdOrClass && warn &&
1938 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1939 R.getBegin()) !=
1940 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001941 if (strictSelectorMatch)
1942 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001943 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1944 MMS_strict)) {
1945 issueDiagnostic = true;
1946 break;
1947 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001948 }
1949
John McCallf85e1932011-06-15 23:02:42 +00001950 // If we didn't see any strict differences, we won't see any loose
1951 // differences. In ARC, however, we also need to check for loose
1952 // mismatches, because most of them are errors.
1953 if (!strictSelectorMatch ||
1954 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001955 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001956 // This checks if the methods differ in type mismatch.
1957 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1958 MMS_loose) &&
1959 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1960 issueDiagnostic = true;
1961 if (getLangOptions().ObjCAutoRefCount)
1962 issueError = true;
1963 break;
1964 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001965 }
1966
John McCallf85e1932011-06-15 23:02:42 +00001967 if (issueDiagnostic) {
1968 if (issueError)
1969 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1970 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001971 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1972 else
1973 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001974
1975 Diag(MethList.Method->getLocStart(),
1976 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001977 << MethList.Method->getSourceRange();
1978 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1979 Diag(Next->Method->getLocStart(), diag::note_also_found)
1980 << Next->Method->getSourceRange();
1981 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001982 }
1983 return MethList.Method;
1984}
1985
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001986ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001987 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1988 if (Pos == MethodPool.end())
1989 return 0;
1990
1991 GlobalMethods &Methods = Pos->second;
1992
1993 if (Methods.first.Method && Methods.first.Method->isDefined())
1994 return Methods.first.Method;
1995 if (Methods.second.Method && Methods.second.Method->isDefined())
1996 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001997 return 0;
1998}
1999
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002000/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2001/// identical selector names in current and its super classes and issues
2002/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002003void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2004 ObjCMethodDecl *Method,
2005 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002006 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2007 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002009 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002010 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002011 SD->lookupMethod(Method->getSelector(), IsInstance);
2012 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002013 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002014 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002015 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002016 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2017 E = Method->param_end();
2018 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2019 for (; ParamI != E; ++ParamI, ++PrevI) {
2020 // Number of parameters are the same and is guaranteed by selector match.
2021 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2022 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2023 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002024 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002025 // respective argument type in the super class method, issue warning;
2026 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002027 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002028 << T1 << T2;
2029 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2030 return;
2031 }
2032 }
2033 ID = SD;
2034 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002035}
2036
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002037/// DiagnoseDuplicateIvars -
2038/// Check for duplicate ivars in the entire class at the start of
2039/// @implementation. This becomes necesssary because class extension can
2040/// add ivars to a class in random order which will not be known until
2041/// class's @implementation is seen.
2042void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2043 ObjCInterfaceDecl *SID) {
2044 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2045 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2046 ObjCIvarDecl* Ivar = (*IVI);
2047 if (Ivar->isInvalidDecl())
2048 continue;
2049 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2050 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2051 if (prevIvar) {
2052 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2053 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2054 Ivar->setInvalidDecl();
2055 }
2056 }
2057 }
2058}
2059
Steve Naroffa56f6162007-12-18 01:30:32 +00002060// Note: For class/category implemenations, allMethods/allProperties is
2061// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002062void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002063 Decl **allMethods, unsigned allNum,
2064 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002065 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002066
2067 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002068 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002069 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2070 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002071
Mike Stump1eb44332009-09-09 15:08:12 +00002072 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002073 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2074 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002075 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002076
Ted Kremenek782f2f52010-01-07 01:20:12 +00002077 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2078 // FIXME: This is wrong. We shouldn't be pretending that there is
2079 // an '@end' in the declaration.
2080 SourceLocation L = ClassDecl->getLocation();
2081 AtEnd.setBegin(L);
2082 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002083 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002084 }
2085
Steve Naroff0701bbb2009-01-08 17:28:14 +00002086 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2087 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2088 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2089
Chris Lattner4d391482007-12-12 07:09:47 +00002090 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002091 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002092 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002093
2094 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002095 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002096 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002097 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002098 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002099 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002100 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002101 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002102 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002103 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002104 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002105 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002106 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002107 InsMap[Method->getSelector()] = Method;
2108 /// The following allows us to typecheck messages to "id".
2109 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002110 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002111 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002112 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002113 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002114 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002115 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002116 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002117 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002118 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002119 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002120 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002121 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002122 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002123 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002124 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002125 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002126 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002127 /// The following allows us to typecheck messages to "Class".
2128 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002129 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002130 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002131 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002132 }
2133 }
2134 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002135 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002136 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002137 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002138 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002139 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002140 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002141 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002142 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002143 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002144
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002145 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002146 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002147 if (C->IsClassExtension()) {
2148 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2149 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002150 }
Chris Lattner4d391482007-12-12 07:09:47 +00002151 }
Steve Naroff09c47192009-01-09 15:36:25 +00002152 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002153 if (CDecl->getIdentifier())
2154 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2155 // user-defined setter/getter. It also synthesizes setter/getter methods
2156 // and adds them to the DeclContext and global method pools.
2157 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2158 E = CDecl->prop_end();
2159 I != E; ++I)
2160 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002161 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002162 }
2163 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002164 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002165 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002166 // Any property declared in a class extension might have user
2167 // declared setter or getter in current class extension or one
2168 // of the other class extensions. Mark them as synthesized as
2169 // property will be synthesized when property with same name is
2170 // seen in the @implementation.
2171 for (const ObjCCategoryDecl *ClsExtDecl =
2172 IDecl->getFirstClassExtension();
2173 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2174 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2175 E = ClsExtDecl->prop_end(); I != E; ++I) {
2176 ObjCPropertyDecl *Property = (*I);
2177 // Skip over properties declared @dynamic
2178 if (const ObjCPropertyImplDecl *PIDecl
2179 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2180 if (PIDecl->getPropertyImplementation()
2181 == ObjCPropertyImplDecl::Dynamic)
2182 continue;
2183
2184 for (const ObjCCategoryDecl *CExtDecl =
2185 IDecl->getFirstClassExtension();
2186 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2187 if (ObjCMethodDecl *GetterMethod =
2188 CExtDecl->getInstanceMethod(Property->getGetterName()))
2189 GetterMethod->setSynthesized(true);
2190 if (!Property->isReadOnly())
2191 if (ObjCMethodDecl *SetterMethod =
2192 CExtDecl->getInstanceMethod(Property->getSetterName()))
2193 SetterMethod->setSynthesized(true);
2194 }
2195 }
2196 }
2197
Ted Kremenekc32647d2010-12-23 21:35:43 +00002198 if (LangOpts.ObjCDefaultSynthProperties &&
2199 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002200 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002201 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002202 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002203 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002204
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002205 if (LangOpts.ObjCNonFragileABI2)
2206 while (IDecl->getSuperClass()) {
2207 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2208 IDecl = IDecl->getSuperClass();
2209 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002210 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002211 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002212 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002213 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002214 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002215
Chris Lattner4d391482007-12-12 07:09:47 +00002216 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002217 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002218 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002219 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002220 Categories; Categories = Categories->getNextClassCategory()) {
2221 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002222 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002223 break;
2224 }
2225 }
2226 }
2227 }
Chris Lattner682bf922009-03-29 16:50:03 +00002228 if (isInterfaceDeclKind) {
2229 // Reject invalid vardecls.
2230 for (unsigned i = 0; i != tuvNum; i++) {
2231 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2232 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2233 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002234 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002235 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002236 }
Chris Lattner682bf922009-03-29 16:50:03 +00002237 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002238 }
Chris Lattner4d391482007-12-12 07:09:47 +00002239}
2240
2241
2242/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2243/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002244static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002245CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002246 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002247}
2248
Ted Kremenek422bae72010-04-18 04:59:38 +00002249static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002250bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002251 // The 'ibaction' attribute is allowed on method definitions because of
2252 // how the IBAction macro is used on both method declarations and definitions.
2253 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002254 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2255 if ((*i)->getKind() != attr::IBAction)
2256 return true;
2257 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002258}
2259
Douglas Gregor926df6c2011-06-11 01:09:30 +00002260/// \brief Check whether the declared result type of the given Objective-C
2261/// method declaration is compatible with the method's class.
2262///
2263static bool
2264CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2265 ObjCInterfaceDecl *CurrentClass) {
2266 QualType ResultType = Method->getResultType();
2267 SourceRange ResultTypeRange;
2268 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2269 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2270
2271 // If an Objective-C method inherits its related result type, then its
2272 // declared result type must be compatible with its own class type. The
2273 // declared result type is compatible if:
2274 if (const ObjCObjectPointerType *ResultObjectType
2275 = ResultType->getAs<ObjCObjectPointerType>()) {
2276 // - it is id or qualified id, or
2277 if (ResultObjectType->isObjCIdType() ||
2278 ResultObjectType->isObjCQualifiedIdType())
2279 return false;
2280
2281 if (CurrentClass) {
2282 if (ObjCInterfaceDecl *ResultClass
2283 = ResultObjectType->getInterfaceDecl()) {
2284 // - it is the same as the method's class type, or
2285 if (CurrentClass == ResultClass)
2286 return false;
2287
2288 // - it is a superclass of the method's class type
2289 if (ResultClass->isSuperClassOf(CurrentClass))
2290 return false;
2291 }
2292 }
2293 }
2294
2295 return true;
2296}
2297
John McCall6c2c2502011-07-22 02:45:48 +00002298namespace {
2299/// A helper class for searching for methods which a particular method
2300/// overrides.
2301class OverrideSearch {
2302 Sema &S;
2303 ObjCMethodDecl *Method;
2304 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2305 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2306 bool Recursive;
2307
2308public:
2309 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2310 Selector selector = method->getSelector();
2311
2312 // Bypass this search if we've never seen an instance/class method
2313 // with this selector before.
2314 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2315 if (it == S.MethodPool.end()) {
2316 if (!S.ExternalSource) return;
2317 it = S.ReadMethodPool(selector);
2318 }
2319 ObjCMethodList &list =
2320 method->isInstanceMethod() ? it->second.first : it->second.second;
2321 if (!list.Method) return;
2322
2323 ObjCContainerDecl *container
2324 = cast<ObjCContainerDecl>(method->getDeclContext());
2325
2326 // Prevent the search from reaching this container again. This is
2327 // important with categories, which override methods from the
2328 // interface and each other.
2329 Searched.insert(container);
2330 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002331 }
John McCall6c2c2502011-07-22 02:45:48 +00002332
2333 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2334 iterator begin() const { return Overridden.begin(); }
2335 iterator end() const { return Overridden.end(); }
2336
2337private:
2338 void searchFromContainer(ObjCContainerDecl *container) {
2339 if (container->isInvalidDecl()) return;
2340
2341 switch (container->getDeclKind()) {
2342#define OBJCCONTAINER(type, base) \
2343 case Decl::type: \
2344 searchFrom(cast<type##Decl>(container)); \
2345 break;
2346#define ABSTRACT_DECL(expansion)
2347#define DECL(type, base) \
2348 case Decl::type:
2349#include "clang/AST/DeclNodes.inc"
2350 llvm_unreachable("not an ObjC container!");
2351 }
2352 }
2353
2354 void searchFrom(ObjCProtocolDecl *protocol) {
2355 // A method in a protocol declaration overrides declarations from
2356 // referenced ("parent") protocols.
2357 search(protocol->getReferencedProtocols());
2358 }
2359
2360 void searchFrom(ObjCCategoryDecl *category) {
2361 // A method in a category declaration overrides declarations from
2362 // the main class and from protocols the category references.
2363 search(category->getClassInterface());
2364 search(category->getReferencedProtocols());
2365 }
2366
2367 void searchFrom(ObjCCategoryImplDecl *impl) {
2368 // A method in a category definition that has a category
2369 // declaration overrides declarations from the category
2370 // declaration.
2371 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2372 search(category);
2373
2374 // Otherwise it overrides declarations from the class.
2375 } else {
2376 search(impl->getClassInterface());
2377 }
2378 }
2379
2380 void searchFrom(ObjCInterfaceDecl *iface) {
2381 // A method in a class declaration overrides declarations from
2382
2383 // - categories,
2384 for (ObjCCategoryDecl *category = iface->getCategoryList();
2385 category; category = category->getNextClassCategory())
2386 search(category);
2387
2388 // - the super class, and
2389 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2390 search(super);
2391
2392 // - any referenced protocols.
2393 search(iface->getReferencedProtocols());
2394 }
2395
2396 void searchFrom(ObjCImplementationDecl *impl) {
2397 // A method in a class implementation overrides declarations from
2398 // the class interface.
2399 search(impl->getClassInterface());
2400 }
2401
2402
2403 void search(const ObjCProtocolList &protocols) {
2404 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2405 i != e; ++i)
2406 search(*i);
2407 }
2408
2409 void search(ObjCContainerDecl *container) {
2410 // Abort if we've already searched this container.
2411 if (!Searched.insert(container)) return;
2412
2413 // Check for a method in this container which matches this selector.
2414 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2415 Method->isInstanceMethod());
2416
2417 // If we find one, record it and bail out.
2418 if (meth) {
2419 Overridden.insert(meth);
2420 return;
2421 }
2422
2423 // Otherwise, search for methods that a hypothetical method here
2424 // would have overridden.
2425
2426 // Note that we're now in a recursive case.
2427 Recursive = true;
2428
2429 searchFromContainer(container);
2430 }
2431};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002432}
2433
John McCalld226f652010-08-21 09:40:31 +00002434Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002435 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002436 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002437 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002438 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002439 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002440 Selector Sel,
2441 // optional arguments. The number of types/arguments is obtained
2442 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002443 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002444 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002445 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002446 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002447 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002448 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002449 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002450 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002451 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002452 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2453 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002454 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002456 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002457 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002458 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Steve Naroffccef3712009-02-20 22:59:16 +00002460 // Methods cannot return interface types. All ObjC objects are
2461 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002462 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002463 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2464 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002465 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002466 }
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002467 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002468 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002469 Diag(MethodLoc, diag::warn_missing_method_return_type)
2470 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002471 }
Mike Stump1eb44332009-09-09 15:08:12 +00002472
2473 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002474 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002475 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002476 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002477 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002478 /*isSynthesized=*/false,
2479 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002480 MethodDeclKind == tok::objc_optional
2481 ? ObjCMethodDecl::Optional
2482 : ObjCMethodDecl::Required,
2483 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Chris Lattner5f9e2722011-07-23 10:55:15 +00002485 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002486
Chris Lattner7db638d2009-04-11 19:42:43 +00002487 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002488 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002489 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Chris Lattnere294d3f2009-04-11 18:57:04 +00002491 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002492 ArgType = Context.getObjCIdType();
2493 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002494 } else {
John McCall58e46772009-10-23 21:48:59 +00002495 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002496 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002497 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002498 }
Mike Stump1eb44332009-09-09 15:08:12 +00002499
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002500 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2501 LookupOrdinaryName, ForRedeclaration);
2502 LookupName(R, S);
2503 if (R.isSingleResult()) {
2504 NamedDecl *PrevDecl = R.getFoundDecl();
2505 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002506 Diag(ArgInfo[i].NameLoc,
2507 (MethodDefinition ? diag::warn_method_param_redefinition
2508 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002509 << ArgInfo[i].Name;
2510 Diag(PrevDecl->getLocation(),
2511 diag::note_previous_declaration);
2512 }
2513 }
2514
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002515 SourceLocation StartLoc = DI
2516 ? DI->getTypeLoc().getBeginLoc()
2517 : ArgInfo[i].NameLoc;
2518
John McCall81ef3e62011-04-23 02:46:06 +00002519 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2520 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2521 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002522
John McCall70798862011-05-02 00:30:12 +00002523 Param->setObjCMethodScopeInfo(i);
2524
Chris Lattner0ed844b2008-04-04 06:12:32 +00002525 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002526 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002527
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002528 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002529 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002530
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002531 S->AddDecl(Param);
2532 IdResolver.AddDecl(Param);
2533
Chris Lattner0ed844b2008-04-04 06:12:32 +00002534 Params.push_back(Param);
2535 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002536
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002537 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002538 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002539 QualType ArgType = Param->getType();
2540 if (ArgType.isNull())
2541 ArgType = Context.getObjCIdType();
2542 else
2543 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002544 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002545 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002546 Diag(Param->getLocation(),
2547 diag::err_object_cannot_be_passed_returned_by_value)
2548 << 1 << ArgType;
2549 Param->setInvalidDecl();
2550 }
2551 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002552
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002553 Params.push_back(Param);
2554 }
2555
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002556 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2557 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002558 ObjCMethod->setObjCDeclQualifier(
2559 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002560
2561 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002562 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002563
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002564 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002565 const ObjCMethodDecl *PrevMethod = 0;
2566 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002567 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002568 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2569 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002570 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002571 PrevMethod = ImpDecl->getClassMethod(Sel);
2572 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002573 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002574
Sean Huntcf807c42010-08-18 23:23:40 +00002575 if (ObjCMethod->hasAttrs() &&
2576 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002577 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002578 } else {
2579 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002580 }
John McCall6c2c2502011-07-22 02:45:48 +00002581
Chris Lattner4d391482007-12-12 07:09:47 +00002582 if (PrevMethod) {
2583 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002584 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002585 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002586 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002587 }
John McCall54abf7d2009-11-04 02:18:39 +00002588
Douglas Gregor926df6c2011-06-11 01:09:30 +00002589 // If this Objective-C method does not have a related result type, but we
2590 // are allowed to infer related result types, try to do so based on the
2591 // method family.
2592 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2593 if (!CurrentClass) {
2594 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2595 CurrentClass = Cat->getClassInterface();
2596 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2597 CurrentClass = Impl->getClassInterface();
2598 else if (ObjCCategoryImplDecl *CatImpl
2599 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2600 CurrentClass = CatImpl->getClassInterface();
2601 }
John McCall6c2c2502011-07-22 02:45:48 +00002602
2603 bool isRelatedResultTypeCompatible =
2604 (getLangOptions().ObjCInferRelatedResultType &&
2605 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass));
2606
2607 // Search for overridden methods and merge information down from them.
2608 OverrideSearch overrides(*this, ObjCMethod);
2609 for (OverrideSearch::iterator
2610 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2611 ObjCMethodDecl *overridden = *i;
2612
2613 // Propagate down the 'related result type' bit from overridden methods.
2614 if (isRelatedResultTypeCompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002615 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002616
2617 // Then merge the declarations.
2618 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002619
2620 // Check for overriding methods
2621 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2622 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2623 WarnConflictingTypedMethods(ObjCMethod, overridden,
2624 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2625 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002626 }
2627
John McCallf85e1932011-06-15 23:02:42 +00002628 bool ARCError = false;
2629 if (getLangOptions().ObjCAutoRefCount)
2630 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2631
John McCall6c2c2502011-07-22 02:45:48 +00002632 if (!ARCError && isRelatedResultTypeCompatible &&
2633 !ObjCMethod->hasRelatedResultType()) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002634 bool InferRelatedResultType = false;
2635 switch (ObjCMethod->getMethodFamily()) {
2636 case OMF_None:
2637 case OMF_copy:
2638 case OMF_dealloc:
2639 case OMF_mutableCopy:
2640 case OMF_release:
2641 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002642 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002643 break;
2644
2645 case OMF_alloc:
2646 case OMF_new:
2647 InferRelatedResultType = ObjCMethod->isClassMethod();
2648 break;
2649
2650 case OMF_init:
2651 case OMF_autorelease:
2652 case OMF_retain:
2653 case OMF_self:
2654 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2655 break;
2656 }
2657
John McCall6c2c2502011-07-22 02:45:48 +00002658 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002659 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002660 }
2661
John McCalld226f652010-08-21 09:40:31 +00002662 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002663}
2664
Chris Lattnercc98eac2008-12-17 07:13:27 +00002665bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002666 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002667 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002668 // Following is also an error. But it is caused my a missing @end
2669 // and diagnostic is issued elsewere.
2670 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2671 return false;
2672 }
2673
Anders Carlsson15281452008-11-04 16:57:32 +00002674 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2675 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002676
Anders Carlsson15281452008-11-04 16:57:32 +00002677 return true;
2678}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002679
Chris Lattnercc98eac2008-12-17 07:13:27 +00002680/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2681/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002682void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002683 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002684 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002685 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002686 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002687 if (!Class) {
2688 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2689 return;
2690 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002691 if (LangOpts.ObjCNonFragileABI) {
2692 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2693 return;
2694 }
Mike Stump1eb44332009-09-09 15:08:12 +00002695
Chris Lattnercc98eac2008-12-17 07:13:27 +00002696 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002697 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002698 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002699 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002700 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002701 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002702 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002703 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2704 /*FIXME: StartL=*/ID->getLocation(),
2705 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002706 ID->getIdentifier(), ID->getType(),
2707 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002708 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002709 }
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Chris Lattnercc98eac2008-12-17 07:13:27 +00002711 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002712 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002713 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002714 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002715 if (getLangOptions().CPlusPlus)
2716 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002717 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002718 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002719 }
2720}
2721
Douglas Gregor160b5632010-04-26 17:32:49 +00002722/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002723VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2724 SourceLocation StartLoc,
2725 SourceLocation IdLoc,
2726 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002727 bool Invalid) {
2728 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2729 // duration shall not be qualified by an address-space qualifier."
2730 // Since all parameters have automatic store duration, they can not have
2731 // an address space.
2732 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002733 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002734 Invalid = true;
2735 }
2736
2737 // An @catch parameter must be an unqualified object pointer type;
2738 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2739 if (Invalid) {
2740 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002741 } else if (T->isDependentType()) {
2742 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002743 } else if (!T->isObjCObjectPointerType()) {
2744 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002745 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002746 } else if (T->isObjCQualifiedIdType()) {
2747 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002748 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002749 }
2750
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002751 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2752 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002753 New->setExceptionVariable(true);
2754
Douglas Gregor160b5632010-04-26 17:32:49 +00002755 if (Invalid)
2756 New->setInvalidDecl();
2757 return New;
2758}
2759
John McCalld226f652010-08-21 09:40:31 +00002760Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002761 const DeclSpec &DS = D.getDeclSpec();
2762
2763 // We allow the "register" storage class on exception variables because
2764 // GCC did, but we drop it completely. Any other storage class is an error.
2765 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2766 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2767 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2768 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2769 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2770 << DS.getStorageClassSpec();
2771 }
2772 if (D.getDeclSpec().isThreadSpecified())
2773 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2774 D.getMutableDeclSpec().ClearStorageClassSpecs();
2775
2776 DiagnoseFunctionSpecifiers(D);
2777
2778 // Check that there are no default arguments inside the type of this
2779 // exception object (C++ only).
2780 if (getLangOptions().CPlusPlus)
2781 CheckExtraCXXDefaultArguments(D);
2782
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002783 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002784 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002785
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002786 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2787 D.getSourceRange().getBegin(),
2788 D.getIdentifierLoc(),
2789 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002790 D.isInvalidType());
2791
2792 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2793 if (D.getCXXScopeSpec().isSet()) {
2794 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2795 << D.getCXXScopeSpec().getRange();
2796 New->setInvalidDecl();
2797 }
2798
2799 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002800 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002801 if (D.getIdentifier())
2802 IdResolver.AddDecl(New);
2803
2804 ProcessDeclAttributes(S, New, D);
2805
2806 if (New->hasAttr<BlocksAttr>())
2807 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002808 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002809}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002810
2811/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002812/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002813void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002814 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002815 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2816 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002817 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002818 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002819 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002820 }
2821}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002822
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002823void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002824 // Load referenced selectors from the external source.
2825 if (ExternalSource) {
2826 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2827 ExternalSource->ReadReferencedSelectors(Sels);
2828 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2829 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2830 }
2831
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002832 // Warning will be issued only when selector table is
2833 // generated (which means there is at lease one implementation
2834 // in the TU). This is to match gcc's behavior.
2835 if (ReferencedSelectors.empty() ||
2836 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002837 return;
2838 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2839 ReferencedSelectors.begin(),
2840 E = ReferencedSelectors.end(); S != E; ++S) {
2841 Selector Sel = (*S).first;
2842 if (!LookupImplementedMethodInGlobalPool(Sel))
2843 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2844 }
2845 return;
2846}