blob: 7a9d6d50729b39df9d741da426eb69ab318087b0 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000026#include "llvm/ADT/DenseSet.h"
27
Chris Lattner4d391482007-12-12 07:09:47 +000028using namespace clang;
29
John McCallf85e1932011-06-15 23:02:42 +000030/// Check whether the given method, which must be in the 'init'
31/// family, is a valid member of that family.
32///
33/// \param receiverTypeIfCall - if null, check this as if declaring it;
34/// if non-null, check this as if making a call to it with the given
35/// receiver type
36///
37/// \return true to indicate that there was an error and appropriate
38/// actions were taken
39bool Sema::checkInitMethod(ObjCMethodDecl *method,
40 QualType receiverTypeIfCall) {
41 if (method->isInvalidDecl()) return true;
42
43 // This castAs is safe: methods that don't return an object
44 // pointer won't be inferred as inits and will reject an explicit
45 // objc_method_family(init).
46
47 // We ignore protocols here. Should we? What about Class?
48
49 const ObjCObjectType *result = method->getResultType()
50 ->castAs<ObjCObjectPointerType>()->getObjectType();
51
52 if (result->isObjCId()) {
53 return false;
54 } else if (result->isObjCClass()) {
55 // fall through: always an error
56 } else {
57 ObjCInterfaceDecl *resultClass = result->getInterface();
58 assert(resultClass && "unexpected object type!");
59
60 // It's okay for the result type to still be a forward declaration
61 // if we're checking an interface declaration.
62 if (resultClass->isForwardDecl()) {
63 if (receiverTypeIfCall.isNull() &&
64 !isa<ObjCImplementationDecl>(method->getDeclContext()))
65 return false;
66
67 // Otherwise, we try to compare class types.
68 } else {
69 // If this method was declared in a protocol, we can't check
70 // anything unless we have a receiver type that's an interface.
71 const ObjCInterfaceDecl *receiverClass = 0;
72 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
73 if (receiverTypeIfCall.isNull())
74 return false;
75
76 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
77 ->getInterfaceDecl();
78
79 // This can be null for calls to e.g. id<Foo>.
80 if (!receiverClass) return false;
81 } else {
82 receiverClass = method->getClassInterface();
83 assert(receiverClass && "method not associated with a class!");
84 }
85
86 // If either class is a subclass of the other, it's fine.
87 if (receiverClass->isSuperClassOf(resultClass) ||
88 resultClass->isSuperClassOf(receiverClass))
89 return false;
90 }
91 }
92
93 SourceLocation loc = method->getLocation();
94
95 // If we're in a system header, and this is not a call, just make
96 // the method unusable.
97 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
98 method->addAttr(new (Context) UnavailableAttr(loc, Context,
99 "init method returns a type unrelated to its receiver type"));
100 return true;
101 }
102
103 // Otherwise, it's an error.
104 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
105 method->setInvalidDecl();
106 return true;
107}
108
Douglas Gregor926df6c2011-06-11 01:09:30 +0000109bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
110 const ObjCMethodDecl *Overridden,
111 bool IsImplementation) {
112 if (Overridden->hasRelatedResultType() &&
113 !NewMethod->hasRelatedResultType()) {
114 // This can only happen when the method follows a naming convention that
115 // implies a related result type, and the original (overridden) method has
116 // a suitable return type, but the new (overriding) method does not have
117 // a suitable return type.
118 QualType ResultType = NewMethod->getResultType();
119 SourceRange ResultTypeRange;
120 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000121 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000122 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
123
124 // Figure out which class this method is part of, if any.
125 ObjCInterfaceDecl *CurrentClass
126 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
127 if (!CurrentClass) {
128 DeclContext *DC = NewMethod->getDeclContext();
129 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
130 CurrentClass = Cat->getClassInterface();
131 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
132 CurrentClass = Impl->getClassInterface();
133 else if (ObjCCategoryImplDecl *CatImpl
134 = dyn_cast<ObjCCategoryImplDecl>(DC))
135 CurrentClass = CatImpl->getClassInterface();
136 }
137
138 if (CurrentClass) {
139 Diag(NewMethod->getLocation(),
140 diag::warn_related_result_type_compatibility_class)
141 << Context.getObjCInterfaceType(CurrentClass)
142 << ResultType
143 << ResultTypeRange;
144 } else {
145 Diag(NewMethod->getLocation(),
146 diag::warn_related_result_type_compatibility_protocol)
147 << ResultType
148 << ResultTypeRange;
149 }
150
151 Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
152 << Overridden->getMethodFamily();
153 }
154
155 return false;
156}
157
John McCallf85e1932011-06-15 23:02:42 +0000158/// \brief Check a method declaration for compatibility with the Objective-C
159/// ARC conventions.
160static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
161 ObjCMethodFamily family = method->getMethodFamily();
162 switch (family) {
163 case OMF_None:
164 case OMF_dealloc:
165 case OMF_retain:
166 case OMF_release:
167 case OMF_autorelease:
168 case OMF_retainCount:
169 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000170 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000171 return false;
172
173 case OMF_init:
174 // If the method doesn't obey the init rules, don't bother annotating it.
175 if (S.checkInitMethod(method, QualType()))
176 return true;
177
178 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
179 S.Context));
180
181 // Don't add a second copy of this attribute, but otherwise don't
182 // let it be suppressed.
183 if (method->hasAttr<NSReturnsRetainedAttr>())
184 return false;
185 break;
186
187 case OMF_alloc:
188 case OMF_copy:
189 case OMF_mutableCopy:
190 case OMF_new:
191 if (method->hasAttr<NSReturnsRetainedAttr>() ||
192 method->hasAttr<NSReturnsNotRetainedAttr>() ||
193 method->hasAttr<NSReturnsAutoreleasedAttr>())
194 return false;
195 break;
196 }
197
198 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
199 S.Context));
200 return false;
201}
202
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000203static void DiagnoseObjCImplementedDeprecations(Sema &S,
204 NamedDecl *ND,
205 SourceLocation ImplLoc,
206 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000207 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000208 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000209 if (select == 0)
210 S.Diag(ND->getLocation(), diag::note_method_declared_at);
211 else
212 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
213 }
214}
215
Steve Naroffebf64432009-02-28 16:59:13 +0000216/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000217/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000218void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000219 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000220 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Steve Naroff394f3f42008-07-25 17:57:26 +0000222 // If we don't have a valid method decl, simply return.
223 if (!MDecl)
224 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000225
226 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000227 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000228 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000229 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000230 AddFactoryMethodToGlobalPool(MDecl, true);
231
Chris Lattner4d391482007-12-12 07:09:47 +0000232 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000233 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000234 PushFunctionScope();
235
Chris Lattner4d391482007-12-12 07:09:47 +0000236 // Create Decl objects for each parameter, entrring them in the scope for
237 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000238
239 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000240 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Daniel Dunbar451318c2008-08-26 06:07:48 +0000242 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
243 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000244
Chris Lattner8123a952008-04-10 02:22:51 +0000245 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000246 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000247 E = MDecl->param_end(); PI != E; ++PI) {
248 ParmVarDecl *Param = (*PI);
249 if (!Param->isInvalidDecl() &&
250 RequireCompleteType(Param->getLocation(), Param->getType(),
251 diag::err_typecheck_decl_incomplete_type))
252 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000253 if ((*PI)->getIdentifier())
254 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000255 }
John McCallf85e1932011-06-15 23:02:42 +0000256
257 // In ARC, disallow definition of retain/release/autorelease/retainCount
258 if (getLangOptions().ObjCAutoRefCount) {
259 switch (MDecl->getMethodFamily()) {
260 case OMF_retain:
261 case OMF_retainCount:
262 case OMF_release:
263 case OMF_autorelease:
264 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
265 << MDecl->getSelector();
266 break;
267
268 case OMF_None:
269 case OMF_dealloc:
270 case OMF_alloc:
271 case OMF_init:
272 case OMF_mutableCopy:
273 case OMF_copy:
274 case OMF_new:
275 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000276 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000277 break;
278 }
279 }
280
Nico Weber9a1ecf02011-08-22 17:25:57 +0000281 // Warn on deprecated methods under -Wdeprecated-implementations,
282 // and prepare for warning on missing super calls.
283 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000284 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000285 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000286 DiagnoseObjCImplementedDeprecations(*this,
287 dyn_cast<NamedDecl>(IMD),
288 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000289
290 // If this is "dealloc", set some bit here.
291 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
292 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
293 // Only do this if the current class actually has a superclass.
294 if (IC->getSuperClass())
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000295 ObjCShouldCallSuperDealloc =
296 !Context.getLangOptions().ObjCAutoRefCount &&
297 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber9a1ecf02011-08-22 17:25:57 +0000298 }
Chris Lattner4d391482007-12-12 07:09:47 +0000299}
300
John McCalld226f652010-08-21 09:40:31 +0000301Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000302ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
303 IdentifierInfo *ClassName, SourceLocation ClassLoc,
304 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000305 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000306 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000307 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000308 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner4d391482007-12-12 07:09:47 +0000310 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000311 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000312 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000313
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000314 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000315 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000316 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000319 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
320 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000321 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000322 if (!IDecl->isForwardDecl()) {
323 IDecl->setInvalidDecl();
324 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
325 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000326
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000327 // Return the previous class interface.
328 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000329 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000330 } else {
331 IDecl->setLocation(AtInterfaceLoc);
332 IDecl->setForwardDecl(false);
333 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000334 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000335 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000336 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000337
338 // Since this ObjCInterfaceDecl was created by a forward declaration,
339 // we now add it to the DeclContext since it wasn't added before
340 // (see ActOnForwardClassDeclaration).
341 IDecl->setLexicalDeclContext(CurContext);
342 CurContext->addDecl(IDecl);
343
344 if (AttrList)
345 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000346 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000347 } else {
348 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
349 ClassName, ClassLoc);
350 if (AttrList)
351 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
352
353 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattner4d391482007-12-12 07:09:47 +0000356 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000357 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000358 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
359 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000360
361 if (!PrevDecl) {
362 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000363 TypoCorrection Corrected = CorrectTypo(
364 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
365 NULL, NULL, false, CTC_NoKeywords);
366 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000367 Diag(SuperLoc, diag::err_undef_superclass_suggest)
368 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000369 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
370 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000371 }
372 }
373
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000374 if (PrevDecl == IDecl) {
375 Diag(SuperLoc, diag::err_recursive_superclass)
376 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
377 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000378 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000379 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000380 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000381
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000382 // Diagnose classes that inherit from deprecated classes.
383 if (SuperClassDecl)
384 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000386 if (PrevDecl && SuperClassDecl == 0) {
387 // The previous declaration was not a class decl. Check if we have a
388 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000389 if (const TypedefNameDecl *TDecl =
390 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000391 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000392 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000393 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
394 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000395 }
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000398 // This handles the following case:
399 //
400 // typedef int SuperClass;
401 // @interface MyClass : SuperClass {} @end
402 //
403 if (!SuperClassDecl) {
404 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
405 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000406 }
407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Richard Smith162e1c12011-04-15 14:24:37 +0000409 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000410 if (!SuperClassDecl)
411 Diag(SuperLoc, diag::err_undef_superclass)
412 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000413 else if (SuperClassDecl->isForwardDecl()) {
414 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000415 << SuperClassDecl->getDeclName() << ClassName
416 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000417 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
418 SuperClassDecl = 0;
419 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000420 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000421 IDecl->setSuperClass(SuperClassDecl);
422 IDecl->setSuperClassLoc(SuperLoc);
423 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000424 }
Chris Lattner4d391482007-12-12 07:09:47 +0000425 } else { // we have a root class.
426 IDecl->setLocEnd(ClassLoc);
427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Sebastian Redl0b17c612010-08-13 00:28:03 +0000429 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000430 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000431 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000432 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000433 IDecl->setLocEnd(EndProtoLoc);
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson15281452008-11-04 16:57:32 +0000436 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000437 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000438}
439
440/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000441/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000442Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
443 IdentifierInfo *AliasName,
444 SourceLocation AliasLocation,
445 IdentifierInfo *ClassName,
446 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000447 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000448 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000449 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000450 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000451 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000452 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000453 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000454 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000455 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000456 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000457 }
458 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000459 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000460 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000461 if (const TypedefNameDecl *TDecl =
462 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000463 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000464 if (T->isObjCObjectType()) {
465 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000466 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000467 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000468 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000469 }
470 }
471 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000472 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
473 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000474 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000475 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000476 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000477 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000480 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000481 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000482 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Anders Carlsson15281452008-11-04 16:57:32 +0000484 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000485 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000486
John McCalld226f652010-08-21 09:40:31 +0000487 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000488}
489
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000490bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000491 IdentifierInfo *PName,
492 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000493 const ObjCList<ObjCProtocolDecl> &PList) {
494
495 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000496 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
497 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000498 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
499 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000500 if (PDecl->getIdentifier() == PName) {
501 Diag(Ploc, diag::err_protocol_has_circular_dependency);
502 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000503 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000504 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000505 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
506 PDecl->getLocation(), PDecl->getReferencedProtocols()))
507 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000508 }
509 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000510 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000511}
512
John McCalld226f652010-08-21 09:40:31 +0000513Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000514Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
515 IdentifierInfo *ProtocolName,
516 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000517 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000518 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000519 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000520 SourceLocation EndProtoLoc,
521 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000522 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000523 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000524 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000525 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000526 if (PDecl) {
527 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000528 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000529 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000530 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000531 // Just return the protocol we already had.
532 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000533 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000534 }
Steve Naroff61d68522009-03-05 15:22:01 +0000535 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000536 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000537 err = CheckForwardProtocolDeclarationForCircularDependency(
538 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Steve Narofff11b5082008-08-13 16:39:22 +0000540 // Make sure the cached decl gets a valid start location.
541 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000542 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000543 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000544 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000545 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000546 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000547 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000548 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000549 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000550 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000551 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000552 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000553 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000554 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000555 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000556 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
557 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000558 PDecl->setLocEnd(EndProtoLoc);
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
561 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000562 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000563}
564
565/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000566/// issues an error if they are not declared. It returns list of
567/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000568void
Chris Lattnere13b9592008-07-26 04:03:38 +0000569Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000570 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000571 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000572 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000573 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000574 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
575 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000576 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000577 TypoCorrection Corrected = CorrectTypo(
578 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
579 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
580 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000581 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000582 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000583 Diag(PDecl->getLocation(), diag::note_previous_decl)
584 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000585 }
586 }
587
588 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000589 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000590 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000591 continue;
592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000594 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000595
596 // If this is a forward declaration and we are supposed to warn in this
597 // case, do it.
598 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000599 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000600 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000601 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000602 }
603}
604
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000605/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000606/// a class method in its extension.
607///
Mike Stump1eb44332009-09-09 15:08:12 +0000608void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000609 ObjCInterfaceDecl *ID) {
610 if (!ID)
611 return; // Possibly due to previous error
612
613 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000614 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
615 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000616 ObjCMethodDecl *MD = *i;
617 MethodMap[MD->getSelector()] = MD;
618 }
619
620 if (MethodMap.empty())
621 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000622 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
623 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000624 ObjCMethodDecl *Method = *i;
625 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
626 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
627 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
628 << Method->getDeclName();
629 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
630 }
631 }
632}
633
Chris Lattner58fe03b2009-04-12 08:43:13 +0000634/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000635Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000636Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000637 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000638 unsigned NumElts,
639 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000640 SmallVector<ObjCProtocolDecl*, 32> Protocols;
641 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Chris Lattner4d391482007-12-12 07:09:47 +0000643 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000644 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000645 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000646 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000647 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000648 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000649 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000650 PushOnScopeChains(PDecl, TUScope, false);
651 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000652 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000653 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000654 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000655 if (!isNew)
656 PDecl->setChangedSinceDeserialization(true);
657 }
Chris Lattner4d391482007-12-12 07:09:47 +0000658 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000659 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
662 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000663 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000664 Protocols.data(), Protocols.size(),
665 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000666 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000667 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000668 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000669}
670
John McCalld226f652010-08-21 09:40:31 +0000671Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000672ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
673 IdentifierInfo *ClassName, SourceLocation ClassLoc,
674 IdentifierInfo *CategoryName,
675 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000676 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000677 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000678 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000679 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000680 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000681 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000682
683 /// Check that class of this category is already completely declared.
684 if (!IDecl || IDecl->isForwardDecl()) {
685 // Create an invalid ObjCCategoryDecl to serve as context for
686 // the enclosing method declarations. We mark the decl invalid
687 // to make it clear that this isn't a valid AST.
688 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
689 ClassLoc, CategoryLoc, CategoryName);
690 CDecl->setInvalidDecl();
691 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000692 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000693 }
694
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000695 if (!CategoryName && IDecl->getImplementation()) {
696 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
697 Diag(IDecl->getImplementation()->getLocation(),
698 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000699 }
700
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000701 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
702 ClassLoc, CategoryLoc, CategoryName);
703 // FIXME: PushOnScopeChains?
704 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000705
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000706 CDecl->setClassInterface(IDecl);
707 // Insert class extension to the list of class's categories.
708 if (!CategoryName)
709 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattner16b34b42009-02-16 21:30:01 +0000711 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000712 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000713
Fariborz Jahanian25760612010-02-15 21:55:26 +0000714 if (CategoryName) {
715 /// Check for duplicate interface declaration for this category
716 ObjCCategoryDecl *CDeclChain;
717 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
718 CDeclChain = CDeclChain->getNextClassCategory()) {
719 if (CDeclChain->getIdentifier() == CategoryName) {
720 // Class extensions can be declared multiple times.
721 Diag(CategoryLoc, diag::warn_dup_category_def)
722 << ClassName << CategoryName;
723 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
724 break;
725 }
Chris Lattner70f19542009-02-16 21:26:43 +0000726 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000727 if (!CDeclChain)
728 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000729 }
Chris Lattner70f19542009-02-16 21:26:43 +0000730
Chris Lattner4d391482007-12-12 07:09:47 +0000731 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000732 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000733 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000734 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000735 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000736 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000737 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Anders Carlsson15281452008-11-04 16:57:32 +0000740 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000741 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000742}
743
744/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000745/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000746/// object.
John McCalld226f652010-08-21 09:40:31 +0000747Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000748 SourceLocation AtCatImplLoc,
749 IdentifierInfo *ClassName, SourceLocation ClassLoc,
750 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000751 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000752 ObjCCategoryDecl *CatIDecl = 0;
753 if (IDecl) {
754 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
755 if (!CatIDecl) {
756 // Category @implementation with no corresponding @interface.
757 // Create and install one.
758 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000759 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000760 CatName);
761 CatIDecl->setClassInterface(IDecl);
762 CatIDecl->insertNextClassCategory();
763 }
764 }
765
Mike Stump1eb44332009-09-09 15:08:12 +0000766 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000767 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
768 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000769 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000770 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000771 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000772 CDecl->setInvalidDecl();
773 }
Chris Lattner4d391482007-12-12 07:09:47 +0000774
Douglas Gregord0434102009-01-09 00:49:46 +0000775 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000776 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000777
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000778 /// Check that CatName, category name, is not used in another implementation.
779 if (CatIDecl) {
780 if (CatIDecl->getImplementation()) {
781 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
782 << CatName;
783 Diag(CatIDecl->getImplementation()->getLocation(),
784 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000785 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000786 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000787 // Warn on implementating category of deprecated class under
788 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000789 DiagnoseObjCImplementedDeprecations(*this,
790 dyn_cast<NamedDecl>(IDecl),
791 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000792 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Anders Carlsson15281452008-11-04 16:57:32 +0000795 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000796 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000797}
798
John McCalld226f652010-08-21 09:40:31 +0000799Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000800 SourceLocation AtClassImplLoc,
801 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000802 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000803 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000804 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000805 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000806 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000807 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
808 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000809 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000810 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000811 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000812 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
813 // If this is a forward declaration of an interface, warn.
814 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000815 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000816 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000817 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000818 } else {
819 // We did not find anything with the name ClassName; try to correct for
820 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000821 TypoCorrection Corrected = CorrectTypo(
822 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
823 NULL, NULL, false, CTC_NoKeywords);
824 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000825 // Suggest the (potentially) correct interface name. However, put the
826 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000827 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000828 // provide a code-modification hint or use the typo name for recovery,
829 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000830 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000831 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000832 << ClassName << CorrectedName;
833 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
834 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000835 IDecl = 0;
836 } else {
837 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
838 }
Chris Lattner4d391482007-12-12 07:09:47 +0000839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Chris Lattner4d391482007-12-12 07:09:47 +0000841 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000842 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000843 if (SuperClassname) {
844 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000845 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
846 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000847 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000848 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
849 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000850 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000851 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000852 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000853 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000854 Diag(SuperClassLoc, diag::err_undef_superclass)
855 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000856 else if (IDecl && IDecl->getSuperClass() != SDecl) {
857 // This implementation and its interface do not have the same
858 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000860 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000861 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000862 }
863 }
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Chris Lattner4d391482007-12-12 07:09:47 +0000866 if (!IDecl) {
867 // Legacy case of @implementation with no corresponding @interface.
868 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000869
Mike Stump390b4cc2009-05-16 07:39:55 +0000870 // FIXME: Do we support attributes on the @implementation? If so we should
871 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000872 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000873 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000874 IDecl->setSuperClass(SDecl);
875 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000876
877 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000878 } else {
879 // Mark the interface as being completed, even if it was just as
880 // @class ....;
881 // declaration; the user cannot reopen it.
882 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
885 ObjCImplementationDecl* IMPDecl =
886 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000887 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Anders Carlsson15281452008-11-04 16:57:32 +0000889 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000890 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner4d391482007-12-12 07:09:47 +0000892 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000893 if (IDecl->getImplementation()) {
894 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000896 Diag(IDecl->getImplementation()->getLocation(),
897 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000898 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000899 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000900 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000901 // Warn on implementating deprecated class under
902 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000903 DiagnoseObjCImplementedDeprecations(*this,
904 dyn_cast<NamedDecl>(IDecl),
905 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000906 }
John McCalld226f652010-08-21 09:40:31 +0000907 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000908}
909
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000910void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
911 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000912 SourceLocation RBrace) {
913 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000914 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000915 if (!IDecl)
916 return;
917 /// Check case of non-existing @interface decl.
918 /// (legacy objective-c @implementation decl without an @interface decl).
919 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000920 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000921 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000922 // Add ivar's to class's DeclContext.
923 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000924 ivars[i]->setLexicalDeclContext(ImpDecl);
925 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000926 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000927 }
928
Chris Lattner4d391482007-12-12 07:09:47 +0000929 return;
930 }
931 // If implementation has empty ivar list, just return.
932 if (numIvars == 0)
933 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Chris Lattner4d391482007-12-12 07:09:47 +0000935 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000936 if (LangOpts.ObjCNonFragileABI2) {
937 if (ImpDecl->getSuperClass())
938 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
939 for (unsigned i = 0; i < numIvars; i++) {
940 ObjCIvarDecl* ImplIvar = ivars[i];
941 if (const ObjCIvarDecl *ClsIvar =
942 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
943 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
944 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
945 continue;
946 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000947 // Instance ivar to Implementation's DeclContext.
948 ImplIvar->setLexicalDeclContext(ImpDecl);
949 IDecl->makeDeclVisibleInContext(ImplIvar, false);
950 ImpDecl->addDecl(ImplIvar);
951 }
952 return;
953 }
Chris Lattner4d391482007-12-12 07:09:47 +0000954 // Check interface's Ivar list against those in the implementation.
955 // names and types must match.
956 //
Chris Lattner4d391482007-12-12 07:09:47 +0000957 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000958 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000959 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
960 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000961 ObjCIvarDecl* ImplIvar = ivars[j++];
962 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000963 assert (ImplIvar && "missing implementation ivar");
964 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Steve Naroffca331292009-03-03 14:49:36 +0000966 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000967 if (Context.getCanonicalType(ImplIvar->getType()) !=
968 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000969 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000970 << ImplIvar->getIdentifier()
971 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000972 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000973 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
974 Expr *ImplBitWidth = ImplIvar->getBitWidth();
975 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000976 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
977 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000978 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
979 << ImplIvar->getIdentifier();
980 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982 }
Steve Naroffca331292009-03-03 14:49:36 +0000983 // Make sure the names are identical.
984 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000985 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000986 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000987 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000988 }
989 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattner609e4c72007-12-12 18:11:49 +0000992 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000993 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000994 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000995 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000996}
997
Steve Naroff3c2eb662008-02-10 21:38:56 +0000998void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000999 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001000 // No point warning no definition of method which is 'unavailable'.
1001 if (method->hasAttr<UnavailableAttr>())
1002 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001003 if (!IncompleteImpl) {
1004 Diag(ImpLoc, diag::warn_incomplete_impl);
1005 IncompleteImpl = true;
1006 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001007 if (DiagID == diag::warn_unimplemented_protocol_method)
1008 Diag(ImpLoc, DiagID) << method->getDeclName();
1009 else
1010 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001011}
1012
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001013/// Determines if type B can be substituted for type A. Returns true if we can
1014/// guarantee that anything that the user will do to an object of type A can
1015/// also be done to an object of type B. This is trivially true if the two
1016/// types are the same, or if B is a subclass of A. It becomes more complex
1017/// in cases where protocols are involved.
1018///
1019/// Object types in Objective-C describe the minimum requirements for an
1020/// object, rather than providing a complete description of a type. For
1021/// example, if A is a subclass of B, then B* may refer to an instance of A.
1022/// The principle of substitutability means that we may use an instance of A
1023/// anywhere that we may use an instance of B - it will implement all of the
1024/// ivars of B and all of the methods of B.
1025///
1026/// This substitutability is important when type checking methods, because
1027/// the implementation may have stricter type definitions than the interface.
1028/// The interface specifies minimum requirements, but the implementation may
1029/// have more accurate ones. For example, a method may privately accept
1030/// instances of B, but only publish that it accepts instances of A. Any
1031/// object passed to it will be type checked against B, and so will implicitly
1032/// by a valid A*. Similarly, a method may return a subclass of the class that
1033/// it is declared as returning.
1034///
1035/// This is most important when considering subclassing. A method in a
1036/// subclass must accept any object as an argument that its superclass's
1037/// implementation accepts. It may, however, accept a more general type
1038/// without breaking substitutability (i.e. you can still use the subclass
1039/// anywhere that you can use the superclass, but not vice versa). The
1040/// converse requirement applies to return types: the return type for a
1041/// subclass method must be a valid object of the kind that the superclass
1042/// advertises, but it may be specified more accurately. This avoids the need
1043/// for explicit down-casting by callers.
1044///
1045/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001046static bool isObjCTypeSubstitutable(ASTContext &Context,
1047 const ObjCObjectPointerType *A,
1048 const ObjCObjectPointerType *B,
1049 bool rejectId) {
1050 // Reject a protocol-unqualified id.
1051 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001052
1053 // If B is a qualified id, then A must also be a qualified id and it must
1054 // implement all of the protocols in B. It may not be a qualified class.
1055 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1056 // stricter definition so it is not substitutable for id<A>.
1057 if (B->isObjCQualifiedIdType()) {
1058 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001059 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1060 QualType(B,0),
1061 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001062 }
1063
1064 /*
1065 // id is a special type that bypasses type checking completely. We want a
1066 // warning when it is used in one place but not another.
1067 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1068
1069
1070 // If B is a qualified id, then A must also be a qualified id (which it isn't
1071 // if we've got this far)
1072 if (B->isObjCQualifiedIdType()) return false;
1073 */
1074
1075 // Now we know that A and B are (potentially-qualified) class types. The
1076 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001077 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001078}
1079
John McCall10302c02010-10-28 02:34:38 +00001080static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1081 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1082}
1083
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001084static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001085 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001086 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001087 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001088 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001089 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001090 if (IsProtocolMethodDecl &&
1091 (MethodDecl->getObjCDeclQualifier() !=
1092 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001093 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001094 S.Diag(MethodImpl->getLocation(),
1095 (IsOverridingMode ?
1096 diag::warn_conflicting_overriding_ret_type_modifiers
1097 : diag::warn_conflicting_ret_type_modifiers))
1098 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001099 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1100 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1101 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1102 }
1103 else
1104 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001105 }
1106
John McCall10302c02010-10-28 02:34:38 +00001107 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001108 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001109 return true;
1110 if (!Warn)
1111 return false;
John McCall10302c02010-10-28 02:34:38 +00001112
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001113 unsigned DiagID =
1114 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1115 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001116
1117 // Mismatches between ObjC pointers go into a different warning
1118 // category, and sometimes they're even completely whitelisted.
1119 if (const ObjCObjectPointerType *ImplPtrTy =
1120 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1121 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001122 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001123 // Allow non-matching return types as long as they don't violate
1124 // the principle of substitutability. Specifically, we permit
1125 // return types that are subclasses of the declared return type,
1126 // or that are more-qualified versions of the declared type.
1127 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001128 return false;
John McCall10302c02010-10-28 02:34:38 +00001129
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001130 DiagID =
1131 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1132 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001133 }
1134 }
1135
1136 S.Diag(MethodImpl->getLocation(), DiagID)
1137 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001138 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001139 << MethodImpl->getResultType()
1140 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001141 S.Diag(MethodDecl->getLocation(),
1142 IsOverridingMode ? diag::note_previous_declaration
1143 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001144 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001145 return false;
John McCall10302c02010-10-28 02:34:38 +00001146}
1147
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001148static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001149 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001150 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001151 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001152 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001153 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001154 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001155 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001156 if (IsProtocolMethodDecl &&
1157 (ImplVar->getObjCDeclQualifier() !=
1158 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001159 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001160 if (IsOverridingMode)
1161 S.Diag(ImplVar->getLocation(),
1162 diag::warn_conflicting_overriding_param_modifiers)
1163 << getTypeRange(ImplVar->getTypeSourceInfo())
1164 << MethodImpl->getDeclName();
1165 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001166 diag::warn_conflicting_param_modifiers)
1167 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001168 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001169 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1170 << getTypeRange(IfaceVar->getTypeSourceInfo());
1171 }
1172 else
1173 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001174 }
1175
John McCall10302c02010-10-28 02:34:38 +00001176 QualType ImplTy = ImplVar->getType();
1177 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001178
John McCall10302c02010-10-28 02:34:38 +00001179 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001180 return true;
1181
1182 if (!Warn)
1183 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001184 unsigned DiagID =
1185 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1186 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001187
1188 // Mismatches between ObjC pointers go into a different warning
1189 // category, and sometimes they're even completely whitelisted.
1190 if (const ObjCObjectPointerType *ImplPtrTy =
1191 ImplTy->getAs<ObjCObjectPointerType>()) {
1192 if (const ObjCObjectPointerType *IfacePtrTy =
1193 IfaceTy->getAs<ObjCObjectPointerType>()) {
1194 // Allow non-matching argument types as long as they don't
1195 // violate the principle of substitutability. Specifically, the
1196 // implementation must accept any objects that the superclass
1197 // accepts, however it may also accept others.
1198 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001199 return false;
John McCall10302c02010-10-28 02:34:38 +00001200
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001201 DiagID =
1202 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1203 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001204 }
1205 }
1206
1207 S.Diag(ImplVar->getLocation(), DiagID)
1208 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001209 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1210 S.Diag(IfaceVar->getLocation(),
1211 (IsOverridingMode ? diag::note_previous_declaration
1212 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001213 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001214 return false;
John McCall10302c02010-10-28 02:34:38 +00001215}
John McCallf85e1932011-06-15 23:02:42 +00001216
1217/// In ARC, check whether the conventional meanings of the two methods
1218/// match. If they don't, it's a hard error.
1219static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1220 ObjCMethodDecl *decl) {
1221 ObjCMethodFamily implFamily = impl->getMethodFamily();
1222 ObjCMethodFamily declFamily = decl->getMethodFamily();
1223 if (implFamily == declFamily) return false;
1224
1225 // Since conventions are sorted by selector, the only possibility is
1226 // that the types differ enough to cause one selector or the other
1227 // to fall out of the family.
1228 assert(implFamily == OMF_None || declFamily == OMF_None);
1229
1230 // No further diagnostics required on invalid declarations.
1231 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1232
1233 const ObjCMethodDecl *unmatched = impl;
1234 ObjCMethodFamily family = declFamily;
1235 unsigned errorID = diag::err_arc_lost_method_convention;
1236 unsigned noteID = diag::note_arc_lost_method_convention;
1237 if (declFamily == OMF_None) {
1238 unmatched = decl;
1239 family = implFamily;
1240 errorID = diag::err_arc_gained_method_convention;
1241 noteID = diag::note_arc_gained_method_convention;
1242 }
1243
1244 // Indexes into a %select clause in the diagnostic.
1245 enum FamilySelector {
1246 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1247 };
1248 FamilySelector familySelector = FamilySelector();
1249
1250 switch (family) {
1251 case OMF_None: llvm_unreachable("logic error, no method convention");
1252 case OMF_retain:
1253 case OMF_release:
1254 case OMF_autorelease:
1255 case OMF_dealloc:
1256 case OMF_retainCount:
1257 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001258 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001259 // Mismatches for these methods don't change ownership
1260 // conventions, so we don't care.
1261 return false;
1262
1263 case OMF_init: familySelector = F_init; break;
1264 case OMF_alloc: familySelector = F_alloc; break;
1265 case OMF_copy: familySelector = F_copy; break;
1266 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1267 case OMF_new: familySelector = F_new; break;
1268 }
1269
1270 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1271 ReasonSelector reasonSelector;
1272
1273 // The only reason these methods don't fall within their families is
1274 // due to unusual result types.
1275 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1276 reasonSelector = R_UnrelatedReturn;
1277 } else {
1278 reasonSelector = R_NonObjectReturn;
1279 }
1280
1281 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1282 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1283
1284 return true;
1285}
John McCall10302c02010-10-28 02:34:38 +00001286
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001287void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001288 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001289 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001290 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001291 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001292 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001293 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1294 return;
1295
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001296 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001297 IsProtocolMethodDecl, IsOverridingMode,
1298 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Chris Lattner3aff9192009-04-11 19:58:42 +00001300 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001301 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001302 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001303 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1304 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001305 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001306
Fariborz Jahanian21121902011-08-08 18:03:17 +00001307 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001308 if (IsOverridingMode)
1309 Diag(ImpMethodDecl->getLocation(),
1310 diag::warn_conflicting_overriding_variadic);
1311 else
1312 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001313 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001314 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001315}
1316
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001317/// WarnExactTypedMethods - This routine issues a warning if method
1318/// implementation declaration matches exactly that of its declaration.
1319void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1320 ObjCMethodDecl *MethodDecl,
1321 bool IsProtocolMethodDecl) {
1322 // don't issue warning when protocol method is optional because primary
1323 // class is not required to implement it and it is safe for protocol
1324 // to implement it.
1325 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1326 return;
1327 // don't issue warning when primary class's method is
1328 // depecated/unavailable.
1329 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1330 MethodDecl->hasAttr<DeprecatedAttr>())
1331 return;
1332
1333 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1334 IsProtocolMethodDecl, false, false);
1335 if (match)
1336 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1337 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1338 IM != EM; ++IM, ++IF) {
1339 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1340 *IM, *IF,
1341 IsProtocolMethodDecl, false, false);
1342 if (!match)
1343 break;
1344 }
1345 if (match)
1346 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001347 if (match)
1348 match = !(MethodDecl->isClassMethod() &&
1349 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001350
1351 if (match) {
1352 Diag(ImpMethodDecl->getLocation(),
1353 diag::warn_category_method_impl_match);
1354 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1355 }
1356}
1357
Mike Stump390b4cc2009-05-16 07:39:55 +00001358/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1359/// improve the efficiency of selector lookups and type checking by associating
1360/// with each protocol / interface / category the flattened instance tables. If
1361/// we used an immutable set to keep the table then it wouldn't add significant
1362/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001363
Steve Naroffefe7f362008-02-08 22:06:17 +00001364/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001365/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001366void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1367 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001368 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001369 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001370 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001371 ObjCContainerDecl *CDecl) {
1372 ObjCInterfaceDecl *IDecl;
1373 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1374 IDecl = C->getClassInterface();
1375 else
1376 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1377 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1378
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001379 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001380 ObjCInterfaceDecl *NSIDecl = 0;
1381 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001382 // check to see if class implements forwardInvocation method and objects
1383 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001384 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001385 // Under such conditions, which means that every method possible is
1386 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001387 // found" warnings.
1388 // FIXME: Use a general GetUnarySelector method for this.
1389 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1390 Selector fISelector = Context.Selectors.getSelector(1, &II);
1391 if (InsMap.count(fISelector))
1392 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1393 // need be implemented in the implementation.
1394 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1395 }
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001397 // If a method lookup fails locally we still need to look and see if
1398 // the method was implemented by a base class or an inherited
1399 // protocol. This lookup is slow, but occurs rarely in correct code
1400 // and otherwise would terminate in a warning.
1401
Chris Lattner4d391482007-12-12 07:09:47 +00001402 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001403 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001404 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001405 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001406 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001407 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001408 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001409 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001410 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001411 // Ugly, but necessary. Method declared in protcol might have
1412 // have been synthesized due to a property declared in the class which
1413 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001414 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001415 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001416 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001417 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001418 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1419 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001420 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001421 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001422 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1423 << PDecl->getDeclName();
1424 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001425 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001426 }
1427 }
Chris Lattner4d391482007-12-12 07:09:47 +00001428 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001429 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001430 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001431 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001432 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001433 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1434 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001435 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001436 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001437 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001438 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001439 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001440 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1441 PDecl->getDeclName();
1442 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001443 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001444 }
Chris Lattner780f3292008-07-21 21:32:27 +00001445 // Check on this protocols's referenced protocols, recursively.
1446 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1447 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001448 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001449}
1450
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001451/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001452/// or protocol against those declared in their implementations.
1453///
1454void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1455 const llvm::DenseSet<Selector> &ClsMap,
1456 llvm::DenseSet<Selector> &InsMapSeen,
1457 llvm::DenseSet<Selector> &ClsMapSeen,
1458 ObjCImplDecl* IMPDecl,
1459 ObjCContainerDecl* CDecl,
1460 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001461 bool ImmediateClass,
1462 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001463 // Check and see if instance methods in class interface have been
1464 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001465 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1466 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001467 if (InsMapSeen.count((*I)->getSelector()))
1468 continue;
1469 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001470 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001471 !InsMap.count((*I)->getSelector())) {
1472 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001473 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1474 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001475 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001476 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001477 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001478 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001479 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001480 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001481 assert(MethodDecl &&
1482 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001483 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001484 if (ImpMethodDecl) {
1485 if (!WarnExactMatch)
1486 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1487 isa<ObjCProtocolDecl>(CDecl));
1488 else
1489 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1490 isa<ObjCProtocolDecl>(CDecl));
1491 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001492 }
1493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001495 // Check and see if class methods in class interface have been
1496 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001497 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001498 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001499 if (ClsMapSeen.count((*I)->getSelector()))
1500 continue;
1501 ClsMapSeen.insert((*I)->getSelector());
1502 if (!ClsMap.count((*I)->getSelector())) {
1503 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001504 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1505 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001506 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001507 ObjCMethodDecl *ImpMethodDecl =
1508 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001509 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001510 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001511 if (!WarnExactMatch)
1512 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1513 isa<ObjCProtocolDecl>(CDecl));
1514 else
1515 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1516 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001517 }
1518 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001519
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001520 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001521 // Also methods in class extensions need be looked at next.
1522 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1523 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1524 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1525 IMPDecl,
1526 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001527 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001528
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001529 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001530 for (ObjCInterfaceDecl::all_protocol_iterator
1531 PI = I->all_referenced_protocol_begin(),
1532 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001533 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1534 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001535 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001536
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001537 // FIXME. For now, we are not checking for extact match of methods
1538 // in category implementation and its primary class's super class.
1539 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001540 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001541 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001542 I->getSuperClass(), IncompleteImpl, false);
1543 }
1544}
1545
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001546/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1547/// category matches with those implemented in its primary class and
1548/// warns each time an exact match is found.
1549void Sema::CheckCategoryVsClassMethodMatches(
1550 ObjCCategoryImplDecl *CatIMPDecl) {
1551 llvm::DenseSet<Selector> InsMap, ClsMap;
1552
1553 for (ObjCImplementationDecl::instmeth_iterator
1554 I = CatIMPDecl->instmeth_begin(),
1555 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1556 InsMap.insert((*I)->getSelector());
1557
1558 for (ObjCImplementationDecl::classmeth_iterator
1559 I = CatIMPDecl->classmeth_begin(),
1560 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1561 ClsMap.insert((*I)->getSelector());
1562 if (InsMap.empty() && ClsMap.empty())
1563 return;
1564
1565 // Get category's primary class.
1566 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1567 if (!CatDecl)
1568 return;
1569 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1570 if (!IDecl)
1571 return;
1572 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1573 bool IncompleteImpl = false;
1574 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1575 CatIMPDecl, IDecl,
1576 IncompleteImpl, false, true /*WarnExactMatch*/);
1577}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001578
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001579void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001580 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001581 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001582 llvm::DenseSet<Selector> InsMap;
1583 // Check and see if instance methods in class interface have been
1584 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001585 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001586 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001587 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001589 // Check and see if properties declared in the interface have either 1)
1590 // an implementation or 2) there is a @synthesize/@dynamic implementation
1591 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001592 if (isa<ObjCInterfaceDecl>(CDecl) &&
1593 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001594 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001595
Chris Lattner4d391482007-12-12 07:09:47 +00001596 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001597 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001598 I = IMPDecl->classmeth_begin(),
1599 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001600 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001602 // Check for type conflict of methods declared in a class/protocol and
1603 // its implementation; if any.
1604 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001605 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1606 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001607 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001608
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001609 // check all methods implemented in category against those declared
1610 // in its primary class.
1611 if (ObjCCategoryImplDecl *CatDecl =
1612 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1613 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Chris Lattner4d391482007-12-12 07:09:47 +00001615 // Check the protocol list for unimplemented methods in the @implementation
1616 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001617 // Check and see if class methods in class interface have been
1618 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Chris Lattnercddc8882009-03-01 00:56:52 +00001620 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001621 for (ObjCInterfaceDecl::all_protocol_iterator
1622 PI = I->all_referenced_protocol_begin(),
1623 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001624 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001625 InsMap, ClsMap, I);
1626 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001627 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1628 Categories; Categories = Categories->getNextClassExtension())
1629 ImplMethodsVsClassMethods(S, IMPDecl,
1630 const_cast<ObjCCategoryDecl*>(Categories),
1631 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001632 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001633 // For extended class, unimplemented methods in its protocols will
1634 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001635 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001636 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1637 E = C->protocol_end(); PI != E; ++PI)
1638 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001639 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001640 // Report unimplemented properties in the category as well.
1641 // When reporting on missing setter/getters, do not report when
1642 // setter/getter is implemented in category's primary class
1643 // implementation.
1644 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1645 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1646 for (ObjCImplementationDecl::instmeth_iterator
1647 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1648 InsMap.insert((*I)->getSelector());
1649 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001650 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001651 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001652 } else
1653 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001654}
1655
Mike Stump1eb44332009-09-09 15:08:12 +00001656/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001657Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001658Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001659 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001660 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001661 unsigned NumElts) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001662 SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Chris Lattner4d391482007-12-12 07:09:47 +00001664 for (unsigned i = 0; i != NumElts; ++i) {
1665 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001666 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001667 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001668 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001669 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001670 // Maybe we will complain about the shadowed template parameter.
1671 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1672 // Just pretend that we didn't see the previous declaration.
1673 PrevDecl = 0;
1674 }
1675
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001676 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001677 // GCC apparently allows the following idiom:
1678 //
1679 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1680 // @class XCElementToggler;
1681 //
Mike Stump1eb44332009-09-09 15:08:12 +00001682 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001683 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001684 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001685 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001686 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001687 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001688 // a forward class declaration matching a typedef name of a class refers
1689 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001690 if (const ObjCObjectType *OI =
1691 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1692 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001693 }
Chris Lattner4d391482007-12-12 07:09:47 +00001694 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001695 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1696 if (!IDecl) { // Not already seen? Make a forward decl.
1697 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1698 IdentList[i], IdentLocs[i], true);
1699
1700 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1701 // the current DeclContext. This prevents clients that walk DeclContext
1702 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1703 // declared later (if at all). We also take care to explicitly make
1704 // sure this declaration is visible for name lookup.
1705 PushOnScopeChains(IDecl, TUScope, false);
1706 CurContext->makeDeclVisibleInContext(IDecl, true);
1707 }
Chris Lattner4d391482007-12-12 07:09:47 +00001708
1709 Interfaces.push_back(IDecl);
1710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Ted Kremenek321c22f2009-11-18 00:28:11 +00001712 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001713 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001714 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001715 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001716 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001717 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001718 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001719}
1720
John McCall0f4c4c42011-06-16 01:15:19 +00001721static bool tryMatchRecordTypes(ASTContext &Context,
1722 Sema::MethodMatchStrategy strategy,
1723 const Type *left, const Type *right);
1724
John McCallf85e1932011-06-15 23:02:42 +00001725static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1726 QualType leftQT, QualType rightQT) {
1727 const Type *left =
1728 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1729 const Type *right =
1730 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1731
1732 if (left == right) return true;
1733
1734 // If we're doing a strict match, the types have to match exactly.
1735 if (strategy == Sema::MMS_strict) return false;
1736
1737 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1738
1739 // Otherwise, use this absurdly complicated algorithm to try to
1740 // validate the basic, low-level compatibility of the two types.
1741
1742 // As a minimum, require the sizes and alignments to match.
1743 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1744 return false;
1745
1746 // Consider all the kinds of non-dependent canonical types:
1747 // - functions and arrays aren't possible as return and parameter types
1748
1749 // - vector types of equal size can be arbitrarily mixed
1750 if (isa<VectorType>(left)) return isa<VectorType>(right);
1751 if (isa<VectorType>(right)) return false;
1752
1753 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001754 // - structs, unions, and Objective-C objects must match more-or-less
1755 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001756 // - everything else should be a scalar
1757 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001758 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001759
1760 // Make scalars agree in kind, except count bools as chars.
1761 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1762 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1763 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1764 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1765
1766 // Note that data member pointers and function member pointers don't
1767 // intermix because of the size differences.
1768
1769 return (leftSK == rightSK);
1770}
Chris Lattner4d391482007-12-12 07:09:47 +00001771
John McCall0f4c4c42011-06-16 01:15:19 +00001772static bool tryMatchRecordTypes(ASTContext &Context,
1773 Sema::MethodMatchStrategy strategy,
1774 const Type *lt, const Type *rt) {
1775 assert(lt && rt && lt != rt);
1776
1777 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1778 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1779 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1780
1781 // Require union-hood to match.
1782 if (left->isUnion() != right->isUnion()) return false;
1783
1784 // Require an exact match if either is non-POD.
1785 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1786 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1787 return false;
1788
1789 // Require size and alignment to match.
1790 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1791
1792 // Require fields to match.
1793 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1794 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1795 for (; li != le && ri != re; ++li, ++ri) {
1796 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1797 return false;
1798 }
1799 return (li == le && ri == re);
1800}
1801
Chris Lattner4d391482007-12-12 07:09:47 +00001802/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1803/// returns true, or false, accordingly.
1804/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001805bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1806 const ObjCMethodDecl *right,
1807 MethodMatchStrategy strategy) {
1808 if (!matchTypes(Context, strategy,
1809 left->getResultType(), right->getResultType()))
1810 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001811
John McCallf85e1932011-06-15 23:02:42 +00001812 if (getLangOptions().ObjCAutoRefCount &&
1813 (left->hasAttr<NSReturnsRetainedAttr>()
1814 != right->hasAttr<NSReturnsRetainedAttr>() ||
1815 left->hasAttr<NSConsumesSelfAttr>()
1816 != right->hasAttr<NSConsumesSelfAttr>()))
1817 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001818
John McCallf85e1932011-06-15 23:02:42 +00001819 ObjCMethodDecl::param_iterator
1820 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001821
John McCallf85e1932011-06-15 23:02:42 +00001822 for (; li != le; ++li, ++ri) {
1823 assert(ri != right->param_end() && "Param mismatch");
1824 ParmVarDecl *lparm = *li, *rparm = *ri;
1825
1826 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1827 return false;
1828
1829 if (getLangOptions().ObjCAutoRefCount &&
1830 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1831 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001832 }
1833 return true;
1834}
1835
Sebastian Redldb9d2142010-08-02 23:18:59 +00001836/// \brief Read the contents of the method pool for a given selector from
1837/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001838///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001839/// This routine should only be called once, when the method pool has no entry
1840/// for this selector.
1841Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001842 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001843 assert(MethodPool.find(Sel) == MethodPool.end() &&
1844 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001845
1846 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001847 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Sebastian Redldb9d2142010-08-02 23:18:59 +00001849 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001850}
1851
Sebastian Redldb9d2142010-08-02 23:18:59 +00001852void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1853 bool instance) {
1854 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1855 if (Pos == MethodPool.end()) {
1856 if (ExternalSource)
1857 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001858 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001859 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1860 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001861 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001862 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001863 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001864 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001865 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001866 Entry.Method = Method;
1867 Entry.Next = 0;
1868 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001869 }
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Chris Lattnerb25df352009-03-04 05:16:45 +00001871 // We've seen a method with this name, see if we have already seen this type
1872 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001873 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1874 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1875
1876 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001877 ObjCMethodDecl *PrevObjCMethod = List->Method;
1878 PrevObjCMethod->setDefined(impl);
1879 // If a method is deprecated, push it in the global pool.
1880 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001881 if (Method->isDeprecated()) {
1882 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001883 List->Method = Method;
1884 }
1885 // If new method is unavailable, push it into global pool
1886 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001887 if (Method->isUnavailable()) {
1888 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001889 List->Method = Method;
1890 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001891 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001892 }
John McCallf85e1932011-06-15 23:02:42 +00001893 }
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Chris Lattnerb25df352009-03-04 05:16:45 +00001895 // We have a new signature for an existing method - add it.
1896 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001897 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1898 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001899}
1900
John McCallf85e1932011-06-15 23:02:42 +00001901/// Determines if this is an "acceptable" loose mismatch in the global
1902/// method pool. This exists mostly as a hack to get around certain
1903/// global mismatches which we can't afford to make warnings / errors.
1904/// Really, what we want is a way to take a method out of the global
1905/// method pool.
1906static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1907 ObjCMethodDecl *other) {
1908 if (!chosen->isInstanceMethod())
1909 return false;
1910
1911 Selector sel = chosen->getSelector();
1912 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1913 return false;
1914
1915 // Don't complain about mismatches for -length if the method we
1916 // chose has an integral result type.
1917 return (chosen->getResultType()->isIntegerType());
1918}
1919
Sebastian Redldb9d2142010-08-02 23:18:59 +00001920ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001921 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001922 bool warn, bool instance) {
1923 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1924 if (Pos == MethodPool.end()) {
1925 if (ExternalSource)
1926 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001927 else
1928 return 0;
1929 }
1930
Sebastian Redldb9d2142010-08-02 23:18:59 +00001931 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Sebastian Redldb9d2142010-08-02 23:18:59 +00001933 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001934 bool issueDiagnostic = false, issueError = false;
1935
1936 // We support a warning which complains about *any* difference in
1937 // method signature.
1938 bool strictSelectorMatch =
1939 (receiverIdOrClass && warn &&
1940 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1941 R.getBegin()) !=
1942 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001943 if (strictSelectorMatch)
1944 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001945 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1946 MMS_strict)) {
1947 issueDiagnostic = true;
1948 break;
1949 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001950 }
1951
John McCallf85e1932011-06-15 23:02:42 +00001952 // If we didn't see any strict differences, we won't see any loose
1953 // differences. In ARC, however, we also need to check for loose
1954 // mismatches, because most of them are errors.
1955 if (!strictSelectorMatch ||
1956 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001957 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001958 // This checks if the methods differ in type mismatch.
1959 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1960 MMS_loose) &&
1961 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1962 issueDiagnostic = true;
1963 if (getLangOptions().ObjCAutoRefCount)
1964 issueError = true;
1965 break;
1966 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001967 }
1968
John McCallf85e1932011-06-15 23:02:42 +00001969 if (issueDiagnostic) {
1970 if (issueError)
1971 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1972 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001973 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1974 else
1975 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001976
1977 Diag(MethList.Method->getLocStart(),
1978 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001979 << MethList.Method->getSourceRange();
1980 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1981 Diag(Next->Method->getLocStart(), diag::note_also_found)
1982 << Next->Method->getSourceRange();
1983 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001984 }
1985 return MethList.Method;
1986}
1987
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001988ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001989 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1990 if (Pos == MethodPool.end())
1991 return 0;
1992
1993 GlobalMethods &Methods = Pos->second;
1994
1995 if (Methods.first.Method && Methods.first.Method->isDefined())
1996 return Methods.first.Method;
1997 if (Methods.second.Method && Methods.second.Method->isDefined())
1998 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001999 return 0;
2000}
2001
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002002/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2003/// identical selector names in current and its super classes and issues
2004/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002005void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2006 ObjCMethodDecl *Method,
2007 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002008 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2009 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002011 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002012 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002013 SD->lookupMethod(Method->getSelector(), IsInstance);
2014 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002015 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002016 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002017 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002018 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2019 E = Method->param_end();
2020 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2021 for (; ParamI != E; ++ParamI, ++PrevI) {
2022 // Number of parameters are the same and is guaranteed by selector match.
2023 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2024 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2025 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002026 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002027 // respective argument type in the super class method, issue warning;
2028 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002029 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002030 << T1 << T2;
2031 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2032 return;
2033 }
2034 }
2035 ID = SD;
2036 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002037}
2038
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002039/// DiagnoseDuplicateIvars -
2040/// Check for duplicate ivars in the entire class at the start of
2041/// @implementation. This becomes necesssary because class extension can
2042/// add ivars to a class in random order which will not be known until
2043/// class's @implementation is seen.
2044void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2045 ObjCInterfaceDecl *SID) {
2046 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2047 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2048 ObjCIvarDecl* Ivar = (*IVI);
2049 if (Ivar->isInvalidDecl())
2050 continue;
2051 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2052 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2053 if (prevIvar) {
2054 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2055 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2056 Ivar->setInvalidDecl();
2057 }
2058 }
2059 }
2060}
2061
Steve Naroffa56f6162007-12-18 01:30:32 +00002062// Note: For class/category implemenations, allMethods/allProperties is
2063// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002064void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002065 Decl **allMethods, unsigned allNum,
2066 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002067 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002068
2069 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002070 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002071 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2072 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002073
Mike Stump1eb44332009-09-09 15:08:12 +00002074 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002075 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2076 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002077 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002078
Ted Kremenek782f2f52010-01-07 01:20:12 +00002079 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2080 // FIXME: This is wrong. We shouldn't be pretending that there is
2081 // an '@end' in the declaration.
2082 SourceLocation L = ClassDecl->getLocation();
2083 AtEnd.setBegin(L);
2084 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002085 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002086 }
2087
Steve Naroff0701bbb2009-01-08 17:28:14 +00002088 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2089 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2090 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2091
Chris Lattner4d391482007-12-12 07:09:47 +00002092 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002093 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002094 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002095
2096 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002097 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002098 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002099 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002100 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002101 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002102 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002103 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002104 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002105 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002106 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002107 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002108 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002109 InsMap[Method->getSelector()] = Method;
2110 /// The following allows us to typecheck messages to "id".
2111 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002112 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002113 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002114 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002115 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002116 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002117 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002118 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002119 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002120 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002121 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002122 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002123 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002124 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002125 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002126 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002127 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002128 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002129 /// The following allows us to typecheck messages to "Class".
2130 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002131 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002132 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002133 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002134 }
2135 }
2136 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002137 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002138 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002139 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002140 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002141 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002142 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002143 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002144 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002145 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002146
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002147 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002148 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002149 if (C->IsClassExtension()) {
2150 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2151 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002152 }
Chris Lattner4d391482007-12-12 07:09:47 +00002153 }
Steve Naroff09c47192009-01-09 15:36:25 +00002154 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002155 if (CDecl->getIdentifier())
2156 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2157 // user-defined setter/getter. It also synthesizes setter/getter methods
2158 // and adds them to the DeclContext and global method pools.
2159 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2160 E = CDecl->prop_end();
2161 I != E; ++I)
2162 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002163 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002164 }
2165 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002166 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002167 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002168 // Any property declared in a class extension might have user
2169 // declared setter or getter in current class extension or one
2170 // of the other class extensions. Mark them as synthesized as
2171 // property will be synthesized when property with same name is
2172 // seen in the @implementation.
2173 for (const ObjCCategoryDecl *ClsExtDecl =
2174 IDecl->getFirstClassExtension();
2175 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2176 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2177 E = ClsExtDecl->prop_end(); I != E; ++I) {
2178 ObjCPropertyDecl *Property = (*I);
2179 // Skip over properties declared @dynamic
2180 if (const ObjCPropertyImplDecl *PIDecl
2181 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2182 if (PIDecl->getPropertyImplementation()
2183 == ObjCPropertyImplDecl::Dynamic)
2184 continue;
2185
2186 for (const ObjCCategoryDecl *CExtDecl =
2187 IDecl->getFirstClassExtension();
2188 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2189 if (ObjCMethodDecl *GetterMethod =
2190 CExtDecl->getInstanceMethod(Property->getGetterName()))
2191 GetterMethod->setSynthesized(true);
2192 if (!Property->isReadOnly())
2193 if (ObjCMethodDecl *SetterMethod =
2194 CExtDecl->getInstanceMethod(Property->getSetterName()))
2195 SetterMethod->setSynthesized(true);
2196 }
2197 }
2198 }
2199
Ted Kremenekc32647d2010-12-23 21:35:43 +00002200 if (LangOpts.ObjCDefaultSynthProperties &&
2201 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002202 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002203 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002204 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002205 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002206
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002207 if (LangOpts.ObjCNonFragileABI2)
2208 while (IDecl->getSuperClass()) {
2209 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2210 IDecl = IDecl->getSuperClass();
2211 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002212 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002213 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002214 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002215 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002216 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002217
Chris Lattner4d391482007-12-12 07:09:47 +00002218 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002219 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002220 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002221 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002222 Categories; Categories = Categories->getNextClassCategory()) {
2223 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002224 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002225 break;
2226 }
2227 }
2228 }
2229 }
Chris Lattner682bf922009-03-29 16:50:03 +00002230 if (isInterfaceDeclKind) {
2231 // Reject invalid vardecls.
2232 for (unsigned i = 0; i != tuvNum; i++) {
2233 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2234 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2235 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002236 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002237 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002238 }
Chris Lattner682bf922009-03-29 16:50:03 +00002239 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002240 }
Chris Lattner4d391482007-12-12 07:09:47 +00002241}
2242
2243
2244/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2245/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002246static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002247CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002248 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002249}
2250
Ted Kremenek422bae72010-04-18 04:59:38 +00002251static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002252bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002253 // The 'ibaction' attribute is allowed on method definitions because of
2254 // how the IBAction macro is used on both method declarations and definitions.
2255 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002256 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2257 if ((*i)->getKind() != attr::IBAction)
2258 return true;
2259 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002260}
2261
Douglas Gregor926df6c2011-06-11 01:09:30 +00002262/// \brief Check whether the declared result type of the given Objective-C
2263/// method declaration is compatible with the method's class.
2264///
2265static bool
2266CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2267 ObjCInterfaceDecl *CurrentClass) {
2268 QualType ResultType = Method->getResultType();
2269 SourceRange ResultTypeRange;
2270 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2271 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2272
2273 // If an Objective-C method inherits its related result type, then its
2274 // declared result type must be compatible with its own class type. The
2275 // declared result type is compatible if:
2276 if (const ObjCObjectPointerType *ResultObjectType
2277 = ResultType->getAs<ObjCObjectPointerType>()) {
2278 // - it is id or qualified id, or
2279 if (ResultObjectType->isObjCIdType() ||
2280 ResultObjectType->isObjCQualifiedIdType())
2281 return false;
2282
2283 if (CurrentClass) {
2284 if (ObjCInterfaceDecl *ResultClass
2285 = ResultObjectType->getInterfaceDecl()) {
2286 // - it is the same as the method's class type, or
2287 if (CurrentClass == ResultClass)
2288 return false;
2289
2290 // - it is a superclass of the method's class type
2291 if (ResultClass->isSuperClassOf(CurrentClass))
2292 return false;
2293 }
2294 }
2295 }
2296
2297 return true;
2298}
2299
John McCall6c2c2502011-07-22 02:45:48 +00002300namespace {
2301/// A helper class for searching for methods which a particular method
2302/// overrides.
2303class OverrideSearch {
2304 Sema &S;
2305 ObjCMethodDecl *Method;
2306 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2307 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2308 bool Recursive;
2309
2310public:
2311 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2312 Selector selector = method->getSelector();
2313
2314 // Bypass this search if we've never seen an instance/class method
2315 // with this selector before.
2316 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2317 if (it == S.MethodPool.end()) {
2318 if (!S.ExternalSource) return;
2319 it = S.ReadMethodPool(selector);
2320 }
2321 ObjCMethodList &list =
2322 method->isInstanceMethod() ? it->second.first : it->second.second;
2323 if (!list.Method) return;
2324
2325 ObjCContainerDecl *container
2326 = cast<ObjCContainerDecl>(method->getDeclContext());
2327
2328 // Prevent the search from reaching this container again. This is
2329 // important with categories, which override methods from the
2330 // interface and each other.
2331 Searched.insert(container);
2332 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002333 }
John McCall6c2c2502011-07-22 02:45:48 +00002334
2335 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2336 iterator begin() const { return Overridden.begin(); }
2337 iterator end() const { return Overridden.end(); }
2338
2339private:
2340 void searchFromContainer(ObjCContainerDecl *container) {
2341 if (container->isInvalidDecl()) return;
2342
2343 switch (container->getDeclKind()) {
2344#define OBJCCONTAINER(type, base) \
2345 case Decl::type: \
2346 searchFrom(cast<type##Decl>(container)); \
2347 break;
2348#define ABSTRACT_DECL(expansion)
2349#define DECL(type, base) \
2350 case Decl::type:
2351#include "clang/AST/DeclNodes.inc"
2352 llvm_unreachable("not an ObjC container!");
2353 }
2354 }
2355
2356 void searchFrom(ObjCProtocolDecl *protocol) {
2357 // A method in a protocol declaration overrides declarations from
2358 // referenced ("parent") protocols.
2359 search(protocol->getReferencedProtocols());
2360 }
2361
2362 void searchFrom(ObjCCategoryDecl *category) {
2363 // A method in a category declaration overrides declarations from
2364 // the main class and from protocols the category references.
2365 search(category->getClassInterface());
2366 search(category->getReferencedProtocols());
2367 }
2368
2369 void searchFrom(ObjCCategoryImplDecl *impl) {
2370 // A method in a category definition that has a category
2371 // declaration overrides declarations from the category
2372 // declaration.
2373 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2374 search(category);
2375
2376 // Otherwise it overrides declarations from the class.
2377 } else {
2378 search(impl->getClassInterface());
2379 }
2380 }
2381
2382 void searchFrom(ObjCInterfaceDecl *iface) {
2383 // A method in a class declaration overrides declarations from
2384
2385 // - categories,
2386 for (ObjCCategoryDecl *category = iface->getCategoryList();
2387 category; category = category->getNextClassCategory())
2388 search(category);
2389
2390 // - the super class, and
2391 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2392 search(super);
2393
2394 // - any referenced protocols.
2395 search(iface->getReferencedProtocols());
2396 }
2397
2398 void searchFrom(ObjCImplementationDecl *impl) {
2399 // A method in a class implementation overrides declarations from
2400 // the class interface.
2401 search(impl->getClassInterface());
2402 }
2403
2404
2405 void search(const ObjCProtocolList &protocols) {
2406 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2407 i != e; ++i)
2408 search(*i);
2409 }
2410
2411 void search(ObjCContainerDecl *container) {
2412 // Abort if we've already searched this container.
2413 if (!Searched.insert(container)) return;
2414
2415 // Check for a method in this container which matches this selector.
2416 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2417 Method->isInstanceMethod());
2418
2419 // If we find one, record it and bail out.
2420 if (meth) {
2421 Overridden.insert(meth);
2422 return;
2423 }
2424
2425 // Otherwise, search for methods that a hypothetical method here
2426 // would have overridden.
2427
2428 // Note that we're now in a recursive case.
2429 Recursive = true;
2430
2431 searchFromContainer(container);
2432 }
2433};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002434}
2435
John McCalld226f652010-08-21 09:40:31 +00002436Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002437 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002438 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002439 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002440 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002441 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002442 Selector Sel,
2443 // optional arguments. The number of types/arguments is obtained
2444 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002445 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002446 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002447 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002448 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002449 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002450 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002451 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002452 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002453 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002454 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2455 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002456 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002457
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002458 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002459 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002460 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Steve Naroffccef3712009-02-20 22:59:16 +00002462 // Methods cannot return interface types. All ObjC objects are
2463 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002464 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002465 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2466 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002467 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002468 }
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002469 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002470 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002471 Diag(MethodLoc, diag::warn_missing_method_return_type)
2472 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002473 }
Mike Stump1eb44332009-09-09 15:08:12 +00002474
2475 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002476 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002477 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002478 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002479 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002480 /*isSynthesized=*/false,
2481 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002482 MethodDeclKind == tok::objc_optional
2483 ? ObjCMethodDecl::Optional
2484 : ObjCMethodDecl::Required,
2485 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002486
Chris Lattner5f9e2722011-07-23 10:55:15 +00002487 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Chris Lattner7db638d2009-04-11 19:42:43 +00002489 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002490 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002491 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002492
Chris Lattnere294d3f2009-04-11 18:57:04 +00002493 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002494 ArgType = Context.getObjCIdType();
2495 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002496 } else {
John McCall58e46772009-10-23 21:48:59 +00002497 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002498 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002499 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002500 }
Mike Stump1eb44332009-09-09 15:08:12 +00002501
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002502 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2503 LookupOrdinaryName, ForRedeclaration);
2504 LookupName(R, S);
2505 if (R.isSingleResult()) {
2506 NamedDecl *PrevDecl = R.getFoundDecl();
2507 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002508 Diag(ArgInfo[i].NameLoc,
2509 (MethodDefinition ? diag::warn_method_param_redefinition
2510 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002511 << ArgInfo[i].Name;
2512 Diag(PrevDecl->getLocation(),
2513 diag::note_previous_declaration);
2514 }
2515 }
2516
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002517 SourceLocation StartLoc = DI
2518 ? DI->getTypeLoc().getBeginLoc()
2519 : ArgInfo[i].NameLoc;
2520
John McCall81ef3e62011-04-23 02:46:06 +00002521 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2522 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2523 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002524
John McCall70798862011-05-02 00:30:12 +00002525 Param->setObjCMethodScopeInfo(i);
2526
Chris Lattner0ed844b2008-04-04 06:12:32 +00002527 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002528 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002529
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002530 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002531 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002532
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002533 S->AddDecl(Param);
2534 IdResolver.AddDecl(Param);
2535
Chris Lattner0ed844b2008-04-04 06:12:32 +00002536 Params.push_back(Param);
2537 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002538
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002539 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002540 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002541 QualType ArgType = Param->getType();
2542 if (ArgType.isNull())
2543 ArgType = Context.getObjCIdType();
2544 else
2545 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002546 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002547 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002548 Diag(Param->getLocation(),
2549 diag::err_object_cannot_be_passed_returned_by_value)
2550 << 1 << ArgType;
2551 Param->setInvalidDecl();
2552 }
2553 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002554
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002555 Params.push_back(Param);
2556 }
2557
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002558 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2559 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002560 ObjCMethod->setObjCDeclQualifier(
2561 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002562
2563 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002564 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002565
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002566 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002567 const ObjCMethodDecl *PrevMethod = 0;
2568 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002569 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002570 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2571 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002572 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002573 PrevMethod = ImpDecl->getClassMethod(Sel);
2574 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002575 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002576
Sean Huntcf807c42010-08-18 23:23:40 +00002577 if (ObjCMethod->hasAttrs() &&
2578 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002579 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002580 } else {
2581 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002582 }
John McCall6c2c2502011-07-22 02:45:48 +00002583
Chris Lattner4d391482007-12-12 07:09:47 +00002584 if (PrevMethod) {
2585 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002586 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002587 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002588 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002589 }
John McCall54abf7d2009-11-04 02:18:39 +00002590
Douglas Gregor926df6c2011-06-11 01:09:30 +00002591 // If this Objective-C method does not have a related result type, but we
2592 // are allowed to infer related result types, try to do so based on the
2593 // method family.
2594 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2595 if (!CurrentClass) {
2596 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2597 CurrentClass = Cat->getClassInterface();
2598 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2599 CurrentClass = Impl->getClassInterface();
2600 else if (ObjCCategoryImplDecl *CatImpl
2601 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2602 CurrentClass = CatImpl->getClassInterface();
2603 }
John McCall6c2c2502011-07-22 02:45:48 +00002604
2605 bool isRelatedResultTypeCompatible =
2606 (getLangOptions().ObjCInferRelatedResultType &&
2607 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass));
2608
2609 // Search for overridden methods and merge information down from them.
2610 OverrideSearch overrides(*this, ObjCMethod);
2611 for (OverrideSearch::iterator
2612 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2613 ObjCMethodDecl *overridden = *i;
2614
2615 // Propagate down the 'related result type' bit from overridden methods.
2616 if (isRelatedResultTypeCompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002617 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002618
2619 // Then merge the declarations.
2620 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002621
2622 // Check for overriding methods
2623 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2624 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2625 WarnConflictingTypedMethods(ObjCMethod, overridden,
2626 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2627 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002628 }
2629
John McCallf85e1932011-06-15 23:02:42 +00002630 bool ARCError = false;
2631 if (getLangOptions().ObjCAutoRefCount)
2632 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2633
John McCall6c2c2502011-07-22 02:45:48 +00002634 if (!ARCError && isRelatedResultTypeCompatible &&
2635 !ObjCMethod->hasRelatedResultType()) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002636 bool InferRelatedResultType = false;
2637 switch (ObjCMethod->getMethodFamily()) {
2638 case OMF_None:
2639 case OMF_copy:
2640 case OMF_dealloc:
2641 case OMF_mutableCopy:
2642 case OMF_release:
2643 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002644 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002645 break;
2646
2647 case OMF_alloc:
2648 case OMF_new:
2649 InferRelatedResultType = ObjCMethod->isClassMethod();
2650 break;
2651
2652 case OMF_init:
2653 case OMF_autorelease:
2654 case OMF_retain:
2655 case OMF_self:
2656 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2657 break;
2658 }
2659
John McCall6c2c2502011-07-22 02:45:48 +00002660 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002661 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002662 }
2663
John McCalld226f652010-08-21 09:40:31 +00002664 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002665}
2666
Chris Lattnercc98eac2008-12-17 07:13:27 +00002667bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002668 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002669 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002670 // Following is also an error. But it is caused by a missing @end
2671 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002672 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2673 return false;
2674 }
2675
Anders Carlsson15281452008-11-04 16:57:32 +00002676 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2677 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002678
Anders Carlsson15281452008-11-04 16:57:32 +00002679 return true;
2680}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002681
Chris Lattnercc98eac2008-12-17 07:13:27 +00002682/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2683/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002684void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002685 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002686 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002687 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002688 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002689 if (!Class) {
2690 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2691 return;
2692 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002693 if (LangOpts.ObjCNonFragileABI) {
2694 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2695 return;
2696 }
Mike Stump1eb44332009-09-09 15:08:12 +00002697
Chris Lattnercc98eac2008-12-17 07:13:27 +00002698 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002699 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002700 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002701 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002702 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002703 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002704 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002705 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2706 /*FIXME: StartL=*/ID->getLocation(),
2707 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002708 ID->getIdentifier(), ID->getType(),
2709 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002710 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002711 }
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Chris Lattnercc98eac2008-12-17 07:13:27 +00002713 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002714 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002715 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002716 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002717 if (getLangOptions().CPlusPlus)
2718 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002719 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002720 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002721 }
2722}
2723
Douglas Gregor160b5632010-04-26 17:32:49 +00002724/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002725VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2726 SourceLocation StartLoc,
2727 SourceLocation IdLoc,
2728 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002729 bool Invalid) {
2730 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2731 // duration shall not be qualified by an address-space qualifier."
2732 // Since all parameters have automatic store duration, they can not have
2733 // an address space.
2734 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002735 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002736 Invalid = true;
2737 }
2738
2739 // An @catch parameter must be an unqualified object pointer type;
2740 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2741 if (Invalid) {
2742 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002743 } else if (T->isDependentType()) {
2744 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002745 } else if (!T->isObjCObjectPointerType()) {
2746 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002747 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002748 } else if (T->isObjCQualifiedIdType()) {
2749 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002750 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002751 }
2752
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002753 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2754 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002755 New->setExceptionVariable(true);
2756
Douglas Gregor160b5632010-04-26 17:32:49 +00002757 if (Invalid)
2758 New->setInvalidDecl();
2759 return New;
2760}
2761
John McCalld226f652010-08-21 09:40:31 +00002762Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002763 const DeclSpec &DS = D.getDeclSpec();
2764
2765 // We allow the "register" storage class on exception variables because
2766 // GCC did, but we drop it completely. Any other storage class is an error.
2767 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2768 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2769 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2770 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2771 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2772 << DS.getStorageClassSpec();
2773 }
2774 if (D.getDeclSpec().isThreadSpecified())
2775 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2776 D.getMutableDeclSpec().ClearStorageClassSpecs();
2777
2778 DiagnoseFunctionSpecifiers(D);
2779
2780 // Check that there are no default arguments inside the type of this
2781 // exception object (C++ only).
2782 if (getLangOptions().CPlusPlus)
2783 CheckExtraCXXDefaultArguments(D);
2784
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002785 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002786 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002787
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002788 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2789 D.getSourceRange().getBegin(),
2790 D.getIdentifierLoc(),
2791 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002792 D.isInvalidType());
2793
2794 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2795 if (D.getCXXScopeSpec().isSet()) {
2796 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2797 << D.getCXXScopeSpec().getRange();
2798 New->setInvalidDecl();
2799 }
2800
2801 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002802 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002803 if (D.getIdentifier())
2804 IdResolver.AddDecl(New);
2805
2806 ProcessDeclAttributes(S, New, D);
2807
2808 if (New->hasAttr<BlocksAttr>())
2809 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002810 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002811}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002812
2813/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002814/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002815void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002816 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002817 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2818 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002819 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002820 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002821 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002822 }
2823}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002824
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002825void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002826 // Load referenced selectors from the external source.
2827 if (ExternalSource) {
2828 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2829 ExternalSource->ReadReferencedSelectors(Sels);
2830 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2831 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2832 }
2833
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002834 // Warning will be issued only when selector table is
2835 // generated (which means there is at lease one implementation
2836 // in the TU). This is to match gcc's behavior.
2837 if (ReferencedSelectors.empty() ||
2838 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002839 return;
2840 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2841 ReferencedSelectors.begin(),
2842 E = ReferencedSelectors.end(); S != E; ++S) {
2843 Selector Sel = (*S).first;
2844 if (!LookupImplementedMethodInGlobalPool(Sel))
2845 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2846 }
2847 return;
2848}