blob: 2ee5a7d83abd3b822f7f4735fb4032ca10bcf421 [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 Weber27f07762011-08-29 22:59:14 +0000300 ObjCShouldCallSuperFinalize =
301 !Context.getLangOptions().ObjCAutoRefCount &&
302 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000303 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000304 }
Chris Lattner4d391482007-12-12 07:09:47 +0000305}
306
John McCalld226f652010-08-21 09:40:31 +0000307Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000308ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
309 IdentifierInfo *ClassName, SourceLocation ClassLoc,
310 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000311 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000312 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000313 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000314 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattner4d391482007-12-12 07:09:47 +0000316 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000317 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000318 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000319
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000320 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000321 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000322 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000325 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
326 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000327 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000328 if (!IDecl->isForwardDecl()) {
329 IDecl->setInvalidDecl();
330 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
331 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000332
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000333 // Return the previous class interface.
334 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000335 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000336 } else {
337 IDecl->setLocation(AtInterfaceLoc);
338 IDecl->setForwardDecl(false);
339 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000340 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000341 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000342 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000343
344 // Since this ObjCInterfaceDecl was created by a forward declaration,
345 // we now add it to the DeclContext since it wasn't added before
346 // (see ActOnForwardClassDeclaration).
347 IDecl->setLexicalDeclContext(CurContext);
348 CurContext->addDecl(IDecl);
349
350 if (AttrList)
351 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000352 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000353 } else {
354 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
355 ClassName, ClassLoc);
356 if (AttrList)
357 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
358
359 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattner4d391482007-12-12 07:09:47 +0000362 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000363 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000364 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
365 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000366
367 if (!PrevDecl) {
368 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000369 TypoCorrection Corrected = CorrectTypo(
370 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
371 NULL, NULL, false, CTC_NoKeywords);
372 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000373 Diag(SuperLoc, diag::err_undef_superclass_suggest)
374 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000375 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
376 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000377 }
378 }
379
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000380 if (PrevDecl == IDecl) {
381 Diag(SuperLoc, diag::err_recursive_superclass)
382 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
383 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000384 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000385 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000386 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000387
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000388 // Diagnose classes that inherit from deprecated classes.
389 if (SuperClassDecl)
390 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000392 if (PrevDecl && SuperClassDecl == 0) {
393 // The previous declaration was not a class decl. Check if we have a
394 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000395 if (const TypedefNameDecl *TDecl =
396 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000397 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000398 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000399 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
400 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000401 }
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000404 // This handles the following case:
405 //
406 // typedef int SuperClass;
407 // @interface MyClass : SuperClass {} @end
408 //
409 if (!SuperClassDecl) {
410 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
411 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000412 }
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Richard Smith162e1c12011-04-15 14:24:37 +0000415 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000416 if (!SuperClassDecl)
417 Diag(SuperLoc, diag::err_undef_superclass)
418 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000419 else if (SuperClassDecl->isForwardDecl()) {
420 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000421 << SuperClassDecl->getDeclName() << ClassName
422 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000423 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
424 SuperClassDecl = 0;
425 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000426 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000427 IDecl->setSuperClass(SuperClassDecl);
428 IDecl->setSuperClassLoc(SuperLoc);
429 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000430 }
Chris Lattner4d391482007-12-12 07:09:47 +0000431 } else { // we have a root class.
432 IDecl->setLocEnd(ClassLoc);
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Sebastian Redl0b17c612010-08-13 00:28:03 +0000435 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000436 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000437 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000438 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000439 IDecl->setLocEnd(EndProtoLoc);
440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlsson15281452008-11-04 16:57:32 +0000442 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000443 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000444}
445
446/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000447/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000448Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
449 IdentifierInfo *AliasName,
450 SourceLocation AliasLocation,
451 IdentifierInfo *ClassName,
452 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000453 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000454 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000455 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000456 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000457 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000458 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000459 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000460 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000461 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000462 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000463 }
464 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000465 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000466 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000467 if (const TypedefNameDecl *TDecl =
468 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000469 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000470 if (T->isObjCObjectType()) {
471 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000472 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000473 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000474 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000475 }
476 }
477 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000478 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
479 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000480 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000481 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000482 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000483 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000486 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000487 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000488 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Anders Carlsson15281452008-11-04 16:57:32 +0000490 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000491 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000492
John McCalld226f652010-08-21 09:40:31 +0000493 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000494}
495
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000496bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000497 IdentifierInfo *PName,
498 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000499 const ObjCList<ObjCProtocolDecl> &PList) {
500
501 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000502 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
503 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000504 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
505 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000506 if (PDecl->getIdentifier() == PName) {
507 Diag(Ploc, diag::err_protocol_has_circular_dependency);
508 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000509 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000510 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000511 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
512 PDecl->getLocation(), PDecl->getReferencedProtocols()))
513 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000514 }
515 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000516 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000517}
518
John McCalld226f652010-08-21 09:40:31 +0000519Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000520Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
521 IdentifierInfo *ProtocolName,
522 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000523 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000524 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000525 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000526 SourceLocation EndProtoLoc,
527 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000528 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000529 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000530 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000531 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000532 if (PDecl) {
533 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000534 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000535 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000536 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000537 // Just return the protocol we already had.
538 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000539 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000540 }
Steve Naroff61d68522009-03-05 15:22:01 +0000541 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000542 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000543 err = CheckForwardProtocolDeclarationForCircularDependency(
544 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Steve Narofff11b5082008-08-13 16:39:22 +0000546 // Make sure the cached decl gets a valid start location.
547 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000548 PDecl->setForwardDecl(false);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000549 // Since this ObjCProtocolDecl was created by a forward declaration,
550 // we now add it to the DeclContext since it wasn't added before
551 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000552 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000553 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000554 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000555 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000556 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000557 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000558 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000559 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000560 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000561 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000562 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000563 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000564 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000565 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
566 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000567 PDecl->setLocEnd(EndProtoLoc);
568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
570 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000571 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000572}
573
574/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000575/// issues an error if they are not declared. It returns list of
576/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000577void
Chris Lattnere13b9592008-07-26 04:03:38 +0000578Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000579 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000580 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000581 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000582 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000583 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
584 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000585 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000586 TypoCorrection Corrected = CorrectTypo(
587 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
588 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
589 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000590 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000591 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000592 Diag(PDecl->getLocation(), diag::note_previous_decl)
593 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000594 }
595 }
596
597 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000598 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000599 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000600 continue;
601 }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000603 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000604
605 // If this is a forward declaration and we are supposed to warn in this
606 // case, do it.
607 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000608 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000609 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000610 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000611 }
612}
613
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000614/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000615/// a class method in its extension.
616///
Mike Stump1eb44332009-09-09 15:08:12 +0000617void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000618 ObjCInterfaceDecl *ID) {
619 if (!ID)
620 return; // Possibly due to previous error
621
622 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000623 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
624 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000625 ObjCMethodDecl *MD = *i;
626 MethodMap[MD->getSelector()] = MD;
627 }
628
629 if (MethodMap.empty())
630 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000631 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
632 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000633 ObjCMethodDecl *Method = *i;
634 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
635 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
636 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
637 << Method->getDeclName();
638 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
639 }
640 }
641}
642
Chris Lattner58fe03b2009-04-12 08:43:13 +0000643/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000644Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000645Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000646 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000647 unsigned NumElts,
648 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000649 SmallVector<ObjCProtocolDecl*, 32> Protocols;
650 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattner4d391482007-12-12 07:09:47 +0000652 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000653 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000654 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000655 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000656 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000657 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000658 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000659 PushOnScopeChains(PDecl, TUScope, false);
660 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000661 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000662 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000663 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000664 if (!isNew)
665 PDecl->setChangedSinceDeserialization(true);
666 }
Chris Lattner4d391482007-12-12 07:09:47 +0000667 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000668 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
671 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000672 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000673 Protocols.data(), Protocols.size(),
674 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000675 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000676 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000677 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000678}
679
John McCalld226f652010-08-21 09:40:31 +0000680Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000681ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
682 IdentifierInfo *ClassName, SourceLocation ClassLoc,
683 IdentifierInfo *CategoryName,
684 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000685 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000686 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000687 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000688 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000689 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000690 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000691
692 /// Check that class of this category is already completely declared.
693 if (!IDecl || IDecl->isForwardDecl()) {
694 // Create an invalid ObjCCategoryDecl to serve as context for
695 // the enclosing method declarations. We mark the decl invalid
696 // to make it clear that this isn't a valid AST.
697 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000698 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000699 CDecl->setInvalidDecl();
700 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000701 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000702 }
703
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000704 if (!CategoryName && IDecl->getImplementation()) {
705 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
706 Diag(IDecl->getImplementation()->getLocation(),
707 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000708 }
709
Fariborz Jahanian25760612010-02-15 21:55:26 +0000710 if (CategoryName) {
711 /// Check for duplicate interface declaration for this category
712 ObjCCategoryDecl *CDeclChain;
713 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
714 CDeclChain = CDeclChain->getNextClassCategory()) {
715 if (CDeclChain->getIdentifier() == CategoryName) {
716 // Class extensions can be declared multiple times.
717 Diag(CategoryLoc, diag::warn_dup_category_def)
718 << ClassName << CategoryName;
719 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
720 break;
721 }
Chris Lattner70f19542009-02-16 21:26:43 +0000722 }
723 }
Chris Lattner70f19542009-02-16 21:26:43 +0000724
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000725 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
726 ClassLoc, CategoryLoc, CategoryName, IDecl);
727 // FIXME: PushOnScopeChains?
728 CurContext->addDecl(CDecl);
729
730 // If the interface is deprecated, warn about it.
731 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
732
Chris Lattner4d391482007-12-12 07:09:47 +0000733 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000734 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000735 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000736 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000737 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000738 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000739 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Anders Carlsson15281452008-11-04 16:57:32 +0000742 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000743 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000744}
745
746/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000747/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000748/// object.
John McCalld226f652010-08-21 09:40:31 +0000749Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000750 SourceLocation AtCatImplLoc,
751 IdentifierInfo *ClassName, SourceLocation ClassLoc,
752 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000753 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000754 ObjCCategoryDecl *CatIDecl = 0;
755 if (IDecl) {
756 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
757 if (!CatIDecl) {
758 // Category @implementation with no corresponding @interface.
759 // Create and install one.
760 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000761 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000762 CatName, IDecl);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000763 }
764 }
765
Mike Stump1eb44332009-09-09 15:08:12 +0000766 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000767 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
768 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000769 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000770 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000771 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000772 CDecl->setInvalidDecl();
773 }
Chris Lattner4d391482007-12-12 07:09:47 +0000774
Douglas Gregord0434102009-01-09 00:49:46 +0000775 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000776 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000777
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000778 /// Check that CatName, category name, is not used in another implementation.
779 if (CatIDecl) {
780 if (CatIDecl->getImplementation()) {
781 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
782 << CatName;
783 Diag(CatIDecl->getImplementation()->getLocation(),
784 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000785 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000786 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000787 // Warn on implementating category of deprecated class under
788 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000789 DiagnoseObjCImplementedDeprecations(*this,
790 dyn_cast<NamedDecl>(IDecl),
791 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000792 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Anders Carlsson15281452008-11-04 16:57:32 +0000795 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000796 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000797}
798
John McCalld226f652010-08-21 09:40:31 +0000799Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000800 SourceLocation AtClassImplLoc,
801 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000802 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000803 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000804 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000805 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000806 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000807 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
808 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000809 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000810 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000811 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000812 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
813 // If this is a forward declaration of an interface, warn.
814 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000815 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000816 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000817 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000818 } else {
819 // We did not find anything with the name ClassName; try to correct for
820 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000821 TypoCorrection Corrected = CorrectTypo(
822 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
823 NULL, NULL, false, CTC_NoKeywords);
824 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000825 // Suggest the (potentially) correct interface name. However, put the
826 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000827 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000828 // provide a code-modification hint or use the typo name for recovery,
829 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000830 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000831 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000832 << ClassName << CorrectedName;
833 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
834 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000835 IDecl = 0;
836 } else {
837 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
838 }
Chris Lattner4d391482007-12-12 07:09:47 +0000839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Chris Lattner4d391482007-12-12 07:09:47 +0000841 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000842 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000843 if (SuperClassname) {
844 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000845 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
846 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000847 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000848 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
849 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000850 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000851 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000852 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000853 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000854 Diag(SuperClassLoc, diag::err_undef_superclass)
855 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000856 else if (IDecl && IDecl->getSuperClass() != SDecl) {
857 // This implementation and its interface do not have the same
858 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000860 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000861 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000862 }
863 }
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Chris Lattner4d391482007-12-12 07:09:47 +0000866 if (!IDecl) {
867 // Legacy case of @implementation with no corresponding @interface.
868 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000869
Mike Stump390b4cc2009-05-16 07:39:55 +0000870 // FIXME: Do we support attributes on the @implementation? If so we should
871 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000872 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000873 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000874 IDecl->setSuperClass(SDecl);
875 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000876
877 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000878 } else {
879 // Mark the interface as being completed, even if it was just as
880 // @class ....;
881 // declaration; the user cannot reopen it.
882 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
885 ObjCImplementationDecl* IMPDecl =
886 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000887 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Anders Carlsson15281452008-11-04 16:57:32 +0000889 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000890 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner4d391482007-12-12 07:09:47 +0000892 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000893 if (IDecl->getImplementation()) {
894 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000896 Diag(IDecl->getImplementation()->getLocation(),
897 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000898 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000899 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000900 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000901 // Warn on implementating deprecated class under
902 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000903 DiagnoseObjCImplementedDeprecations(*this,
904 dyn_cast<NamedDecl>(IDecl),
905 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000906 }
John McCalld226f652010-08-21 09:40:31 +0000907 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000908}
909
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000910void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
911 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000912 SourceLocation RBrace) {
913 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000914 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000915 if (!IDecl)
916 return;
917 /// Check case of non-existing @interface decl.
918 /// (legacy objective-c @implementation decl without an @interface decl).
919 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000920 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000921 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000922 // Add ivar's to class's DeclContext.
923 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000924 ivars[i]->setLexicalDeclContext(ImpDecl);
925 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000926 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000927 }
928
Chris Lattner4d391482007-12-12 07:09:47 +0000929 return;
930 }
931 // If implementation has empty ivar list, just return.
932 if (numIvars == 0)
933 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Chris Lattner4d391482007-12-12 07:09:47 +0000935 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000936 if (LangOpts.ObjCNonFragileABI2) {
937 if (ImpDecl->getSuperClass())
938 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
939 for (unsigned i = 0; i < numIvars; i++) {
940 ObjCIvarDecl* ImplIvar = ivars[i];
941 if (const ObjCIvarDecl *ClsIvar =
942 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
943 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
944 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
945 continue;
946 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000947 // Instance ivar to Implementation's DeclContext.
948 ImplIvar->setLexicalDeclContext(ImpDecl);
949 IDecl->makeDeclVisibleInContext(ImplIvar, false);
950 ImpDecl->addDecl(ImplIvar);
951 }
952 return;
953 }
Chris Lattner4d391482007-12-12 07:09:47 +0000954 // Check interface's Ivar list against those in the implementation.
955 // names and types must match.
956 //
Chris Lattner4d391482007-12-12 07:09:47 +0000957 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000958 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000959 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
960 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000961 ObjCIvarDecl* ImplIvar = ivars[j++];
962 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000963 assert (ImplIvar && "missing implementation ivar");
964 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Steve Naroffca331292009-03-03 14:49:36 +0000966 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000967 if (Context.getCanonicalType(ImplIvar->getType()) !=
968 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000969 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000970 << ImplIvar->getIdentifier()
971 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000972 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000973 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
974 Expr *ImplBitWidth = ImplIvar->getBitWidth();
975 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000976 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
977 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000978 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
979 << ImplIvar->getIdentifier();
980 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982 }
Steve Naroffca331292009-03-03 14:49:36 +0000983 // Make sure the names are identical.
984 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000985 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000986 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000987 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000988 }
989 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattner609e4c72007-12-12 18:11:49 +0000992 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000993 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000994 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000995 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000996}
997
Steve Naroff3c2eb662008-02-10 21:38:56 +0000998void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000999 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001000 // No point warning no definition of method which is 'unavailable'.
1001 if (method->hasAttr<UnavailableAttr>())
1002 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001003 if (!IncompleteImpl) {
1004 Diag(ImpLoc, diag::warn_incomplete_impl);
1005 IncompleteImpl = true;
1006 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001007 if (DiagID == diag::warn_unimplemented_protocol_method)
1008 Diag(ImpLoc, DiagID) << method->getDeclName();
1009 else
1010 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001011}
1012
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001013/// Determines if type B can be substituted for type A. Returns true if we can
1014/// guarantee that anything that the user will do to an object of type A can
1015/// also be done to an object of type B. This is trivially true if the two
1016/// types are the same, or if B is a subclass of A. It becomes more complex
1017/// in cases where protocols are involved.
1018///
1019/// Object types in Objective-C describe the minimum requirements for an
1020/// object, rather than providing a complete description of a type. For
1021/// example, if A is a subclass of B, then B* may refer to an instance of A.
1022/// The principle of substitutability means that we may use an instance of A
1023/// anywhere that we may use an instance of B - it will implement all of the
1024/// ivars of B and all of the methods of B.
1025///
1026/// This substitutability is important when type checking methods, because
1027/// the implementation may have stricter type definitions than the interface.
1028/// The interface specifies minimum requirements, but the implementation may
1029/// have more accurate ones. For example, a method may privately accept
1030/// instances of B, but only publish that it accepts instances of A. Any
1031/// object passed to it will be type checked against B, and so will implicitly
1032/// by a valid A*. Similarly, a method may return a subclass of the class that
1033/// it is declared as returning.
1034///
1035/// This is most important when considering subclassing. A method in a
1036/// subclass must accept any object as an argument that its superclass's
1037/// implementation accepts. It may, however, accept a more general type
1038/// without breaking substitutability (i.e. you can still use the subclass
1039/// anywhere that you can use the superclass, but not vice versa). The
1040/// converse requirement applies to return types: the return type for a
1041/// subclass method must be a valid object of the kind that the superclass
1042/// advertises, but it may be specified more accurately. This avoids the need
1043/// for explicit down-casting by callers.
1044///
1045/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001046static bool isObjCTypeSubstitutable(ASTContext &Context,
1047 const ObjCObjectPointerType *A,
1048 const ObjCObjectPointerType *B,
1049 bool rejectId) {
1050 // Reject a protocol-unqualified id.
1051 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001052
1053 // If B is a qualified id, then A must also be a qualified id and it must
1054 // implement all of the protocols in B. It may not be a qualified class.
1055 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1056 // stricter definition so it is not substitutable for id<A>.
1057 if (B->isObjCQualifiedIdType()) {
1058 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001059 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1060 QualType(B,0),
1061 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001062 }
1063
1064 /*
1065 // id is a special type that bypasses type checking completely. We want a
1066 // warning when it is used in one place but not another.
1067 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1068
1069
1070 // If B is a qualified id, then A must also be a qualified id (which it isn't
1071 // if we've got this far)
1072 if (B->isObjCQualifiedIdType()) return false;
1073 */
1074
1075 // Now we know that A and B are (potentially-qualified) class types. The
1076 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001077 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001078}
1079
John McCall10302c02010-10-28 02:34:38 +00001080static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1081 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1082}
1083
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001084static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001085 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001086 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001087 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001088 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001089 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001090 if (IsProtocolMethodDecl &&
1091 (MethodDecl->getObjCDeclQualifier() !=
1092 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001093 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001094 S.Diag(MethodImpl->getLocation(),
1095 (IsOverridingMode ?
1096 diag::warn_conflicting_overriding_ret_type_modifiers
1097 : diag::warn_conflicting_ret_type_modifiers))
1098 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001099 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1100 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1101 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1102 }
1103 else
1104 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001105 }
1106
John McCall10302c02010-10-28 02:34:38 +00001107 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001108 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001109 return true;
1110 if (!Warn)
1111 return false;
John McCall10302c02010-10-28 02:34:38 +00001112
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001113 unsigned DiagID =
1114 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1115 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001116
1117 // Mismatches between ObjC pointers go into a different warning
1118 // category, and sometimes they're even completely whitelisted.
1119 if (const ObjCObjectPointerType *ImplPtrTy =
1120 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1121 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001122 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001123 // Allow non-matching return types as long as they don't violate
1124 // the principle of substitutability. Specifically, we permit
1125 // return types that are subclasses of the declared return type,
1126 // or that are more-qualified versions of the declared type.
1127 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001128 return false;
John McCall10302c02010-10-28 02:34:38 +00001129
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001130 DiagID =
1131 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1132 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001133 }
1134 }
1135
1136 S.Diag(MethodImpl->getLocation(), DiagID)
1137 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001138 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001139 << MethodImpl->getResultType()
1140 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001141 S.Diag(MethodDecl->getLocation(),
1142 IsOverridingMode ? diag::note_previous_declaration
1143 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001144 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001145 return false;
John McCall10302c02010-10-28 02:34:38 +00001146}
1147
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001148static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001149 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001150 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001151 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001152 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001153 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001154 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001155 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001156 if (IsProtocolMethodDecl &&
1157 (ImplVar->getObjCDeclQualifier() !=
1158 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001159 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001160 if (IsOverridingMode)
1161 S.Diag(ImplVar->getLocation(),
1162 diag::warn_conflicting_overriding_param_modifiers)
1163 << getTypeRange(ImplVar->getTypeSourceInfo())
1164 << MethodImpl->getDeclName();
1165 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001166 diag::warn_conflicting_param_modifiers)
1167 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001168 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001169 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1170 << getTypeRange(IfaceVar->getTypeSourceInfo());
1171 }
1172 else
1173 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001174 }
1175
John McCall10302c02010-10-28 02:34:38 +00001176 QualType ImplTy = ImplVar->getType();
1177 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001178
John McCall10302c02010-10-28 02:34:38 +00001179 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001180 return true;
1181
1182 if (!Warn)
1183 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001184 unsigned DiagID =
1185 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1186 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001187
1188 // Mismatches between ObjC pointers go into a different warning
1189 // category, and sometimes they're even completely whitelisted.
1190 if (const ObjCObjectPointerType *ImplPtrTy =
1191 ImplTy->getAs<ObjCObjectPointerType>()) {
1192 if (const ObjCObjectPointerType *IfacePtrTy =
1193 IfaceTy->getAs<ObjCObjectPointerType>()) {
1194 // Allow non-matching argument types as long as they don't
1195 // violate the principle of substitutability. Specifically, the
1196 // implementation must accept any objects that the superclass
1197 // accepts, however it may also accept others.
1198 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001199 return false;
John McCall10302c02010-10-28 02:34:38 +00001200
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001201 DiagID =
1202 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1203 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001204 }
1205 }
1206
1207 S.Diag(ImplVar->getLocation(), DiagID)
1208 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001209 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1210 S.Diag(IfaceVar->getLocation(),
1211 (IsOverridingMode ? diag::note_previous_declaration
1212 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001213 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001214 return false;
John McCall10302c02010-10-28 02:34:38 +00001215}
John McCallf85e1932011-06-15 23:02:42 +00001216
1217/// In ARC, check whether the conventional meanings of the two methods
1218/// match. If they don't, it's a hard error.
1219static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1220 ObjCMethodDecl *decl) {
1221 ObjCMethodFamily implFamily = impl->getMethodFamily();
1222 ObjCMethodFamily declFamily = decl->getMethodFamily();
1223 if (implFamily == declFamily) return false;
1224
1225 // Since conventions are sorted by selector, the only possibility is
1226 // that the types differ enough to cause one selector or the other
1227 // to fall out of the family.
1228 assert(implFamily == OMF_None || declFamily == OMF_None);
1229
1230 // No further diagnostics required on invalid declarations.
1231 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1232
1233 const ObjCMethodDecl *unmatched = impl;
1234 ObjCMethodFamily family = declFamily;
1235 unsigned errorID = diag::err_arc_lost_method_convention;
1236 unsigned noteID = diag::note_arc_lost_method_convention;
1237 if (declFamily == OMF_None) {
1238 unmatched = decl;
1239 family = implFamily;
1240 errorID = diag::err_arc_gained_method_convention;
1241 noteID = diag::note_arc_gained_method_convention;
1242 }
1243
1244 // Indexes into a %select clause in the diagnostic.
1245 enum FamilySelector {
1246 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1247 };
1248 FamilySelector familySelector = FamilySelector();
1249
1250 switch (family) {
1251 case OMF_None: llvm_unreachable("logic error, no method convention");
1252 case OMF_retain:
1253 case OMF_release:
1254 case OMF_autorelease:
1255 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001256 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001257 case OMF_retainCount:
1258 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001259 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001260 // Mismatches for these methods don't change ownership
1261 // conventions, so we don't care.
1262 return false;
1263
1264 case OMF_init: familySelector = F_init; break;
1265 case OMF_alloc: familySelector = F_alloc; break;
1266 case OMF_copy: familySelector = F_copy; break;
1267 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1268 case OMF_new: familySelector = F_new; break;
1269 }
1270
1271 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1272 ReasonSelector reasonSelector;
1273
1274 // The only reason these methods don't fall within their families is
1275 // due to unusual result types.
1276 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1277 reasonSelector = R_UnrelatedReturn;
1278 } else {
1279 reasonSelector = R_NonObjectReturn;
1280 }
1281
1282 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1283 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1284
1285 return true;
1286}
John McCall10302c02010-10-28 02:34:38 +00001287
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001288void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001289 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001290 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001291 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001292 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001293 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001294 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1295 return;
1296
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001297 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001298 IsProtocolMethodDecl, IsOverridingMode,
1299 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Chris Lattner3aff9192009-04-11 19:58:42 +00001301 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001302 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001303 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001304 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1305 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001306 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001307
Fariborz Jahanian21121902011-08-08 18:03:17 +00001308 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001309 if (IsOverridingMode)
1310 Diag(ImpMethodDecl->getLocation(),
1311 diag::warn_conflicting_overriding_variadic);
1312 else
1313 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001314 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001315 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001316}
1317
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001318/// WarnExactTypedMethods - This routine issues a warning if method
1319/// implementation declaration matches exactly that of its declaration.
1320void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1321 ObjCMethodDecl *MethodDecl,
1322 bool IsProtocolMethodDecl) {
1323 // don't issue warning when protocol method is optional because primary
1324 // class is not required to implement it and it is safe for protocol
1325 // to implement it.
1326 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1327 return;
1328 // don't issue warning when primary class's method is
1329 // depecated/unavailable.
1330 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1331 MethodDecl->hasAttr<DeprecatedAttr>())
1332 return;
1333
1334 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1335 IsProtocolMethodDecl, false, false);
1336 if (match)
1337 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1338 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1339 IM != EM; ++IM, ++IF) {
1340 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1341 *IM, *IF,
1342 IsProtocolMethodDecl, false, false);
1343 if (!match)
1344 break;
1345 }
1346 if (match)
1347 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001348 if (match)
1349 match = !(MethodDecl->isClassMethod() &&
1350 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001351
1352 if (match) {
1353 Diag(ImpMethodDecl->getLocation(),
1354 diag::warn_category_method_impl_match);
1355 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1356 }
1357}
1358
Mike Stump390b4cc2009-05-16 07:39:55 +00001359/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1360/// improve the efficiency of selector lookups and type checking by associating
1361/// with each protocol / interface / category the flattened instance tables. If
1362/// we used an immutable set to keep the table then it wouldn't add significant
1363/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001364
Steve Naroffefe7f362008-02-08 22:06:17 +00001365/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001366/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001367void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1368 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001369 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001370 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001371 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001372 ObjCContainerDecl *CDecl) {
1373 ObjCInterfaceDecl *IDecl;
1374 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1375 IDecl = C->getClassInterface();
1376 else
1377 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1378 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1379
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001380 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001381 ObjCInterfaceDecl *NSIDecl = 0;
1382 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001383 // check to see if class implements forwardInvocation method and objects
1384 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001385 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001386 // Under such conditions, which means that every method possible is
1387 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001388 // found" warnings.
1389 // FIXME: Use a general GetUnarySelector method for this.
1390 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1391 Selector fISelector = Context.Selectors.getSelector(1, &II);
1392 if (InsMap.count(fISelector))
1393 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1394 // need be implemented in the implementation.
1395 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1396 }
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001398 // If a method lookup fails locally we still need to look and see if
1399 // the method was implemented by a base class or an inherited
1400 // protocol. This lookup is slow, but occurs rarely in correct code
1401 // and otherwise would terminate in a warning.
1402
Chris Lattner4d391482007-12-12 07:09:47 +00001403 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001404 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001405 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001406 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001407 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001408 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001409 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001410 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001411 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001412 // Ugly, but necessary. Method declared in protcol might have
1413 // have been synthesized due to a property declared in the class which
1414 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001415 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001416 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001417 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001418 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001419 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1420 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001421 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001422 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001423 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1424 << PDecl->getDeclName();
1425 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001426 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001427 }
1428 }
Chris Lattner4d391482007-12-12 07:09:47 +00001429 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001430 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001431 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001432 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001433 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001434 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1435 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001436 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001437 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001438 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001439 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001440 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001441 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1442 PDecl->getDeclName();
1443 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001444 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001445 }
Chris Lattner780f3292008-07-21 21:32:27 +00001446 // Check on this protocols's referenced protocols, recursively.
1447 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1448 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001449 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001450}
1451
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001452/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001453/// or protocol against those declared in their implementations.
1454///
1455void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1456 const llvm::DenseSet<Selector> &ClsMap,
1457 llvm::DenseSet<Selector> &InsMapSeen,
1458 llvm::DenseSet<Selector> &ClsMapSeen,
1459 ObjCImplDecl* IMPDecl,
1460 ObjCContainerDecl* CDecl,
1461 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001462 bool ImmediateClass,
1463 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001464 // Check and see if instance methods in class interface have been
1465 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001466 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1467 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001468 if (InsMapSeen.count((*I)->getSelector()))
1469 continue;
1470 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001471 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001472 !InsMap.count((*I)->getSelector())) {
1473 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001474 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1475 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001476 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001477 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001478 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001479 IMPDecl->getInstanceMethod((*I)->getSelector());
1480 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1481 "Expected to find the method through lookup as well");
1482 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001483 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001484 if (ImpMethodDecl) {
1485 if (!WarnExactMatch)
1486 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1487 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001488 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001489 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1490 isa<ObjCProtocolDecl>(CDecl));
1491 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001492 }
1493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001495 // Check and see if class methods in class interface have been
1496 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001497 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001498 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001499 if (ClsMapSeen.count((*I)->getSelector()))
1500 continue;
1501 ClsMapSeen.insert((*I)->getSelector());
1502 if (!ClsMap.count((*I)->getSelector())) {
1503 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001504 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1505 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001506 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001507 ObjCMethodDecl *ImpMethodDecl =
1508 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001509 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1510 "Expected to find the method through lookup as well");
1511 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001512 if (!WarnExactMatch)
1513 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1514 isa<ObjCProtocolDecl>(CDecl));
1515 else
1516 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1517 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001518 }
1519 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001520
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001521 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001522 // Also methods in class extensions need be looked at next.
1523 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1524 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1525 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1526 IMPDecl,
1527 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001528 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001529
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001530 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001531 for (ObjCInterfaceDecl::all_protocol_iterator
1532 PI = I->all_referenced_protocol_begin(),
1533 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001534 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1535 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001536 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001537
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001538 // FIXME. For now, we are not checking for extact match of methods
1539 // in category implementation and its primary class's super class.
1540 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001541 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001542 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001543 I->getSuperClass(), IncompleteImpl, false);
1544 }
1545}
1546
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001547/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1548/// category matches with those implemented in its primary class and
1549/// warns each time an exact match is found.
1550void Sema::CheckCategoryVsClassMethodMatches(
1551 ObjCCategoryImplDecl *CatIMPDecl) {
1552 llvm::DenseSet<Selector> InsMap, ClsMap;
1553
1554 for (ObjCImplementationDecl::instmeth_iterator
1555 I = CatIMPDecl->instmeth_begin(),
1556 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1557 InsMap.insert((*I)->getSelector());
1558
1559 for (ObjCImplementationDecl::classmeth_iterator
1560 I = CatIMPDecl->classmeth_begin(),
1561 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1562 ClsMap.insert((*I)->getSelector());
1563 if (InsMap.empty() && ClsMap.empty())
1564 return;
1565
1566 // Get category's primary class.
1567 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1568 if (!CatDecl)
1569 return;
1570 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1571 if (!IDecl)
1572 return;
1573 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1574 bool IncompleteImpl = false;
1575 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1576 CatIMPDecl, IDecl,
1577 IncompleteImpl, false, true /*WarnExactMatch*/);
1578}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001579
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001580void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001581 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001582 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001583 llvm::DenseSet<Selector> InsMap;
1584 // Check and see if instance methods in class interface have been
1585 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001586 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001587 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001588 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001590 // Check and see if properties declared in the interface have either 1)
1591 // an implementation or 2) there is a @synthesize/@dynamic implementation
1592 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001593 if (isa<ObjCInterfaceDecl>(CDecl) &&
1594 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001595 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001596
Chris Lattner4d391482007-12-12 07:09:47 +00001597 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001598 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001599 I = IMPDecl->classmeth_begin(),
1600 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001601 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001603 // Check for type conflict of methods declared in a class/protocol and
1604 // its implementation; if any.
1605 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001606 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1607 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001608 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001609
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001610 // check all methods implemented in category against those declared
1611 // in its primary class.
1612 if (ObjCCategoryImplDecl *CatDecl =
1613 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1614 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Chris Lattner4d391482007-12-12 07:09:47 +00001616 // Check the protocol list for unimplemented methods in the @implementation
1617 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001618 // Check and see if class methods in class interface have been
1619 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Chris Lattnercddc8882009-03-01 00:56:52 +00001621 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001622 for (ObjCInterfaceDecl::all_protocol_iterator
1623 PI = I->all_referenced_protocol_begin(),
1624 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001625 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001626 InsMap, ClsMap, I);
1627 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001628 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1629 Categories; Categories = Categories->getNextClassExtension())
1630 ImplMethodsVsClassMethods(S, IMPDecl,
1631 const_cast<ObjCCategoryDecl*>(Categories),
1632 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001633 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001634 // For extended class, unimplemented methods in its protocols will
1635 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001636 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001637 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1638 E = C->protocol_end(); PI != E; ++PI)
1639 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001640 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001641 // Report unimplemented properties in the category as well.
1642 // When reporting on missing setter/getters, do not report when
1643 // setter/getter is implemented in category's primary class
1644 // implementation.
1645 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1646 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1647 for (ObjCImplementationDecl::instmeth_iterator
1648 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1649 InsMap.insert((*I)->getSelector());
1650 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001651 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001652 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001653 } else
1654 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001655}
1656
Mike Stump1eb44332009-09-09 15:08:12 +00001657/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001658Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001659Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001660 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001661 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001662 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001663 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001664 for (unsigned i = 0; i != NumElts; ++i) {
1665 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001666 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001667 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001668 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001669 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001670 // Maybe we will complain about the shadowed template parameter.
1671 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1672 // Just pretend that we didn't see the previous declaration.
1673 PrevDecl = 0;
1674 }
1675
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001676 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001677 // GCC apparently allows the following idiom:
1678 //
1679 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1680 // @class XCElementToggler;
1681 //
Mike Stump1eb44332009-09-09 15:08:12 +00001682 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001683 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001684 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001685 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001686 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001687 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001688 // a forward class declaration matching a typedef name of a class refers
1689 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001690 if (const ObjCObjectType *OI =
1691 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1692 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001693 }
Chris Lattner4d391482007-12-12 07:09:47 +00001694 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001695 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1696 if (!IDecl) { // Not already seen? Make a forward decl.
1697 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1698 IdentList[i], IdentLocs[i], true);
1699
1700 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1701 // the current DeclContext. This prevents clients that walk DeclContext
1702 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1703 // declared later (if at all). We also take care to explicitly make
1704 // sure this declaration is visible for name lookup.
1705 PushOnScopeChains(IDecl, TUScope, false);
1706 CurContext->makeDeclVisibleInContext(IDecl, true);
1707 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001708 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1709 IDecl, IdentLocs[i]);
1710 CurContext->addDecl(CDecl);
1711 CheckObjCDeclScope(CDecl);
1712 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001713 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001714
1715 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001716}
1717
John McCall0f4c4c42011-06-16 01:15:19 +00001718static bool tryMatchRecordTypes(ASTContext &Context,
1719 Sema::MethodMatchStrategy strategy,
1720 const Type *left, const Type *right);
1721
John McCallf85e1932011-06-15 23:02:42 +00001722static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1723 QualType leftQT, QualType rightQT) {
1724 const Type *left =
1725 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1726 const Type *right =
1727 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1728
1729 if (left == right) return true;
1730
1731 // If we're doing a strict match, the types have to match exactly.
1732 if (strategy == Sema::MMS_strict) return false;
1733
1734 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1735
1736 // Otherwise, use this absurdly complicated algorithm to try to
1737 // validate the basic, low-level compatibility of the two types.
1738
1739 // As a minimum, require the sizes and alignments to match.
1740 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1741 return false;
1742
1743 // Consider all the kinds of non-dependent canonical types:
1744 // - functions and arrays aren't possible as return and parameter types
1745
1746 // - vector types of equal size can be arbitrarily mixed
1747 if (isa<VectorType>(left)) return isa<VectorType>(right);
1748 if (isa<VectorType>(right)) return false;
1749
1750 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001751 // - structs, unions, and Objective-C objects must match more-or-less
1752 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001753 // - everything else should be a scalar
1754 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001755 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001756
1757 // Make scalars agree in kind, except count bools as chars.
1758 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1759 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1760 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1761 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1762
1763 // Note that data member pointers and function member pointers don't
1764 // intermix because of the size differences.
1765
1766 return (leftSK == rightSK);
1767}
Chris Lattner4d391482007-12-12 07:09:47 +00001768
John McCall0f4c4c42011-06-16 01:15:19 +00001769static bool tryMatchRecordTypes(ASTContext &Context,
1770 Sema::MethodMatchStrategy strategy,
1771 const Type *lt, const Type *rt) {
1772 assert(lt && rt && lt != rt);
1773
1774 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1775 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1776 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1777
1778 // Require union-hood to match.
1779 if (left->isUnion() != right->isUnion()) return false;
1780
1781 // Require an exact match if either is non-POD.
1782 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1783 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1784 return false;
1785
1786 // Require size and alignment to match.
1787 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1788
1789 // Require fields to match.
1790 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1791 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1792 for (; li != le && ri != re; ++li, ++ri) {
1793 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1794 return false;
1795 }
1796 return (li == le && ri == re);
1797}
1798
Chris Lattner4d391482007-12-12 07:09:47 +00001799/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1800/// returns true, or false, accordingly.
1801/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001802bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1803 const ObjCMethodDecl *right,
1804 MethodMatchStrategy strategy) {
1805 if (!matchTypes(Context, strategy,
1806 left->getResultType(), right->getResultType()))
1807 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001808
John McCallf85e1932011-06-15 23:02:42 +00001809 if (getLangOptions().ObjCAutoRefCount &&
1810 (left->hasAttr<NSReturnsRetainedAttr>()
1811 != right->hasAttr<NSReturnsRetainedAttr>() ||
1812 left->hasAttr<NSConsumesSelfAttr>()
1813 != right->hasAttr<NSConsumesSelfAttr>()))
1814 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001815
John McCallf85e1932011-06-15 23:02:42 +00001816 ObjCMethodDecl::param_iterator
1817 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001818
John McCallf85e1932011-06-15 23:02:42 +00001819 for (; li != le; ++li, ++ri) {
1820 assert(ri != right->param_end() && "Param mismatch");
1821 ParmVarDecl *lparm = *li, *rparm = *ri;
1822
1823 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1824 return false;
1825
1826 if (getLangOptions().ObjCAutoRefCount &&
1827 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1828 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001829 }
1830 return true;
1831}
1832
Sebastian Redldb9d2142010-08-02 23:18:59 +00001833/// \brief Read the contents of the method pool for a given selector from
1834/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001835///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001836/// This routine should only be called once, when the method pool has no entry
1837/// for this selector.
1838Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001839 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001840 assert(MethodPool.find(Sel) == MethodPool.end() &&
1841 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001842
1843 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001844 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Sebastian Redldb9d2142010-08-02 23:18:59 +00001846 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001847}
1848
Sebastian Redldb9d2142010-08-02 23:18:59 +00001849void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1850 bool instance) {
1851 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1852 if (Pos == MethodPool.end()) {
1853 if (ExternalSource)
1854 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001855 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001856 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1857 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001858 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001859 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001860 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001861 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001862 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001863 Entry.Method = Method;
1864 Entry.Next = 0;
1865 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001866 }
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Chris Lattnerb25df352009-03-04 05:16:45 +00001868 // We've seen a method with this name, see if we have already seen this type
1869 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001870 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1871 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1872
1873 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001874 ObjCMethodDecl *PrevObjCMethod = List->Method;
1875 PrevObjCMethod->setDefined(impl);
1876 // If a method is deprecated, push it in the global pool.
1877 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001878 if (Method->isDeprecated()) {
1879 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001880 List->Method = Method;
1881 }
1882 // If new method is unavailable, push it into global pool
1883 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001884 if (Method->isUnavailable()) {
1885 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001886 List->Method = Method;
1887 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001888 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001889 }
John McCallf85e1932011-06-15 23:02:42 +00001890 }
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Chris Lattnerb25df352009-03-04 05:16:45 +00001892 // We have a new signature for an existing method - add it.
1893 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001894 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1895 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001896}
1897
John McCallf85e1932011-06-15 23:02:42 +00001898/// Determines if this is an "acceptable" loose mismatch in the global
1899/// method pool. This exists mostly as a hack to get around certain
1900/// global mismatches which we can't afford to make warnings / errors.
1901/// Really, what we want is a way to take a method out of the global
1902/// method pool.
1903static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1904 ObjCMethodDecl *other) {
1905 if (!chosen->isInstanceMethod())
1906 return false;
1907
1908 Selector sel = chosen->getSelector();
1909 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1910 return false;
1911
1912 // Don't complain about mismatches for -length if the method we
1913 // chose has an integral result type.
1914 return (chosen->getResultType()->isIntegerType());
1915}
1916
Sebastian Redldb9d2142010-08-02 23:18:59 +00001917ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001918 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001919 bool warn, bool instance) {
1920 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1921 if (Pos == MethodPool.end()) {
1922 if (ExternalSource)
1923 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001924 else
1925 return 0;
1926 }
1927
Sebastian Redldb9d2142010-08-02 23:18:59 +00001928 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Sebastian Redldb9d2142010-08-02 23:18:59 +00001930 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001931 bool issueDiagnostic = false, issueError = false;
1932
1933 // We support a warning which complains about *any* difference in
1934 // method signature.
1935 bool strictSelectorMatch =
1936 (receiverIdOrClass && warn &&
1937 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1938 R.getBegin()) !=
1939 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001940 if (strictSelectorMatch)
1941 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001942 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1943 MMS_strict)) {
1944 issueDiagnostic = true;
1945 break;
1946 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001947 }
1948
John McCallf85e1932011-06-15 23:02:42 +00001949 // If we didn't see any strict differences, we won't see any loose
1950 // differences. In ARC, however, we also need to check for loose
1951 // mismatches, because most of them are errors.
1952 if (!strictSelectorMatch ||
1953 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001954 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001955 // This checks if the methods differ in type mismatch.
1956 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1957 MMS_loose) &&
1958 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1959 issueDiagnostic = true;
1960 if (getLangOptions().ObjCAutoRefCount)
1961 issueError = true;
1962 break;
1963 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001964 }
1965
John McCallf85e1932011-06-15 23:02:42 +00001966 if (issueDiagnostic) {
1967 if (issueError)
1968 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1969 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001970 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1971 else
1972 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001973
1974 Diag(MethList.Method->getLocStart(),
1975 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001976 << MethList.Method->getSourceRange();
1977 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1978 Diag(Next->Method->getLocStart(), diag::note_also_found)
1979 << Next->Method->getSourceRange();
1980 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001981 }
1982 return MethList.Method;
1983}
1984
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001985ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001986 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1987 if (Pos == MethodPool.end())
1988 return 0;
1989
1990 GlobalMethods &Methods = Pos->second;
1991
1992 if (Methods.first.Method && Methods.first.Method->isDefined())
1993 return Methods.first.Method;
1994 if (Methods.second.Method && Methods.second.Method->isDefined())
1995 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001996 return 0;
1997}
1998
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001999/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2000/// identical selector names in current and its super classes and issues
2001/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002002void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2003 ObjCMethodDecl *Method,
2004 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002005 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2006 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002008 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002009 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002010 SD->lookupMethod(Method->getSelector(), IsInstance);
2011 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002012 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002013 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002014 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002015 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2016 E = Method->param_end();
2017 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2018 for (; ParamI != E; ++ParamI, ++PrevI) {
2019 // Number of parameters are the same and is guaranteed by selector match.
2020 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2021 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2022 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002023 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002024 // respective argument type in the super class method, issue warning;
2025 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002026 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002027 << T1 << T2;
2028 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2029 return;
2030 }
2031 }
2032 ID = SD;
2033 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002034}
2035
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002036/// DiagnoseDuplicateIvars -
2037/// Check for duplicate ivars in the entire class at the start of
2038/// @implementation. This becomes necesssary because class extension can
2039/// add ivars to a class in random order which will not be known until
2040/// class's @implementation is seen.
2041void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2042 ObjCInterfaceDecl *SID) {
2043 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2044 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2045 ObjCIvarDecl* Ivar = (*IVI);
2046 if (Ivar->isInvalidDecl())
2047 continue;
2048 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2049 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2050 if (prevIvar) {
2051 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2052 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2053 Ivar->setInvalidDecl();
2054 }
2055 }
2056 }
2057}
2058
Steve Naroffa56f6162007-12-18 01:30:32 +00002059// Note: For class/category implemenations, allMethods/allProperties is
2060// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002061void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002062 Decl **allMethods, unsigned allNum,
2063 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002064 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002065
2066 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002067 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002068 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2069 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002070
Mike Stump1eb44332009-09-09 15:08:12 +00002071 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002072 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2073 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002074 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002075
Ted Kremenek782f2f52010-01-07 01:20:12 +00002076 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2077 // FIXME: This is wrong. We shouldn't be pretending that there is
2078 // an '@end' in the declaration.
2079 SourceLocation L = ClassDecl->getLocation();
2080 AtEnd.setBegin(L);
2081 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002082 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002083 }
2084
Steve Naroff0701bbb2009-01-08 17:28:14 +00002085 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2086 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2087 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2088
Chris Lattner4d391482007-12-12 07:09:47 +00002089 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002090 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002091 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002092
2093 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002094 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002095 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002096 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002097 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002098 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002099 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002100 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002101 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002102 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002103 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002104 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002105 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002106 InsMap[Method->getSelector()] = Method;
2107 /// The following allows us to typecheck messages to "id".
2108 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002109 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002110 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002111 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002112 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002113 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002114 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002115 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002116 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002117 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002118 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002119 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002120 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002121 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002122 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002123 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002124 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002125 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002126 /// The following allows us to typecheck messages to "Class".
2127 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002128 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002129 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002130 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002131 }
2132 }
2133 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002134 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002135 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002136 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002137 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002138 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002139 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002140 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002141 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002142 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002143
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002144 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002145 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002146 if (C->IsClassExtension()) {
2147 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2148 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002149 }
Chris Lattner4d391482007-12-12 07:09:47 +00002150 }
Steve Naroff09c47192009-01-09 15:36:25 +00002151 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002152 if (CDecl->getIdentifier())
2153 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2154 // user-defined setter/getter. It also synthesizes setter/getter methods
2155 // and adds them to the DeclContext and global method pools.
2156 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2157 E = CDecl->prop_end();
2158 I != E; ++I)
2159 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002160 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002161 }
2162 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002163 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002164 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002165 // Any property declared in a class extension might have user
2166 // declared setter or getter in current class extension or one
2167 // of the other class extensions. Mark them as synthesized as
2168 // property will be synthesized when property with same name is
2169 // seen in the @implementation.
2170 for (const ObjCCategoryDecl *ClsExtDecl =
2171 IDecl->getFirstClassExtension();
2172 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2173 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2174 E = ClsExtDecl->prop_end(); I != E; ++I) {
2175 ObjCPropertyDecl *Property = (*I);
2176 // Skip over properties declared @dynamic
2177 if (const ObjCPropertyImplDecl *PIDecl
2178 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2179 if (PIDecl->getPropertyImplementation()
2180 == ObjCPropertyImplDecl::Dynamic)
2181 continue;
2182
2183 for (const ObjCCategoryDecl *CExtDecl =
2184 IDecl->getFirstClassExtension();
2185 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2186 if (ObjCMethodDecl *GetterMethod =
2187 CExtDecl->getInstanceMethod(Property->getGetterName()))
2188 GetterMethod->setSynthesized(true);
2189 if (!Property->isReadOnly())
2190 if (ObjCMethodDecl *SetterMethod =
2191 CExtDecl->getInstanceMethod(Property->getSetterName()))
2192 SetterMethod->setSynthesized(true);
2193 }
2194 }
2195 }
2196
Ted Kremenekc32647d2010-12-23 21:35:43 +00002197 if (LangOpts.ObjCDefaultSynthProperties &&
2198 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002199 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002200 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002201 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002202 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002203
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002204 if (LangOpts.ObjCNonFragileABI2)
2205 while (IDecl->getSuperClass()) {
2206 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2207 IDecl = IDecl->getSuperClass();
2208 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002209 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002210 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002211 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002212 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002213 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002214
Chris Lattner4d391482007-12-12 07:09:47 +00002215 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002216 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002217 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002218 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002219 Categories; Categories = Categories->getNextClassCategory()) {
2220 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002221 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002222 break;
2223 }
2224 }
2225 }
2226 }
Chris Lattner682bf922009-03-29 16:50:03 +00002227 if (isInterfaceDeclKind) {
2228 // Reject invalid vardecls.
2229 for (unsigned i = 0; i != tuvNum; i++) {
2230 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2231 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2232 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002233 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002234 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002235 }
Chris Lattner682bf922009-03-29 16:50:03 +00002236 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002237 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002238 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002239}
2240
2241
2242/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2243/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002244static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002245CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002246 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002247}
2248
Ted Kremenek422bae72010-04-18 04:59:38 +00002249static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002250bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002251 // The 'ibaction' attribute is allowed on method definitions because of
2252 // how the IBAction macro is used on both method declarations and definitions.
2253 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002254 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2255 if ((*i)->getKind() != attr::IBAction)
2256 return true;
2257 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002258}
2259
Douglas Gregor926df6c2011-06-11 01:09:30 +00002260/// \brief Check whether the declared result type of the given Objective-C
2261/// method declaration is compatible with the method's class.
2262///
2263static bool
2264CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2265 ObjCInterfaceDecl *CurrentClass) {
2266 QualType ResultType = Method->getResultType();
2267 SourceRange ResultTypeRange;
2268 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2269 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2270
2271 // If an Objective-C method inherits its related result type, then its
2272 // declared result type must be compatible with its own class type. The
2273 // declared result type is compatible if:
2274 if (const ObjCObjectPointerType *ResultObjectType
2275 = ResultType->getAs<ObjCObjectPointerType>()) {
2276 // - it is id or qualified id, or
2277 if (ResultObjectType->isObjCIdType() ||
2278 ResultObjectType->isObjCQualifiedIdType())
2279 return false;
2280
2281 if (CurrentClass) {
2282 if (ObjCInterfaceDecl *ResultClass
2283 = ResultObjectType->getInterfaceDecl()) {
2284 // - it is the same as the method's class type, or
2285 if (CurrentClass == ResultClass)
2286 return false;
2287
2288 // - it is a superclass of the method's class type
2289 if (ResultClass->isSuperClassOf(CurrentClass))
2290 return false;
2291 }
2292 }
2293 }
2294
2295 return true;
2296}
2297
John McCall6c2c2502011-07-22 02:45:48 +00002298namespace {
2299/// A helper class for searching for methods which a particular method
2300/// overrides.
2301class OverrideSearch {
2302 Sema &S;
2303 ObjCMethodDecl *Method;
2304 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2305 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2306 bool Recursive;
2307
2308public:
2309 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2310 Selector selector = method->getSelector();
2311
2312 // Bypass this search if we've never seen an instance/class method
2313 // with this selector before.
2314 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2315 if (it == S.MethodPool.end()) {
2316 if (!S.ExternalSource) return;
2317 it = S.ReadMethodPool(selector);
2318 }
2319 ObjCMethodList &list =
2320 method->isInstanceMethod() ? it->second.first : it->second.second;
2321 if (!list.Method) return;
2322
2323 ObjCContainerDecl *container
2324 = cast<ObjCContainerDecl>(method->getDeclContext());
2325
2326 // Prevent the search from reaching this container again. This is
2327 // important with categories, which override methods from the
2328 // interface and each other.
2329 Searched.insert(container);
2330 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002331 }
John McCall6c2c2502011-07-22 02:45:48 +00002332
2333 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2334 iterator begin() const { return Overridden.begin(); }
2335 iterator end() const { return Overridden.end(); }
2336
2337private:
2338 void searchFromContainer(ObjCContainerDecl *container) {
2339 if (container->isInvalidDecl()) return;
2340
2341 switch (container->getDeclKind()) {
2342#define OBJCCONTAINER(type, base) \
2343 case Decl::type: \
2344 searchFrom(cast<type##Decl>(container)); \
2345 break;
2346#define ABSTRACT_DECL(expansion)
2347#define DECL(type, base) \
2348 case Decl::type:
2349#include "clang/AST/DeclNodes.inc"
2350 llvm_unreachable("not an ObjC container!");
2351 }
2352 }
2353
2354 void searchFrom(ObjCProtocolDecl *protocol) {
2355 // A method in a protocol declaration overrides declarations from
2356 // referenced ("parent") protocols.
2357 search(protocol->getReferencedProtocols());
2358 }
2359
2360 void searchFrom(ObjCCategoryDecl *category) {
2361 // A method in a category declaration overrides declarations from
2362 // the main class and from protocols the category references.
2363 search(category->getClassInterface());
2364 search(category->getReferencedProtocols());
2365 }
2366
2367 void searchFrom(ObjCCategoryImplDecl *impl) {
2368 // A method in a category definition that has a category
2369 // declaration overrides declarations from the category
2370 // declaration.
2371 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2372 search(category);
2373
2374 // Otherwise it overrides declarations from the class.
2375 } else {
2376 search(impl->getClassInterface());
2377 }
2378 }
2379
2380 void searchFrom(ObjCInterfaceDecl *iface) {
2381 // A method in a class declaration overrides declarations from
2382
2383 // - categories,
2384 for (ObjCCategoryDecl *category = iface->getCategoryList();
2385 category; category = category->getNextClassCategory())
2386 search(category);
2387
2388 // - the super class, and
2389 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2390 search(super);
2391
2392 // - any referenced protocols.
2393 search(iface->getReferencedProtocols());
2394 }
2395
2396 void searchFrom(ObjCImplementationDecl *impl) {
2397 // A method in a class implementation overrides declarations from
2398 // the class interface.
2399 search(impl->getClassInterface());
2400 }
2401
2402
2403 void search(const ObjCProtocolList &protocols) {
2404 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2405 i != e; ++i)
2406 search(*i);
2407 }
2408
2409 void search(ObjCContainerDecl *container) {
2410 // Abort if we've already searched this container.
2411 if (!Searched.insert(container)) return;
2412
2413 // Check for a method in this container which matches this selector.
2414 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2415 Method->isInstanceMethod());
2416
2417 // If we find one, record it and bail out.
2418 if (meth) {
2419 Overridden.insert(meth);
2420 return;
2421 }
2422
2423 // Otherwise, search for methods that a hypothetical method here
2424 // would have overridden.
2425
2426 // Note that we're now in a recursive case.
2427 Recursive = true;
2428
2429 searchFromContainer(container);
2430 }
2431};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002432}
2433
John McCalld226f652010-08-21 09:40:31 +00002434Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002435 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002436 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002437 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002438 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002439 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002440 Selector Sel,
2441 // optional arguments. The number of types/arguments is obtained
2442 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002443 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002444 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002445 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002446 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002447 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002448 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002449 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002450 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002451 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002452 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2453 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002454 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002456 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002457 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002458 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Steve Naroffccef3712009-02-20 22:59:16 +00002460 // Methods cannot return interface types. All ObjC objects are
2461 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002462 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002463 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2464 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002465 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002466 }
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002467 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002468 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002469 Diag(MethodLoc, diag::warn_missing_method_return_type)
2470 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002471 }
Mike Stump1eb44332009-09-09 15:08:12 +00002472
2473 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002474 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002475 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002476 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002477 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002478 /*isSynthesized=*/false,
2479 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002480 MethodDeclKind == tok::objc_optional
2481 ? ObjCMethodDecl::Optional
2482 : ObjCMethodDecl::Required,
2483 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Chris Lattner5f9e2722011-07-23 10:55:15 +00002485 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002486
Chris Lattner7db638d2009-04-11 19:42:43 +00002487 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002488 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002489 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Chris Lattnere294d3f2009-04-11 18:57:04 +00002491 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002492 ArgType = Context.getObjCIdType();
2493 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002494 } else {
John McCall58e46772009-10-23 21:48:59 +00002495 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002496 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002497 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002498 }
Mike Stump1eb44332009-09-09 15:08:12 +00002499
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002500 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2501 LookupOrdinaryName, ForRedeclaration);
2502 LookupName(R, S);
2503 if (R.isSingleResult()) {
2504 NamedDecl *PrevDecl = R.getFoundDecl();
2505 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002506 Diag(ArgInfo[i].NameLoc,
2507 (MethodDefinition ? diag::warn_method_param_redefinition
2508 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002509 << ArgInfo[i].Name;
2510 Diag(PrevDecl->getLocation(),
2511 diag::note_previous_declaration);
2512 }
2513 }
2514
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002515 SourceLocation StartLoc = DI
2516 ? DI->getTypeLoc().getBeginLoc()
2517 : ArgInfo[i].NameLoc;
2518
John McCall81ef3e62011-04-23 02:46:06 +00002519 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2520 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2521 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002522
John McCall70798862011-05-02 00:30:12 +00002523 Param->setObjCMethodScopeInfo(i);
2524
Chris Lattner0ed844b2008-04-04 06:12:32 +00002525 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002526 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002527
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002528 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002529 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002530
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002531 S->AddDecl(Param);
2532 IdResolver.AddDecl(Param);
2533
Chris Lattner0ed844b2008-04-04 06:12:32 +00002534 Params.push_back(Param);
2535 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002536
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002537 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002538 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002539 QualType ArgType = Param->getType();
2540 if (ArgType.isNull())
2541 ArgType = Context.getObjCIdType();
2542 else
2543 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002544 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002545 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002546 Diag(Param->getLocation(),
2547 diag::err_object_cannot_be_passed_returned_by_value)
2548 << 1 << ArgType;
2549 Param->setInvalidDecl();
2550 }
2551 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002552
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002553 Params.push_back(Param);
2554 }
2555
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002556 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2557 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002558 ObjCMethod->setObjCDeclQualifier(
2559 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002560
2561 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002562 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002563
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002564 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002565 const ObjCMethodDecl *PrevMethod = 0;
2566 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002567 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002568 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2569 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002570 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002571 PrevMethod = ImpDecl->getClassMethod(Sel);
2572 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002573 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002574
Sean Huntcf807c42010-08-18 23:23:40 +00002575 if (ObjCMethod->hasAttrs() &&
2576 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002577 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002578 } else {
2579 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002580 }
John McCall6c2c2502011-07-22 02:45:48 +00002581
Chris Lattner4d391482007-12-12 07:09:47 +00002582 if (PrevMethod) {
2583 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002584 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002585 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002586 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002587 }
John McCall54abf7d2009-11-04 02:18:39 +00002588
Douglas Gregor926df6c2011-06-11 01:09:30 +00002589 // If this Objective-C method does not have a related result type, but we
2590 // are allowed to infer related result types, try to do so based on the
2591 // method family.
2592 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2593 if (!CurrentClass) {
2594 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2595 CurrentClass = Cat->getClassInterface();
2596 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2597 CurrentClass = Impl->getClassInterface();
2598 else if (ObjCCategoryImplDecl *CatImpl
2599 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2600 CurrentClass = CatImpl->getClassInterface();
2601 }
John McCall6c2c2502011-07-22 02:45:48 +00002602
2603 bool isRelatedResultTypeCompatible =
2604 (getLangOptions().ObjCInferRelatedResultType &&
2605 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass));
2606
2607 // Search for overridden methods and merge information down from them.
2608 OverrideSearch overrides(*this, ObjCMethod);
2609 for (OverrideSearch::iterator
2610 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2611 ObjCMethodDecl *overridden = *i;
2612
2613 // Propagate down the 'related result type' bit from overridden methods.
2614 if (isRelatedResultTypeCompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002615 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002616
2617 // Then merge the declarations.
2618 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002619
2620 // Check for overriding methods
2621 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2622 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2623 WarnConflictingTypedMethods(ObjCMethod, overridden,
2624 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2625 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002626 }
2627
John McCallf85e1932011-06-15 23:02:42 +00002628 bool ARCError = false;
2629 if (getLangOptions().ObjCAutoRefCount)
2630 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2631
John McCall6c2c2502011-07-22 02:45:48 +00002632 if (!ARCError && isRelatedResultTypeCompatible &&
2633 !ObjCMethod->hasRelatedResultType()) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002634 bool InferRelatedResultType = false;
2635 switch (ObjCMethod->getMethodFamily()) {
2636 case OMF_None:
2637 case OMF_copy:
2638 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002639 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002640 case OMF_mutableCopy:
2641 case OMF_release:
2642 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002643 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002644 break;
2645
2646 case OMF_alloc:
2647 case OMF_new:
2648 InferRelatedResultType = ObjCMethod->isClassMethod();
2649 break;
2650
2651 case OMF_init:
2652 case OMF_autorelease:
2653 case OMF_retain:
2654 case OMF_self:
2655 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2656 break;
2657 }
2658
John McCall6c2c2502011-07-22 02:45:48 +00002659 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002660 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002661 }
2662
John McCalld226f652010-08-21 09:40:31 +00002663 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002664}
2665
Chris Lattnercc98eac2008-12-17 07:13:27 +00002666bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002667 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002668 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002669 // Following is also an error. But it is caused by a missing @end
2670 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002671 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2672 return false;
2673 }
2674
Anders Carlsson15281452008-11-04 16:57:32 +00002675 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2676 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002677
Anders Carlsson15281452008-11-04 16:57:32 +00002678 return true;
2679}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002680
Chris Lattnercc98eac2008-12-17 07:13:27 +00002681/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2682/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002683void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002684 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002685 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002686 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002687 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002688 if (!Class) {
2689 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2690 return;
2691 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002692 if (LangOpts.ObjCNonFragileABI) {
2693 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2694 return;
2695 }
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Chris Lattnercc98eac2008-12-17 07:13:27 +00002697 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002698 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002699 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002700 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002701 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002702 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002703 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002704 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2705 /*FIXME: StartL=*/ID->getLocation(),
2706 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002707 ID->getIdentifier(), ID->getType(),
2708 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002709 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002710 }
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Chris Lattnercc98eac2008-12-17 07:13:27 +00002712 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002713 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002714 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002715 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002716 if (getLangOptions().CPlusPlus)
2717 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002718 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002719 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002720 }
2721}
2722
Douglas Gregor160b5632010-04-26 17:32:49 +00002723/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002724VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2725 SourceLocation StartLoc,
2726 SourceLocation IdLoc,
2727 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002728 bool Invalid) {
2729 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2730 // duration shall not be qualified by an address-space qualifier."
2731 // Since all parameters have automatic store duration, they can not have
2732 // an address space.
2733 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002734 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002735 Invalid = true;
2736 }
2737
2738 // An @catch parameter must be an unqualified object pointer type;
2739 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2740 if (Invalid) {
2741 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002742 } else if (T->isDependentType()) {
2743 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002744 } else if (!T->isObjCObjectPointerType()) {
2745 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002746 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002747 } else if (T->isObjCQualifiedIdType()) {
2748 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002749 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002750 }
2751
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002752 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2753 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002754 New->setExceptionVariable(true);
2755
Douglas Gregor160b5632010-04-26 17:32:49 +00002756 if (Invalid)
2757 New->setInvalidDecl();
2758 return New;
2759}
2760
John McCalld226f652010-08-21 09:40:31 +00002761Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002762 const DeclSpec &DS = D.getDeclSpec();
2763
2764 // We allow the "register" storage class on exception variables because
2765 // GCC did, but we drop it completely. Any other storage class is an error.
2766 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2767 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2768 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2769 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2770 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2771 << DS.getStorageClassSpec();
2772 }
2773 if (D.getDeclSpec().isThreadSpecified())
2774 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2775 D.getMutableDeclSpec().ClearStorageClassSpecs();
2776
2777 DiagnoseFunctionSpecifiers(D);
2778
2779 // Check that there are no default arguments inside the type of this
2780 // exception object (C++ only).
2781 if (getLangOptions().CPlusPlus)
2782 CheckExtraCXXDefaultArguments(D);
2783
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002784 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002785 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002786
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002787 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2788 D.getSourceRange().getBegin(),
2789 D.getIdentifierLoc(),
2790 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002791 D.isInvalidType());
2792
2793 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2794 if (D.getCXXScopeSpec().isSet()) {
2795 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2796 << D.getCXXScopeSpec().getRange();
2797 New->setInvalidDecl();
2798 }
2799
2800 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002801 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002802 if (D.getIdentifier())
2803 IdResolver.AddDecl(New);
2804
2805 ProcessDeclAttributes(S, New, D);
2806
2807 if (New->hasAttr<BlocksAttr>())
2808 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002809 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002810}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002811
2812/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002813/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002814void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002815 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002816 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2817 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002818 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002819 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002820 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002821 }
2822}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002823
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002824void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002825 // Load referenced selectors from the external source.
2826 if (ExternalSource) {
2827 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2828 ExternalSource->ReadReferencedSelectors(Sels);
2829 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2830 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2831 }
2832
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002833 // Warning will be issued only when selector table is
2834 // generated (which means there is at lease one implementation
2835 // in the TU). This is to match gcc's behavior.
2836 if (ReferencedSelectors.empty() ||
2837 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002838 return;
2839 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2840 ReferencedSelectors.begin(),
2841 E = ReferencedSelectors.end(); S != E; ++S) {
2842 Selector Sel = (*S).first;
2843 if (!LookupImplementedMethodInGlobalPool(Sel))
2844 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2845 }
2846 return;
2847}