blob: d255a3686ff2a8dc9405868dddac6bb211c816a9 [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
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000281 // Warn on implementating deprecated methods under
282 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000283 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
284 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);
Chris Lattner4d391482007-12-12 07:09:47 +0000289}
290
John McCalld226f652010-08-21 09:40:31 +0000291Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000292ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
293 IdentifierInfo *ClassName, SourceLocation ClassLoc,
294 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000295 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000296 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000297 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000298 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Chris Lattner4d391482007-12-12 07:09:47 +0000300 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000301 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000302 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000303
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000304 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000305 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000306 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000309 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
310 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000311 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000312 if (!IDecl->isForwardDecl()) {
313 IDecl->setInvalidDecl();
314 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
315 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000316
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000317 // Return the previous class interface.
318 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000319 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000320 } else {
321 IDecl->setLocation(AtInterfaceLoc);
322 IDecl->setForwardDecl(false);
323 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000324 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000325 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000326 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000327
328 // Since this ObjCInterfaceDecl was created by a forward declaration,
329 // we now add it to the DeclContext since it wasn't added before
330 // (see ActOnForwardClassDeclaration).
331 IDecl->setLexicalDeclContext(CurContext);
332 CurContext->addDecl(IDecl);
333
334 if (AttrList)
335 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000336 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000337 } else {
338 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
339 ClassName, ClassLoc);
340 if (AttrList)
341 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
342
343 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner4d391482007-12-12 07:09:47 +0000346 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000347 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000348 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
349 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000350
351 if (!PrevDecl) {
352 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000353 TypoCorrection Corrected = CorrectTypo(
354 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
355 NULL, NULL, false, CTC_NoKeywords);
356 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000357 Diag(SuperLoc, diag::err_undef_superclass_suggest)
358 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000359 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
360 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000361 }
362 }
363
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000364 if (PrevDecl == IDecl) {
365 Diag(SuperLoc, diag::err_recursive_superclass)
366 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
367 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000368 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000369 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000370 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000371
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000372 // Diagnose classes that inherit from deprecated classes.
373 if (SuperClassDecl)
374 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000376 if (PrevDecl && SuperClassDecl == 0) {
377 // The previous declaration was not a class decl. Check if we have a
378 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000379 if (const TypedefNameDecl *TDecl =
380 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000381 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000382 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000383 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
384 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000385 }
386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000388 // This handles the following case:
389 //
390 // typedef int SuperClass;
391 // @interface MyClass : SuperClass {} @end
392 //
393 if (!SuperClassDecl) {
394 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
395 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000396 }
397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Richard Smith162e1c12011-04-15 14:24:37 +0000399 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000400 if (!SuperClassDecl)
401 Diag(SuperLoc, diag::err_undef_superclass)
402 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000403 else if (SuperClassDecl->isForwardDecl()) {
404 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000405 << SuperClassDecl->getDeclName() << ClassName
406 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000407 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
408 SuperClassDecl = 0;
409 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000410 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000411 IDecl->setSuperClass(SuperClassDecl);
412 IDecl->setSuperClassLoc(SuperLoc);
413 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000414 }
Chris Lattner4d391482007-12-12 07:09:47 +0000415 } else { // we have a root class.
416 IDecl->setLocEnd(ClassLoc);
417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Sebastian Redl0b17c612010-08-13 00:28:03 +0000419 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000420 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000421 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000422 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000423 IDecl->setLocEnd(EndProtoLoc);
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlsson15281452008-11-04 16:57:32 +0000426 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000427 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000428}
429
430/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000431/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000432Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
433 IdentifierInfo *AliasName,
434 SourceLocation AliasLocation,
435 IdentifierInfo *ClassName,
436 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000437 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000438 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000439 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000440 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000441 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000442 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000443 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000444 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000445 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000446 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000447 }
448 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000449 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000450 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000451 if (const TypedefNameDecl *TDecl =
452 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000453 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000454 if (T->isObjCObjectType()) {
455 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000456 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000457 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000458 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000459 }
460 }
461 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000462 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
463 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000464 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000465 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000466 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000467 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000470 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000471 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000472 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Anders Carlsson15281452008-11-04 16:57:32 +0000474 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000475 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000476
John McCalld226f652010-08-21 09:40:31 +0000477 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000478}
479
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000480bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000481 IdentifierInfo *PName,
482 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000483 const ObjCList<ObjCProtocolDecl> &PList) {
484
485 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000486 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
487 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000488 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
489 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000490 if (PDecl->getIdentifier() == PName) {
491 Diag(Ploc, diag::err_protocol_has_circular_dependency);
492 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000493 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000494 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000495 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
496 PDecl->getLocation(), PDecl->getReferencedProtocols()))
497 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000498 }
499 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000500 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000501}
502
John McCalld226f652010-08-21 09:40:31 +0000503Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000504Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
505 IdentifierInfo *ProtocolName,
506 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000507 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000508 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000509 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000510 SourceLocation EndProtoLoc,
511 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000512 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000513 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000514 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000515 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000516 if (PDecl) {
517 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000518 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000519 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000520 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000521 // Just return the protocol we already had.
522 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000523 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000524 }
Steve Naroff61d68522009-03-05 15:22:01 +0000525 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000526 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000527 err = CheckForwardProtocolDeclarationForCircularDependency(
528 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Steve Narofff11b5082008-08-13 16:39:22 +0000530 // Make sure the cached decl gets a valid start location.
531 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000532 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000533 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000534 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000535 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000536 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000537 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000538 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000539 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000540 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000541 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000542 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000543 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000544 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000545 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000546 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
547 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000548 PDecl->setLocEnd(EndProtoLoc);
549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
551 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000552 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000553}
554
555/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000556/// issues an error if they are not declared. It returns list of
557/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000558void
Chris Lattnere13b9592008-07-26 04:03:38 +0000559Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000560 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000561 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000562 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000563 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000564 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
565 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000566 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000567 TypoCorrection Corrected = CorrectTypo(
568 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
569 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
570 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000571 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000572 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000573 Diag(PDecl->getLocation(), diag::note_previous_decl)
574 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000575 }
576 }
577
578 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000579 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000580 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000581 continue;
582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000584 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000585
586 // If this is a forward declaration and we are supposed to warn in this
587 // case, do it.
588 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000589 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000590 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000591 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000592 }
593}
594
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000595/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000596/// a class method in its extension.
597///
Mike Stump1eb44332009-09-09 15:08:12 +0000598void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000599 ObjCInterfaceDecl *ID) {
600 if (!ID)
601 return; // Possibly due to previous error
602
603 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000604 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
605 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000606 ObjCMethodDecl *MD = *i;
607 MethodMap[MD->getSelector()] = MD;
608 }
609
610 if (MethodMap.empty())
611 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000612 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
613 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000614 ObjCMethodDecl *Method = *i;
615 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
616 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
617 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
618 << Method->getDeclName();
619 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
620 }
621 }
622}
623
Chris Lattner58fe03b2009-04-12 08:43:13 +0000624/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000625Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000626Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000627 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000628 unsigned NumElts,
629 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000630 SmallVector<ObjCProtocolDecl*, 32> Protocols;
631 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Chris Lattner4d391482007-12-12 07:09:47 +0000633 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000634 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000635 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000636 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000637 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000638 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000639 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000640 PushOnScopeChains(PDecl, TUScope, false);
641 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000642 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000643 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000644 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000645 if (!isNew)
646 PDecl->setChangedSinceDeserialization(true);
647 }
Chris Lattner4d391482007-12-12 07:09:47 +0000648 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000649 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
652 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000653 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000654 Protocols.data(), Protocols.size(),
655 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000656 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000657 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000658 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000659}
660
John McCalld226f652010-08-21 09:40:31 +0000661Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000662ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
663 IdentifierInfo *ClassName, SourceLocation ClassLoc,
664 IdentifierInfo *CategoryName,
665 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000666 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000667 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000668 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000669 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000670 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000671 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000672
673 /// Check that class of this category is already completely declared.
674 if (!IDecl || IDecl->isForwardDecl()) {
675 // Create an invalid ObjCCategoryDecl to serve as context for
676 // the enclosing method declarations. We mark the decl invalid
677 // to make it clear that this isn't a valid AST.
678 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
679 ClassLoc, CategoryLoc, CategoryName);
680 CDecl->setInvalidDecl();
681 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000682 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000683 }
684
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000685 if (!CategoryName && IDecl->getImplementation()) {
686 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
687 Diag(IDecl->getImplementation()->getLocation(),
688 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000689 }
690
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000691 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
692 ClassLoc, CategoryLoc, CategoryName);
693 // FIXME: PushOnScopeChains?
694 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000695
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000696 CDecl->setClassInterface(IDecl);
697 // Insert class extension to the list of class's categories.
698 if (!CategoryName)
699 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Chris Lattner16b34b42009-02-16 21:30:01 +0000701 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000702 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000703
Fariborz Jahanian25760612010-02-15 21:55:26 +0000704 if (CategoryName) {
705 /// Check for duplicate interface declaration for this category
706 ObjCCategoryDecl *CDeclChain;
707 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
708 CDeclChain = CDeclChain->getNextClassCategory()) {
709 if (CDeclChain->getIdentifier() == CategoryName) {
710 // Class extensions can be declared multiple times.
711 Diag(CategoryLoc, diag::warn_dup_category_def)
712 << ClassName << CategoryName;
713 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
714 break;
715 }
Chris Lattner70f19542009-02-16 21:26:43 +0000716 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000717 if (!CDeclChain)
718 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000719 }
Chris Lattner70f19542009-02-16 21:26:43 +0000720
Chris Lattner4d391482007-12-12 07:09:47 +0000721 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000722 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000723 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000724 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000725 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000726 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000727 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Anders Carlsson15281452008-11-04 16:57:32 +0000730 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000731 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000732}
733
734/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000735/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000736/// object.
John McCalld226f652010-08-21 09:40:31 +0000737Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000738 SourceLocation AtCatImplLoc,
739 IdentifierInfo *ClassName, SourceLocation ClassLoc,
740 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000741 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000742 ObjCCategoryDecl *CatIDecl = 0;
743 if (IDecl) {
744 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
745 if (!CatIDecl) {
746 // Category @implementation with no corresponding @interface.
747 // Create and install one.
748 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000749 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000750 CatName);
751 CatIDecl->setClassInterface(IDecl);
752 CatIDecl->insertNextClassCategory();
753 }
754 }
755
Mike Stump1eb44332009-09-09 15:08:12 +0000756 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000757 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
758 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000759 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000760 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000761 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000762 CDecl->setInvalidDecl();
763 }
Chris Lattner4d391482007-12-12 07:09:47 +0000764
Douglas Gregord0434102009-01-09 00:49:46 +0000765 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000766 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000767
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000768 /// Check that CatName, category name, is not used in another implementation.
769 if (CatIDecl) {
770 if (CatIDecl->getImplementation()) {
771 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
772 << CatName;
773 Diag(CatIDecl->getImplementation()->getLocation(),
774 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000775 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000776 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000777 // Warn on implementating category of deprecated class under
778 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000779 DiagnoseObjCImplementedDeprecations(*this,
780 dyn_cast<NamedDecl>(IDecl),
781 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000782 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Anders Carlsson15281452008-11-04 16:57:32 +0000785 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000786 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000787}
788
John McCalld226f652010-08-21 09:40:31 +0000789Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000790 SourceLocation AtClassImplLoc,
791 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000792 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000793 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000794 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000795 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000796 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000797 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
798 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000799 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000800 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000801 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000802 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
803 // If this is a forward declaration of an interface, warn.
804 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000805 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000806 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000807 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000808 } else {
809 // We did not find anything with the name ClassName; try to correct for
810 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000811 TypoCorrection Corrected = CorrectTypo(
812 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
813 NULL, NULL, false, CTC_NoKeywords);
814 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000815 // Suggest the (potentially) correct interface name. However, put the
816 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000817 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000818 // provide a code-modification hint or use the typo name for recovery,
819 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000820 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000821 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000822 << ClassName << CorrectedName;
823 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
824 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000825 IDecl = 0;
826 } else {
827 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
828 }
Chris Lattner4d391482007-12-12 07:09:47 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattner4d391482007-12-12 07:09:47 +0000831 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000832 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000833 if (SuperClassname) {
834 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000835 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
836 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000837 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000838 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
839 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000840 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000841 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000842 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000843 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000844 Diag(SuperClassLoc, diag::err_undef_superclass)
845 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000846 else if (IDecl && IDecl->getSuperClass() != SDecl) {
847 // This implementation and its interface do not have the same
848 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000849 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000850 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000851 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000852 }
853 }
854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Chris Lattner4d391482007-12-12 07:09:47 +0000856 if (!IDecl) {
857 // Legacy case of @implementation with no corresponding @interface.
858 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000859
Mike Stump390b4cc2009-05-16 07:39:55 +0000860 // FIXME: Do we support attributes on the @implementation? If so we should
861 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000862 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000863 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000864 IDecl->setSuperClass(SDecl);
865 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000866
867 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000868 } else {
869 // Mark the interface as being completed, even if it was just as
870 // @class ....;
871 // declaration; the user cannot reopen it.
872 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
875 ObjCImplementationDecl* IMPDecl =
876 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000877 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Anders Carlsson15281452008-11-04 16:57:32 +0000879 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000880 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner4d391482007-12-12 07:09:47 +0000882 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000883 if (IDecl->getImplementation()) {
884 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000885 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000886 Diag(IDecl->getImplementation()->getLocation(),
887 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000888 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000889 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000890 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000891 // Warn on implementating deprecated class under
892 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000893 DiagnoseObjCImplementedDeprecations(*this,
894 dyn_cast<NamedDecl>(IDecl),
895 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000896 }
John McCalld226f652010-08-21 09:40:31 +0000897 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000898}
899
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000900void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
901 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000902 SourceLocation RBrace) {
903 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000904 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000905 if (!IDecl)
906 return;
907 /// Check case of non-existing @interface decl.
908 /// (legacy objective-c @implementation decl without an @interface decl).
909 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000910 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000911 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000912 // Add ivar's to class's DeclContext.
913 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000914 ivars[i]->setLexicalDeclContext(ImpDecl);
915 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000916 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000917 }
918
Chris Lattner4d391482007-12-12 07:09:47 +0000919 return;
920 }
921 // If implementation has empty ivar list, just return.
922 if (numIvars == 0)
923 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Chris Lattner4d391482007-12-12 07:09:47 +0000925 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000926 if (LangOpts.ObjCNonFragileABI2) {
927 if (ImpDecl->getSuperClass())
928 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
929 for (unsigned i = 0; i < numIvars; i++) {
930 ObjCIvarDecl* ImplIvar = ivars[i];
931 if (const ObjCIvarDecl *ClsIvar =
932 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
933 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
934 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
935 continue;
936 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000937 // Instance ivar to Implementation's DeclContext.
938 ImplIvar->setLexicalDeclContext(ImpDecl);
939 IDecl->makeDeclVisibleInContext(ImplIvar, false);
940 ImpDecl->addDecl(ImplIvar);
941 }
942 return;
943 }
Chris Lattner4d391482007-12-12 07:09:47 +0000944 // Check interface's Ivar list against those in the implementation.
945 // names and types must match.
946 //
Chris Lattner4d391482007-12-12 07:09:47 +0000947 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000948 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000949 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
950 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000951 ObjCIvarDecl* ImplIvar = ivars[j++];
952 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000953 assert (ImplIvar && "missing implementation ivar");
954 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Steve Naroffca331292009-03-03 14:49:36 +0000956 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000957 if (Context.getCanonicalType(ImplIvar->getType()) !=
958 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000959 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000960 << ImplIvar->getIdentifier()
961 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000962 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000963 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
964 Expr *ImplBitWidth = ImplIvar->getBitWidth();
965 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000966 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
967 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000968 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
969 << ImplIvar->getIdentifier();
970 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972 }
Steve Naroffca331292009-03-03 14:49:36 +0000973 // Make sure the names are identical.
974 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000975 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000976 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000977 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000978 }
979 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000980 }
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattner609e4c72007-12-12 18:11:49 +0000982 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000983 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000984 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000985 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000986}
987
Steve Naroff3c2eb662008-02-10 21:38:56 +0000988void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000989 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +0000990 // No point warning no definition of method which is 'unavailable'.
991 if (method->hasAttr<UnavailableAttr>())
992 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +0000993 if (!IncompleteImpl) {
994 Diag(ImpLoc, diag::warn_incomplete_impl);
995 IncompleteImpl = true;
996 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000997 if (DiagID == diag::warn_unimplemented_protocol_method)
998 Diag(ImpLoc, DiagID) << method->getDeclName();
999 else
1000 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001001}
1002
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001003/// Determines if type B can be substituted for type A. Returns true if we can
1004/// guarantee that anything that the user will do to an object of type A can
1005/// also be done to an object of type B. This is trivially true if the two
1006/// types are the same, or if B is a subclass of A. It becomes more complex
1007/// in cases where protocols are involved.
1008///
1009/// Object types in Objective-C describe the minimum requirements for an
1010/// object, rather than providing a complete description of a type. For
1011/// example, if A is a subclass of B, then B* may refer to an instance of A.
1012/// The principle of substitutability means that we may use an instance of A
1013/// anywhere that we may use an instance of B - it will implement all of the
1014/// ivars of B and all of the methods of B.
1015///
1016/// This substitutability is important when type checking methods, because
1017/// the implementation may have stricter type definitions than the interface.
1018/// The interface specifies minimum requirements, but the implementation may
1019/// have more accurate ones. For example, a method may privately accept
1020/// instances of B, but only publish that it accepts instances of A. Any
1021/// object passed to it will be type checked against B, and so will implicitly
1022/// by a valid A*. Similarly, a method may return a subclass of the class that
1023/// it is declared as returning.
1024///
1025/// This is most important when considering subclassing. A method in a
1026/// subclass must accept any object as an argument that its superclass's
1027/// implementation accepts. It may, however, accept a more general type
1028/// without breaking substitutability (i.e. you can still use the subclass
1029/// anywhere that you can use the superclass, but not vice versa). The
1030/// converse requirement applies to return types: the return type for a
1031/// subclass method must be a valid object of the kind that the superclass
1032/// advertises, but it may be specified more accurately. This avoids the need
1033/// for explicit down-casting by callers.
1034///
1035/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001036static bool isObjCTypeSubstitutable(ASTContext &Context,
1037 const ObjCObjectPointerType *A,
1038 const ObjCObjectPointerType *B,
1039 bool rejectId) {
1040 // Reject a protocol-unqualified id.
1041 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001042
1043 // If B is a qualified id, then A must also be a qualified id and it must
1044 // implement all of the protocols in B. It may not be a qualified class.
1045 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1046 // stricter definition so it is not substitutable for id<A>.
1047 if (B->isObjCQualifiedIdType()) {
1048 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001049 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1050 QualType(B,0),
1051 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001052 }
1053
1054 /*
1055 // id is a special type that bypasses type checking completely. We want a
1056 // warning when it is used in one place but not another.
1057 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1058
1059
1060 // If B is a qualified id, then A must also be a qualified id (which it isn't
1061 // if we've got this far)
1062 if (B->isObjCQualifiedIdType()) return false;
1063 */
1064
1065 // Now we know that A and B are (potentially-qualified) class types. The
1066 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001067 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001068}
1069
John McCall10302c02010-10-28 02:34:38 +00001070static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1071 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1072}
1073
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001074static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001075 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001076 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001077 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001078 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001079 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001080 if (IsProtocolMethodDecl &&
1081 (MethodDecl->getObjCDeclQualifier() !=
1082 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001083 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001084 S.Diag(MethodImpl->getLocation(),
1085 (IsOverridingMode ?
1086 diag::warn_conflicting_overriding_ret_type_modifiers
1087 : diag::warn_conflicting_ret_type_modifiers))
1088 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001089 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1090 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1091 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1092 }
1093 else
1094 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001095 }
1096
John McCall10302c02010-10-28 02:34:38 +00001097 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001098 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001099 return true;
1100 if (!Warn)
1101 return false;
John McCall10302c02010-10-28 02:34:38 +00001102
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001103 unsigned DiagID =
1104 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1105 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001106
1107 // Mismatches between ObjC pointers go into a different warning
1108 // category, and sometimes they're even completely whitelisted.
1109 if (const ObjCObjectPointerType *ImplPtrTy =
1110 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1111 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001112 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001113 // Allow non-matching return types as long as they don't violate
1114 // the principle of substitutability. Specifically, we permit
1115 // return types that are subclasses of the declared return type,
1116 // or that are more-qualified versions of the declared type.
1117 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001118 return false;
John McCall10302c02010-10-28 02:34:38 +00001119
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001120 DiagID =
1121 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1122 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001123 }
1124 }
1125
1126 S.Diag(MethodImpl->getLocation(), DiagID)
1127 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001128 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001129 << MethodImpl->getResultType()
1130 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001131 S.Diag(MethodDecl->getLocation(),
1132 IsOverridingMode ? diag::note_previous_declaration
1133 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001134 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001135 return false;
John McCall10302c02010-10-28 02:34:38 +00001136}
1137
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001138static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001139 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001140 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001141 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001142 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001143 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001144 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001145 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001146 if (IsProtocolMethodDecl &&
1147 (ImplVar->getObjCDeclQualifier() !=
1148 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001149 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001150 if (IsOverridingMode)
1151 S.Diag(ImplVar->getLocation(),
1152 diag::warn_conflicting_overriding_param_modifiers)
1153 << getTypeRange(ImplVar->getTypeSourceInfo())
1154 << MethodImpl->getDeclName();
1155 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001156 diag::warn_conflicting_param_modifiers)
1157 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001158 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001159 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1160 << getTypeRange(IfaceVar->getTypeSourceInfo());
1161 }
1162 else
1163 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001164 }
1165
John McCall10302c02010-10-28 02:34:38 +00001166 QualType ImplTy = ImplVar->getType();
1167 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001168
John McCall10302c02010-10-28 02:34:38 +00001169 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001170 return true;
1171
1172 if (!Warn)
1173 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001174 unsigned DiagID =
1175 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1176 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001177
1178 // Mismatches between ObjC pointers go into a different warning
1179 // category, and sometimes they're even completely whitelisted.
1180 if (const ObjCObjectPointerType *ImplPtrTy =
1181 ImplTy->getAs<ObjCObjectPointerType>()) {
1182 if (const ObjCObjectPointerType *IfacePtrTy =
1183 IfaceTy->getAs<ObjCObjectPointerType>()) {
1184 // Allow non-matching argument types as long as they don't
1185 // violate the principle of substitutability. Specifically, the
1186 // implementation must accept any objects that the superclass
1187 // accepts, however it may also accept others.
1188 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001189 return false;
John McCall10302c02010-10-28 02:34:38 +00001190
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001191 DiagID =
1192 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1193 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001194 }
1195 }
1196
1197 S.Diag(ImplVar->getLocation(), DiagID)
1198 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001199 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1200 S.Diag(IfaceVar->getLocation(),
1201 (IsOverridingMode ? diag::note_previous_declaration
1202 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001203 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001204 return false;
John McCall10302c02010-10-28 02:34:38 +00001205}
John McCallf85e1932011-06-15 23:02:42 +00001206
1207/// In ARC, check whether the conventional meanings of the two methods
1208/// match. If they don't, it's a hard error.
1209static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1210 ObjCMethodDecl *decl) {
1211 ObjCMethodFamily implFamily = impl->getMethodFamily();
1212 ObjCMethodFamily declFamily = decl->getMethodFamily();
1213 if (implFamily == declFamily) return false;
1214
1215 // Since conventions are sorted by selector, the only possibility is
1216 // that the types differ enough to cause one selector or the other
1217 // to fall out of the family.
1218 assert(implFamily == OMF_None || declFamily == OMF_None);
1219
1220 // No further diagnostics required on invalid declarations.
1221 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1222
1223 const ObjCMethodDecl *unmatched = impl;
1224 ObjCMethodFamily family = declFamily;
1225 unsigned errorID = diag::err_arc_lost_method_convention;
1226 unsigned noteID = diag::note_arc_lost_method_convention;
1227 if (declFamily == OMF_None) {
1228 unmatched = decl;
1229 family = implFamily;
1230 errorID = diag::err_arc_gained_method_convention;
1231 noteID = diag::note_arc_gained_method_convention;
1232 }
1233
1234 // Indexes into a %select clause in the diagnostic.
1235 enum FamilySelector {
1236 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1237 };
1238 FamilySelector familySelector = FamilySelector();
1239
1240 switch (family) {
1241 case OMF_None: llvm_unreachable("logic error, no method convention");
1242 case OMF_retain:
1243 case OMF_release:
1244 case OMF_autorelease:
1245 case OMF_dealloc:
1246 case OMF_retainCount:
1247 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001248 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001249 // Mismatches for these methods don't change ownership
1250 // conventions, so we don't care.
1251 return false;
1252
1253 case OMF_init: familySelector = F_init; break;
1254 case OMF_alloc: familySelector = F_alloc; break;
1255 case OMF_copy: familySelector = F_copy; break;
1256 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1257 case OMF_new: familySelector = F_new; break;
1258 }
1259
1260 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1261 ReasonSelector reasonSelector;
1262
1263 // The only reason these methods don't fall within their families is
1264 // due to unusual result types.
1265 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1266 reasonSelector = R_UnrelatedReturn;
1267 } else {
1268 reasonSelector = R_NonObjectReturn;
1269 }
1270
1271 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1272 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1273
1274 return true;
1275}
John McCall10302c02010-10-28 02:34:38 +00001276
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001277void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001278 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001279 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001280 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001281 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001282 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001283 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1284 return;
1285
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001286 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001287 IsProtocolMethodDecl, IsOverridingMode,
1288 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Chris Lattner3aff9192009-04-11 19:58:42 +00001290 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001291 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001292 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001293 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1294 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001295 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001296
Fariborz Jahanian21121902011-08-08 18:03:17 +00001297 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001298 if (IsOverridingMode)
1299 Diag(ImpMethodDecl->getLocation(),
1300 diag::warn_conflicting_overriding_variadic);
1301 else
1302 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001303 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001304 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001305}
1306
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001307/// WarnExactTypedMethods - This routine issues a warning if method
1308/// implementation declaration matches exactly that of its declaration.
1309void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1310 ObjCMethodDecl *MethodDecl,
1311 bool IsProtocolMethodDecl) {
1312 // don't issue warning when protocol method is optional because primary
1313 // class is not required to implement it and it is safe for protocol
1314 // to implement it.
1315 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1316 return;
1317 // don't issue warning when primary class's method is
1318 // depecated/unavailable.
1319 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1320 MethodDecl->hasAttr<DeprecatedAttr>())
1321 return;
1322
1323 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1324 IsProtocolMethodDecl, false, false);
1325 if (match)
1326 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1327 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1328 IM != EM; ++IM, ++IF) {
1329 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1330 *IM, *IF,
1331 IsProtocolMethodDecl, false, false);
1332 if (!match)
1333 break;
1334 }
1335 if (match)
1336 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001337 if (match)
1338 match = !(MethodDecl->isClassMethod() &&
1339 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001340
1341 if (match) {
1342 Diag(ImpMethodDecl->getLocation(),
1343 diag::warn_category_method_impl_match);
1344 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1345 }
1346}
1347
Mike Stump390b4cc2009-05-16 07:39:55 +00001348/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1349/// improve the efficiency of selector lookups and type checking by associating
1350/// with each protocol / interface / category the flattened instance tables. If
1351/// we used an immutable set to keep the table then it wouldn't add significant
1352/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001353
Steve Naroffefe7f362008-02-08 22:06:17 +00001354/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001355/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001356void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1357 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001358 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001359 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001360 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001361 ObjCContainerDecl *CDecl) {
1362 ObjCInterfaceDecl *IDecl;
1363 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1364 IDecl = C->getClassInterface();
1365 else
1366 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1367 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1368
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001369 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001370 ObjCInterfaceDecl *NSIDecl = 0;
1371 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001372 // check to see if class implements forwardInvocation method and objects
1373 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001374 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001375 // Under such conditions, which means that every method possible is
1376 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001377 // found" warnings.
1378 // FIXME: Use a general GetUnarySelector method for this.
1379 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1380 Selector fISelector = Context.Selectors.getSelector(1, &II);
1381 if (InsMap.count(fISelector))
1382 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1383 // need be implemented in the implementation.
1384 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001387 // If a method lookup fails locally we still need to look and see if
1388 // the method was implemented by a base class or an inherited
1389 // protocol. This lookup is slow, but occurs rarely in correct code
1390 // and otherwise would terminate in a warning.
1391
Chris Lattner4d391482007-12-12 07:09:47 +00001392 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001393 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001394 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001395 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001396 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001397 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001398 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001399 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001400 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001401 // Ugly, but necessary. Method declared in protcol might have
1402 // have been synthesized due to a property declared in the class which
1403 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001404 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001405 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001406 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001407 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001408 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1409 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001410 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001411 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001412 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1413 << PDecl->getDeclName();
1414 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001415 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001416 }
1417 }
Chris Lattner4d391482007-12-12 07:09:47 +00001418 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001419 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001420 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001421 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001422 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001423 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1424 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001425 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001426 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001427 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001428 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001429 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001430 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1431 PDecl->getDeclName();
1432 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001433 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001434 }
Chris Lattner780f3292008-07-21 21:32:27 +00001435 // Check on this protocols's referenced protocols, recursively.
1436 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1437 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001438 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001439}
1440
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001441/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001442/// or protocol against those declared in their implementations.
1443///
1444void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1445 const llvm::DenseSet<Selector> &ClsMap,
1446 llvm::DenseSet<Selector> &InsMapSeen,
1447 llvm::DenseSet<Selector> &ClsMapSeen,
1448 ObjCImplDecl* IMPDecl,
1449 ObjCContainerDecl* CDecl,
1450 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001451 bool ImmediateClass,
1452 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001453 // Check and see if instance methods in class interface have been
1454 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001455 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1456 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001457 if (InsMapSeen.count((*I)->getSelector()))
1458 continue;
1459 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001460 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001461 !InsMap.count((*I)->getSelector())) {
1462 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001463 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1464 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001465 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001466 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001467 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001468 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001469 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001470 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001471 assert(MethodDecl &&
1472 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001473 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001474 if (ImpMethodDecl) {
1475 if (!WarnExactMatch)
1476 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1477 isa<ObjCProtocolDecl>(CDecl));
1478 else
1479 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1480 isa<ObjCProtocolDecl>(CDecl));
1481 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001482 }
1483 }
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001485 // Check and see if class methods in class interface have been
1486 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001487 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001488 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001489 if (ClsMapSeen.count((*I)->getSelector()))
1490 continue;
1491 ClsMapSeen.insert((*I)->getSelector());
1492 if (!ClsMap.count((*I)->getSelector())) {
1493 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001494 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1495 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001496 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001497 ObjCMethodDecl *ImpMethodDecl =
1498 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001499 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001500 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001501 if (!WarnExactMatch)
1502 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1503 isa<ObjCProtocolDecl>(CDecl));
1504 else
1505 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1506 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001507 }
1508 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001509
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001510 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001511 // Also methods in class extensions need be looked at next.
1512 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1513 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1514 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1515 IMPDecl,
1516 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001517 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001518
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001519 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001520 for (ObjCInterfaceDecl::all_protocol_iterator
1521 PI = I->all_referenced_protocol_begin(),
1522 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001523 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1524 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001525 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001526
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001527 // FIXME. For now, we are not checking for extact match of methods
1528 // in category implementation and its primary class's super class.
1529 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001530 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001531 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001532 I->getSuperClass(), IncompleteImpl, false);
1533 }
1534}
1535
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001536/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1537/// category matches with those implemented in its primary class and
1538/// warns each time an exact match is found.
1539void Sema::CheckCategoryVsClassMethodMatches(
1540 ObjCCategoryImplDecl *CatIMPDecl) {
1541 llvm::DenseSet<Selector> InsMap, ClsMap;
1542
1543 for (ObjCImplementationDecl::instmeth_iterator
1544 I = CatIMPDecl->instmeth_begin(),
1545 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1546 InsMap.insert((*I)->getSelector());
1547
1548 for (ObjCImplementationDecl::classmeth_iterator
1549 I = CatIMPDecl->classmeth_begin(),
1550 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1551 ClsMap.insert((*I)->getSelector());
1552 if (InsMap.empty() && ClsMap.empty())
1553 return;
1554
1555 // Get category's primary class.
1556 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1557 if (!CatDecl)
1558 return;
1559 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1560 if (!IDecl)
1561 return;
1562 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1563 bool IncompleteImpl = false;
1564 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1565 CatIMPDecl, IDecl,
1566 IncompleteImpl, false, true /*WarnExactMatch*/);
1567}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001568
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001569void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001570 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001571 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001572 llvm::DenseSet<Selector> InsMap;
1573 // Check and see if instance methods in class interface have been
1574 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001575 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001576 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001577 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001579 // Check and see if properties declared in the interface have either 1)
1580 // an implementation or 2) there is a @synthesize/@dynamic implementation
1581 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001582 if (isa<ObjCInterfaceDecl>(CDecl) &&
1583 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001584 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001585
Chris Lattner4d391482007-12-12 07:09:47 +00001586 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001587 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001588 I = IMPDecl->classmeth_begin(),
1589 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001590 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001592 // Check for type conflict of methods declared in a class/protocol and
1593 // its implementation; if any.
1594 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001595 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1596 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001597 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001598
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001599 // check all methods implemented in category against those declared
1600 // in its primary class.
1601 if (ObjCCategoryImplDecl *CatDecl =
1602 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1603 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Chris Lattner4d391482007-12-12 07:09:47 +00001605 // Check the protocol list for unimplemented methods in the @implementation
1606 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001607 // Check and see if class methods in class interface have been
1608 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Chris Lattnercddc8882009-03-01 00:56:52 +00001610 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001611 for (ObjCInterfaceDecl::all_protocol_iterator
1612 PI = I->all_referenced_protocol_begin(),
1613 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001614 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001615 InsMap, ClsMap, I);
1616 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001617 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1618 Categories; Categories = Categories->getNextClassExtension())
1619 ImplMethodsVsClassMethods(S, IMPDecl,
1620 const_cast<ObjCCategoryDecl*>(Categories),
1621 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001622 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001623 // For extended class, unimplemented methods in its protocols will
1624 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001625 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001626 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1627 E = C->protocol_end(); PI != E; ++PI)
1628 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001629 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001630 // Report unimplemented properties in the category as well.
1631 // When reporting on missing setter/getters, do not report when
1632 // setter/getter is implemented in category's primary class
1633 // implementation.
1634 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1635 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1636 for (ObjCImplementationDecl::instmeth_iterator
1637 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1638 InsMap.insert((*I)->getSelector());
1639 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001640 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001641 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001642 } else
1643 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001644}
1645
Mike Stump1eb44332009-09-09 15:08:12 +00001646/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001647Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001648Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001649 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001650 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001651 unsigned NumElts) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001652 SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Chris Lattner4d391482007-12-12 07:09:47 +00001654 for (unsigned i = 0; i != NumElts; ++i) {
1655 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001656 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001657 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001658 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001659 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001660 // Maybe we will complain about the shadowed template parameter.
1661 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1662 // Just pretend that we didn't see the previous declaration.
1663 PrevDecl = 0;
1664 }
1665
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001666 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001667 // GCC apparently allows the following idiom:
1668 //
1669 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1670 // @class XCElementToggler;
1671 //
Mike Stump1eb44332009-09-09 15:08:12 +00001672 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001673 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001674 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001675 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001676 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001677 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001678 // a forward class declaration matching a typedef name of a class refers
1679 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001680 if (const ObjCObjectType *OI =
1681 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1682 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001683 }
Chris Lattner4d391482007-12-12 07:09:47 +00001684 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001685 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1686 if (!IDecl) { // Not already seen? Make a forward decl.
1687 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1688 IdentList[i], IdentLocs[i], true);
1689
1690 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1691 // the current DeclContext. This prevents clients that walk DeclContext
1692 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1693 // declared later (if at all). We also take care to explicitly make
1694 // sure this declaration is visible for name lookup.
1695 PushOnScopeChains(IDecl, TUScope, false);
1696 CurContext->makeDeclVisibleInContext(IDecl, true);
1697 }
Chris Lattner4d391482007-12-12 07:09:47 +00001698
1699 Interfaces.push_back(IDecl);
1700 }
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Ted Kremenek321c22f2009-11-18 00:28:11 +00001702 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001703 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001704 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001705 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001706 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001707 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001708 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001709}
1710
John McCall0f4c4c42011-06-16 01:15:19 +00001711static bool tryMatchRecordTypes(ASTContext &Context,
1712 Sema::MethodMatchStrategy strategy,
1713 const Type *left, const Type *right);
1714
John McCallf85e1932011-06-15 23:02:42 +00001715static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1716 QualType leftQT, QualType rightQT) {
1717 const Type *left =
1718 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1719 const Type *right =
1720 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1721
1722 if (left == right) return true;
1723
1724 // If we're doing a strict match, the types have to match exactly.
1725 if (strategy == Sema::MMS_strict) return false;
1726
1727 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1728
1729 // Otherwise, use this absurdly complicated algorithm to try to
1730 // validate the basic, low-level compatibility of the two types.
1731
1732 // As a minimum, require the sizes and alignments to match.
1733 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1734 return false;
1735
1736 // Consider all the kinds of non-dependent canonical types:
1737 // - functions and arrays aren't possible as return and parameter types
1738
1739 // - vector types of equal size can be arbitrarily mixed
1740 if (isa<VectorType>(left)) return isa<VectorType>(right);
1741 if (isa<VectorType>(right)) return false;
1742
1743 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001744 // - structs, unions, and Objective-C objects must match more-or-less
1745 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001746 // - everything else should be a scalar
1747 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001748 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001749
1750 // Make scalars agree in kind, except count bools as chars.
1751 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1752 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1753 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1754 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1755
1756 // Note that data member pointers and function member pointers don't
1757 // intermix because of the size differences.
1758
1759 return (leftSK == rightSK);
1760}
Chris Lattner4d391482007-12-12 07:09:47 +00001761
John McCall0f4c4c42011-06-16 01:15:19 +00001762static bool tryMatchRecordTypes(ASTContext &Context,
1763 Sema::MethodMatchStrategy strategy,
1764 const Type *lt, const Type *rt) {
1765 assert(lt && rt && lt != rt);
1766
1767 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1768 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1769 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1770
1771 // Require union-hood to match.
1772 if (left->isUnion() != right->isUnion()) return false;
1773
1774 // Require an exact match if either is non-POD.
1775 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1776 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1777 return false;
1778
1779 // Require size and alignment to match.
1780 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1781
1782 // Require fields to match.
1783 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1784 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1785 for (; li != le && ri != re; ++li, ++ri) {
1786 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1787 return false;
1788 }
1789 return (li == le && ri == re);
1790}
1791
Chris Lattner4d391482007-12-12 07:09:47 +00001792/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1793/// returns true, or false, accordingly.
1794/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001795bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1796 const ObjCMethodDecl *right,
1797 MethodMatchStrategy strategy) {
1798 if (!matchTypes(Context, strategy,
1799 left->getResultType(), right->getResultType()))
1800 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001801
John McCallf85e1932011-06-15 23:02:42 +00001802 if (getLangOptions().ObjCAutoRefCount &&
1803 (left->hasAttr<NSReturnsRetainedAttr>()
1804 != right->hasAttr<NSReturnsRetainedAttr>() ||
1805 left->hasAttr<NSConsumesSelfAttr>()
1806 != right->hasAttr<NSConsumesSelfAttr>()))
1807 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001808
John McCallf85e1932011-06-15 23:02:42 +00001809 ObjCMethodDecl::param_iterator
1810 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001811
John McCallf85e1932011-06-15 23:02:42 +00001812 for (; li != le; ++li, ++ri) {
1813 assert(ri != right->param_end() && "Param mismatch");
1814 ParmVarDecl *lparm = *li, *rparm = *ri;
1815
1816 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1817 return false;
1818
1819 if (getLangOptions().ObjCAutoRefCount &&
1820 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1821 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001822 }
1823 return true;
1824}
1825
Sebastian Redldb9d2142010-08-02 23:18:59 +00001826/// \brief Read the contents of the method pool for a given selector from
1827/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001828///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001829/// This routine should only be called once, when the method pool has no entry
1830/// for this selector.
1831Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001832 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001833 assert(MethodPool.find(Sel) == MethodPool.end() &&
1834 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001835
1836 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001837 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Sebastian Redldb9d2142010-08-02 23:18:59 +00001839 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001840}
1841
Sebastian Redldb9d2142010-08-02 23:18:59 +00001842void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1843 bool instance) {
1844 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1845 if (Pos == MethodPool.end()) {
1846 if (ExternalSource)
1847 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001848 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001849 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1850 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001851 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001852 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001853 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001854 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001855 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001856 Entry.Method = Method;
1857 Entry.Next = 0;
1858 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001859 }
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Chris Lattnerb25df352009-03-04 05:16:45 +00001861 // We've seen a method with this name, see if we have already seen this type
1862 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001863 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1864 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1865
1866 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001867 ObjCMethodDecl *PrevObjCMethod = List->Method;
1868 PrevObjCMethod->setDefined(impl);
1869 // If a method is deprecated, push it in the global pool.
1870 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001871 if (Method->isDeprecated()) {
1872 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001873 List->Method = Method;
1874 }
1875 // If new method is unavailable, push it into global pool
1876 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001877 if (Method->isUnavailable()) {
1878 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001879 List->Method = Method;
1880 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001881 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001882 }
John McCallf85e1932011-06-15 23:02:42 +00001883 }
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Chris Lattnerb25df352009-03-04 05:16:45 +00001885 // We have a new signature for an existing method - add it.
1886 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001887 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1888 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001889}
1890
John McCallf85e1932011-06-15 23:02:42 +00001891/// Determines if this is an "acceptable" loose mismatch in the global
1892/// method pool. This exists mostly as a hack to get around certain
1893/// global mismatches which we can't afford to make warnings / errors.
1894/// Really, what we want is a way to take a method out of the global
1895/// method pool.
1896static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1897 ObjCMethodDecl *other) {
1898 if (!chosen->isInstanceMethod())
1899 return false;
1900
1901 Selector sel = chosen->getSelector();
1902 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1903 return false;
1904
1905 // Don't complain about mismatches for -length if the method we
1906 // chose has an integral result type.
1907 return (chosen->getResultType()->isIntegerType());
1908}
1909
Sebastian Redldb9d2142010-08-02 23:18:59 +00001910ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001911 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001912 bool warn, bool instance) {
1913 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1914 if (Pos == MethodPool.end()) {
1915 if (ExternalSource)
1916 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001917 else
1918 return 0;
1919 }
1920
Sebastian Redldb9d2142010-08-02 23:18:59 +00001921 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Sebastian Redldb9d2142010-08-02 23:18:59 +00001923 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001924 bool issueDiagnostic = false, issueError = false;
1925
1926 // We support a warning which complains about *any* difference in
1927 // method signature.
1928 bool strictSelectorMatch =
1929 (receiverIdOrClass && warn &&
1930 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1931 R.getBegin()) !=
1932 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001933 if (strictSelectorMatch)
1934 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001935 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1936 MMS_strict)) {
1937 issueDiagnostic = true;
1938 break;
1939 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001940 }
1941
John McCallf85e1932011-06-15 23:02:42 +00001942 // If we didn't see any strict differences, we won't see any loose
1943 // differences. In ARC, however, we also need to check for loose
1944 // mismatches, because most of them are errors.
1945 if (!strictSelectorMatch ||
1946 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001947 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001948 // This checks if the methods differ in type mismatch.
1949 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1950 MMS_loose) &&
1951 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1952 issueDiagnostic = true;
1953 if (getLangOptions().ObjCAutoRefCount)
1954 issueError = true;
1955 break;
1956 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001957 }
1958
John McCallf85e1932011-06-15 23:02:42 +00001959 if (issueDiagnostic) {
1960 if (issueError)
1961 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1962 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001963 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1964 else
1965 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001966
1967 Diag(MethList.Method->getLocStart(),
1968 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001969 << MethList.Method->getSourceRange();
1970 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1971 Diag(Next->Method->getLocStart(), diag::note_also_found)
1972 << Next->Method->getSourceRange();
1973 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001974 }
1975 return MethList.Method;
1976}
1977
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001978ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001979 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1980 if (Pos == MethodPool.end())
1981 return 0;
1982
1983 GlobalMethods &Methods = Pos->second;
1984
1985 if (Methods.first.Method && Methods.first.Method->isDefined())
1986 return Methods.first.Method;
1987 if (Methods.second.Method && Methods.second.Method->isDefined())
1988 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001989 return 0;
1990}
1991
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001992/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1993/// identical selector names in current and its super classes and issues
1994/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001995void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1996 ObjCMethodDecl *Method,
1997 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001998 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1999 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002001 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002002 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002003 SD->lookupMethod(Method->getSelector(), IsInstance);
2004 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002005 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002006 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002007 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002008 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2009 E = Method->param_end();
2010 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2011 for (; ParamI != E; ++ParamI, ++PrevI) {
2012 // Number of parameters are the same and is guaranteed by selector match.
2013 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2014 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2015 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002016 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002017 // respective argument type in the super class method, issue warning;
2018 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002019 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002020 << T1 << T2;
2021 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2022 return;
2023 }
2024 }
2025 ID = SD;
2026 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002027}
2028
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002029/// DiagnoseDuplicateIvars -
2030/// Check for duplicate ivars in the entire class at the start of
2031/// @implementation. This becomes necesssary because class extension can
2032/// add ivars to a class in random order which will not be known until
2033/// class's @implementation is seen.
2034void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2035 ObjCInterfaceDecl *SID) {
2036 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2037 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2038 ObjCIvarDecl* Ivar = (*IVI);
2039 if (Ivar->isInvalidDecl())
2040 continue;
2041 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2042 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2043 if (prevIvar) {
2044 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2045 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2046 Ivar->setInvalidDecl();
2047 }
2048 }
2049 }
2050}
2051
Steve Naroffa56f6162007-12-18 01:30:32 +00002052// Note: For class/category implemenations, allMethods/allProperties is
2053// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002054void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002055 Decl **allMethods, unsigned allNum,
2056 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002057 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002058
2059 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002060 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002061 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2062 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002063
Mike Stump1eb44332009-09-09 15:08:12 +00002064 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002065 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2066 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002067 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002068
Ted Kremenek782f2f52010-01-07 01:20:12 +00002069 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2070 // FIXME: This is wrong. We shouldn't be pretending that there is
2071 // an '@end' in the declaration.
2072 SourceLocation L = ClassDecl->getLocation();
2073 AtEnd.setBegin(L);
2074 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002075 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002076 }
2077
Steve Naroff0701bbb2009-01-08 17:28:14 +00002078 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2079 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2080 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2081
Chris Lattner4d391482007-12-12 07:09:47 +00002082 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002083 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002084 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002085
2086 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002087 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002088 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002089 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002090 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002091 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002092 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002093 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002094 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002095 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002096 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002097 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002098 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002099 InsMap[Method->getSelector()] = Method;
2100 /// The following allows us to typecheck messages to "id".
2101 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002102 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002103 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002104 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002105 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002106 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002107 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002108 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002109 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002110 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002111 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002112 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002113 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002114 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002115 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002116 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002117 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002118 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002119 /// The following allows us to typecheck messages to "Class".
2120 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002121 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002122 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002123 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002124 }
2125 }
2126 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002127 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002128 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002129 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002130 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002131 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002132 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002133 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002134 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002135 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002136
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002137 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002138 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002139 if (C->IsClassExtension()) {
2140 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2141 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002142 }
Chris Lattner4d391482007-12-12 07:09:47 +00002143 }
Steve Naroff09c47192009-01-09 15:36:25 +00002144 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002145 if (CDecl->getIdentifier())
2146 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2147 // user-defined setter/getter. It also synthesizes setter/getter methods
2148 // and adds them to the DeclContext and global method pools.
2149 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2150 E = CDecl->prop_end();
2151 I != E; ++I)
2152 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002153 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002154 }
2155 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002156 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002157 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002158 // Any property declared in a class extension might have user
2159 // declared setter or getter in current class extension or one
2160 // of the other class extensions. Mark them as synthesized as
2161 // property will be synthesized when property with same name is
2162 // seen in the @implementation.
2163 for (const ObjCCategoryDecl *ClsExtDecl =
2164 IDecl->getFirstClassExtension();
2165 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2166 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2167 E = ClsExtDecl->prop_end(); I != E; ++I) {
2168 ObjCPropertyDecl *Property = (*I);
2169 // Skip over properties declared @dynamic
2170 if (const ObjCPropertyImplDecl *PIDecl
2171 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2172 if (PIDecl->getPropertyImplementation()
2173 == ObjCPropertyImplDecl::Dynamic)
2174 continue;
2175
2176 for (const ObjCCategoryDecl *CExtDecl =
2177 IDecl->getFirstClassExtension();
2178 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2179 if (ObjCMethodDecl *GetterMethod =
2180 CExtDecl->getInstanceMethod(Property->getGetterName()))
2181 GetterMethod->setSynthesized(true);
2182 if (!Property->isReadOnly())
2183 if (ObjCMethodDecl *SetterMethod =
2184 CExtDecl->getInstanceMethod(Property->getSetterName()))
2185 SetterMethod->setSynthesized(true);
2186 }
2187 }
2188 }
2189
Ted Kremenekc32647d2010-12-23 21:35:43 +00002190 if (LangOpts.ObjCDefaultSynthProperties &&
2191 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002192 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002193 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002194 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002195 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002196
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002197 if (LangOpts.ObjCNonFragileABI2)
2198 while (IDecl->getSuperClass()) {
2199 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2200 IDecl = IDecl->getSuperClass();
2201 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002202 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002203 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002204 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002205 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002206 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002207
Chris Lattner4d391482007-12-12 07:09:47 +00002208 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002209 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002210 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002211 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002212 Categories; Categories = Categories->getNextClassCategory()) {
2213 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002214 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002215 break;
2216 }
2217 }
2218 }
2219 }
Chris Lattner682bf922009-03-29 16:50:03 +00002220 if (isInterfaceDeclKind) {
2221 // Reject invalid vardecls.
2222 for (unsigned i = 0; i != tuvNum; i++) {
2223 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2224 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2225 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002226 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002227 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002228 }
Chris Lattner682bf922009-03-29 16:50:03 +00002229 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002230 }
Chris Lattner4d391482007-12-12 07:09:47 +00002231}
2232
2233
2234/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2235/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002236static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002237CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002238 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002239}
2240
Ted Kremenek422bae72010-04-18 04:59:38 +00002241static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002242bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002243 // The 'ibaction' attribute is allowed on method definitions because of
2244 // how the IBAction macro is used on both method declarations and definitions.
2245 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002246 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2247 if ((*i)->getKind() != attr::IBAction)
2248 return true;
2249 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002250}
2251
Douglas Gregor926df6c2011-06-11 01:09:30 +00002252/// \brief Check whether the declared result type of the given Objective-C
2253/// method declaration is compatible with the method's class.
2254///
2255static bool
2256CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2257 ObjCInterfaceDecl *CurrentClass) {
2258 QualType ResultType = Method->getResultType();
2259 SourceRange ResultTypeRange;
2260 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2261 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2262
2263 // If an Objective-C method inherits its related result type, then its
2264 // declared result type must be compatible with its own class type. The
2265 // declared result type is compatible if:
2266 if (const ObjCObjectPointerType *ResultObjectType
2267 = ResultType->getAs<ObjCObjectPointerType>()) {
2268 // - it is id or qualified id, or
2269 if (ResultObjectType->isObjCIdType() ||
2270 ResultObjectType->isObjCQualifiedIdType())
2271 return false;
2272
2273 if (CurrentClass) {
2274 if (ObjCInterfaceDecl *ResultClass
2275 = ResultObjectType->getInterfaceDecl()) {
2276 // - it is the same as the method's class type, or
2277 if (CurrentClass == ResultClass)
2278 return false;
2279
2280 // - it is a superclass of the method's class type
2281 if (ResultClass->isSuperClassOf(CurrentClass))
2282 return false;
2283 }
2284 }
2285 }
2286
2287 return true;
2288}
2289
John McCall6c2c2502011-07-22 02:45:48 +00002290namespace {
2291/// A helper class for searching for methods which a particular method
2292/// overrides.
2293class OverrideSearch {
2294 Sema &S;
2295 ObjCMethodDecl *Method;
2296 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2297 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2298 bool Recursive;
2299
2300public:
2301 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2302 Selector selector = method->getSelector();
2303
2304 // Bypass this search if we've never seen an instance/class method
2305 // with this selector before.
2306 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2307 if (it == S.MethodPool.end()) {
2308 if (!S.ExternalSource) return;
2309 it = S.ReadMethodPool(selector);
2310 }
2311 ObjCMethodList &list =
2312 method->isInstanceMethod() ? it->second.first : it->second.second;
2313 if (!list.Method) return;
2314
2315 ObjCContainerDecl *container
2316 = cast<ObjCContainerDecl>(method->getDeclContext());
2317
2318 // Prevent the search from reaching this container again. This is
2319 // important with categories, which override methods from the
2320 // interface and each other.
2321 Searched.insert(container);
2322 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002323 }
John McCall6c2c2502011-07-22 02:45:48 +00002324
2325 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2326 iterator begin() const { return Overridden.begin(); }
2327 iterator end() const { return Overridden.end(); }
2328
2329private:
2330 void searchFromContainer(ObjCContainerDecl *container) {
2331 if (container->isInvalidDecl()) return;
2332
2333 switch (container->getDeclKind()) {
2334#define OBJCCONTAINER(type, base) \
2335 case Decl::type: \
2336 searchFrom(cast<type##Decl>(container)); \
2337 break;
2338#define ABSTRACT_DECL(expansion)
2339#define DECL(type, base) \
2340 case Decl::type:
2341#include "clang/AST/DeclNodes.inc"
2342 llvm_unreachable("not an ObjC container!");
2343 }
2344 }
2345
2346 void searchFrom(ObjCProtocolDecl *protocol) {
2347 // A method in a protocol declaration overrides declarations from
2348 // referenced ("parent") protocols.
2349 search(protocol->getReferencedProtocols());
2350 }
2351
2352 void searchFrom(ObjCCategoryDecl *category) {
2353 // A method in a category declaration overrides declarations from
2354 // the main class and from protocols the category references.
2355 search(category->getClassInterface());
2356 search(category->getReferencedProtocols());
2357 }
2358
2359 void searchFrom(ObjCCategoryImplDecl *impl) {
2360 // A method in a category definition that has a category
2361 // declaration overrides declarations from the category
2362 // declaration.
2363 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2364 search(category);
2365
2366 // Otherwise it overrides declarations from the class.
2367 } else {
2368 search(impl->getClassInterface());
2369 }
2370 }
2371
2372 void searchFrom(ObjCInterfaceDecl *iface) {
2373 // A method in a class declaration overrides declarations from
2374
2375 // - categories,
2376 for (ObjCCategoryDecl *category = iface->getCategoryList();
2377 category; category = category->getNextClassCategory())
2378 search(category);
2379
2380 // - the super class, and
2381 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2382 search(super);
2383
2384 // - any referenced protocols.
2385 search(iface->getReferencedProtocols());
2386 }
2387
2388 void searchFrom(ObjCImplementationDecl *impl) {
2389 // A method in a class implementation overrides declarations from
2390 // the class interface.
2391 search(impl->getClassInterface());
2392 }
2393
2394
2395 void search(const ObjCProtocolList &protocols) {
2396 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2397 i != e; ++i)
2398 search(*i);
2399 }
2400
2401 void search(ObjCContainerDecl *container) {
2402 // Abort if we've already searched this container.
2403 if (!Searched.insert(container)) return;
2404
2405 // Check for a method in this container which matches this selector.
2406 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2407 Method->isInstanceMethod());
2408
2409 // If we find one, record it and bail out.
2410 if (meth) {
2411 Overridden.insert(meth);
2412 return;
2413 }
2414
2415 // Otherwise, search for methods that a hypothetical method here
2416 // would have overridden.
2417
2418 // Note that we're now in a recursive case.
2419 Recursive = true;
2420
2421 searchFromContainer(container);
2422 }
2423};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002424}
2425
John McCalld226f652010-08-21 09:40:31 +00002426Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002427 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002428 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002429 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002430 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002431 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002432 Selector Sel,
2433 // optional arguments. The number of types/arguments is obtained
2434 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002435 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002436 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002437 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002438 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002439 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002440 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002441 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002442 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002443 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002444 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2445 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002446 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002447
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002448 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002449 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002450 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002451
Steve Naroffccef3712009-02-20 22:59:16 +00002452 // Methods cannot return interface types. All ObjC objects are
2453 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002454 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002455 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2456 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002457 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002458 }
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002459 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002460 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002461 Diag(MethodLoc, diag::warn_missing_method_return_type)
2462 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002463 }
Mike Stump1eb44332009-09-09 15:08:12 +00002464
2465 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002466 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002467 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002468 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002469 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002470 /*isSynthesized=*/false,
2471 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002472 MethodDeclKind == tok::objc_optional
2473 ? ObjCMethodDecl::Optional
2474 : ObjCMethodDecl::Required,
2475 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002476
Chris Lattner5f9e2722011-07-23 10:55:15 +00002477 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002478
Chris Lattner7db638d2009-04-11 19:42:43 +00002479 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002480 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002481 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Chris Lattnere294d3f2009-04-11 18:57:04 +00002483 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002484 ArgType = Context.getObjCIdType();
2485 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002486 } else {
John McCall58e46772009-10-23 21:48:59 +00002487 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002488 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002489 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002490 }
Mike Stump1eb44332009-09-09 15:08:12 +00002491
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002492 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2493 LookupOrdinaryName, ForRedeclaration);
2494 LookupName(R, S);
2495 if (R.isSingleResult()) {
2496 NamedDecl *PrevDecl = R.getFoundDecl();
2497 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002498 Diag(ArgInfo[i].NameLoc,
2499 (MethodDefinition ? diag::warn_method_param_redefinition
2500 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002501 << ArgInfo[i].Name;
2502 Diag(PrevDecl->getLocation(),
2503 diag::note_previous_declaration);
2504 }
2505 }
2506
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002507 SourceLocation StartLoc = DI
2508 ? DI->getTypeLoc().getBeginLoc()
2509 : ArgInfo[i].NameLoc;
2510
John McCall81ef3e62011-04-23 02:46:06 +00002511 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2512 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2513 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002514
John McCall70798862011-05-02 00:30:12 +00002515 Param->setObjCMethodScopeInfo(i);
2516
Chris Lattner0ed844b2008-04-04 06:12:32 +00002517 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002518 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002519
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002520 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002521 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002522
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002523 S->AddDecl(Param);
2524 IdResolver.AddDecl(Param);
2525
Chris Lattner0ed844b2008-04-04 06:12:32 +00002526 Params.push_back(Param);
2527 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002528
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002529 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002530 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002531 QualType ArgType = Param->getType();
2532 if (ArgType.isNull())
2533 ArgType = Context.getObjCIdType();
2534 else
2535 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002536 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002537 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002538 Diag(Param->getLocation(),
2539 diag::err_object_cannot_be_passed_returned_by_value)
2540 << 1 << ArgType;
2541 Param->setInvalidDecl();
2542 }
2543 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002544
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002545 Params.push_back(Param);
2546 }
2547
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002548 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2549 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002550 ObjCMethod->setObjCDeclQualifier(
2551 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002552
2553 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002554 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002555
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002556 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002557 const ObjCMethodDecl *PrevMethod = 0;
2558 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002559 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002560 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2561 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002562 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002563 PrevMethod = ImpDecl->getClassMethod(Sel);
2564 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002565 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002566
Sean Huntcf807c42010-08-18 23:23:40 +00002567 if (ObjCMethod->hasAttrs() &&
2568 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002569 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002570 } else {
2571 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002572 }
John McCall6c2c2502011-07-22 02:45:48 +00002573
Chris Lattner4d391482007-12-12 07:09:47 +00002574 if (PrevMethod) {
2575 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002576 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002577 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002578 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002579 }
John McCall54abf7d2009-11-04 02:18:39 +00002580
Douglas Gregor926df6c2011-06-11 01:09:30 +00002581 // If this Objective-C method does not have a related result type, but we
2582 // are allowed to infer related result types, try to do so based on the
2583 // method family.
2584 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2585 if (!CurrentClass) {
2586 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2587 CurrentClass = Cat->getClassInterface();
2588 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2589 CurrentClass = Impl->getClassInterface();
2590 else if (ObjCCategoryImplDecl *CatImpl
2591 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2592 CurrentClass = CatImpl->getClassInterface();
2593 }
John McCall6c2c2502011-07-22 02:45:48 +00002594
2595 bool isRelatedResultTypeCompatible =
2596 (getLangOptions().ObjCInferRelatedResultType &&
2597 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass));
2598
2599 // Search for overridden methods and merge information down from them.
2600 OverrideSearch overrides(*this, ObjCMethod);
2601 for (OverrideSearch::iterator
2602 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2603 ObjCMethodDecl *overridden = *i;
2604
2605 // Propagate down the 'related result type' bit from overridden methods.
2606 if (isRelatedResultTypeCompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002607 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002608
2609 // Then merge the declarations.
2610 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002611
2612 // Check for overriding methods
2613 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2614 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2615 WarnConflictingTypedMethods(ObjCMethod, overridden,
2616 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2617 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002618 }
2619
John McCallf85e1932011-06-15 23:02:42 +00002620 bool ARCError = false;
2621 if (getLangOptions().ObjCAutoRefCount)
2622 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2623
John McCall6c2c2502011-07-22 02:45:48 +00002624 if (!ARCError && isRelatedResultTypeCompatible &&
2625 !ObjCMethod->hasRelatedResultType()) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002626 bool InferRelatedResultType = false;
2627 switch (ObjCMethod->getMethodFamily()) {
2628 case OMF_None:
2629 case OMF_copy:
2630 case OMF_dealloc:
2631 case OMF_mutableCopy:
2632 case OMF_release:
2633 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002634 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002635 break;
2636
2637 case OMF_alloc:
2638 case OMF_new:
2639 InferRelatedResultType = ObjCMethod->isClassMethod();
2640 break;
2641
2642 case OMF_init:
2643 case OMF_autorelease:
2644 case OMF_retain:
2645 case OMF_self:
2646 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2647 break;
2648 }
2649
John McCall6c2c2502011-07-22 02:45:48 +00002650 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002651 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002652 }
2653
John McCalld226f652010-08-21 09:40:31 +00002654 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002655}
2656
Chris Lattnercc98eac2008-12-17 07:13:27 +00002657bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002658 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002659 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002660 // Following is also an error. But it is caused my a missing @end
2661 // and diagnostic is issued elsewere.
2662 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2663 return false;
2664 }
2665
Anders Carlsson15281452008-11-04 16:57:32 +00002666 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2667 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002668
Anders Carlsson15281452008-11-04 16:57:32 +00002669 return true;
2670}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002671
Chris Lattnercc98eac2008-12-17 07:13:27 +00002672/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2673/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002674void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002675 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002676 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002677 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002678 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002679 if (!Class) {
2680 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2681 return;
2682 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002683 if (LangOpts.ObjCNonFragileABI) {
2684 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2685 return;
2686 }
Mike Stump1eb44332009-09-09 15:08:12 +00002687
Chris Lattnercc98eac2008-12-17 07:13:27 +00002688 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002689 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002690 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002691 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002692 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002693 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002694 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002695 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2696 /*FIXME: StartL=*/ID->getLocation(),
2697 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002698 ID->getIdentifier(), ID->getType(),
2699 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002700 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002701 }
Mike Stump1eb44332009-09-09 15:08:12 +00002702
Chris Lattnercc98eac2008-12-17 07:13:27 +00002703 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002704 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002705 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002706 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002707 if (getLangOptions().CPlusPlus)
2708 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002709 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002710 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002711 }
2712}
2713
Douglas Gregor160b5632010-04-26 17:32:49 +00002714/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002715VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2716 SourceLocation StartLoc,
2717 SourceLocation IdLoc,
2718 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002719 bool Invalid) {
2720 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2721 // duration shall not be qualified by an address-space qualifier."
2722 // Since all parameters have automatic store duration, they can not have
2723 // an address space.
2724 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002725 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002726 Invalid = true;
2727 }
2728
2729 // An @catch parameter must be an unqualified object pointer type;
2730 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2731 if (Invalid) {
2732 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002733 } else if (T->isDependentType()) {
2734 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002735 } else if (!T->isObjCObjectPointerType()) {
2736 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002737 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002738 } else if (T->isObjCQualifiedIdType()) {
2739 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002740 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002741 }
2742
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002743 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2744 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002745 New->setExceptionVariable(true);
2746
Douglas Gregor160b5632010-04-26 17:32:49 +00002747 if (Invalid)
2748 New->setInvalidDecl();
2749 return New;
2750}
2751
John McCalld226f652010-08-21 09:40:31 +00002752Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002753 const DeclSpec &DS = D.getDeclSpec();
2754
2755 // We allow the "register" storage class on exception variables because
2756 // GCC did, but we drop it completely. Any other storage class is an error.
2757 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2758 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2759 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2760 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2761 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2762 << DS.getStorageClassSpec();
2763 }
2764 if (D.getDeclSpec().isThreadSpecified())
2765 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2766 D.getMutableDeclSpec().ClearStorageClassSpecs();
2767
2768 DiagnoseFunctionSpecifiers(D);
2769
2770 // Check that there are no default arguments inside the type of this
2771 // exception object (C++ only).
2772 if (getLangOptions().CPlusPlus)
2773 CheckExtraCXXDefaultArguments(D);
2774
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002775 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002776 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002777
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002778 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2779 D.getSourceRange().getBegin(),
2780 D.getIdentifierLoc(),
2781 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002782 D.isInvalidType());
2783
2784 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2785 if (D.getCXXScopeSpec().isSet()) {
2786 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2787 << D.getCXXScopeSpec().getRange();
2788 New->setInvalidDecl();
2789 }
2790
2791 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002792 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002793 if (D.getIdentifier())
2794 IdResolver.AddDecl(New);
2795
2796 ProcessDeclAttributes(S, New, D);
2797
2798 if (New->hasAttr<BlocksAttr>())
2799 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002800 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002801}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002802
2803/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002804/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002805void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002806 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002807 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2808 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002809 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002810 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002811 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002812 }
2813}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002814
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002815void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002816 // Load referenced selectors from the external source.
2817 if (ExternalSource) {
2818 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2819 ExternalSource->ReadReferencedSelectors(Sels);
2820 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2821 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2822 }
2823
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002824 // Warning will be issued only when selector table is
2825 // generated (which means there is at lease one implementation
2826 // in the TU). This is to match gcc's behavior.
2827 if (ReferencedSelectors.empty() ||
2828 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002829 return;
2830 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2831 ReferencedSelectors.begin(),
2832 E = ReferencedSelectors.end(); S != E; ++S) {
2833 Selector Sel = (*S).first;
2834 if (!LookupImplementedMethodInGlobalPool(Sel))
2835 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2836 }
2837 return;
2838}