blob: 0822edf7049a693264125c4924d203fa71c08e50 [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
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000109void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000110 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
Douglas Gregore97179c2011-09-08 01:46:34 +0000151 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
152 Diag(Overridden->getLocation(),
153 diag::note_related_result_type_overridden_family)
154 << Family;
155 else
156 Diag(Overridden->getLocation(),
157 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000158 }
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000159 if (getLangOptions().ObjCAutoRefCount) {
160 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
161 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
162 Diag(NewMethod->getLocation(),
163 diag::err_nsreturns_retained_attribute_mismatch) << 1;
164 Diag(Overridden->getLocation(), diag::note_previous_decl)
165 << "method";
166 }
167 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
168 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
169 Diag(NewMethod->getLocation(),
170 diag::err_nsreturns_retained_attribute_mismatch) << 0;
171 Diag(Overridden->getLocation(), diag::note_previous_decl)
172 << "method";
173 }
174 for (ObjCMethodDecl::param_iterator oi = Overridden->param_begin(),
175 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
176 ni != ne; ++ni, ++oi) {
177 ParmVarDecl *oldDecl = (*oi);
178 ParmVarDecl *newDecl = (*ni);
179 if (newDecl->hasAttr<NSConsumedAttr>() !=
180 oldDecl->hasAttr<NSConsumedAttr>()) {
181 Diag(newDecl->getLocation(),
182 diag::err_nsconsumed_attribute_mismatch);
183 Diag(oldDecl->getLocation(), diag::note_previous_decl)
184 << "parameter";
185 }
186 }
187 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000188}
189
John McCallf85e1932011-06-15 23:02:42 +0000190/// \brief Check a method declaration for compatibility with the Objective-C
191/// ARC conventions.
192static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
193 ObjCMethodFamily family = method->getMethodFamily();
194 switch (family) {
195 case OMF_None:
196 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000197 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000198 case OMF_retain:
199 case OMF_release:
200 case OMF_autorelease:
201 case OMF_retainCount:
202 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000203 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000204 return false;
205
206 case OMF_init:
207 // If the method doesn't obey the init rules, don't bother annotating it.
208 if (S.checkInitMethod(method, QualType()))
209 return true;
210
211 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
212 S.Context));
213
214 // Don't add a second copy of this attribute, but otherwise don't
215 // let it be suppressed.
216 if (method->hasAttr<NSReturnsRetainedAttr>())
217 return false;
218 break;
219
220 case OMF_alloc:
221 case OMF_copy:
222 case OMF_mutableCopy:
223 case OMF_new:
224 if (method->hasAttr<NSReturnsRetainedAttr>() ||
225 method->hasAttr<NSReturnsNotRetainedAttr>() ||
226 method->hasAttr<NSReturnsAutoreleasedAttr>())
227 return false;
228 break;
229 }
230
231 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
232 S.Context));
233 return false;
234}
235
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000236static void DiagnoseObjCImplementedDeprecations(Sema &S,
237 NamedDecl *ND,
238 SourceLocation ImplLoc,
239 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000240 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000241 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000242 if (select == 0)
243 S.Diag(ND->getLocation(), diag::note_method_declared_at);
244 else
245 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
246 }
247}
248
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000249/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
250/// pool.
251void Sema::AddAnyMethodToGlobalPool(Decl *D) {
252 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
253
254 // If we don't have a valid method decl, simply return.
255 if (!MDecl)
256 return;
257 if (MDecl->isInstanceMethod())
258 AddInstanceMethodToGlobalPool(MDecl, true);
259 else
260 AddFactoryMethodToGlobalPool(MDecl, true);
261}
262
Steve Naroffebf64432009-02-28 16:59:13 +0000263/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000264/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000265void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000266 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000267 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Steve Naroff394f3f42008-07-25 17:57:26 +0000269 // If we don't have a valid method decl, simply return.
270 if (!MDecl)
271 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000272
Chris Lattner4d391482007-12-12 07:09:47 +0000273 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000274 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000275 PushFunctionScope();
276
Chris Lattner4d391482007-12-12 07:09:47 +0000277 // Create Decl objects for each parameter, entrring them in the scope for
278 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000279
280 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000281 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Daniel Dunbar451318c2008-08-26 06:07:48 +0000283 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
284 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000285
Chris Lattner8123a952008-04-10 02:22:51 +0000286 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000287 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000288 E = MDecl->param_end(); PI != E; ++PI) {
289 ParmVarDecl *Param = (*PI);
290 if (!Param->isInvalidDecl() &&
291 RequireCompleteType(Param->getLocation(), Param->getType(),
292 diag::err_typecheck_decl_incomplete_type))
293 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000294 if ((*PI)->getIdentifier())
295 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000296 }
John McCallf85e1932011-06-15 23:02:42 +0000297
298 // In ARC, disallow definition of retain/release/autorelease/retainCount
299 if (getLangOptions().ObjCAutoRefCount) {
300 switch (MDecl->getMethodFamily()) {
301 case OMF_retain:
302 case OMF_retainCount:
303 case OMF_release:
304 case OMF_autorelease:
305 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
306 << MDecl->getSelector();
307 break;
308
309 case OMF_None:
310 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000311 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000312 case OMF_alloc:
313 case OMF_init:
314 case OMF_mutableCopy:
315 case OMF_copy:
316 case OMF_new:
317 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000318 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000319 break;
320 }
321 }
322
Nico Weber9a1ecf02011-08-22 17:25:57 +0000323 // Warn on deprecated methods under -Wdeprecated-implementations,
324 // and prepare for warning on missing super calls.
325 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000326 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000327 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000328 DiagnoseObjCImplementedDeprecations(*this,
329 dyn_cast<NamedDecl>(IMD),
330 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000331
Nico Weber80cb6e62011-08-28 22:35:17 +0000332 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000333 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
334 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
335 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000336 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000337 ObjCShouldCallSuperDealloc =
338 !Context.getLangOptions().ObjCAutoRefCount &&
339 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000340 ObjCShouldCallSuperFinalize =
341 !Context.getLangOptions().ObjCAutoRefCount &&
342 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000343 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000344 }
Chris Lattner4d391482007-12-12 07:09:47 +0000345}
346
John McCalld226f652010-08-21 09:40:31 +0000347Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000348ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
349 IdentifierInfo *ClassName, SourceLocation ClassLoc,
350 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000351 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000352 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000353 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000354 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattner4d391482007-12-12 07:09:47 +0000356 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000357 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000358 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000359
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000360 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000361 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000362 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000365 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
366 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000367 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000368 if (!IDecl->isForwardDecl()) {
369 IDecl->setInvalidDecl();
370 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
371 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000372
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000373 // Return the previous class interface.
374 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000375 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000376 } else {
377 IDecl->setLocation(AtInterfaceLoc);
378 IDecl->setForwardDecl(false);
379 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000380 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000381 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000382 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000383
384 // Since this ObjCInterfaceDecl was created by a forward declaration,
385 // we now add it to the DeclContext since it wasn't added before
386 // (see ActOnForwardClassDeclaration).
387 IDecl->setLexicalDeclContext(CurContext);
388 CurContext->addDecl(IDecl);
389
390 if (AttrList)
391 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000392 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000393 } else {
394 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
395 ClassName, ClassLoc);
396 if (AttrList)
397 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
398
399 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner4d391482007-12-12 07:09:47 +0000402 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000403 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000404 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
405 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000406
407 if (!PrevDecl) {
408 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000409 TypoCorrection Corrected = CorrectTypo(
410 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
411 NULL, NULL, false, CTC_NoKeywords);
412 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000413 Diag(SuperLoc, diag::err_undef_superclass_suggest)
414 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000415 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
416 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000417 }
418 }
419
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000420 if (PrevDecl == IDecl) {
421 Diag(SuperLoc, diag::err_recursive_superclass)
422 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
423 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000424 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000425 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000426 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000427
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000428 // Diagnose classes that inherit from deprecated classes.
429 if (SuperClassDecl)
430 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000432 if (PrevDecl && SuperClassDecl == 0) {
433 // The previous declaration was not a class decl. Check if we have a
434 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000435 if (const TypedefNameDecl *TDecl =
436 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000437 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000438 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000439 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
440 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000441 }
442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000444 // This handles the following case:
445 //
446 // typedef int SuperClass;
447 // @interface MyClass : SuperClass {} @end
448 //
449 if (!SuperClassDecl) {
450 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
451 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000452 }
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Richard Smith162e1c12011-04-15 14:24:37 +0000455 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000456 if (!SuperClassDecl)
457 Diag(SuperLoc, diag::err_undef_superclass)
458 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000459 else if (SuperClassDecl->isForwardDecl()) {
460 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000461 << SuperClassDecl->getDeclName() << ClassName
462 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000463 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
464 SuperClassDecl = 0;
465 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000466 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000467 IDecl->setSuperClass(SuperClassDecl);
468 IDecl->setSuperClassLoc(SuperLoc);
469 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000470 }
Chris Lattner4d391482007-12-12 07:09:47 +0000471 } else { // we have a root class.
472 IDecl->setLocEnd(ClassLoc);
473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Sebastian Redl0b17c612010-08-13 00:28:03 +0000475 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000476 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000477 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000478 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000479 IDecl->setLocEnd(EndProtoLoc);
480 }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Anders Carlsson15281452008-11-04 16:57:32 +0000482 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000483 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000484}
485
486/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000487/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000488Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
489 IdentifierInfo *AliasName,
490 SourceLocation AliasLocation,
491 IdentifierInfo *ClassName,
492 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000493 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000494 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000495 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000496 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000497 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000498 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000499 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000500 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000501 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000502 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000503 }
504 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000505 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000506 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000507 if (const TypedefNameDecl *TDecl =
508 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000509 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000510 if (T->isObjCObjectType()) {
511 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000512 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000513 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000514 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000515 }
516 }
517 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000518 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
519 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000520 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000521 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000522 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000523 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000526 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000527 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000528 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Anders Carlsson15281452008-11-04 16:57:32 +0000530 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000531 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000532
John McCalld226f652010-08-21 09:40:31 +0000533 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000534}
535
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000536bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000537 IdentifierInfo *PName,
538 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000539 const ObjCList<ObjCProtocolDecl> &PList) {
540
541 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000542 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
543 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000544 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
545 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000546 if (PDecl->getIdentifier() == PName) {
547 Diag(Ploc, diag::err_protocol_has_circular_dependency);
548 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000549 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000550 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000551 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
552 PDecl->getLocation(), PDecl->getReferencedProtocols()))
553 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000554 }
555 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000556 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000557}
558
John McCalld226f652010-08-21 09:40:31 +0000559Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000560Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
561 IdentifierInfo *ProtocolName,
562 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000563 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000564 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000565 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000566 SourceLocation EndProtoLoc,
567 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000568 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000569 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000570 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000571 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000572 if (PDecl) {
573 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000574 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000575 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000576 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000577 // Just return the protocol we already had.
578 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000579 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000580 }
Steve Naroff61d68522009-03-05 15:22:01 +0000581 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000582 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000583 err = CheckForwardProtocolDeclarationForCircularDependency(
584 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Steve Narofff11b5082008-08-13 16:39:22 +0000586 // Make sure the cached decl gets a valid start location.
587 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000588 PDecl->setForwardDecl(false);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000589 // Since this ObjCProtocolDecl was created by a forward declaration,
590 // we now add it to the DeclContext since it wasn't added before
591 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000592 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000593 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000594 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000595 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000596 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000597 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000598 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000599 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000600 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000601 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000602 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000603 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000604 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000605 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
606 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000607 PDecl->setLocEnd(EndProtoLoc);
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
610 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000611 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000612}
613
614/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000615/// issues an error if they are not declared. It returns list of
616/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000617void
Chris Lattnere13b9592008-07-26 04:03:38 +0000618Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000619 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000620 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000621 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000622 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000623 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
624 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000625 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000626 TypoCorrection Corrected = CorrectTypo(
627 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
628 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
629 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000630 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000631 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000632 Diag(PDecl->getLocation(), diag::note_previous_decl)
633 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000634 }
635 }
636
637 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000638 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000640 continue;
641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000643 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000644
645 // If this is a forward declaration and we are supposed to warn in this
646 // case, do it.
647 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000648 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000650 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000651 }
652}
653
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000654/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000655/// a class method in its extension.
656///
Mike Stump1eb44332009-09-09 15:08:12 +0000657void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000658 ObjCInterfaceDecl *ID) {
659 if (!ID)
660 return; // Possibly due to previous error
661
662 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000663 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
664 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000665 ObjCMethodDecl *MD = *i;
666 MethodMap[MD->getSelector()] = MD;
667 }
668
669 if (MethodMap.empty())
670 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000671 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
672 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000673 ObjCMethodDecl *Method = *i;
674 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
675 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
676 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
677 << Method->getDeclName();
678 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
679 }
680 }
681}
682
Chris Lattner58fe03b2009-04-12 08:43:13 +0000683/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000684Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000685Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000686 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000687 unsigned NumElts,
688 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000689 SmallVector<ObjCProtocolDecl*, 32> Protocols;
690 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattner4d391482007-12-12 07:09:47 +0000692 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000693 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000694 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000695 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000696 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000697 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000698 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000699 PushOnScopeChains(PDecl, TUScope, false);
700 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000701 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000702 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000703 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000704 if (!isNew)
705 PDecl->setChangedSinceDeserialization(true);
706 }
Chris Lattner4d391482007-12-12 07:09:47 +0000707 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000708 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
711 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000712 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000713 Protocols.data(), Protocols.size(),
714 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000715 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000716 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000717 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000718}
719
John McCalld226f652010-08-21 09:40:31 +0000720Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000721ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
722 IdentifierInfo *ClassName, SourceLocation ClassLoc,
723 IdentifierInfo *CategoryName,
724 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000725 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000726 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000727 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000728 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000729 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000730 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000731
732 /// Check that class of this category is already completely declared.
733 if (!IDecl || IDecl->isForwardDecl()) {
734 // Create an invalid ObjCCategoryDecl to serve as context for
735 // the enclosing method declarations. We mark the decl invalid
736 // to make it clear that this isn't a valid AST.
737 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000738 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000739 CDecl->setInvalidDecl();
740 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000741 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000742 }
743
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000744 if (!CategoryName && IDecl->getImplementation()) {
745 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
746 Diag(IDecl->getImplementation()->getLocation(),
747 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000748 }
749
Fariborz Jahanian25760612010-02-15 21:55:26 +0000750 if (CategoryName) {
751 /// Check for duplicate interface declaration for this category
752 ObjCCategoryDecl *CDeclChain;
753 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
754 CDeclChain = CDeclChain->getNextClassCategory()) {
755 if (CDeclChain->getIdentifier() == CategoryName) {
756 // Class extensions can be declared multiple times.
757 Diag(CategoryLoc, diag::warn_dup_category_def)
758 << ClassName << CategoryName;
759 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
760 break;
761 }
Chris Lattner70f19542009-02-16 21:26:43 +0000762 }
763 }
Chris Lattner70f19542009-02-16 21:26:43 +0000764
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000765 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
766 ClassLoc, CategoryLoc, CategoryName, IDecl);
767 // FIXME: PushOnScopeChains?
768 CurContext->addDecl(CDecl);
769
770 // If the interface is deprecated, warn about it.
771 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
772
Chris Lattner4d391482007-12-12 07:09:47 +0000773 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000774 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000775 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000776 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000777 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000778 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000779 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Anders Carlsson15281452008-11-04 16:57:32 +0000782 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000783 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000784}
785
786/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000788/// object.
John McCalld226f652010-08-21 09:40:31 +0000789Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000790 SourceLocation AtCatImplLoc,
791 IdentifierInfo *ClassName, SourceLocation ClassLoc,
792 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000793 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000794 ObjCCategoryDecl *CatIDecl = 0;
795 if (IDecl) {
796 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
797 if (!CatIDecl) {
798 // Category @implementation with no corresponding @interface.
799 // Create and install one.
800 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000801 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000802 CatName, IDecl);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000803 }
804 }
805
Mike Stump1eb44332009-09-09 15:08:12 +0000806 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000807 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
808 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000809 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000810 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000811 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000812 CDecl->setInvalidDecl();
813 }
Chris Lattner4d391482007-12-12 07:09:47 +0000814
Douglas Gregord0434102009-01-09 00:49:46 +0000815 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000817
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000818 /// Check that CatName, category name, is not used in another implementation.
819 if (CatIDecl) {
820 if (CatIDecl->getImplementation()) {
821 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
822 << CatName;
823 Diag(CatIDecl->getImplementation()->getLocation(),
824 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000825 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000826 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000827 // Warn on implementating category of deprecated class under
828 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000829 DiagnoseObjCImplementedDeprecations(*this,
830 dyn_cast<NamedDecl>(IDecl),
831 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000832 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Anders Carlsson15281452008-11-04 16:57:32 +0000835 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000836 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000837}
838
John McCalld226f652010-08-21 09:40:31 +0000839Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000840 SourceLocation AtClassImplLoc,
841 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000842 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000843 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000844 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000845 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000846 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000847 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
848 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000849 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000850 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000851 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000852 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
853 // If this is a forward declaration of an interface, warn.
854 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000855 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000856 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000857 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000858 } else {
859 // We did not find anything with the name ClassName; try to correct for
860 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000861 TypoCorrection Corrected = CorrectTypo(
862 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
863 NULL, NULL, false, CTC_NoKeywords);
864 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000865 // Suggest the (potentially) correct interface name. However, put the
866 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000867 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000868 // provide a code-modification hint or use the typo name for recovery,
869 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000870 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000871 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000872 << ClassName << CorrectedName;
873 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
874 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000875 IDecl = 0;
876 } else {
877 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
878 }
Chris Lattner4d391482007-12-12 07:09:47 +0000879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Chris Lattner4d391482007-12-12 07:09:47 +0000881 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000883 if (SuperClassname) {
884 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000885 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
886 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000887 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000888 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
889 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000890 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000891 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000892 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000893 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000894 Diag(SuperClassLoc, diag::err_undef_superclass)
895 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000896 else if (IDecl && IDecl->getSuperClass() != SDecl) {
897 // This implementation and its interface do not have the same
898 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000899 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000900 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000901 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000902 }
903 }
904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Chris Lattner4d391482007-12-12 07:09:47 +0000906 if (!IDecl) {
907 // Legacy case of @implementation with no corresponding @interface.
908 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000909
Mike Stump390b4cc2009-05-16 07:39:55 +0000910 // FIXME: Do we support attributes on the @implementation? If so we should
911 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000912 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000913 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000914 IDecl->setSuperClass(SDecl);
915 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000916
917 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000918 } else {
919 // Mark the interface as being completed, even if it was just as
920 // @class ....;
921 // declaration; the user cannot reopen it.
922 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000923 }
Mike Stump1eb44332009-09-09 15:08:12 +0000924
925 ObjCImplementationDecl* IMPDecl =
926 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000927 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Anders Carlsson15281452008-11-04 16:57:32 +0000929 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000930 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattner4d391482007-12-12 07:09:47 +0000932 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000933 if (IDecl->getImplementation()) {
934 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000935 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000936 Diag(IDecl->getImplementation()->getLocation(),
937 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000938 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000939 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000940 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000941 // Warn on implementating deprecated class under
942 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000943 DiagnoseObjCImplementedDeprecations(*this,
944 dyn_cast<NamedDecl>(IDecl),
945 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000946 }
John McCalld226f652010-08-21 09:40:31 +0000947 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000948}
949
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000950void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
951 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000952 SourceLocation RBrace) {
953 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000954 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000955 if (!IDecl)
956 return;
957 /// Check case of non-existing @interface decl.
958 /// (legacy objective-c @implementation decl without an @interface decl).
959 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000960 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000961 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000962 // Add ivar's to class's DeclContext.
963 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000964 ivars[i]->setLexicalDeclContext(ImpDecl);
965 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000966 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000967 }
968
Chris Lattner4d391482007-12-12 07:09:47 +0000969 return;
970 }
971 // If implementation has empty ivar list, just return.
972 if (numIvars == 0)
973 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Chris Lattner4d391482007-12-12 07:09:47 +0000975 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000976 if (LangOpts.ObjCNonFragileABI2) {
977 if (ImpDecl->getSuperClass())
978 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
979 for (unsigned i = 0; i < numIvars; i++) {
980 ObjCIvarDecl* ImplIvar = ivars[i];
981 if (const ObjCIvarDecl *ClsIvar =
982 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
983 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
984 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
985 continue;
986 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000987 // Instance ivar to Implementation's DeclContext.
988 ImplIvar->setLexicalDeclContext(ImpDecl);
989 IDecl->makeDeclVisibleInContext(ImplIvar, false);
990 ImpDecl->addDecl(ImplIvar);
991 }
992 return;
993 }
Chris Lattner4d391482007-12-12 07:09:47 +0000994 // Check interface's Ivar list against those in the implementation.
995 // names and types must match.
996 //
Chris Lattner4d391482007-12-12 07:09:47 +0000997 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000998 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000999 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1000 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001001 ObjCIvarDecl* ImplIvar = ivars[j++];
1002 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001003 assert (ImplIvar && "missing implementation ivar");
1004 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Steve Naroffca331292009-03-03 14:49:36 +00001006 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +00001007 if (Context.getCanonicalType(ImplIvar->getType()) !=
1008 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001009 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001010 << ImplIvar->getIdentifier()
1011 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001012 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +00001013 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
1014 Expr *ImplBitWidth = ImplIvar->getBitWidth();
1015 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001016 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
1017 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +00001018 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
1019 << ImplIvar->getIdentifier();
1020 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
1021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022 }
Steve Naroffca331292009-03-03 14:49:36 +00001023 // Make sure the names are identical.
1024 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001025 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001026 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001027 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001028 }
1029 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001030 }
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattner609e4c72007-12-12 18:11:49 +00001032 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001033 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001034 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001035 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001036}
1037
Steve Naroff3c2eb662008-02-10 21:38:56 +00001038void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001039 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001040 // No point warning no definition of method which is 'unavailable'.
1041 if (method->hasAttr<UnavailableAttr>())
1042 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001043 if (!IncompleteImpl) {
1044 Diag(ImpLoc, diag::warn_incomplete_impl);
1045 IncompleteImpl = true;
1046 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001047 if (DiagID == diag::warn_unimplemented_protocol_method)
1048 Diag(ImpLoc, DiagID) << method->getDeclName();
1049 else
1050 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001051}
1052
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001053/// Determines if type B can be substituted for type A. Returns true if we can
1054/// guarantee that anything that the user will do to an object of type A can
1055/// also be done to an object of type B. This is trivially true if the two
1056/// types are the same, or if B is a subclass of A. It becomes more complex
1057/// in cases where protocols are involved.
1058///
1059/// Object types in Objective-C describe the minimum requirements for an
1060/// object, rather than providing a complete description of a type. For
1061/// example, if A is a subclass of B, then B* may refer to an instance of A.
1062/// The principle of substitutability means that we may use an instance of A
1063/// anywhere that we may use an instance of B - it will implement all of the
1064/// ivars of B and all of the methods of B.
1065///
1066/// This substitutability is important when type checking methods, because
1067/// the implementation may have stricter type definitions than the interface.
1068/// The interface specifies minimum requirements, but the implementation may
1069/// have more accurate ones. For example, a method may privately accept
1070/// instances of B, but only publish that it accepts instances of A. Any
1071/// object passed to it will be type checked against B, and so will implicitly
1072/// by a valid A*. Similarly, a method may return a subclass of the class that
1073/// it is declared as returning.
1074///
1075/// This is most important when considering subclassing. A method in a
1076/// subclass must accept any object as an argument that its superclass's
1077/// implementation accepts. It may, however, accept a more general type
1078/// without breaking substitutability (i.e. you can still use the subclass
1079/// anywhere that you can use the superclass, but not vice versa). The
1080/// converse requirement applies to return types: the return type for a
1081/// subclass method must be a valid object of the kind that the superclass
1082/// advertises, but it may be specified more accurately. This avoids the need
1083/// for explicit down-casting by callers.
1084///
1085/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001086static bool isObjCTypeSubstitutable(ASTContext &Context,
1087 const ObjCObjectPointerType *A,
1088 const ObjCObjectPointerType *B,
1089 bool rejectId) {
1090 // Reject a protocol-unqualified id.
1091 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001092
1093 // If B is a qualified id, then A must also be a qualified id and it must
1094 // implement all of the protocols in B. It may not be a qualified class.
1095 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1096 // stricter definition so it is not substitutable for id<A>.
1097 if (B->isObjCQualifiedIdType()) {
1098 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001099 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1100 QualType(B,0),
1101 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001102 }
1103
1104 /*
1105 // id is a special type that bypasses type checking completely. We want a
1106 // warning when it is used in one place but not another.
1107 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1108
1109
1110 // If B is a qualified id, then A must also be a qualified id (which it isn't
1111 // if we've got this far)
1112 if (B->isObjCQualifiedIdType()) return false;
1113 */
1114
1115 // Now we know that A and B are (potentially-qualified) class types. The
1116 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001117 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001118}
1119
John McCall10302c02010-10-28 02:34:38 +00001120static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1121 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1122}
1123
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001124static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001125 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001126 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001127 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001128 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001129 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001130 if (IsProtocolMethodDecl &&
1131 (MethodDecl->getObjCDeclQualifier() !=
1132 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001133 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001134 S.Diag(MethodImpl->getLocation(),
1135 (IsOverridingMode ?
1136 diag::warn_conflicting_overriding_ret_type_modifiers
1137 : diag::warn_conflicting_ret_type_modifiers))
1138 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001139 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1140 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1141 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1142 }
1143 else
1144 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001145 }
1146
John McCall10302c02010-10-28 02:34:38 +00001147 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001148 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001149 return true;
1150 if (!Warn)
1151 return false;
John McCall10302c02010-10-28 02:34:38 +00001152
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001153 unsigned DiagID =
1154 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1155 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001156
1157 // Mismatches between ObjC pointers go into a different warning
1158 // category, and sometimes they're even completely whitelisted.
1159 if (const ObjCObjectPointerType *ImplPtrTy =
1160 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1161 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001162 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001163 // Allow non-matching return types as long as they don't violate
1164 // the principle of substitutability. Specifically, we permit
1165 // return types that are subclasses of the declared return type,
1166 // or that are more-qualified versions of the declared type.
1167 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001168 return false;
John McCall10302c02010-10-28 02:34:38 +00001169
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001170 DiagID =
1171 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1172 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001173 }
1174 }
1175
1176 S.Diag(MethodImpl->getLocation(), DiagID)
1177 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001178 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001179 << MethodImpl->getResultType()
1180 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001181 S.Diag(MethodDecl->getLocation(),
1182 IsOverridingMode ? diag::note_previous_declaration
1183 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001184 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001185 return false;
John McCall10302c02010-10-28 02:34:38 +00001186}
1187
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001188static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001189 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001190 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001191 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001192 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001193 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001194 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001195 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001196 if (IsProtocolMethodDecl &&
1197 (ImplVar->getObjCDeclQualifier() !=
1198 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001199 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001200 if (IsOverridingMode)
1201 S.Diag(ImplVar->getLocation(),
1202 diag::warn_conflicting_overriding_param_modifiers)
1203 << getTypeRange(ImplVar->getTypeSourceInfo())
1204 << MethodImpl->getDeclName();
1205 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001206 diag::warn_conflicting_param_modifiers)
1207 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001208 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001209 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1210 << getTypeRange(IfaceVar->getTypeSourceInfo());
1211 }
1212 else
1213 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001214 }
1215
John McCall10302c02010-10-28 02:34:38 +00001216 QualType ImplTy = ImplVar->getType();
1217 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001218
John McCall10302c02010-10-28 02:34:38 +00001219 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001220 return true;
1221
1222 if (!Warn)
1223 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001224 unsigned DiagID =
1225 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1226 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001227
1228 // Mismatches between ObjC pointers go into a different warning
1229 // category, and sometimes they're even completely whitelisted.
1230 if (const ObjCObjectPointerType *ImplPtrTy =
1231 ImplTy->getAs<ObjCObjectPointerType>()) {
1232 if (const ObjCObjectPointerType *IfacePtrTy =
1233 IfaceTy->getAs<ObjCObjectPointerType>()) {
1234 // Allow non-matching argument types as long as they don't
1235 // violate the principle of substitutability. Specifically, the
1236 // implementation must accept any objects that the superclass
1237 // accepts, however it may also accept others.
1238 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001239 return false;
John McCall10302c02010-10-28 02:34:38 +00001240
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001241 DiagID =
1242 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1243 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001244 }
1245 }
1246
1247 S.Diag(ImplVar->getLocation(), DiagID)
1248 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001249 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1250 S.Diag(IfaceVar->getLocation(),
1251 (IsOverridingMode ? diag::note_previous_declaration
1252 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001253 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001254 return false;
John McCall10302c02010-10-28 02:34:38 +00001255}
John McCallf85e1932011-06-15 23:02:42 +00001256
1257/// In ARC, check whether the conventional meanings of the two methods
1258/// match. If they don't, it's a hard error.
1259static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1260 ObjCMethodDecl *decl) {
1261 ObjCMethodFamily implFamily = impl->getMethodFamily();
1262 ObjCMethodFamily declFamily = decl->getMethodFamily();
1263 if (implFamily == declFamily) return false;
1264
1265 // Since conventions are sorted by selector, the only possibility is
1266 // that the types differ enough to cause one selector or the other
1267 // to fall out of the family.
1268 assert(implFamily == OMF_None || declFamily == OMF_None);
1269
1270 // No further diagnostics required on invalid declarations.
1271 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1272
1273 const ObjCMethodDecl *unmatched = impl;
1274 ObjCMethodFamily family = declFamily;
1275 unsigned errorID = diag::err_arc_lost_method_convention;
1276 unsigned noteID = diag::note_arc_lost_method_convention;
1277 if (declFamily == OMF_None) {
1278 unmatched = decl;
1279 family = implFamily;
1280 errorID = diag::err_arc_gained_method_convention;
1281 noteID = diag::note_arc_gained_method_convention;
1282 }
1283
1284 // Indexes into a %select clause in the diagnostic.
1285 enum FamilySelector {
1286 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1287 };
1288 FamilySelector familySelector = FamilySelector();
1289
1290 switch (family) {
1291 case OMF_None: llvm_unreachable("logic error, no method convention");
1292 case OMF_retain:
1293 case OMF_release:
1294 case OMF_autorelease:
1295 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001296 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001297 case OMF_retainCount:
1298 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001299 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001300 // Mismatches for these methods don't change ownership
1301 // conventions, so we don't care.
1302 return false;
1303
1304 case OMF_init: familySelector = F_init; break;
1305 case OMF_alloc: familySelector = F_alloc; break;
1306 case OMF_copy: familySelector = F_copy; break;
1307 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1308 case OMF_new: familySelector = F_new; break;
1309 }
1310
1311 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1312 ReasonSelector reasonSelector;
1313
1314 // The only reason these methods don't fall within their families is
1315 // due to unusual result types.
1316 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1317 reasonSelector = R_UnrelatedReturn;
1318 } else {
1319 reasonSelector = R_NonObjectReturn;
1320 }
1321
1322 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1323 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1324
1325 return true;
1326}
John McCall10302c02010-10-28 02:34:38 +00001327
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001328void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001329 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001330 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001331 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001332 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001333 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001334 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1335 return;
1336
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001337 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001338 IsProtocolMethodDecl, IsOverridingMode,
1339 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Chris Lattner3aff9192009-04-11 19:58:42 +00001341 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001342 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001343 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001344 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1345 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001346 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001347
Fariborz Jahanian21121902011-08-08 18:03:17 +00001348 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001349 if (IsOverridingMode)
1350 Diag(ImpMethodDecl->getLocation(),
1351 diag::warn_conflicting_overriding_variadic);
1352 else
1353 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001354 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001355 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001356}
1357
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001358/// WarnExactTypedMethods - This routine issues a warning if method
1359/// implementation declaration matches exactly that of its declaration.
1360void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1361 ObjCMethodDecl *MethodDecl,
1362 bool IsProtocolMethodDecl) {
1363 // don't issue warning when protocol method is optional because primary
1364 // class is not required to implement it and it is safe for protocol
1365 // to implement it.
1366 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1367 return;
1368 // don't issue warning when primary class's method is
1369 // depecated/unavailable.
1370 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1371 MethodDecl->hasAttr<DeprecatedAttr>())
1372 return;
1373
1374 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1375 IsProtocolMethodDecl, false, false);
1376 if (match)
1377 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1378 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1379 IM != EM; ++IM, ++IF) {
1380 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1381 *IM, *IF,
1382 IsProtocolMethodDecl, false, false);
1383 if (!match)
1384 break;
1385 }
1386 if (match)
1387 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001388 if (match)
1389 match = !(MethodDecl->isClassMethod() &&
1390 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001391
1392 if (match) {
1393 Diag(ImpMethodDecl->getLocation(),
1394 diag::warn_category_method_impl_match);
1395 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1396 }
1397}
1398
Mike Stump390b4cc2009-05-16 07:39:55 +00001399/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1400/// improve the efficiency of selector lookups and type checking by associating
1401/// with each protocol / interface / category the flattened instance tables. If
1402/// we used an immutable set to keep the table then it wouldn't add significant
1403/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001404
Steve Naroffefe7f362008-02-08 22:06:17 +00001405/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001406/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001407void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1408 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001409 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001410 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001411 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001412 ObjCContainerDecl *CDecl) {
1413 ObjCInterfaceDecl *IDecl;
1414 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1415 IDecl = C->getClassInterface();
1416 else
1417 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1418 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1419
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001420 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001421 ObjCInterfaceDecl *NSIDecl = 0;
1422 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001423 // check to see if class implements forwardInvocation method and objects
1424 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001425 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001426 // Under such conditions, which means that every method possible is
1427 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001428 // found" warnings.
1429 // FIXME: Use a general GetUnarySelector method for this.
1430 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1431 Selector fISelector = Context.Selectors.getSelector(1, &II);
1432 if (InsMap.count(fISelector))
1433 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1434 // need be implemented in the implementation.
1435 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1436 }
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001438 // If a method lookup fails locally we still need to look and see if
1439 // the method was implemented by a base class or an inherited
1440 // protocol. This lookup is slow, but occurs rarely in correct code
1441 // and otherwise would terminate in a warning.
1442
Chris Lattner4d391482007-12-12 07:09:47 +00001443 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001444 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001445 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001446 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001447 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001448 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001449 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001450 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001451 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001452 // Ugly, but necessary. Method declared in protcol might have
1453 // have been synthesized due to a property declared in the class which
1454 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001455 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001456 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001457 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001458 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001459 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001460 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001461 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001462 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001463 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1464 << PDecl->getDeclName();
1465 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001466 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001467 }
1468 }
Chris Lattner4d391482007-12-12 07:09:47 +00001469 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001470 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001471 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001472 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001474 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1475 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001476 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001477 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001478 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1479 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001480 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001481 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001482 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1483 PDecl->getDeclName();
1484 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001485 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001486 }
Chris Lattner780f3292008-07-21 21:32:27 +00001487 // Check on this protocols's referenced protocols, recursively.
1488 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1489 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001490 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001491}
1492
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001493/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001494/// or protocol against those declared in their implementations.
1495///
1496void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1497 const llvm::DenseSet<Selector> &ClsMap,
1498 llvm::DenseSet<Selector> &InsMapSeen,
1499 llvm::DenseSet<Selector> &ClsMapSeen,
1500 ObjCImplDecl* IMPDecl,
1501 ObjCContainerDecl* CDecl,
1502 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001503 bool ImmediateClass,
1504 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001505 // Check and see if instance methods in class interface have been
1506 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001507 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1508 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001509 if (InsMapSeen.count((*I)->getSelector()))
1510 continue;
1511 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001512 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001513 !InsMap.count((*I)->getSelector())) {
1514 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001515 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1516 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001517 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001518 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001519 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001520 IMPDecl->getInstanceMethod((*I)->getSelector());
1521 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1522 "Expected to find the method through lookup as well");
1523 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001524 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001525 if (ImpMethodDecl) {
1526 if (!WarnExactMatch)
1527 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1528 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001529 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001530 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1531 isa<ObjCProtocolDecl>(CDecl));
1532 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001533 }
1534 }
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001536 // Check and see if class methods in class interface have been
1537 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001538 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001539 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001540 if (ClsMapSeen.count((*I)->getSelector()))
1541 continue;
1542 ClsMapSeen.insert((*I)->getSelector());
1543 if (!ClsMap.count((*I)->getSelector())) {
1544 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001545 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1546 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001547 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001548 ObjCMethodDecl *ImpMethodDecl =
1549 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001550 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1551 "Expected to find the method through lookup as well");
1552 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001553 if (!WarnExactMatch)
1554 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1555 isa<ObjCProtocolDecl>(CDecl));
1556 else
1557 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1558 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001559 }
1560 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001561
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001562 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001563 // Also methods in class extensions need be looked at next.
1564 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1565 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1566 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1567 IMPDecl,
1568 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001569 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001570
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001571 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001572 for (ObjCInterfaceDecl::all_protocol_iterator
1573 PI = I->all_referenced_protocol_begin(),
1574 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001575 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1576 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001577 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001578
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001579 // FIXME. For now, we are not checking for extact match of methods
1580 // in category implementation and its primary class's super class.
1581 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001582 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001583 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001584 I->getSuperClass(), IncompleteImpl, false);
1585 }
1586}
1587
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001588/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1589/// category matches with those implemented in its primary class and
1590/// warns each time an exact match is found.
1591void Sema::CheckCategoryVsClassMethodMatches(
1592 ObjCCategoryImplDecl *CatIMPDecl) {
1593 llvm::DenseSet<Selector> InsMap, ClsMap;
1594
1595 for (ObjCImplementationDecl::instmeth_iterator
1596 I = CatIMPDecl->instmeth_begin(),
1597 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1598 InsMap.insert((*I)->getSelector());
1599
1600 for (ObjCImplementationDecl::classmeth_iterator
1601 I = CatIMPDecl->classmeth_begin(),
1602 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1603 ClsMap.insert((*I)->getSelector());
1604 if (InsMap.empty() && ClsMap.empty())
1605 return;
1606
1607 // Get category's primary class.
1608 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1609 if (!CatDecl)
1610 return;
1611 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1612 if (!IDecl)
1613 return;
1614 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1615 bool IncompleteImpl = false;
1616 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1617 CatIMPDecl, IDecl,
1618 IncompleteImpl, false, true /*WarnExactMatch*/);
1619}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001620
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001621void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001622 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001623 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001624 llvm::DenseSet<Selector> InsMap;
1625 // Check and see if instance methods in class interface have been
1626 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001627 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001628 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001629 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001631 // Check and see if properties declared in the interface have either 1)
1632 // an implementation or 2) there is a @synthesize/@dynamic implementation
1633 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001634 if (isa<ObjCInterfaceDecl>(CDecl) &&
1635 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001636 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001637
Chris Lattner4d391482007-12-12 07:09:47 +00001638 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001639 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001640 I = IMPDecl->classmeth_begin(),
1641 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001642 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001644 // Check for type conflict of methods declared in a class/protocol and
1645 // its implementation; if any.
1646 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001647 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1648 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001649 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001650
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001651 // check all methods implemented in category against those declared
1652 // in its primary class.
1653 if (ObjCCategoryImplDecl *CatDecl =
1654 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1655 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Chris Lattner4d391482007-12-12 07:09:47 +00001657 // Check the protocol list for unimplemented methods in the @implementation
1658 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001659 // Check and see if class methods in class interface have been
1660 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Chris Lattnercddc8882009-03-01 00:56:52 +00001662 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001663 for (ObjCInterfaceDecl::all_protocol_iterator
1664 PI = I->all_referenced_protocol_begin(),
1665 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001666 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001667 InsMap, ClsMap, I);
1668 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001669 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1670 Categories; Categories = Categories->getNextClassExtension())
1671 ImplMethodsVsClassMethods(S, IMPDecl,
1672 const_cast<ObjCCategoryDecl*>(Categories),
1673 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001674 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001675 // For extended class, unimplemented methods in its protocols will
1676 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001677 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001678 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1679 E = C->protocol_end(); PI != E; ++PI)
1680 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001681 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001682 // Report unimplemented properties in the category as well.
1683 // When reporting on missing setter/getters, do not report when
1684 // setter/getter is implemented in category's primary class
1685 // implementation.
1686 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1687 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1688 for (ObjCImplementationDecl::instmeth_iterator
1689 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1690 InsMap.insert((*I)->getSelector());
1691 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001692 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001693 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001694 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001695 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001696}
1697
Mike Stump1eb44332009-09-09 15:08:12 +00001698/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001699Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001700Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001701 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001702 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001703 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001704 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001705 for (unsigned i = 0; i != NumElts; ++i) {
1706 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001707 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001708 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001709 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001710 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001711 // Maybe we will complain about the shadowed template parameter.
1712 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1713 // Just pretend that we didn't see the previous declaration.
1714 PrevDecl = 0;
1715 }
1716
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001717 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001718 // GCC apparently allows the following idiom:
1719 //
1720 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1721 // @class XCElementToggler;
1722 //
Mike Stump1eb44332009-09-09 15:08:12 +00001723 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001724 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001725 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001726 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001727 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001728 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001729 // a forward class declaration matching a typedef name of a class refers
1730 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001731 if (const ObjCObjectType *OI =
1732 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1733 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001734 }
Chris Lattner4d391482007-12-12 07:09:47 +00001735 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001736 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1737 if (!IDecl) { // Not already seen? Make a forward decl.
1738 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1739 IdentList[i], IdentLocs[i], true);
1740
1741 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1742 // the current DeclContext. This prevents clients that walk DeclContext
1743 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1744 // declared later (if at all). We also take care to explicitly make
1745 // sure this declaration is visible for name lookup.
1746 PushOnScopeChains(IDecl, TUScope, false);
1747 CurContext->makeDeclVisibleInContext(IDecl, true);
1748 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001749 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1750 IDecl, IdentLocs[i]);
1751 CurContext->addDecl(CDecl);
1752 CheckObjCDeclScope(CDecl);
1753 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001754 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001755
1756 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001757}
1758
John McCall0f4c4c42011-06-16 01:15:19 +00001759static bool tryMatchRecordTypes(ASTContext &Context,
1760 Sema::MethodMatchStrategy strategy,
1761 const Type *left, const Type *right);
1762
John McCallf85e1932011-06-15 23:02:42 +00001763static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1764 QualType leftQT, QualType rightQT) {
1765 const Type *left =
1766 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1767 const Type *right =
1768 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1769
1770 if (left == right) return true;
1771
1772 // If we're doing a strict match, the types have to match exactly.
1773 if (strategy == Sema::MMS_strict) return false;
1774
1775 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1776
1777 // Otherwise, use this absurdly complicated algorithm to try to
1778 // validate the basic, low-level compatibility of the two types.
1779
1780 // As a minimum, require the sizes and alignments to match.
1781 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1782 return false;
1783
1784 // Consider all the kinds of non-dependent canonical types:
1785 // - functions and arrays aren't possible as return and parameter types
1786
1787 // - vector types of equal size can be arbitrarily mixed
1788 if (isa<VectorType>(left)) return isa<VectorType>(right);
1789 if (isa<VectorType>(right)) return false;
1790
1791 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001792 // - structs, unions, and Objective-C objects must match more-or-less
1793 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001794 // - everything else should be a scalar
1795 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001796 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001797
John McCall1d9b3b22011-09-09 05:25:32 +00001798 // Make scalars agree in kind, except count bools as chars, and group
1799 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001800 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1801 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1802 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1803 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001804 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1805 leftSK = Type::STK_ObjCObjectPointer;
1806 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1807 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001808
1809 // Note that data member pointers and function member pointers don't
1810 // intermix because of the size differences.
1811
1812 return (leftSK == rightSK);
1813}
Chris Lattner4d391482007-12-12 07:09:47 +00001814
John McCall0f4c4c42011-06-16 01:15:19 +00001815static bool tryMatchRecordTypes(ASTContext &Context,
1816 Sema::MethodMatchStrategy strategy,
1817 const Type *lt, const Type *rt) {
1818 assert(lt && rt && lt != rt);
1819
1820 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1821 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1822 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1823
1824 // Require union-hood to match.
1825 if (left->isUnion() != right->isUnion()) return false;
1826
1827 // Require an exact match if either is non-POD.
1828 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1829 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1830 return false;
1831
1832 // Require size and alignment to match.
1833 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1834
1835 // Require fields to match.
1836 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1837 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1838 for (; li != le && ri != re; ++li, ++ri) {
1839 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1840 return false;
1841 }
1842 return (li == le && ri == re);
1843}
1844
Chris Lattner4d391482007-12-12 07:09:47 +00001845/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1846/// returns true, or false, accordingly.
1847/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001848bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1849 const ObjCMethodDecl *right,
1850 MethodMatchStrategy strategy) {
1851 if (!matchTypes(Context, strategy,
1852 left->getResultType(), right->getResultType()))
1853 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001854
John McCallf85e1932011-06-15 23:02:42 +00001855 if (getLangOptions().ObjCAutoRefCount &&
1856 (left->hasAttr<NSReturnsRetainedAttr>()
1857 != right->hasAttr<NSReturnsRetainedAttr>() ||
1858 left->hasAttr<NSConsumesSelfAttr>()
1859 != right->hasAttr<NSConsumesSelfAttr>()))
1860 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001861
John McCallf85e1932011-06-15 23:02:42 +00001862 ObjCMethodDecl::param_iterator
1863 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001864
John McCallf85e1932011-06-15 23:02:42 +00001865 for (; li != le; ++li, ++ri) {
1866 assert(ri != right->param_end() && "Param mismatch");
1867 ParmVarDecl *lparm = *li, *rparm = *ri;
1868
1869 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1870 return false;
1871
1872 if (getLangOptions().ObjCAutoRefCount &&
1873 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1874 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001875 }
1876 return true;
1877}
1878
Sebastian Redldb9d2142010-08-02 23:18:59 +00001879/// \brief Read the contents of the method pool for a given selector from
1880/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001881///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001882/// This routine should only be called once, when the method pool has no entry
1883/// for this selector.
1884Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001885 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001886 assert(MethodPool.find(Sel) == MethodPool.end() &&
1887 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001888
1889 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001890 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Sebastian Redldb9d2142010-08-02 23:18:59 +00001892 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001893}
1894
Sebastian Redldb9d2142010-08-02 23:18:59 +00001895void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1896 bool instance) {
1897 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1898 if (Pos == MethodPool.end()) {
1899 if (ExternalSource)
1900 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001901 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001902 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1903 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001904 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001905 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001906 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001907 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001908 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001909 Entry.Method = Method;
1910 Entry.Next = 0;
1911 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001912 }
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Chris Lattnerb25df352009-03-04 05:16:45 +00001914 // We've seen a method with this name, see if we have already seen this type
1915 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001916 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1917 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1918
1919 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001920 ObjCMethodDecl *PrevObjCMethod = List->Method;
1921 PrevObjCMethod->setDefined(impl);
1922 // If a method is deprecated, push it in the global pool.
1923 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001924 if (Method->isDeprecated()) {
1925 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001926 List->Method = Method;
1927 }
1928 // If new method is unavailable, push it into global pool
1929 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001930 if (Method->isUnavailable()) {
1931 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001932 List->Method = Method;
1933 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001934 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001935 }
John McCallf85e1932011-06-15 23:02:42 +00001936 }
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Chris Lattnerb25df352009-03-04 05:16:45 +00001938 // We have a new signature for an existing method - add it.
1939 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001940 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1941 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001942}
1943
John McCallf85e1932011-06-15 23:02:42 +00001944/// Determines if this is an "acceptable" loose mismatch in the global
1945/// method pool. This exists mostly as a hack to get around certain
1946/// global mismatches which we can't afford to make warnings / errors.
1947/// Really, what we want is a way to take a method out of the global
1948/// method pool.
1949static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1950 ObjCMethodDecl *other) {
1951 if (!chosen->isInstanceMethod())
1952 return false;
1953
1954 Selector sel = chosen->getSelector();
1955 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1956 return false;
1957
1958 // Don't complain about mismatches for -length if the method we
1959 // chose has an integral result type.
1960 return (chosen->getResultType()->isIntegerType());
1961}
1962
Sebastian Redldb9d2142010-08-02 23:18:59 +00001963ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001964 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001965 bool warn, bool instance) {
1966 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1967 if (Pos == MethodPool.end()) {
1968 if (ExternalSource)
1969 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001970 else
1971 return 0;
1972 }
1973
Sebastian Redldb9d2142010-08-02 23:18:59 +00001974 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001975
Sebastian Redldb9d2142010-08-02 23:18:59 +00001976 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001977 bool issueDiagnostic = false, issueError = false;
1978
1979 // We support a warning which complains about *any* difference in
1980 // method signature.
1981 bool strictSelectorMatch =
1982 (receiverIdOrClass && warn &&
1983 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1984 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00001985 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001986 if (strictSelectorMatch)
1987 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001988 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1989 MMS_strict)) {
1990 issueDiagnostic = true;
1991 break;
1992 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001993 }
1994
John McCallf85e1932011-06-15 23:02:42 +00001995 // If we didn't see any strict differences, we won't see any loose
1996 // differences. In ARC, however, we also need to check for loose
1997 // mismatches, because most of them are errors.
1998 if (!strictSelectorMatch ||
1999 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002000 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002001 // This checks if the methods differ in type mismatch.
2002 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2003 MMS_loose) &&
2004 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2005 issueDiagnostic = true;
2006 if (getLangOptions().ObjCAutoRefCount)
2007 issueError = true;
2008 break;
2009 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002010 }
2011
John McCallf85e1932011-06-15 23:02:42 +00002012 if (issueDiagnostic) {
2013 if (issueError)
2014 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2015 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002016 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2017 else
2018 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002019
2020 Diag(MethList.Method->getLocStart(),
2021 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002022 << MethList.Method->getSourceRange();
2023 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2024 Diag(Next->Method->getLocStart(), diag::note_also_found)
2025 << Next->Method->getSourceRange();
2026 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002027 }
2028 return MethList.Method;
2029}
2030
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002031ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002032 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2033 if (Pos == MethodPool.end())
2034 return 0;
2035
2036 GlobalMethods &Methods = Pos->second;
2037
2038 if (Methods.first.Method && Methods.first.Method->isDefined())
2039 return Methods.first.Method;
2040 if (Methods.second.Method && Methods.second.Method->isDefined())
2041 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002042 return 0;
2043}
2044
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002045/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2046/// identical selector names in current and its super classes and issues
2047/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002048void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2049 ObjCMethodDecl *Method,
2050 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002051 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2052 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002054 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002055 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002056 SD->lookupMethod(Method->getSelector(), IsInstance);
2057 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002058 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002059 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002060 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002061 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2062 E = Method->param_end();
2063 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2064 for (; ParamI != E; ++ParamI, ++PrevI) {
2065 // Number of parameters are the same and is guaranteed by selector match.
2066 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2067 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2068 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002069 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002070 // respective argument type in the super class method, issue warning;
2071 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002072 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002073 << T1 << T2;
2074 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2075 return;
2076 }
2077 }
2078 ID = SD;
2079 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002080}
2081
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002082/// DiagnoseDuplicateIvars -
2083/// Check for duplicate ivars in the entire class at the start of
2084/// @implementation. This becomes necesssary because class extension can
2085/// add ivars to a class in random order which will not be known until
2086/// class's @implementation is seen.
2087void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2088 ObjCInterfaceDecl *SID) {
2089 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2090 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2091 ObjCIvarDecl* Ivar = (*IVI);
2092 if (Ivar->isInvalidDecl())
2093 continue;
2094 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2095 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2096 if (prevIvar) {
2097 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2098 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2099 Ivar->setInvalidDecl();
2100 }
2101 }
2102 }
2103}
2104
Steve Naroffa56f6162007-12-18 01:30:32 +00002105// Note: For class/category implemenations, allMethods/allProperties is
2106// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002107void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002108 Decl **allMethods, unsigned allNum,
2109 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002110 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002111
2112 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002113 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002114 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2115 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002116
Mike Stump1eb44332009-09-09 15:08:12 +00002117 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002118 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2119 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002120 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002121
Ted Kremenek782f2f52010-01-07 01:20:12 +00002122 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2123 // FIXME: This is wrong. We shouldn't be pretending that there is
2124 // an '@end' in the declaration.
2125 SourceLocation L = ClassDecl->getLocation();
2126 AtEnd.setBegin(L);
2127 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002128 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002129 }
2130
Steve Naroff0701bbb2009-01-08 17:28:14 +00002131 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2132 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2133 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2134
Chris Lattner4d391482007-12-12 07:09:47 +00002135 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002136 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002137 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002138
2139 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002140 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002141 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002142 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002143 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002144 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002145 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002146 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002147 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002148 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002149 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002150 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002151 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002152 InsMap[Method->getSelector()] = Method;
2153 /// The following allows us to typecheck messages to "id".
2154 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002155 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002156 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002157 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002158 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002159 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002160 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002161 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002162 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002163 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002164 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002165 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002166 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002167 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002168 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002169 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002170 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002171 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002172 /// The following allows us to typecheck messages to "Class".
2173 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002174 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002175 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002176 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002177 }
2178 }
2179 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002180 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002181 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002182 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002183 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002184 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002185 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002186 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002187 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002188 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002189
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002190 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002191 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002192 if (C->IsClassExtension()) {
2193 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2194 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002195 }
Chris Lattner4d391482007-12-12 07:09:47 +00002196 }
Steve Naroff09c47192009-01-09 15:36:25 +00002197 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002198 if (CDecl->getIdentifier())
2199 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2200 // user-defined setter/getter. It also synthesizes setter/getter methods
2201 // and adds them to the DeclContext and global method pools.
2202 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2203 E = CDecl->prop_end();
2204 I != E; ++I)
2205 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002206 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002207 }
2208 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002209 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002210 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002211 // Any property declared in a class extension might have user
2212 // declared setter or getter in current class extension or one
2213 // of the other class extensions. Mark them as synthesized as
2214 // property will be synthesized when property with same name is
2215 // seen in the @implementation.
2216 for (const ObjCCategoryDecl *ClsExtDecl =
2217 IDecl->getFirstClassExtension();
2218 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2219 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2220 E = ClsExtDecl->prop_end(); I != E; ++I) {
2221 ObjCPropertyDecl *Property = (*I);
2222 // Skip over properties declared @dynamic
2223 if (const ObjCPropertyImplDecl *PIDecl
2224 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2225 if (PIDecl->getPropertyImplementation()
2226 == ObjCPropertyImplDecl::Dynamic)
2227 continue;
2228
2229 for (const ObjCCategoryDecl *CExtDecl =
2230 IDecl->getFirstClassExtension();
2231 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2232 if (ObjCMethodDecl *GetterMethod =
2233 CExtDecl->getInstanceMethod(Property->getGetterName()))
2234 GetterMethod->setSynthesized(true);
2235 if (!Property->isReadOnly())
2236 if (ObjCMethodDecl *SetterMethod =
2237 CExtDecl->getInstanceMethod(Property->getSetterName()))
2238 SetterMethod->setSynthesized(true);
2239 }
2240 }
2241 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002242 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002243 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002244 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002245
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002246 if (LangOpts.ObjCNonFragileABI2)
2247 while (IDecl->getSuperClass()) {
2248 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2249 IDecl = IDecl->getSuperClass();
2250 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002251 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002252 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002253 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002254 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002255 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002256
Chris Lattner4d391482007-12-12 07:09:47 +00002257 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002258 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002259 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002260 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002261 Categories; Categories = Categories->getNextClassCategory()) {
2262 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002263 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002264 break;
2265 }
2266 }
2267 }
2268 }
Chris Lattner682bf922009-03-29 16:50:03 +00002269 if (isInterfaceDeclKind) {
2270 // Reject invalid vardecls.
2271 for (unsigned i = 0; i != tuvNum; i++) {
2272 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2273 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2274 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002275 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002276 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002277 }
Chris Lattner682bf922009-03-29 16:50:03 +00002278 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002279 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002280 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002281}
2282
2283
2284/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2285/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002286static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002287CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002288 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002289}
2290
Ted Kremenek422bae72010-04-18 04:59:38 +00002291static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002292bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002293 // The 'ibaction' attribute is allowed on method definitions because of
2294 // how the IBAction macro is used on both method declarations and definitions.
2295 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002296 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2297 if ((*i)->getKind() != attr::IBAction)
2298 return true;
2299 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002300}
2301
Douglas Gregore97179c2011-09-08 01:46:34 +00002302namespace {
2303 /// \brief Describes the compatibility of a result type with its method.
2304 enum ResultTypeCompatibilityKind {
2305 RTC_Compatible,
2306 RTC_Incompatible,
2307 RTC_Unknown
2308 };
2309}
2310
Douglas Gregor926df6c2011-06-11 01:09:30 +00002311/// \brief Check whether the declared result type of the given Objective-C
2312/// method declaration is compatible with the method's class.
2313///
Douglas Gregore97179c2011-09-08 01:46:34 +00002314static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002315CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2316 ObjCInterfaceDecl *CurrentClass) {
2317 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002318
2319 // If an Objective-C method inherits its related result type, then its
2320 // declared result type must be compatible with its own class type. The
2321 // declared result type is compatible if:
2322 if (const ObjCObjectPointerType *ResultObjectType
2323 = ResultType->getAs<ObjCObjectPointerType>()) {
2324 // - it is id or qualified id, or
2325 if (ResultObjectType->isObjCIdType() ||
2326 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002327 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002328
2329 if (CurrentClass) {
2330 if (ObjCInterfaceDecl *ResultClass
2331 = ResultObjectType->getInterfaceDecl()) {
2332 // - it is the same as the method's class type, or
2333 if (CurrentClass == ResultClass)
Douglas Gregore97179c2011-09-08 01:46:34 +00002334 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002335
2336 // - it is a superclass of the method's class type
2337 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002338 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002339 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002340 } else {
2341 // Any Objective-C pointer type might be acceptable for a protocol
2342 // method; we just don't know.
2343 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002344 }
2345 }
2346
Douglas Gregore97179c2011-09-08 01:46:34 +00002347 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002348}
2349
John McCall6c2c2502011-07-22 02:45:48 +00002350namespace {
2351/// A helper class for searching for methods which a particular method
2352/// overrides.
2353class OverrideSearch {
2354 Sema &S;
2355 ObjCMethodDecl *Method;
2356 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2357 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2358 bool Recursive;
2359
2360public:
2361 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2362 Selector selector = method->getSelector();
2363
2364 // Bypass this search if we've never seen an instance/class method
2365 // with this selector before.
2366 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2367 if (it == S.MethodPool.end()) {
2368 if (!S.ExternalSource) return;
2369 it = S.ReadMethodPool(selector);
2370 }
2371 ObjCMethodList &list =
2372 method->isInstanceMethod() ? it->second.first : it->second.second;
2373 if (!list.Method) return;
2374
2375 ObjCContainerDecl *container
2376 = cast<ObjCContainerDecl>(method->getDeclContext());
2377
2378 // Prevent the search from reaching this container again. This is
2379 // important with categories, which override methods from the
2380 // interface and each other.
2381 Searched.insert(container);
2382 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002383 }
John McCall6c2c2502011-07-22 02:45:48 +00002384
2385 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2386 iterator begin() const { return Overridden.begin(); }
2387 iterator end() const { return Overridden.end(); }
2388
2389private:
2390 void searchFromContainer(ObjCContainerDecl *container) {
2391 if (container->isInvalidDecl()) return;
2392
2393 switch (container->getDeclKind()) {
2394#define OBJCCONTAINER(type, base) \
2395 case Decl::type: \
2396 searchFrom(cast<type##Decl>(container)); \
2397 break;
2398#define ABSTRACT_DECL(expansion)
2399#define DECL(type, base) \
2400 case Decl::type:
2401#include "clang/AST/DeclNodes.inc"
2402 llvm_unreachable("not an ObjC container!");
2403 }
2404 }
2405
2406 void searchFrom(ObjCProtocolDecl *protocol) {
2407 // A method in a protocol declaration overrides declarations from
2408 // referenced ("parent") protocols.
2409 search(protocol->getReferencedProtocols());
2410 }
2411
2412 void searchFrom(ObjCCategoryDecl *category) {
2413 // A method in a category declaration overrides declarations from
2414 // the main class and from protocols the category references.
2415 search(category->getClassInterface());
2416 search(category->getReferencedProtocols());
2417 }
2418
2419 void searchFrom(ObjCCategoryImplDecl *impl) {
2420 // A method in a category definition that has a category
2421 // declaration overrides declarations from the category
2422 // declaration.
2423 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2424 search(category);
2425
2426 // Otherwise it overrides declarations from the class.
2427 } else {
2428 search(impl->getClassInterface());
2429 }
2430 }
2431
2432 void searchFrom(ObjCInterfaceDecl *iface) {
2433 // A method in a class declaration overrides declarations from
2434
2435 // - categories,
2436 for (ObjCCategoryDecl *category = iface->getCategoryList();
2437 category; category = category->getNextClassCategory())
2438 search(category);
2439
2440 // - the super class, and
2441 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2442 search(super);
2443
2444 // - any referenced protocols.
2445 search(iface->getReferencedProtocols());
2446 }
2447
2448 void searchFrom(ObjCImplementationDecl *impl) {
2449 // A method in a class implementation overrides declarations from
2450 // the class interface.
2451 search(impl->getClassInterface());
2452 }
2453
2454
2455 void search(const ObjCProtocolList &protocols) {
2456 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2457 i != e; ++i)
2458 search(*i);
2459 }
2460
2461 void search(ObjCContainerDecl *container) {
2462 // Abort if we've already searched this container.
2463 if (!Searched.insert(container)) return;
2464
2465 // Check for a method in this container which matches this selector.
2466 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2467 Method->isInstanceMethod());
2468
2469 // If we find one, record it and bail out.
2470 if (meth) {
2471 Overridden.insert(meth);
2472 return;
2473 }
2474
2475 // Otherwise, search for methods that a hypothetical method here
2476 // would have overridden.
2477
2478 // Note that we're now in a recursive case.
2479 Recursive = true;
2480
2481 searchFromContainer(container);
2482 }
2483};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002484}
2485
John McCalld226f652010-08-21 09:40:31 +00002486Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002487 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002488 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002489 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002490 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002491 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002492 Selector Sel,
2493 // optional arguments. The number of types/arguments is obtained
2494 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002495 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002496 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002497 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002498 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002499 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002500 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002501 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002502 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002503 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002504 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2505 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002506 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002507
Douglas Gregore97179c2011-09-08 01:46:34 +00002508 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002509 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002510 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002511 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002512
Steve Naroffccef3712009-02-20 22:59:16 +00002513 // Methods cannot return interface types. All ObjC objects are
2514 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002515 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002516 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2517 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002518 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002519 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002520
2521 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002522 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002523 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002524 Diag(MethodLoc, diag::warn_missing_method_return_type)
2525 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002526 }
Mike Stump1eb44332009-09-09 15:08:12 +00002527
2528 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002529 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002530 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002531 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002532 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002533 /*isSynthesized=*/false,
2534 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002535 MethodDeclKind == tok::objc_optional
2536 ? ObjCMethodDecl::Optional
2537 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002538 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002539
Chris Lattner5f9e2722011-07-23 10:55:15 +00002540 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002541
Chris Lattner7db638d2009-04-11 19:42:43 +00002542 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002543 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002544 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002545
Chris Lattnere294d3f2009-04-11 18:57:04 +00002546 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002547 ArgType = Context.getObjCIdType();
2548 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002549 } else {
John McCall58e46772009-10-23 21:48:59 +00002550 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002551 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002552 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002553 }
Mike Stump1eb44332009-09-09 15:08:12 +00002554
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002555 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2556 LookupOrdinaryName, ForRedeclaration);
2557 LookupName(R, S);
2558 if (R.isSingleResult()) {
2559 NamedDecl *PrevDecl = R.getFoundDecl();
2560 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002561 Diag(ArgInfo[i].NameLoc,
2562 (MethodDefinition ? diag::warn_method_param_redefinition
2563 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002564 << ArgInfo[i].Name;
2565 Diag(PrevDecl->getLocation(),
2566 diag::note_previous_declaration);
2567 }
2568 }
2569
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002570 SourceLocation StartLoc = DI
2571 ? DI->getTypeLoc().getBeginLoc()
2572 : ArgInfo[i].NameLoc;
2573
John McCall81ef3e62011-04-23 02:46:06 +00002574 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2575 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2576 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002577
John McCall70798862011-05-02 00:30:12 +00002578 Param->setObjCMethodScopeInfo(i);
2579
Chris Lattner0ed844b2008-04-04 06:12:32 +00002580 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002581 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002583 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002584 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002586 S->AddDecl(Param);
2587 IdResolver.AddDecl(Param);
2588
Chris Lattner0ed844b2008-04-04 06:12:32 +00002589 Params.push_back(Param);
2590 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002591
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002592 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002593 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002594 QualType ArgType = Param->getType();
2595 if (ArgType.isNull())
2596 ArgType = Context.getObjCIdType();
2597 else
2598 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002599 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002600 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002601 Diag(Param->getLocation(),
2602 diag::err_object_cannot_be_passed_returned_by_value)
2603 << 1 << ArgType;
2604 Param->setInvalidDecl();
2605 }
2606 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002607
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002608 Params.push_back(Param);
2609 }
2610
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002611 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2612 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002613 ObjCMethod->setObjCDeclQualifier(
2614 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002615
2616 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002617 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002618
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002619 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002620 const ObjCMethodDecl *PrevMethod = 0;
2621 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002622 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002623 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2624 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002625 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002626 PrevMethod = ImpDecl->getClassMethod(Sel);
2627 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002628 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002629
Sean Huntcf807c42010-08-18 23:23:40 +00002630 if (ObjCMethod->hasAttrs() &&
2631 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002632 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002633 } else {
2634 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002635 }
John McCall6c2c2502011-07-22 02:45:48 +00002636
Chris Lattner4d391482007-12-12 07:09:47 +00002637 if (PrevMethod) {
2638 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002639 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002640 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002641 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002642 }
John McCall54abf7d2009-11-04 02:18:39 +00002643
Douglas Gregor926df6c2011-06-11 01:09:30 +00002644 // If this Objective-C method does not have a related result type, but we
2645 // are allowed to infer related result types, try to do so based on the
2646 // method family.
2647 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2648 if (!CurrentClass) {
2649 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2650 CurrentClass = Cat->getClassInterface();
2651 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2652 CurrentClass = Impl->getClassInterface();
2653 else if (ObjCCategoryImplDecl *CatImpl
2654 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2655 CurrentClass = CatImpl->getClassInterface();
2656 }
John McCall6c2c2502011-07-22 02:45:48 +00002657
Douglas Gregore97179c2011-09-08 01:46:34 +00002658 ResultTypeCompatibilityKind RTC
2659 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002660
2661 // Search for overridden methods and merge information down from them.
2662 OverrideSearch overrides(*this, ObjCMethod);
2663 for (OverrideSearch::iterator
2664 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2665 ObjCMethodDecl *overridden = *i;
2666
2667 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002668 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002669 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002670
2671 // Then merge the declarations.
2672 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002673
2674 // Check for overriding methods
2675 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2676 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2677 WarnConflictingTypedMethods(ObjCMethod, overridden,
2678 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2679 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002680 }
2681
John McCallf85e1932011-06-15 23:02:42 +00002682 bool ARCError = false;
2683 if (getLangOptions().ObjCAutoRefCount)
2684 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2685
Douglas Gregore97179c2011-09-08 01:46:34 +00002686 // Infer the related result type when possible.
2687 if (!ARCError && RTC == RTC_Compatible &&
2688 !ObjCMethod->hasRelatedResultType() &&
2689 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002690 bool InferRelatedResultType = false;
2691 switch (ObjCMethod->getMethodFamily()) {
2692 case OMF_None:
2693 case OMF_copy:
2694 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002695 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002696 case OMF_mutableCopy:
2697 case OMF_release:
2698 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002699 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002700 break;
2701
2702 case OMF_alloc:
2703 case OMF_new:
2704 InferRelatedResultType = ObjCMethod->isClassMethod();
2705 break;
2706
2707 case OMF_init:
2708 case OMF_autorelease:
2709 case OMF_retain:
2710 case OMF_self:
2711 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2712 break;
2713 }
2714
John McCall6c2c2502011-07-22 02:45:48 +00002715 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002716 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002717 }
2718
John McCalld226f652010-08-21 09:40:31 +00002719 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002720}
2721
Chris Lattnercc98eac2008-12-17 07:13:27 +00002722bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002723 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002724 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002725 // Following is also an error. But it is caused by a missing @end
2726 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002727 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2728 return false;
2729 }
2730
Anders Carlsson15281452008-11-04 16:57:32 +00002731 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2732 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002733
Anders Carlsson15281452008-11-04 16:57:32 +00002734 return true;
2735}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002736
Chris Lattnercc98eac2008-12-17 07:13:27 +00002737/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2738/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002739void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002740 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002741 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002742 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002743 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002744 if (!Class) {
2745 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2746 return;
2747 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002748 if (LangOpts.ObjCNonFragileABI) {
2749 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2750 return;
2751 }
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Chris Lattnercc98eac2008-12-17 07:13:27 +00002753 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002754 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002755 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002756 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002757 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002758 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002759 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002760 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2761 /*FIXME: StartL=*/ID->getLocation(),
2762 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002763 ID->getIdentifier(), ID->getType(),
2764 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002765 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002766 }
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Chris Lattnercc98eac2008-12-17 07:13:27 +00002768 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002769 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002770 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002771 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002772 if (getLangOptions().CPlusPlus)
2773 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002774 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002775 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002776 }
2777}
2778
Douglas Gregor160b5632010-04-26 17:32:49 +00002779/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002780VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2781 SourceLocation StartLoc,
2782 SourceLocation IdLoc,
2783 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002784 bool Invalid) {
2785 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2786 // duration shall not be qualified by an address-space qualifier."
2787 // Since all parameters have automatic store duration, they can not have
2788 // an address space.
2789 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002790 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002791 Invalid = true;
2792 }
2793
2794 // An @catch parameter must be an unqualified object pointer type;
2795 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2796 if (Invalid) {
2797 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002798 } else if (T->isDependentType()) {
2799 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002800 } else if (!T->isObjCObjectPointerType()) {
2801 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002802 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002803 } else if (T->isObjCQualifiedIdType()) {
2804 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002805 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002806 }
2807
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002808 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2809 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002810 New->setExceptionVariable(true);
2811
Douglas Gregor160b5632010-04-26 17:32:49 +00002812 if (Invalid)
2813 New->setInvalidDecl();
2814 return New;
2815}
2816
John McCalld226f652010-08-21 09:40:31 +00002817Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002818 const DeclSpec &DS = D.getDeclSpec();
2819
2820 // We allow the "register" storage class on exception variables because
2821 // GCC did, but we drop it completely. Any other storage class is an error.
2822 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2823 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2824 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2825 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2826 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2827 << DS.getStorageClassSpec();
2828 }
2829 if (D.getDeclSpec().isThreadSpecified())
2830 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2831 D.getMutableDeclSpec().ClearStorageClassSpecs();
2832
2833 DiagnoseFunctionSpecifiers(D);
2834
2835 // Check that there are no default arguments inside the type of this
2836 // exception object (C++ only).
2837 if (getLangOptions().CPlusPlus)
2838 CheckExtraCXXDefaultArguments(D);
2839
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002840 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002841 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002842
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002843 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2844 D.getSourceRange().getBegin(),
2845 D.getIdentifierLoc(),
2846 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002847 D.isInvalidType());
2848
2849 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2850 if (D.getCXXScopeSpec().isSet()) {
2851 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2852 << D.getCXXScopeSpec().getRange();
2853 New->setInvalidDecl();
2854 }
2855
2856 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002857 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002858 if (D.getIdentifier())
2859 IdResolver.AddDecl(New);
2860
2861 ProcessDeclAttributes(S, New, D);
2862
2863 if (New->hasAttr<BlocksAttr>())
2864 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002865 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002866}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002867
2868/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002869/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002870void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002871 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002872 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2873 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002874 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002875 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002876 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002877 }
2878}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002879
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002880void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002881 // Load referenced selectors from the external source.
2882 if (ExternalSource) {
2883 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2884 ExternalSource->ReadReferencedSelectors(Sels);
2885 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2886 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2887 }
2888
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002889 // Warning will be issued only when selector table is
2890 // generated (which means there is at lease one implementation
2891 // in the TU). This is to match gcc's behavior.
2892 if (ReferencedSelectors.empty() ||
2893 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002894 return;
2895 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2896 ReferencedSelectors.begin(),
2897 E = ReferencedSelectors.end(); S != E; ++S) {
2898 Selector Sel = (*S).first;
2899 if (!LookupImplementedMethodInGlobalPool(Sel))
2900 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2901 }
2902 return;
2903}