blob: 9b6166bc5b22e180e5a59027cb222d77105d186f [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000026#include "llvm/ADT/DenseSet.h"
27
Chris Lattner4d391482007-12-12 07:09:47 +000028using namespace clang;
29
John McCallf85e1932011-06-15 23:02:42 +000030/// Check whether the given method, which must be in the 'init'
31/// family, is a valid member of that family.
32///
33/// \param receiverTypeIfCall - if null, check this as if declaring it;
34/// if non-null, check this as if making a call to it with the given
35/// receiver type
36///
37/// \return true to indicate that there was an error and appropriate
38/// actions were taken
39bool Sema::checkInitMethod(ObjCMethodDecl *method,
40 QualType receiverTypeIfCall) {
41 if (method->isInvalidDecl()) return true;
42
43 // This castAs is safe: methods that don't return an object
44 // pointer won't be inferred as inits and will reject an explicit
45 // objc_method_family(init).
46
47 // We ignore protocols here. Should we? What about Class?
48
49 const ObjCObjectType *result = method->getResultType()
50 ->castAs<ObjCObjectPointerType>()->getObjectType();
51
52 if (result->isObjCId()) {
53 return false;
54 } else if (result->isObjCClass()) {
55 // fall through: always an error
56 } else {
57 ObjCInterfaceDecl *resultClass = result->getInterface();
58 assert(resultClass && "unexpected object type!");
59
60 // It's okay for the result type to still be a forward declaration
61 // if we're checking an interface declaration.
62 if (resultClass->isForwardDecl()) {
63 if (receiverTypeIfCall.isNull() &&
64 !isa<ObjCImplementationDecl>(method->getDeclContext()))
65 return false;
66
67 // Otherwise, we try to compare class types.
68 } else {
69 // If this method was declared in a protocol, we can't check
70 // anything unless we have a receiver type that's an interface.
71 const ObjCInterfaceDecl *receiverClass = 0;
72 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
73 if (receiverTypeIfCall.isNull())
74 return false;
75
76 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
77 ->getInterfaceDecl();
78
79 // This can be null for calls to e.g. id<Foo>.
80 if (!receiverClass) return false;
81 } else {
82 receiverClass = method->getClassInterface();
83 assert(receiverClass && "method not associated with a class!");
84 }
85
86 // If either class is a subclass of the other, it's fine.
87 if (receiverClass->isSuperClassOf(resultClass) ||
88 resultClass->isSuperClassOf(receiverClass))
89 return false;
90 }
91 }
92
93 SourceLocation loc = method->getLocation();
94
95 // If we're in a system header, and this is not a call, just make
96 // the method unusable.
97 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
98 method->addAttr(new (Context) UnavailableAttr(loc, Context,
99 "init method returns a type unrelated to its receiver type"));
100 return true;
101 }
102
103 // Otherwise, it's an error.
104 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
105 method->setInvalidDecl();
106 return true;
107}
108
Douglas Gregor926df6c2011-06-11 01:09:30 +0000109bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
110 const ObjCMethodDecl *Overridden,
111 bool IsImplementation) {
112 if (Overridden->hasRelatedResultType() &&
113 !NewMethod->hasRelatedResultType()) {
114 // This can only happen when the method follows a naming convention that
115 // implies a related result type, and the original (overridden) method has
116 // a suitable return type, but the new (overriding) method does not have
117 // a suitable return type.
118 QualType ResultType = NewMethod->getResultType();
119 SourceRange ResultTypeRange;
120 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000121 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000122 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
123
124 // Figure out which class this method is part of, if any.
125 ObjCInterfaceDecl *CurrentClass
126 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
127 if (!CurrentClass) {
128 DeclContext *DC = NewMethod->getDeclContext();
129 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
130 CurrentClass = Cat->getClassInterface();
131 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
132 CurrentClass = Impl->getClassInterface();
133 else if (ObjCCategoryImplDecl *CatImpl
134 = dyn_cast<ObjCCategoryImplDecl>(DC))
135 CurrentClass = CatImpl->getClassInterface();
136 }
137
138 if (CurrentClass) {
139 Diag(NewMethod->getLocation(),
140 diag::warn_related_result_type_compatibility_class)
141 << Context.getObjCInterfaceType(CurrentClass)
142 << ResultType
143 << ResultTypeRange;
144 } else {
145 Diag(NewMethod->getLocation(),
146 diag::warn_related_result_type_compatibility_protocol)
147 << ResultType
148 << ResultTypeRange;
149 }
150
151 Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
152 << Overridden->getMethodFamily();
153 }
154
155 return false;
156}
157
John McCallf85e1932011-06-15 23:02:42 +0000158/// \brief Check a method declaration for compatibility with the Objective-C
159/// ARC conventions.
160static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
161 ObjCMethodFamily family = method->getMethodFamily();
162 switch (family) {
163 case OMF_None:
164 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000165 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000166 case OMF_retain:
167 case OMF_release:
168 case OMF_autorelease:
169 case OMF_retainCount:
170 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000171 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000172 return false;
173
174 case OMF_init:
175 // If the method doesn't obey the init rules, don't bother annotating it.
176 if (S.checkInitMethod(method, QualType()))
177 return true;
178
179 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
180 S.Context));
181
182 // Don't add a second copy of this attribute, but otherwise don't
183 // let it be suppressed.
184 if (method->hasAttr<NSReturnsRetainedAttr>())
185 return false;
186 break;
187
188 case OMF_alloc:
189 case OMF_copy:
190 case OMF_mutableCopy:
191 case OMF_new:
192 if (method->hasAttr<NSReturnsRetainedAttr>() ||
193 method->hasAttr<NSReturnsNotRetainedAttr>() ||
194 method->hasAttr<NSReturnsAutoreleasedAttr>())
195 return false;
196 break;
197 }
198
199 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
200 S.Context));
201 return false;
202}
203
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000204static void DiagnoseObjCImplementedDeprecations(Sema &S,
205 NamedDecl *ND,
206 SourceLocation ImplLoc,
207 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000208 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000209 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000210 if (select == 0)
211 S.Diag(ND->getLocation(), diag::note_method_declared_at);
212 else
213 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
214 }
215}
216
Steve Naroffebf64432009-02-28 16:59:13 +0000217/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000218/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000219void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000220 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000221 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Steve Naroff394f3f42008-07-25 17:57:26 +0000223 // If we don't have a valid method decl, simply return.
224 if (!MDecl)
225 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000226
227 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000228 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000229 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000230 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000231 AddFactoryMethodToGlobalPool(MDecl, true);
232
Chris Lattner4d391482007-12-12 07:09:47 +0000233 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000234 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000235 PushFunctionScope();
236
Chris Lattner4d391482007-12-12 07:09:47 +0000237 // Create Decl objects for each parameter, entrring them in the scope for
238 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000239
240 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000241 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Daniel Dunbar451318c2008-08-26 06:07:48 +0000243 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
244 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000245
Chris Lattner8123a952008-04-10 02:22:51 +0000246 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000247 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000248 E = MDecl->param_end(); PI != E; ++PI) {
249 ParmVarDecl *Param = (*PI);
250 if (!Param->isInvalidDecl() &&
251 RequireCompleteType(Param->getLocation(), Param->getType(),
252 diag::err_typecheck_decl_incomplete_type))
253 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000254 if ((*PI)->getIdentifier())
255 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000256 }
John McCallf85e1932011-06-15 23:02:42 +0000257
258 // In ARC, disallow definition of retain/release/autorelease/retainCount
259 if (getLangOptions().ObjCAutoRefCount) {
260 switch (MDecl->getMethodFamily()) {
261 case OMF_retain:
262 case OMF_retainCount:
263 case OMF_release:
264 case OMF_autorelease:
265 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
266 << MDecl->getSelector();
267 break;
268
269 case OMF_None:
270 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000271 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000272 case OMF_alloc:
273 case OMF_init:
274 case OMF_mutableCopy:
275 case OMF_copy:
276 case OMF_new:
277 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000278 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000279 break;
280 }
281 }
282
Nico Weber9a1ecf02011-08-22 17:25:57 +0000283 // Warn on deprecated methods under -Wdeprecated-implementations,
284 // and prepare for warning on missing super calls.
285 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000286 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000287 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000288 DiagnoseObjCImplementedDeprecations(*this,
289 dyn_cast<NamedDecl>(IMD),
290 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000291
Nico Weber80cb6e62011-08-28 22:35:17 +0000292 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000293 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
294 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
295 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000296 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000297 ObjCShouldCallSuperDealloc =
298 !Context.getLangOptions().ObjCAutoRefCount &&
299 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000300 ObjCShouldCallSuperFinalize = MDecl->getMethodFamily() == OMF_finalize;
301 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000302 }
Chris Lattner4d391482007-12-12 07:09:47 +0000303}
304
John McCalld226f652010-08-21 09:40:31 +0000305Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000306ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
307 IdentifierInfo *ClassName, SourceLocation ClassLoc,
308 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000309 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000310 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000311 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000312 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Chris Lattner4d391482007-12-12 07:09:47 +0000314 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000315 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000316 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000317
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000318 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000319 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000320 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000323 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
324 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000325 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000326 if (!IDecl->isForwardDecl()) {
327 IDecl->setInvalidDecl();
328 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
329 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000330
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000331 // Return the previous class interface.
332 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000333 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000334 } else {
335 IDecl->setLocation(AtInterfaceLoc);
336 IDecl->setForwardDecl(false);
337 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000338 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000339 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000340 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000341
342 // Since this ObjCInterfaceDecl was created by a forward declaration,
343 // we now add it to the DeclContext since it wasn't added before
344 // (see ActOnForwardClassDeclaration).
345 IDecl->setLexicalDeclContext(CurContext);
346 CurContext->addDecl(IDecl);
347
348 if (AttrList)
349 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000350 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000351 } else {
352 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
353 ClassName, ClassLoc);
354 if (AttrList)
355 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
356
357 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattner4d391482007-12-12 07:09:47 +0000360 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000361 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000362 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
363 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000364
365 if (!PrevDecl) {
366 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000367 TypoCorrection Corrected = CorrectTypo(
368 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
369 NULL, NULL, false, CTC_NoKeywords);
370 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000371 Diag(SuperLoc, diag::err_undef_superclass_suggest)
372 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000373 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
374 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000375 }
376 }
377
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000378 if (PrevDecl == IDecl) {
379 Diag(SuperLoc, diag::err_recursive_superclass)
380 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
381 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000382 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000383 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000384 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000385
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000386 // Diagnose classes that inherit from deprecated classes.
387 if (SuperClassDecl)
388 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000390 if (PrevDecl && SuperClassDecl == 0) {
391 // The previous declaration was not a class decl. Check if we have a
392 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000393 if (const TypedefNameDecl *TDecl =
394 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000395 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000396 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000397 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
398 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000399 }
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000402 // This handles the following case:
403 //
404 // typedef int SuperClass;
405 // @interface MyClass : SuperClass {} @end
406 //
407 if (!SuperClassDecl) {
408 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
409 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000410 }
411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Richard Smith162e1c12011-04-15 14:24:37 +0000413 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000414 if (!SuperClassDecl)
415 Diag(SuperLoc, diag::err_undef_superclass)
416 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000417 else if (SuperClassDecl->isForwardDecl()) {
418 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000419 << SuperClassDecl->getDeclName() << ClassName
420 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000421 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
422 SuperClassDecl = 0;
423 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000424 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000425 IDecl->setSuperClass(SuperClassDecl);
426 IDecl->setSuperClassLoc(SuperLoc);
427 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000428 }
Chris Lattner4d391482007-12-12 07:09:47 +0000429 } else { // we have a root class.
430 IDecl->setLocEnd(ClassLoc);
431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Sebastian Redl0b17c612010-08-13 00:28:03 +0000433 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000434 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000435 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000436 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000437 IDecl->setLocEnd(EndProtoLoc);
438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Anders Carlsson15281452008-11-04 16:57:32 +0000440 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000441 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000442}
443
444/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000445/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000446Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
447 IdentifierInfo *AliasName,
448 SourceLocation AliasLocation,
449 IdentifierInfo *ClassName,
450 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000451 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000452 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000453 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000454 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000455 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000456 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000457 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000458 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000459 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000460 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000461 }
462 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000463 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000464 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000465 if (const TypedefNameDecl *TDecl =
466 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000467 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000468 if (T->isObjCObjectType()) {
469 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000470 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000471 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000472 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000473 }
474 }
475 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000476 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
477 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000478 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000479 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000480 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000481 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000484 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000485 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000486 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Anders Carlsson15281452008-11-04 16:57:32 +0000488 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000489 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000490
John McCalld226f652010-08-21 09:40:31 +0000491 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000492}
493
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000494bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000495 IdentifierInfo *PName,
496 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000497 const ObjCList<ObjCProtocolDecl> &PList) {
498
499 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000500 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
501 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000502 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
503 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000504 if (PDecl->getIdentifier() == PName) {
505 Diag(Ploc, diag::err_protocol_has_circular_dependency);
506 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000507 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000508 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000509 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
510 PDecl->getLocation(), PDecl->getReferencedProtocols()))
511 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000512 }
513 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000514 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000515}
516
John McCalld226f652010-08-21 09:40:31 +0000517Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000518Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
519 IdentifierInfo *ProtocolName,
520 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000521 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000522 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000523 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000524 SourceLocation EndProtoLoc,
525 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000526 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000527 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000528 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000529 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000530 if (PDecl) {
531 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000532 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000533 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000534 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000535 // Just return the protocol we already had.
536 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000537 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000538 }
Steve Naroff61d68522009-03-05 15:22:01 +0000539 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000540 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000541 err = CheckForwardProtocolDeclarationForCircularDependency(
542 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Steve Narofff11b5082008-08-13 16:39:22 +0000544 // Make sure the cached decl gets a valid start location.
545 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000546 PDecl->setForwardDecl(false);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000547 // Since this ObjCProtocolDecl was created by a forward declaration,
548 // we now add it to the DeclContext since it wasn't added before
549 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000550 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000551 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000552 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000553 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000554 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000555 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000556 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000557 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000558 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000559 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000560 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000561 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000562 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000563 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
564 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000565 PDecl->setLocEnd(EndProtoLoc);
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
568 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000569 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000570}
571
572/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000573/// issues an error if they are not declared. It returns list of
574/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000575void
Chris Lattnere13b9592008-07-26 04:03:38 +0000576Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000577 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000578 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000579 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000580 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000581 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
582 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000583 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000584 TypoCorrection Corrected = CorrectTypo(
585 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
586 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
587 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000588 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000589 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000590 Diag(PDecl->getLocation(), diag::note_previous_decl)
591 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000592 }
593 }
594
595 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000596 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000597 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000598 continue;
599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000601 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000602
603 // If this is a forward declaration and we are supposed to warn in this
604 // case, do it.
605 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000606 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000607 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000608 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000609 }
610}
611
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000612/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000613/// a class method in its extension.
614///
Mike Stump1eb44332009-09-09 15:08:12 +0000615void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000616 ObjCInterfaceDecl *ID) {
617 if (!ID)
618 return; // Possibly due to previous error
619
620 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000621 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
622 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000623 ObjCMethodDecl *MD = *i;
624 MethodMap[MD->getSelector()] = MD;
625 }
626
627 if (MethodMap.empty())
628 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000629 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
630 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000631 ObjCMethodDecl *Method = *i;
632 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
633 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
634 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
635 << Method->getDeclName();
636 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
637 }
638 }
639}
640
Chris Lattner58fe03b2009-04-12 08:43:13 +0000641/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000642Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000643Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000644 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000645 unsigned NumElts,
646 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000647 SmallVector<ObjCProtocolDecl*, 32> Protocols;
648 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Chris Lattner4d391482007-12-12 07:09:47 +0000650 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000651 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000652 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000653 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000654 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000655 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000656 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000657 PushOnScopeChains(PDecl, TUScope, false);
658 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000659 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000660 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000661 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000662 if (!isNew)
663 PDecl->setChangedSinceDeserialization(true);
664 }
Chris Lattner4d391482007-12-12 07:09:47 +0000665 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000666 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
669 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000670 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000671 Protocols.data(), Protocols.size(),
672 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000673 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000674 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000675 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000676}
677
John McCalld226f652010-08-21 09:40:31 +0000678Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000679ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
680 IdentifierInfo *ClassName, SourceLocation ClassLoc,
681 IdentifierInfo *CategoryName,
682 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000683 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000684 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000685 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000686 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000687 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000688 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000689
690 /// Check that class of this category is already completely declared.
691 if (!IDecl || IDecl->isForwardDecl()) {
692 // Create an invalid ObjCCategoryDecl to serve as context for
693 // the enclosing method declarations. We mark the decl invalid
694 // to make it clear that this isn't a valid AST.
695 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
696 ClassLoc, CategoryLoc, CategoryName);
697 CDecl->setInvalidDecl();
698 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000699 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000700 }
701
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000702 if (!CategoryName && IDecl->getImplementation()) {
703 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
704 Diag(IDecl->getImplementation()->getLocation(),
705 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000706 }
707
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000708 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
709 ClassLoc, CategoryLoc, CategoryName);
710 // FIXME: PushOnScopeChains?
711 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000712
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000713 CDecl->setClassInterface(IDecl);
714 // Insert class extension to the list of class's categories.
715 if (!CategoryName)
716 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattner16b34b42009-02-16 21:30:01 +0000718 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000719 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000720
Fariborz Jahanian25760612010-02-15 21:55:26 +0000721 if (CategoryName) {
722 /// Check for duplicate interface declaration for this category
723 ObjCCategoryDecl *CDeclChain;
724 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
725 CDeclChain = CDeclChain->getNextClassCategory()) {
726 if (CDeclChain->getIdentifier() == CategoryName) {
727 // Class extensions can be declared multiple times.
728 Diag(CategoryLoc, diag::warn_dup_category_def)
729 << ClassName << CategoryName;
730 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
731 break;
732 }
Chris Lattner70f19542009-02-16 21:26:43 +0000733 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000734 if (!CDeclChain)
735 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000736 }
Chris Lattner70f19542009-02-16 21:26:43 +0000737
Chris Lattner4d391482007-12-12 07:09:47 +0000738 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000739 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000740 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000741 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000742 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000743 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000744 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Anders Carlsson15281452008-11-04 16:57:32 +0000747 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000748 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000749}
750
751/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000752/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000753/// object.
John McCalld226f652010-08-21 09:40:31 +0000754Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000755 SourceLocation AtCatImplLoc,
756 IdentifierInfo *ClassName, SourceLocation ClassLoc,
757 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000758 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000759 ObjCCategoryDecl *CatIDecl = 0;
760 if (IDecl) {
761 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
762 if (!CatIDecl) {
763 // Category @implementation with no corresponding @interface.
764 // Create and install one.
765 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000766 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000767 CatName);
768 CatIDecl->setClassInterface(IDecl);
769 CatIDecl->insertNextClassCategory();
770 }
771 }
772
Mike Stump1eb44332009-09-09 15:08:12 +0000773 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000774 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
775 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000776 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000777 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000778 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000779 CDecl->setInvalidDecl();
780 }
Chris Lattner4d391482007-12-12 07:09:47 +0000781
Douglas Gregord0434102009-01-09 00:49:46 +0000782 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000783 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000784
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000785 /// Check that CatName, category name, is not used in another implementation.
786 if (CatIDecl) {
787 if (CatIDecl->getImplementation()) {
788 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
789 << CatName;
790 Diag(CatIDecl->getImplementation()->getLocation(),
791 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000792 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000793 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000794 // Warn on implementating category of deprecated class under
795 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000796 DiagnoseObjCImplementedDeprecations(*this,
797 dyn_cast<NamedDecl>(IDecl),
798 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000799 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000800 }
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Anders Carlsson15281452008-11-04 16:57:32 +0000802 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000803 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000804}
805
John McCalld226f652010-08-21 09:40:31 +0000806Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000807 SourceLocation AtClassImplLoc,
808 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000809 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000810 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000811 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000812 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000813 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000814 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
815 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000816 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000817 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000818 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000819 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
820 // If this is a forward declaration of an interface, warn.
821 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000822 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000823 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000824 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000825 } else {
826 // We did not find anything with the name ClassName; try to correct for
827 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000828 TypoCorrection Corrected = CorrectTypo(
829 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
830 NULL, NULL, false, CTC_NoKeywords);
831 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000832 // Suggest the (potentially) correct interface name. However, put the
833 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000834 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000835 // provide a code-modification hint or use the typo name for recovery,
836 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000837 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000838 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000839 << ClassName << CorrectedName;
840 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
841 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000842 IDecl = 0;
843 } else {
844 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
845 }
Chris Lattner4d391482007-12-12 07:09:47 +0000846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattner4d391482007-12-12 07:09:47 +0000848 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000849 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000850 if (SuperClassname) {
851 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000852 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
853 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000854 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000855 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
856 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000857 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000858 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000859 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000860 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000861 Diag(SuperClassLoc, diag::err_undef_superclass)
862 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000863 else if (IDecl && IDecl->getSuperClass() != SDecl) {
864 // This implementation and its interface do not have the same
865 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000866 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000867 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000868 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000869 }
870 }
871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Chris Lattner4d391482007-12-12 07:09:47 +0000873 if (!IDecl) {
874 // Legacy case of @implementation with no corresponding @interface.
875 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000876
Mike Stump390b4cc2009-05-16 07:39:55 +0000877 // FIXME: Do we support attributes on the @implementation? If so we should
878 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000879 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000880 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000881 IDecl->setSuperClass(SDecl);
882 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000883
884 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000885 } else {
886 // Mark the interface as being completed, even if it was just as
887 // @class ....;
888 // declaration; the user cannot reopen it.
889 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891
892 ObjCImplementationDecl* IMPDecl =
893 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000894 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Anders Carlsson15281452008-11-04 16:57:32 +0000896 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000897 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Chris Lattner4d391482007-12-12 07:09:47 +0000899 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000900 if (IDecl->getImplementation()) {
901 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000902 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000903 Diag(IDecl->getImplementation()->getLocation(),
904 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000905 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000906 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000907 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000908 // Warn on implementating deprecated class under
909 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000910 DiagnoseObjCImplementedDeprecations(*this,
911 dyn_cast<NamedDecl>(IDecl),
912 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000913 }
John McCalld226f652010-08-21 09:40:31 +0000914 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000915}
916
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000917void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
918 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000919 SourceLocation RBrace) {
920 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000921 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000922 if (!IDecl)
923 return;
924 /// Check case of non-existing @interface decl.
925 /// (legacy objective-c @implementation decl without an @interface decl).
926 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000927 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000928 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000929 // Add ivar's to class's DeclContext.
930 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000931 ivars[i]->setLexicalDeclContext(ImpDecl);
932 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000933 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000934 }
935
Chris Lattner4d391482007-12-12 07:09:47 +0000936 return;
937 }
938 // If implementation has empty ivar list, just return.
939 if (numIvars == 0)
940 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Chris Lattner4d391482007-12-12 07:09:47 +0000942 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000943 if (LangOpts.ObjCNonFragileABI2) {
944 if (ImpDecl->getSuperClass())
945 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
946 for (unsigned i = 0; i < numIvars; i++) {
947 ObjCIvarDecl* ImplIvar = ivars[i];
948 if (const ObjCIvarDecl *ClsIvar =
949 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
950 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
951 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
952 continue;
953 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000954 // Instance ivar to Implementation's DeclContext.
955 ImplIvar->setLexicalDeclContext(ImpDecl);
956 IDecl->makeDeclVisibleInContext(ImplIvar, false);
957 ImpDecl->addDecl(ImplIvar);
958 }
959 return;
960 }
Chris Lattner4d391482007-12-12 07:09:47 +0000961 // Check interface's Ivar list against those in the implementation.
962 // names and types must match.
963 //
Chris Lattner4d391482007-12-12 07:09:47 +0000964 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000965 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000966 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
967 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000968 ObjCIvarDecl* ImplIvar = ivars[j++];
969 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000970 assert (ImplIvar && "missing implementation ivar");
971 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Steve Naroffca331292009-03-03 14:49:36 +0000973 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000974 if (Context.getCanonicalType(ImplIvar->getType()) !=
975 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000976 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000977 << ImplIvar->getIdentifier()
978 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000979 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000980 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
981 Expr *ImplBitWidth = ImplIvar->getBitWidth();
982 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000983 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
984 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000985 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
986 << ImplIvar->getIdentifier();
987 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989 }
Steve Naroffca331292009-03-03 14:49:36 +0000990 // Make sure the names are identical.
991 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000992 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000993 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000994 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000995 }
996 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Chris Lattner609e4c72007-12-12 18:11:49 +0000999 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001000 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001001 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001002 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001003}
1004
Steve Naroff3c2eb662008-02-10 21:38:56 +00001005void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001006 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001007 // No point warning no definition of method which is 'unavailable'.
1008 if (method->hasAttr<UnavailableAttr>())
1009 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001010 if (!IncompleteImpl) {
1011 Diag(ImpLoc, diag::warn_incomplete_impl);
1012 IncompleteImpl = true;
1013 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001014 if (DiagID == diag::warn_unimplemented_protocol_method)
1015 Diag(ImpLoc, DiagID) << method->getDeclName();
1016 else
1017 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001018}
1019
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001020/// Determines if type B can be substituted for type A. Returns true if we can
1021/// guarantee that anything that the user will do to an object of type A can
1022/// also be done to an object of type B. This is trivially true if the two
1023/// types are the same, or if B is a subclass of A. It becomes more complex
1024/// in cases where protocols are involved.
1025///
1026/// Object types in Objective-C describe the minimum requirements for an
1027/// object, rather than providing a complete description of a type. For
1028/// example, if A is a subclass of B, then B* may refer to an instance of A.
1029/// The principle of substitutability means that we may use an instance of A
1030/// anywhere that we may use an instance of B - it will implement all of the
1031/// ivars of B and all of the methods of B.
1032///
1033/// This substitutability is important when type checking methods, because
1034/// the implementation may have stricter type definitions than the interface.
1035/// The interface specifies minimum requirements, but the implementation may
1036/// have more accurate ones. For example, a method may privately accept
1037/// instances of B, but only publish that it accepts instances of A. Any
1038/// object passed to it will be type checked against B, and so will implicitly
1039/// by a valid A*. Similarly, a method may return a subclass of the class that
1040/// it is declared as returning.
1041///
1042/// This is most important when considering subclassing. A method in a
1043/// subclass must accept any object as an argument that its superclass's
1044/// implementation accepts. It may, however, accept a more general type
1045/// without breaking substitutability (i.e. you can still use the subclass
1046/// anywhere that you can use the superclass, but not vice versa). The
1047/// converse requirement applies to return types: the return type for a
1048/// subclass method must be a valid object of the kind that the superclass
1049/// advertises, but it may be specified more accurately. This avoids the need
1050/// for explicit down-casting by callers.
1051///
1052/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001053static bool isObjCTypeSubstitutable(ASTContext &Context,
1054 const ObjCObjectPointerType *A,
1055 const ObjCObjectPointerType *B,
1056 bool rejectId) {
1057 // Reject a protocol-unqualified id.
1058 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001059
1060 // If B is a qualified id, then A must also be a qualified id and it must
1061 // implement all of the protocols in B. It may not be a qualified class.
1062 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1063 // stricter definition so it is not substitutable for id<A>.
1064 if (B->isObjCQualifiedIdType()) {
1065 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001066 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1067 QualType(B,0),
1068 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001069 }
1070
1071 /*
1072 // id is a special type that bypasses type checking completely. We want a
1073 // warning when it is used in one place but not another.
1074 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1075
1076
1077 // If B is a qualified id, then A must also be a qualified id (which it isn't
1078 // if we've got this far)
1079 if (B->isObjCQualifiedIdType()) return false;
1080 */
1081
1082 // Now we know that A and B are (potentially-qualified) class types. The
1083 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001084 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001085}
1086
John McCall10302c02010-10-28 02:34:38 +00001087static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1088 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1089}
1090
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001091static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001092 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001093 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001094 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001095 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001096 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001097 if (IsProtocolMethodDecl &&
1098 (MethodDecl->getObjCDeclQualifier() !=
1099 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001100 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001101 S.Diag(MethodImpl->getLocation(),
1102 (IsOverridingMode ?
1103 diag::warn_conflicting_overriding_ret_type_modifiers
1104 : diag::warn_conflicting_ret_type_modifiers))
1105 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001106 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1107 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1108 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1109 }
1110 else
1111 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001112 }
1113
John McCall10302c02010-10-28 02:34:38 +00001114 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001115 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001116 return true;
1117 if (!Warn)
1118 return false;
John McCall10302c02010-10-28 02:34:38 +00001119
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001120 unsigned DiagID =
1121 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1122 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001123
1124 // Mismatches between ObjC pointers go into a different warning
1125 // category, and sometimes they're even completely whitelisted.
1126 if (const ObjCObjectPointerType *ImplPtrTy =
1127 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1128 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001129 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001130 // Allow non-matching return types as long as they don't violate
1131 // the principle of substitutability. Specifically, we permit
1132 // return types that are subclasses of the declared return type,
1133 // or that are more-qualified versions of the declared type.
1134 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001135 return false;
John McCall10302c02010-10-28 02:34:38 +00001136
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001137 DiagID =
1138 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1139 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001140 }
1141 }
1142
1143 S.Diag(MethodImpl->getLocation(), DiagID)
1144 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001145 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001146 << MethodImpl->getResultType()
1147 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001148 S.Diag(MethodDecl->getLocation(),
1149 IsOverridingMode ? diag::note_previous_declaration
1150 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001151 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001152 return false;
John McCall10302c02010-10-28 02:34:38 +00001153}
1154
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001155static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001156 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001157 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001158 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001159 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001160 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001161 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001162 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001163 if (IsProtocolMethodDecl &&
1164 (ImplVar->getObjCDeclQualifier() !=
1165 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001166 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001167 if (IsOverridingMode)
1168 S.Diag(ImplVar->getLocation(),
1169 diag::warn_conflicting_overriding_param_modifiers)
1170 << getTypeRange(ImplVar->getTypeSourceInfo())
1171 << MethodImpl->getDeclName();
1172 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001173 diag::warn_conflicting_param_modifiers)
1174 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001175 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001176 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1177 << getTypeRange(IfaceVar->getTypeSourceInfo());
1178 }
1179 else
1180 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001181 }
1182
John McCall10302c02010-10-28 02:34:38 +00001183 QualType ImplTy = ImplVar->getType();
1184 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001185
John McCall10302c02010-10-28 02:34:38 +00001186 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001187 return true;
1188
1189 if (!Warn)
1190 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001191 unsigned DiagID =
1192 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1193 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001194
1195 // Mismatches between ObjC pointers go into a different warning
1196 // category, and sometimes they're even completely whitelisted.
1197 if (const ObjCObjectPointerType *ImplPtrTy =
1198 ImplTy->getAs<ObjCObjectPointerType>()) {
1199 if (const ObjCObjectPointerType *IfacePtrTy =
1200 IfaceTy->getAs<ObjCObjectPointerType>()) {
1201 // Allow non-matching argument types as long as they don't
1202 // violate the principle of substitutability. Specifically, the
1203 // implementation must accept any objects that the superclass
1204 // accepts, however it may also accept others.
1205 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001206 return false;
John McCall10302c02010-10-28 02:34:38 +00001207
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001208 DiagID =
1209 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1210 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001211 }
1212 }
1213
1214 S.Diag(ImplVar->getLocation(), DiagID)
1215 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001216 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1217 S.Diag(IfaceVar->getLocation(),
1218 (IsOverridingMode ? diag::note_previous_declaration
1219 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001220 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001221 return false;
John McCall10302c02010-10-28 02:34:38 +00001222}
John McCallf85e1932011-06-15 23:02:42 +00001223
1224/// In ARC, check whether the conventional meanings of the two methods
1225/// match. If they don't, it's a hard error.
1226static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1227 ObjCMethodDecl *decl) {
1228 ObjCMethodFamily implFamily = impl->getMethodFamily();
1229 ObjCMethodFamily declFamily = decl->getMethodFamily();
1230 if (implFamily == declFamily) return false;
1231
1232 // Since conventions are sorted by selector, the only possibility is
1233 // that the types differ enough to cause one selector or the other
1234 // to fall out of the family.
1235 assert(implFamily == OMF_None || declFamily == OMF_None);
1236
1237 // No further diagnostics required on invalid declarations.
1238 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1239
1240 const ObjCMethodDecl *unmatched = impl;
1241 ObjCMethodFamily family = declFamily;
1242 unsigned errorID = diag::err_arc_lost_method_convention;
1243 unsigned noteID = diag::note_arc_lost_method_convention;
1244 if (declFamily == OMF_None) {
1245 unmatched = decl;
1246 family = implFamily;
1247 errorID = diag::err_arc_gained_method_convention;
1248 noteID = diag::note_arc_gained_method_convention;
1249 }
1250
1251 // Indexes into a %select clause in the diagnostic.
1252 enum FamilySelector {
1253 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1254 };
1255 FamilySelector familySelector = FamilySelector();
1256
1257 switch (family) {
1258 case OMF_None: llvm_unreachable("logic error, no method convention");
1259 case OMF_retain:
1260 case OMF_release:
1261 case OMF_autorelease:
1262 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001263 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001264 case OMF_retainCount:
1265 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001266 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001267 // Mismatches for these methods don't change ownership
1268 // conventions, so we don't care.
1269 return false;
1270
1271 case OMF_init: familySelector = F_init; break;
1272 case OMF_alloc: familySelector = F_alloc; break;
1273 case OMF_copy: familySelector = F_copy; break;
1274 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1275 case OMF_new: familySelector = F_new; break;
1276 }
1277
1278 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1279 ReasonSelector reasonSelector;
1280
1281 // The only reason these methods don't fall within their families is
1282 // due to unusual result types.
1283 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1284 reasonSelector = R_UnrelatedReturn;
1285 } else {
1286 reasonSelector = R_NonObjectReturn;
1287 }
1288
1289 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1290 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1291
1292 return true;
1293}
John McCall10302c02010-10-28 02:34:38 +00001294
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001295void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001296 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001297 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001298 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001299 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001300 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001301 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1302 return;
1303
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001304 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001305 IsProtocolMethodDecl, IsOverridingMode,
1306 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Chris Lattner3aff9192009-04-11 19:58:42 +00001308 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001309 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001310 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001311 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1312 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001313 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001314
Fariborz Jahanian21121902011-08-08 18:03:17 +00001315 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001316 if (IsOverridingMode)
1317 Diag(ImpMethodDecl->getLocation(),
1318 diag::warn_conflicting_overriding_variadic);
1319 else
1320 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001321 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001322 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001323}
1324
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001325/// WarnExactTypedMethods - This routine issues a warning if method
1326/// implementation declaration matches exactly that of its declaration.
1327void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1328 ObjCMethodDecl *MethodDecl,
1329 bool IsProtocolMethodDecl) {
1330 // don't issue warning when protocol method is optional because primary
1331 // class is not required to implement it and it is safe for protocol
1332 // to implement it.
1333 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1334 return;
1335 // don't issue warning when primary class's method is
1336 // depecated/unavailable.
1337 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1338 MethodDecl->hasAttr<DeprecatedAttr>())
1339 return;
1340
1341 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1342 IsProtocolMethodDecl, false, false);
1343 if (match)
1344 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1345 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1346 IM != EM; ++IM, ++IF) {
1347 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1348 *IM, *IF,
1349 IsProtocolMethodDecl, false, false);
1350 if (!match)
1351 break;
1352 }
1353 if (match)
1354 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001355 if (match)
1356 match = !(MethodDecl->isClassMethod() &&
1357 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001358
1359 if (match) {
1360 Diag(ImpMethodDecl->getLocation(),
1361 diag::warn_category_method_impl_match);
1362 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1363 }
1364}
1365
Mike Stump390b4cc2009-05-16 07:39:55 +00001366/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1367/// improve the efficiency of selector lookups and type checking by associating
1368/// with each protocol / interface / category the flattened instance tables. If
1369/// we used an immutable set to keep the table then it wouldn't add significant
1370/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001371
Steve Naroffefe7f362008-02-08 22:06:17 +00001372/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001373/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001374void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1375 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001376 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001377 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001378 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001379 ObjCContainerDecl *CDecl) {
1380 ObjCInterfaceDecl *IDecl;
1381 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1382 IDecl = C->getClassInterface();
1383 else
1384 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1385 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1386
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001387 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001388 ObjCInterfaceDecl *NSIDecl = 0;
1389 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001390 // check to see if class implements forwardInvocation method and objects
1391 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001392 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001393 // Under such conditions, which means that every method possible is
1394 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001395 // found" warnings.
1396 // FIXME: Use a general GetUnarySelector method for this.
1397 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1398 Selector fISelector = Context.Selectors.getSelector(1, &II);
1399 if (InsMap.count(fISelector))
1400 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1401 // need be implemented in the implementation.
1402 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1403 }
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001405 // If a method lookup fails locally we still need to look and see if
1406 // the method was implemented by a base class or an inherited
1407 // protocol. This lookup is slow, but occurs rarely in correct code
1408 // and otherwise would terminate in a warning.
1409
Chris Lattner4d391482007-12-12 07:09:47 +00001410 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001411 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001412 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001413 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001414 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001415 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001416 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001417 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001418 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001419 // Ugly, but necessary. Method declared in protcol might have
1420 // have been synthesized due to a property declared in the class which
1421 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001422 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001423 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001424 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001425 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001426 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1427 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001428 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001429 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001430 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1431 << PDecl->getDeclName();
1432 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001433 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001434 }
1435 }
Chris Lattner4d391482007-12-12 07:09:47 +00001436 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001437 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001438 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001439 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001440 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001441 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1442 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001443 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001444 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001445 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001446 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001447 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001448 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1449 PDecl->getDeclName();
1450 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001451 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001452 }
Chris Lattner780f3292008-07-21 21:32:27 +00001453 // Check on this protocols's referenced protocols, recursively.
1454 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1455 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001456 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001457}
1458
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001459/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001460/// or protocol against those declared in their implementations.
1461///
1462void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1463 const llvm::DenseSet<Selector> &ClsMap,
1464 llvm::DenseSet<Selector> &InsMapSeen,
1465 llvm::DenseSet<Selector> &ClsMapSeen,
1466 ObjCImplDecl* IMPDecl,
1467 ObjCContainerDecl* CDecl,
1468 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001469 bool ImmediateClass,
1470 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001471 // Check and see if instance methods in class interface have been
1472 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001473 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1474 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001475 if (InsMapSeen.count((*I)->getSelector()))
1476 continue;
1477 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001478 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001479 !InsMap.count((*I)->getSelector())) {
1480 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001481 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1482 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001483 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001484 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001485 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001486 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001487 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001488 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001489 assert(MethodDecl &&
1490 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001491 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001492 if (ImpMethodDecl) {
1493 if (!WarnExactMatch)
1494 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1495 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001496 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001497 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1498 isa<ObjCProtocolDecl>(CDecl));
1499 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001500 }
1501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001503 // Check and see if class methods in class interface have been
1504 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001505 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001506 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001507 if (ClsMapSeen.count((*I)->getSelector()))
1508 continue;
1509 ClsMapSeen.insert((*I)->getSelector());
1510 if (!ClsMap.count((*I)->getSelector())) {
1511 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001512 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1513 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001514 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001515 ObjCMethodDecl *ImpMethodDecl =
1516 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001517 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001518 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001519 if (!WarnExactMatch)
1520 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1521 isa<ObjCProtocolDecl>(CDecl));
1522 else
1523 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1524 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001525 }
1526 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001527
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001528 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001529 // Also methods in class extensions need be looked at next.
1530 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1531 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1532 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1533 IMPDecl,
1534 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001535 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001536
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001537 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001538 for (ObjCInterfaceDecl::all_protocol_iterator
1539 PI = I->all_referenced_protocol_begin(),
1540 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001541 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1542 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001543 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001544
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001545 // FIXME. For now, we are not checking for extact match of methods
1546 // in category implementation and its primary class's super class.
1547 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001548 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001549 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001550 I->getSuperClass(), IncompleteImpl, false);
1551 }
1552}
1553
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001554/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1555/// category matches with those implemented in its primary class and
1556/// warns each time an exact match is found.
1557void Sema::CheckCategoryVsClassMethodMatches(
1558 ObjCCategoryImplDecl *CatIMPDecl) {
1559 llvm::DenseSet<Selector> InsMap, ClsMap;
1560
1561 for (ObjCImplementationDecl::instmeth_iterator
1562 I = CatIMPDecl->instmeth_begin(),
1563 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1564 InsMap.insert((*I)->getSelector());
1565
1566 for (ObjCImplementationDecl::classmeth_iterator
1567 I = CatIMPDecl->classmeth_begin(),
1568 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1569 ClsMap.insert((*I)->getSelector());
1570 if (InsMap.empty() && ClsMap.empty())
1571 return;
1572
1573 // Get category's primary class.
1574 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1575 if (!CatDecl)
1576 return;
1577 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1578 if (!IDecl)
1579 return;
1580 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1581 bool IncompleteImpl = false;
1582 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1583 CatIMPDecl, IDecl,
1584 IncompleteImpl, false, true /*WarnExactMatch*/);
1585}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001586
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001587void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001588 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001589 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001590 llvm::DenseSet<Selector> InsMap;
1591 // Check and see if instance methods in class interface have been
1592 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001593 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001594 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001595 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001597 // Check and see if properties declared in the interface have either 1)
1598 // an implementation or 2) there is a @synthesize/@dynamic implementation
1599 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001600 if (isa<ObjCInterfaceDecl>(CDecl) &&
1601 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001602 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001603
Chris Lattner4d391482007-12-12 07:09:47 +00001604 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001605 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001606 I = IMPDecl->classmeth_begin(),
1607 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001608 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001610 // Check for type conflict of methods declared in a class/protocol and
1611 // its implementation; if any.
1612 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001613 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1614 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001615 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001616
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001617 // check all methods implemented in category against those declared
1618 // in its primary class.
1619 if (ObjCCategoryImplDecl *CatDecl =
1620 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1621 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Chris Lattner4d391482007-12-12 07:09:47 +00001623 // Check the protocol list for unimplemented methods in the @implementation
1624 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001625 // Check and see if class methods in class interface have been
1626 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Chris Lattnercddc8882009-03-01 00:56:52 +00001628 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001629 for (ObjCInterfaceDecl::all_protocol_iterator
1630 PI = I->all_referenced_protocol_begin(),
1631 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001632 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001633 InsMap, ClsMap, I);
1634 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001635 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1636 Categories; Categories = Categories->getNextClassExtension())
1637 ImplMethodsVsClassMethods(S, IMPDecl,
1638 const_cast<ObjCCategoryDecl*>(Categories),
1639 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001640 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001641 // For extended class, unimplemented methods in its protocols will
1642 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001643 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001644 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1645 E = C->protocol_end(); PI != E; ++PI)
1646 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001647 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001648 // Report unimplemented properties in the category as well.
1649 // When reporting on missing setter/getters, do not report when
1650 // setter/getter is implemented in category's primary class
1651 // implementation.
1652 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1653 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1654 for (ObjCImplementationDecl::instmeth_iterator
1655 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1656 InsMap.insert((*I)->getSelector());
1657 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001658 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001659 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001660 } else
1661 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001662}
1663
Mike Stump1eb44332009-09-09 15:08:12 +00001664/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001665Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001666Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001667 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001668 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001669 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001670 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001671 for (unsigned i = 0; i != NumElts; ++i) {
1672 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001673 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001674 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001675 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001676 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001677 // Maybe we will complain about the shadowed template parameter.
1678 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1679 // Just pretend that we didn't see the previous declaration.
1680 PrevDecl = 0;
1681 }
1682
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001683 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001684 // GCC apparently allows the following idiom:
1685 //
1686 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1687 // @class XCElementToggler;
1688 //
Mike Stump1eb44332009-09-09 15:08:12 +00001689 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001690 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001691 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001692 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001693 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001694 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001695 // a forward class declaration matching a typedef name of a class refers
1696 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001697 if (const ObjCObjectType *OI =
1698 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1699 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001700 }
Chris Lattner4d391482007-12-12 07:09:47 +00001701 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001702 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1703 if (!IDecl) { // Not already seen? Make a forward decl.
1704 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1705 IdentList[i], IdentLocs[i], true);
1706
1707 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1708 // the current DeclContext. This prevents clients that walk DeclContext
1709 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1710 // declared later (if at all). We also take care to explicitly make
1711 // sure this declaration is visible for name lookup.
1712 PushOnScopeChains(IDecl, TUScope, false);
1713 CurContext->makeDeclVisibleInContext(IDecl, true);
1714 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001715 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1716 IDecl, IdentLocs[i]);
1717 CurContext->addDecl(CDecl);
1718 CheckObjCDeclScope(CDecl);
1719 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001720 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001721
1722 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001723}
1724
John McCall0f4c4c42011-06-16 01:15:19 +00001725static bool tryMatchRecordTypes(ASTContext &Context,
1726 Sema::MethodMatchStrategy strategy,
1727 const Type *left, const Type *right);
1728
John McCallf85e1932011-06-15 23:02:42 +00001729static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1730 QualType leftQT, QualType rightQT) {
1731 const Type *left =
1732 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1733 const Type *right =
1734 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1735
1736 if (left == right) return true;
1737
1738 // If we're doing a strict match, the types have to match exactly.
1739 if (strategy == Sema::MMS_strict) return false;
1740
1741 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1742
1743 // Otherwise, use this absurdly complicated algorithm to try to
1744 // validate the basic, low-level compatibility of the two types.
1745
1746 // As a minimum, require the sizes and alignments to match.
1747 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1748 return false;
1749
1750 // Consider all the kinds of non-dependent canonical types:
1751 // - functions and arrays aren't possible as return and parameter types
1752
1753 // - vector types of equal size can be arbitrarily mixed
1754 if (isa<VectorType>(left)) return isa<VectorType>(right);
1755 if (isa<VectorType>(right)) return false;
1756
1757 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001758 // - structs, unions, and Objective-C objects must match more-or-less
1759 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001760 // - everything else should be a scalar
1761 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001762 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001763
1764 // Make scalars agree in kind, except count bools as chars.
1765 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1766 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1767 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1768 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1769
1770 // Note that data member pointers and function member pointers don't
1771 // intermix because of the size differences.
1772
1773 return (leftSK == rightSK);
1774}
Chris Lattner4d391482007-12-12 07:09:47 +00001775
John McCall0f4c4c42011-06-16 01:15:19 +00001776static bool tryMatchRecordTypes(ASTContext &Context,
1777 Sema::MethodMatchStrategy strategy,
1778 const Type *lt, const Type *rt) {
1779 assert(lt && rt && lt != rt);
1780
1781 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1782 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1783 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1784
1785 // Require union-hood to match.
1786 if (left->isUnion() != right->isUnion()) return false;
1787
1788 // Require an exact match if either is non-POD.
1789 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1790 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1791 return false;
1792
1793 // Require size and alignment to match.
1794 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1795
1796 // Require fields to match.
1797 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1798 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1799 for (; li != le && ri != re; ++li, ++ri) {
1800 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1801 return false;
1802 }
1803 return (li == le && ri == re);
1804}
1805
Chris Lattner4d391482007-12-12 07:09:47 +00001806/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1807/// returns true, or false, accordingly.
1808/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001809bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1810 const ObjCMethodDecl *right,
1811 MethodMatchStrategy strategy) {
1812 if (!matchTypes(Context, strategy,
1813 left->getResultType(), right->getResultType()))
1814 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001815
John McCallf85e1932011-06-15 23:02:42 +00001816 if (getLangOptions().ObjCAutoRefCount &&
1817 (left->hasAttr<NSReturnsRetainedAttr>()
1818 != right->hasAttr<NSReturnsRetainedAttr>() ||
1819 left->hasAttr<NSConsumesSelfAttr>()
1820 != right->hasAttr<NSConsumesSelfAttr>()))
1821 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001822
John McCallf85e1932011-06-15 23:02:42 +00001823 ObjCMethodDecl::param_iterator
1824 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001825
John McCallf85e1932011-06-15 23:02:42 +00001826 for (; li != le; ++li, ++ri) {
1827 assert(ri != right->param_end() && "Param mismatch");
1828 ParmVarDecl *lparm = *li, *rparm = *ri;
1829
1830 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1831 return false;
1832
1833 if (getLangOptions().ObjCAutoRefCount &&
1834 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1835 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001836 }
1837 return true;
1838}
1839
Sebastian Redldb9d2142010-08-02 23:18:59 +00001840/// \brief Read the contents of the method pool for a given selector from
1841/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001842///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001843/// This routine should only be called once, when the method pool has no entry
1844/// for this selector.
1845Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001846 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001847 assert(MethodPool.find(Sel) == MethodPool.end() &&
1848 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001849
1850 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001851 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Sebastian Redldb9d2142010-08-02 23:18:59 +00001853 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001854}
1855
Sebastian Redldb9d2142010-08-02 23:18:59 +00001856void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1857 bool instance) {
1858 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1859 if (Pos == MethodPool.end()) {
1860 if (ExternalSource)
1861 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001862 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001863 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1864 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001865 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001866 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001867 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001868 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001869 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001870 Entry.Method = Method;
1871 Entry.Next = 0;
1872 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001873 }
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Chris Lattnerb25df352009-03-04 05:16:45 +00001875 // We've seen a method with this name, see if we have already seen this type
1876 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001877 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1878 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1879
1880 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001881 ObjCMethodDecl *PrevObjCMethod = List->Method;
1882 PrevObjCMethod->setDefined(impl);
1883 // If a method is deprecated, push it in the global pool.
1884 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001885 if (Method->isDeprecated()) {
1886 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001887 List->Method = Method;
1888 }
1889 // If new method is unavailable, push it into global pool
1890 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001891 if (Method->isUnavailable()) {
1892 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001893 List->Method = Method;
1894 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001895 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001896 }
John McCallf85e1932011-06-15 23:02:42 +00001897 }
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Chris Lattnerb25df352009-03-04 05:16:45 +00001899 // We have a new signature for an existing method - add it.
1900 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001901 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1902 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001903}
1904
John McCallf85e1932011-06-15 23:02:42 +00001905/// Determines if this is an "acceptable" loose mismatch in the global
1906/// method pool. This exists mostly as a hack to get around certain
1907/// global mismatches which we can't afford to make warnings / errors.
1908/// Really, what we want is a way to take a method out of the global
1909/// method pool.
1910static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1911 ObjCMethodDecl *other) {
1912 if (!chosen->isInstanceMethod())
1913 return false;
1914
1915 Selector sel = chosen->getSelector();
1916 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1917 return false;
1918
1919 // Don't complain about mismatches for -length if the method we
1920 // chose has an integral result type.
1921 return (chosen->getResultType()->isIntegerType());
1922}
1923
Sebastian Redldb9d2142010-08-02 23:18:59 +00001924ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001925 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001926 bool warn, bool instance) {
1927 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1928 if (Pos == MethodPool.end()) {
1929 if (ExternalSource)
1930 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001931 else
1932 return 0;
1933 }
1934
Sebastian Redldb9d2142010-08-02 23:18:59 +00001935 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Sebastian Redldb9d2142010-08-02 23:18:59 +00001937 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001938 bool issueDiagnostic = false, issueError = false;
1939
1940 // We support a warning which complains about *any* difference in
1941 // method signature.
1942 bool strictSelectorMatch =
1943 (receiverIdOrClass && warn &&
1944 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1945 R.getBegin()) !=
1946 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001947 if (strictSelectorMatch)
1948 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001949 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1950 MMS_strict)) {
1951 issueDiagnostic = true;
1952 break;
1953 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001954 }
1955
John McCallf85e1932011-06-15 23:02:42 +00001956 // If we didn't see any strict differences, we won't see any loose
1957 // differences. In ARC, however, we also need to check for loose
1958 // mismatches, because most of them are errors.
1959 if (!strictSelectorMatch ||
1960 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001961 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001962 // This checks if the methods differ in type mismatch.
1963 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1964 MMS_loose) &&
1965 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1966 issueDiagnostic = true;
1967 if (getLangOptions().ObjCAutoRefCount)
1968 issueError = true;
1969 break;
1970 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001971 }
1972
John McCallf85e1932011-06-15 23:02:42 +00001973 if (issueDiagnostic) {
1974 if (issueError)
1975 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1976 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001977 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1978 else
1979 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001980
1981 Diag(MethList.Method->getLocStart(),
1982 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001983 << MethList.Method->getSourceRange();
1984 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1985 Diag(Next->Method->getLocStart(), diag::note_also_found)
1986 << Next->Method->getSourceRange();
1987 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001988 }
1989 return MethList.Method;
1990}
1991
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001992ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001993 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1994 if (Pos == MethodPool.end())
1995 return 0;
1996
1997 GlobalMethods &Methods = Pos->second;
1998
1999 if (Methods.first.Method && Methods.first.Method->isDefined())
2000 return Methods.first.Method;
2001 if (Methods.second.Method && Methods.second.Method->isDefined())
2002 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002003 return 0;
2004}
2005
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002006/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2007/// identical selector names in current and its super classes and issues
2008/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002009void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2010 ObjCMethodDecl *Method,
2011 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002012 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2013 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002015 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002016 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002017 SD->lookupMethod(Method->getSelector(), IsInstance);
2018 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002019 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002020 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002021 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002022 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2023 E = Method->param_end();
2024 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2025 for (; ParamI != E; ++ParamI, ++PrevI) {
2026 // Number of parameters are the same and is guaranteed by selector match.
2027 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2028 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2029 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002030 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002031 // respective argument type in the super class method, issue warning;
2032 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002033 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002034 << T1 << T2;
2035 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2036 return;
2037 }
2038 }
2039 ID = SD;
2040 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002041}
2042
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002043/// DiagnoseDuplicateIvars -
2044/// Check for duplicate ivars in the entire class at the start of
2045/// @implementation. This becomes necesssary because class extension can
2046/// add ivars to a class in random order which will not be known until
2047/// class's @implementation is seen.
2048void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2049 ObjCInterfaceDecl *SID) {
2050 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2051 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2052 ObjCIvarDecl* Ivar = (*IVI);
2053 if (Ivar->isInvalidDecl())
2054 continue;
2055 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2056 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2057 if (prevIvar) {
2058 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2059 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2060 Ivar->setInvalidDecl();
2061 }
2062 }
2063 }
2064}
2065
Steve Naroffa56f6162007-12-18 01:30:32 +00002066// Note: For class/category implemenations, allMethods/allProperties is
2067// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002068void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002069 Decl **allMethods, unsigned allNum,
2070 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002071 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002072
2073 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002074 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002075 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2076 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002077
Mike Stump1eb44332009-09-09 15:08:12 +00002078 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002079 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2080 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002081 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002082
Ted Kremenek782f2f52010-01-07 01:20:12 +00002083 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2084 // FIXME: This is wrong. We shouldn't be pretending that there is
2085 // an '@end' in the declaration.
2086 SourceLocation L = ClassDecl->getLocation();
2087 AtEnd.setBegin(L);
2088 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002089 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002090 }
2091
Steve Naroff0701bbb2009-01-08 17:28:14 +00002092 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2093 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2094 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2095
Chris Lattner4d391482007-12-12 07:09:47 +00002096 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002097 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002098 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002099
2100 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002101 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002102 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002103 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002104 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002105 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002106 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002107 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002108 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002109 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002110 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002111 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002112 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002113 InsMap[Method->getSelector()] = Method;
2114 /// The following allows us to typecheck messages to "id".
2115 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002116 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002117 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002118 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002119 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002120 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002121 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002122 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002123 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002124 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002125 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002126 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002127 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002128 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002129 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002130 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002131 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002132 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002133 /// The following allows us to typecheck messages to "Class".
2134 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002135 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002136 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002137 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002138 }
2139 }
2140 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002141 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002142 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002143 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002144 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002145 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002146 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002147 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002148 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002149 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002150
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002151 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002152 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002153 if (C->IsClassExtension()) {
2154 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2155 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002156 }
Chris Lattner4d391482007-12-12 07:09:47 +00002157 }
Steve Naroff09c47192009-01-09 15:36:25 +00002158 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002159 if (CDecl->getIdentifier())
2160 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2161 // user-defined setter/getter. It also synthesizes setter/getter methods
2162 // and adds them to the DeclContext and global method pools.
2163 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2164 E = CDecl->prop_end();
2165 I != E; ++I)
2166 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002167 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002168 }
2169 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002170 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002171 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002172 // Any property declared in a class extension might have user
2173 // declared setter or getter in current class extension or one
2174 // of the other class extensions. Mark them as synthesized as
2175 // property will be synthesized when property with same name is
2176 // seen in the @implementation.
2177 for (const ObjCCategoryDecl *ClsExtDecl =
2178 IDecl->getFirstClassExtension();
2179 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2180 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2181 E = ClsExtDecl->prop_end(); I != E; ++I) {
2182 ObjCPropertyDecl *Property = (*I);
2183 // Skip over properties declared @dynamic
2184 if (const ObjCPropertyImplDecl *PIDecl
2185 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2186 if (PIDecl->getPropertyImplementation()
2187 == ObjCPropertyImplDecl::Dynamic)
2188 continue;
2189
2190 for (const ObjCCategoryDecl *CExtDecl =
2191 IDecl->getFirstClassExtension();
2192 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2193 if (ObjCMethodDecl *GetterMethod =
2194 CExtDecl->getInstanceMethod(Property->getGetterName()))
2195 GetterMethod->setSynthesized(true);
2196 if (!Property->isReadOnly())
2197 if (ObjCMethodDecl *SetterMethod =
2198 CExtDecl->getInstanceMethod(Property->getSetterName()))
2199 SetterMethod->setSynthesized(true);
2200 }
2201 }
2202 }
2203
Ted Kremenekc32647d2010-12-23 21:35:43 +00002204 if (LangOpts.ObjCDefaultSynthProperties &&
2205 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002206 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002207 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002208 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002209 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002210
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002211 if (LangOpts.ObjCNonFragileABI2)
2212 while (IDecl->getSuperClass()) {
2213 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2214 IDecl = IDecl->getSuperClass();
2215 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002216 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002217 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002218 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002219 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002220 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002221
Chris Lattner4d391482007-12-12 07:09:47 +00002222 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002223 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002224 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002225 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002226 Categories; Categories = Categories->getNextClassCategory()) {
2227 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002228 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002229 break;
2230 }
2231 }
2232 }
2233 }
Chris Lattner682bf922009-03-29 16:50:03 +00002234 if (isInterfaceDeclKind) {
2235 // Reject invalid vardecls.
2236 for (unsigned i = 0; i != tuvNum; i++) {
2237 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2238 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2239 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002240 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002241 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002242 }
Chris Lattner682bf922009-03-29 16:50:03 +00002243 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002244 }
Chris Lattner4d391482007-12-12 07:09:47 +00002245}
2246
2247
2248/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2249/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002250static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002251CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002252 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002253}
2254
Ted Kremenek422bae72010-04-18 04:59:38 +00002255static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002256bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002257 // The 'ibaction' attribute is allowed on method definitions because of
2258 // how the IBAction macro is used on both method declarations and definitions.
2259 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002260 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2261 if ((*i)->getKind() != attr::IBAction)
2262 return true;
2263 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002264}
2265
Douglas Gregor926df6c2011-06-11 01:09:30 +00002266/// \brief Check whether the declared result type of the given Objective-C
2267/// method declaration is compatible with the method's class.
2268///
2269static bool
2270CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2271 ObjCInterfaceDecl *CurrentClass) {
2272 QualType ResultType = Method->getResultType();
2273 SourceRange ResultTypeRange;
2274 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2275 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2276
2277 // If an Objective-C method inherits its related result type, then its
2278 // declared result type must be compatible with its own class type. The
2279 // declared result type is compatible if:
2280 if (const ObjCObjectPointerType *ResultObjectType
2281 = ResultType->getAs<ObjCObjectPointerType>()) {
2282 // - it is id or qualified id, or
2283 if (ResultObjectType->isObjCIdType() ||
2284 ResultObjectType->isObjCQualifiedIdType())
2285 return false;
2286
2287 if (CurrentClass) {
2288 if (ObjCInterfaceDecl *ResultClass
2289 = ResultObjectType->getInterfaceDecl()) {
2290 // - it is the same as the method's class type, or
2291 if (CurrentClass == ResultClass)
2292 return false;
2293
2294 // - it is a superclass of the method's class type
2295 if (ResultClass->isSuperClassOf(CurrentClass))
2296 return false;
2297 }
2298 }
2299 }
2300
2301 return true;
2302}
2303
John McCall6c2c2502011-07-22 02:45:48 +00002304namespace {
2305/// A helper class for searching for methods which a particular method
2306/// overrides.
2307class OverrideSearch {
2308 Sema &S;
2309 ObjCMethodDecl *Method;
2310 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2311 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2312 bool Recursive;
2313
2314public:
2315 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2316 Selector selector = method->getSelector();
2317
2318 // Bypass this search if we've never seen an instance/class method
2319 // with this selector before.
2320 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2321 if (it == S.MethodPool.end()) {
2322 if (!S.ExternalSource) return;
2323 it = S.ReadMethodPool(selector);
2324 }
2325 ObjCMethodList &list =
2326 method->isInstanceMethod() ? it->second.first : it->second.second;
2327 if (!list.Method) return;
2328
2329 ObjCContainerDecl *container
2330 = cast<ObjCContainerDecl>(method->getDeclContext());
2331
2332 // Prevent the search from reaching this container again. This is
2333 // important with categories, which override methods from the
2334 // interface and each other.
2335 Searched.insert(container);
2336 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002337 }
John McCall6c2c2502011-07-22 02:45:48 +00002338
2339 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2340 iterator begin() const { return Overridden.begin(); }
2341 iterator end() const { return Overridden.end(); }
2342
2343private:
2344 void searchFromContainer(ObjCContainerDecl *container) {
2345 if (container->isInvalidDecl()) return;
2346
2347 switch (container->getDeclKind()) {
2348#define OBJCCONTAINER(type, base) \
2349 case Decl::type: \
2350 searchFrom(cast<type##Decl>(container)); \
2351 break;
2352#define ABSTRACT_DECL(expansion)
2353#define DECL(type, base) \
2354 case Decl::type:
2355#include "clang/AST/DeclNodes.inc"
2356 llvm_unreachable("not an ObjC container!");
2357 }
2358 }
2359
2360 void searchFrom(ObjCProtocolDecl *protocol) {
2361 // A method in a protocol declaration overrides declarations from
2362 // referenced ("parent") protocols.
2363 search(protocol->getReferencedProtocols());
2364 }
2365
2366 void searchFrom(ObjCCategoryDecl *category) {
2367 // A method in a category declaration overrides declarations from
2368 // the main class and from protocols the category references.
2369 search(category->getClassInterface());
2370 search(category->getReferencedProtocols());
2371 }
2372
2373 void searchFrom(ObjCCategoryImplDecl *impl) {
2374 // A method in a category definition that has a category
2375 // declaration overrides declarations from the category
2376 // declaration.
2377 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2378 search(category);
2379
2380 // Otherwise it overrides declarations from the class.
2381 } else {
2382 search(impl->getClassInterface());
2383 }
2384 }
2385
2386 void searchFrom(ObjCInterfaceDecl *iface) {
2387 // A method in a class declaration overrides declarations from
2388
2389 // - categories,
2390 for (ObjCCategoryDecl *category = iface->getCategoryList();
2391 category; category = category->getNextClassCategory())
2392 search(category);
2393
2394 // - the super class, and
2395 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2396 search(super);
2397
2398 // - any referenced protocols.
2399 search(iface->getReferencedProtocols());
2400 }
2401
2402 void searchFrom(ObjCImplementationDecl *impl) {
2403 // A method in a class implementation overrides declarations from
2404 // the class interface.
2405 search(impl->getClassInterface());
2406 }
2407
2408
2409 void search(const ObjCProtocolList &protocols) {
2410 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2411 i != e; ++i)
2412 search(*i);
2413 }
2414
2415 void search(ObjCContainerDecl *container) {
2416 // Abort if we've already searched this container.
2417 if (!Searched.insert(container)) return;
2418
2419 // Check for a method in this container which matches this selector.
2420 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2421 Method->isInstanceMethod());
2422
2423 // If we find one, record it and bail out.
2424 if (meth) {
2425 Overridden.insert(meth);
2426 return;
2427 }
2428
2429 // Otherwise, search for methods that a hypothetical method here
2430 // would have overridden.
2431
2432 // Note that we're now in a recursive case.
2433 Recursive = true;
2434
2435 searchFromContainer(container);
2436 }
2437};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002438}
2439
John McCalld226f652010-08-21 09:40:31 +00002440Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002441 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002442 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002443 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002444 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002445 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002446 Selector Sel,
2447 // optional arguments. The number of types/arguments is obtained
2448 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002449 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002450 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002451 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002452 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002453 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002454 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002455 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002456 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002457 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002458 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2459 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002460 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002462 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002463 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002464 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002465
Steve Naroffccef3712009-02-20 22:59:16 +00002466 // Methods cannot return interface types. All ObjC objects are
2467 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002468 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002469 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2470 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002471 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002472 }
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002473 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002474 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002475 Diag(MethodLoc, diag::warn_missing_method_return_type)
2476 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002477 }
Mike Stump1eb44332009-09-09 15:08:12 +00002478
2479 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002480 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002481 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002482 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002483 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002484 /*isSynthesized=*/false,
2485 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002486 MethodDeclKind == tok::objc_optional
2487 ? ObjCMethodDecl::Optional
2488 : ObjCMethodDecl::Required,
2489 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Chris Lattner5f9e2722011-07-23 10:55:15 +00002491 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002492
Chris Lattner7db638d2009-04-11 19:42:43 +00002493 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002494 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002495 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002496
Chris Lattnere294d3f2009-04-11 18:57:04 +00002497 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002498 ArgType = Context.getObjCIdType();
2499 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002500 } else {
John McCall58e46772009-10-23 21:48:59 +00002501 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002502 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002503 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002504 }
Mike Stump1eb44332009-09-09 15:08:12 +00002505
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002506 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2507 LookupOrdinaryName, ForRedeclaration);
2508 LookupName(R, S);
2509 if (R.isSingleResult()) {
2510 NamedDecl *PrevDecl = R.getFoundDecl();
2511 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002512 Diag(ArgInfo[i].NameLoc,
2513 (MethodDefinition ? diag::warn_method_param_redefinition
2514 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002515 << ArgInfo[i].Name;
2516 Diag(PrevDecl->getLocation(),
2517 diag::note_previous_declaration);
2518 }
2519 }
2520
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002521 SourceLocation StartLoc = DI
2522 ? DI->getTypeLoc().getBeginLoc()
2523 : ArgInfo[i].NameLoc;
2524
John McCall81ef3e62011-04-23 02:46:06 +00002525 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2526 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2527 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002528
John McCall70798862011-05-02 00:30:12 +00002529 Param->setObjCMethodScopeInfo(i);
2530
Chris Lattner0ed844b2008-04-04 06:12:32 +00002531 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002532 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002533
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002534 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002535 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002536
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002537 S->AddDecl(Param);
2538 IdResolver.AddDecl(Param);
2539
Chris Lattner0ed844b2008-04-04 06:12:32 +00002540 Params.push_back(Param);
2541 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002542
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002543 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002544 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002545 QualType ArgType = Param->getType();
2546 if (ArgType.isNull())
2547 ArgType = Context.getObjCIdType();
2548 else
2549 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002550 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002551 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002552 Diag(Param->getLocation(),
2553 diag::err_object_cannot_be_passed_returned_by_value)
2554 << 1 << ArgType;
2555 Param->setInvalidDecl();
2556 }
2557 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002558
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002559 Params.push_back(Param);
2560 }
2561
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002562 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2563 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002564 ObjCMethod->setObjCDeclQualifier(
2565 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002566
2567 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002568 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002569
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002570 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002571 const ObjCMethodDecl *PrevMethod = 0;
2572 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002573 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002574 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2575 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002576 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002577 PrevMethod = ImpDecl->getClassMethod(Sel);
2578 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002579 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002580
Sean Huntcf807c42010-08-18 23:23:40 +00002581 if (ObjCMethod->hasAttrs() &&
2582 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002583 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002584 } else {
2585 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002586 }
John McCall6c2c2502011-07-22 02:45:48 +00002587
Chris Lattner4d391482007-12-12 07:09:47 +00002588 if (PrevMethod) {
2589 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002590 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002591 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002592 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002593 }
John McCall54abf7d2009-11-04 02:18:39 +00002594
Douglas Gregor926df6c2011-06-11 01:09:30 +00002595 // If this Objective-C method does not have a related result type, but we
2596 // are allowed to infer related result types, try to do so based on the
2597 // method family.
2598 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2599 if (!CurrentClass) {
2600 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2601 CurrentClass = Cat->getClassInterface();
2602 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2603 CurrentClass = Impl->getClassInterface();
2604 else if (ObjCCategoryImplDecl *CatImpl
2605 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2606 CurrentClass = CatImpl->getClassInterface();
2607 }
John McCall6c2c2502011-07-22 02:45:48 +00002608
2609 bool isRelatedResultTypeCompatible =
2610 (getLangOptions().ObjCInferRelatedResultType &&
2611 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass));
2612
2613 // Search for overridden methods and merge information down from them.
2614 OverrideSearch overrides(*this, ObjCMethod);
2615 for (OverrideSearch::iterator
2616 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2617 ObjCMethodDecl *overridden = *i;
2618
2619 // Propagate down the 'related result type' bit from overridden methods.
2620 if (isRelatedResultTypeCompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002621 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002622
2623 // Then merge the declarations.
2624 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002625
2626 // Check for overriding methods
2627 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2628 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2629 WarnConflictingTypedMethods(ObjCMethod, overridden,
2630 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2631 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002632 }
2633
John McCallf85e1932011-06-15 23:02:42 +00002634 bool ARCError = false;
2635 if (getLangOptions().ObjCAutoRefCount)
2636 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2637
John McCall6c2c2502011-07-22 02:45:48 +00002638 if (!ARCError && isRelatedResultTypeCompatible &&
2639 !ObjCMethod->hasRelatedResultType()) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002640 bool InferRelatedResultType = false;
2641 switch (ObjCMethod->getMethodFamily()) {
2642 case OMF_None:
2643 case OMF_copy:
2644 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002645 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002646 case OMF_mutableCopy:
2647 case OMF_release:
2648 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002649 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002650 break;
2651
2652 case OMF_alloc:
2653 case OMF_new:
2654 InferRelatedResultType = ObjCMethod->isClassMethod();
2655 break;
2656
2657 case OMF_init:
2658 case OMF_autorelease:
2659 case OMF_retain:
2660 case OMF_self:
2661 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2662 break;
2663 }
2664
John McCall6c2c2502011-07-22 02:45:48 +00002665 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002666 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002667 }
2668
John McCalld226f652010-08-21 09:40:31 +00002669 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002670}
2671
Chris Lattnercc98eac2008-12-17 07:13:27 +00002672bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002673 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002674 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002675 // Following is also an error. But it is caused by a missing @end
2676 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002677 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2678 return false;
2679 }
2680
Anders Carlsson15281452008-11-04 16:57:32 +00002681 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2682 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Anders Carlsson15281452008-11-04 16:57:32 +00002684 return true;
2685}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002686
Chris Lattnercc98eac2008-12-17 07:13:27 +00002687/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2688/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002689void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002690 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002691 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002692 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002693 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002694 if (!Class) {
2695 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2696 return;
2697 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002698 if (LangOpts.ObjCNonFragileABI) {
2699 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2700 return;
2701 }
Mike Stump1eb44332009-09-09 15:08:12 +00002702
Chris Lattnercc98eac2008-12-17 07:13:27 +00002703 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002704 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002705 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002706 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002707 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002708 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002709 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002710 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2711 /*FIXME: StartL=*/ID->getLocation(),
2712 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002713 ID->getIdentifier(), ID->getType(),
2714 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002715 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002716 }
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Chris Lattnercc98eac2008-12-17 07:13:27 +00002718 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002719 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002720 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002721 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002722 if (getLangOptions().CPlusPlus)
2723 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002724 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002725 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002726 }
2727}
2728
Douglas Gregor160b5632010-04-26 17:32:49 +00002729/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002730VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2731 SourceLocation StartLoc,
2732 SourceLocation IdLoc,
2733 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002734 bool Invalid) {
2735 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2736 // duration shall not be qualified by an address-space qualifier."
2737 // Since all parameters have automatic store duration, they can not have
2738 // an address space.
2739 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002740 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002741 Invalid = true;
2742 }
2743
2744 // An @catch parameter must be an unqualified object pointer type;
2745 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2746 if (Invalid) {
2747 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002748 } else if (T->isDependentType()) {
2749 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002750 } else if (!T->isObjCObjectPointerType()) {
2751 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002752 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002753 } else if (T->isObjCQualifiedIdType()) {
2754 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002755 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002756 }
2757
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002758 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2759 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002760 New->setExceptionVariable(true);
2761
Douglas Gregor160b5632010-04-26 17:32:49 +00002762 if (Invalid)
2763 New->setInvalidDecl();
2764 return New;
2765}
2766
John McCalld226f652010-08-21 09:40:31 +00002767Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002768 const DeclSpec &DS = D.getDeclSpec();
2769
2770 // We allow the "register" storage class on exception variables because
2771 // GCC did, but we drop it completely. Any other storage class is an error.
2772 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2773 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2774 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2775 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2776 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2777 << DS.getStorageClassSpec();
2778 }
2779 if (D.getDeclSpec().isThreadSpecified())
2780 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2781 D.getMutableDeclSpec().ClearStorageClassSpecs();
2782
2783 DiagnoseFunctionSpecifiers(D);
2784
2785 // Check that there are no default arguments inside the type of this
2786 // exception object (C++ only).
2787 if (getLangOptions().CPlusPlus)
2788 CheckExtraCXXDefaultArguments(D);
2789
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002790 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002791 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002792
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002793 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2794 D.getSourceRange().getBegin(),
2795 D.getIdentifierLoc(),
2796 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002797 D.isInvalidType());
2798
2799 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2800 if (D.getCXXScopeSpec().isSet()) {
2801 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2802 << D.getCXXScopeSpec().getRange();
2803 New->setInvalidDecl();
2804 }
2805
2806 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002807 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002808 if (D.getIdentifier())
2809 IdResolver.AddDecl(New);
2810
2811 ProcessDeclAttributes(S, New, D);
2812
2813 if (New->hasAttr<BlocksAttr>())
2814 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002815 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002816}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002817
2818/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002819/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002820void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002821 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002822 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2823 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002824 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002825 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002826 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002827 }
2828}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002829
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002830void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002831 // Load referenced selectors from the external source.
2832 if (ExternalSource) {
2833 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2834 ExternalSource->ReadReferencedSelectors(Sels);
2835 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2836 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2837 }
2838
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002839 // Warning will be issued only when selector table is
2840 // generated (which means there is at lease one implementation
2841 // in the TU). This is to match gcc's behavior.
2842 if (ReferencedSelectors.empty() ||
2843 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002844 return;
2845 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2846 ReferencedSelectors.begin(),
2847 E = ReferencedSelectors.end(); S != E; ++S) {
2848 Selector Sel = (*S).first;
2849 if (!LookupImplementedMethodInGlobalPool(Sel))
2850 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2851 }
2852 return;
2853}