blob: 3a34ffb5433e2d2342600529dc9ee817ef746077 [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 =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000338 !(Context.getLangOptions().ObjCAutoRefCount ||
339 Context.getLangOptions().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000340 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000341 ObjCShouldCallSuperFinalize =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000342 Context.getLangOptions().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000343 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000344 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000345 }
Chris Lattner4d391482007-12-12 07:09:47 +0000346}
347
John McCalld226f652010-08-21 09:40:31 +0000348Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000349ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
350 IdentifierInfo *ClassName, SourceLocation ClassLoc,
351 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000352 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000353 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000354 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000355 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner4d391482007-12-12 07:09:47 +0000357 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000358 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000359 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000360
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000362 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000363 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000366 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
367 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000368 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000369 if (!IDecl->isForwardDecl()) {
370 IDecl->setInvalidDecl();
371 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
372 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000373
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000374 // Return the previous class interface.
375 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000376 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000377 } else {
378 IDecl->setLocation(AtInterfaceLoc);
379 IDecl->setForwardDecl(false);
380 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000381 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000382 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000383 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000384
385 // Since this ObjCInterfaceDecl was created by a forward declaration,
386 // we now add it to the DeclContext since it wasn't added before
387 // (see ActOnForwardClassDeclaration).
388 IDecl->setLexicalDeclContext(CurContext);
389 CurContext->addDecl(IDecl);
390
391 if (AttrList)
392 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000393 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000394 } else {
395 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
396 ClassName, ClassLoc);
397 if (AttrList)
398 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
399
400 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner4d391482007-12-12 07:09:47 +0000403 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000404 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000405 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
406 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000407
408 if (!PrevDecl) {
409 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000410 TypoCorrection Corrected = CorrectTypo(
411 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
412 NULL, NULL, false, CTC_NoKeywords);
413 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000414 Diag(SuperLoc, diag::err_undef_superclass_suggest)
415 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000416 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
417 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000418 }
419 }
420
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000421 if (PrevDecl == IDecl) {
422 Diag(SuperLoc, diag::err_recursive_superclass)
423 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
424 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000425 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000426 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000427 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000428
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000429 // Diagnose classes that inherit from deprecated classes.
430 if (SuperClassDecl)
431 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000433 if (PrevDecl && SuperClassDecl == 0) {
434 // The previous declaration was not a class decl. Check if we have a
435 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000436 if (const TypedefNameDecl *TDecl =
437 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000438 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000439 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000440 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
441 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000442 }
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000445 // This handles the following case:
446 //
447 // typedef int SuperClass;
448 // @interface MyClass : SuperClass {} @end
449 //
450 if (!SuperClassDecl) {
451 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
452 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000453 }
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Richard Smith162e1c12011-04-15 14:24:37 +0000456 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000457 if (!SuperClassDecl)
458 Diag(SuperLoc, diag::err_undef_superclass)
459 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000460 else if (SuperClassDecl->isForwardDecl()) {
461 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000462 << SuperClassDecl->getDeclName() << ClassName
463 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000464 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
465 SuperClassDecl = 0;
466 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000467 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000468 IDecl->setSuperClass(SuperClassDecl);
469 IDecl->setSuperClassLoc(SuperLoc);
470 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000471 }
Chris Lattner4d391482007-12-12 07:09:47 +0000472 } else { // we have a root class.
473 IDecl->setLocEnd(ClassLoc);
474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Sebastian Redl0b17c612010-08-13 00:28:03 +0000476 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000477 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000478 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000479 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000480 IDecl->setLocEnd(EndProtoLoc);
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Anders Carlsson15281452008-11-04 16:57:32 +0000483 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000484 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000485}
486
487/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000488/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000489Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
490 IdentifierInfo *AliasName,
491 SourceLocation AliasLocation,
492 IdentifierInfo *ClassName,
493 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000494 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000495 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000496 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000497 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000498 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000499 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000500 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000501 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000502 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000503 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000504 }
505 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000506 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000507 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000508 if (const TypedefNameDecl *TDecl =
509 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000510 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000511 if (T->isObjCObjectType()) {
512 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000513 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000514 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000515 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000516 }
517 }
518 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000519 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
520 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000521 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000522 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000523 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000524 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000527 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000528 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000529 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlsson15281452008-11-04 16:57:32 +0000531 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000532 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000533
John McCalld226f652010-08-21 09:40:31 +0000534 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000535}
536
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000537bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000538 IdentifierInfo *PName,
539 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000540 const ObjCList<ObjCProtocolDecl> &PList) {
541
542 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000543 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
544 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000545 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
546 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000547 if (PDecl->getIdentifier() == PName) {
548 Diag(Ploc, diag::err_protocol_has_circular_dependency);
549 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000550 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000551 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000552 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
553 PDecl->getLocation(), PDecl->getReferencedProtocols()))
554 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000555 }
556 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000557 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000558}
559
John McCalld226f652010-08-21 09:40:31 +0000560Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000561Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
562 IdentifierInfo *ProtocolName,
563 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000564 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000565 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000566 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000567 SourceLocation EndProtoLoc,
568 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000569 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000570 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000571 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000572 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000573 if (PDecl) {
574 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000575 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000576 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000577 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000578 // Just return the protocol we already had.
579 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000580 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000581 }
Steve Naroff61d68522009-03-05 15:22:01 +0000582 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000583 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000584 err = CheckForwardProtocolDeclarationForCircularDependency(
585 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Steve Narofff11b5082008-08-13 16:39:22 +0000587 // Make sure the cached decl gets a valid start location.
588 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000589 PDecl->setForwardDecl(false);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000590 // Since this ObjCProtocolDecl was created by a forward declaration,
591 // we now add it to the DeclContext since it wasn't added before
592 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000593 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000594 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000595 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000596 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000597 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000598 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000599 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000600 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000601 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000602 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000603 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000604 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000605 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000606 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
607 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000608 PDecl->setLocEnd(EndProtoLoc);
609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
611 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000612 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000613}
614
615/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000616/// issues an error if they are not declared. It returns list of
617/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000618void
Chris Lattnere13b9592008-07-26 04:03:38 +0000619Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000620 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000621 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000622 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000623 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000624 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
625 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000626 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000627 TypoCorrection Corrected = CorrectTypo(
628 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
629 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
630 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000631 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000632 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000633 Diag(PDecl->getLocation(), diag::note_previous_decl)
634 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000635 }
636 }
637
638 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000639 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000640 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000641 continue;
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000644 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000645
646 // If this is a forward declaration and we are supposed to warn in this
647 // case, do it.
648 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000649 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000650 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000651 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000652 }
653}
654
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000655/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000656/// a class method in its extension.
657///
Mike Stump1eb44332009-09-09 15:08:12 +0000658void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000659 ObjCInterfaceDecl *ID) {
660 if (!ID)
661 return; // Possibly due to previous error
662
663 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000664 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
665 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000666 ObjCMethodDecl *MD = *i;
667 MethodMap[MD->getSelector()] = MD;
668 }
669
670 if (MethodMap.empty())
671 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000672 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
673 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000674 ObjCMethodDecl *Method = *i;
675 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
676 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
677 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
678 << Method->getDeclName();
679 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
680 }
681 }
682}
683
Chris Lattner58fe03b2009-04-12 08:43:13 +0000684/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000685Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000686Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000687 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000688 unsigned NumElts,
689 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000690 SmallVector<ObjCProtocolDecl*, 32> Protocols;
691 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Chris Lattner4d391482007-12-12 07:09:47 +0000693 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000694 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000695 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000696 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000697 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000698 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000699 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000700 PushOnScopeChains(PDecl, TUScope, false);
701 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000702 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000703 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000704 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000705 if (!isNew)
706 PDecl->setChangedSinceDeserialization(true);
707 }
Chris Lattner4d391482007-12-12 07:09:47 +0000708 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000709 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
712 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000713 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000714 Protocols.data(), Protocols.size(),
715 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000716 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000717 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000718 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000719}
720
John McCalld226f652010-08-21 09:40:31 +0000721Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000722ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
723 IdentifierInfo *ClassName, SourceLocation ClassLoc,
724 IdentifierInfo *CategoryName,
725 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000726 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000727 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000728 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000729 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000730 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000731 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000732
733 /// Check that class of this category is already completely declared.
734 if (!IDecl || IDecl->isForwardDecl()) {
735 // Create an invalid ObjCCategoryDecl to serve as context for
736 // the enclosing method declarations. We mark the decl invalid
737 // to make it clear that this isn't a valid AST.
738 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000739 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000740 CDecl->setInvalidDecl();
741 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000742 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000743 }
744
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000745 if (!CategoryName && IDecl->getImplementation()) {
746 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
747 Diag(IDecl->getImplementation()->getLocation(),
748 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000749 }
750
Fariborz Jahanian25760612010-02-15 21:55:26 +0000751 if (CategoryName) {
752 /// Check for duplicate interface declaration for this category
753 ObjCCategoryDecl *CDeclChain;
754 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
755 CDeclChain = CDeclChain->getNextClassCategory()) {
756 if (CDeclChain->getIdentifier() == CategoryName) {
757 // Class extensions can be declared multiple times.
758 Diag(CategoryLoc, diag::warn_dup_category_def)
759 << ClassName << CategoryName;
760 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
761 break;
762 }
Chris Lattner70f19542009-02-16 21:26:43 +0000763 }
764 }
Chris Lattner70f19542009-02-16 21:26:43 +0000765
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000766 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
767 ClassLoc, CategoryLoc, CategoryName, IDecl);
768 // FIXME: PushOnScopeChains?
769 CurContext->addDecl(CDecl);
770
771 // If the interface is deprecated, warn about it.
772 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
773
Chris Lattner4d391482007-12-12 07:09:47 +0000774 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000775 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000776 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000777 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000778 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000779 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000780 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Anders Carlsson15281452008-11-04 16:57:32 +0000783 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000784 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000785}
786
787/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000788/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000789/// object.
John McCalld226f652010-08-21 09:40:31 +0000790Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000791 SourceLocation AtCatImplLoc,
792 IdentifierInfo *ClassName, SourceLocation ClassLoc,
793 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000794 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000795 ObjCCategoryDecl *CatIDecl = 0;
796 if (IDecl) {
797 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
798 if (!CatIDecl) {
799 // Category @implementation with no corresponding @interface.
800 // Create and install one.
801 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000802 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000803 CatName, IDecl);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000804 }
805 }
806
Mike Stump1eb44332009-09-09 15:08:12 +0000807 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000808 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
809 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000810 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000811 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000812 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000813 CDecl->setInvalidDecl();
814 }
Chris Lattner4d391482007-12-12 07:09:47 +0000815
Douglas Gregord0434102009-01-09 00:49:46 +0000816 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000817 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000818
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000819 /// Check that CatName, category name, is not used in another implementation.
820 if (CatIDecl) {
821 if (CatIDecl->getImplementation()) {
822 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
823 << CatName;
824 Diag(CatIDecl->getImplementation()->getLocation(),
825 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000826 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000827 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000828 // Warn on implementating category of deprecated class under
829 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000830 DiagnoseObjCImplementedDeprecations(*this,
831 dyn_cast<NamedDecl>(IDecl),
832 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000833 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000834 }
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Anders Carlsson15281452008-11-04 16:57:32 +0000836 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000837 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000838}
839
John McCalld226f652010-08-21 09:40:31 +0000840Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000841 SourceLocation AtClassImplLoc,
842 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000843 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000844 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000845 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000846 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000847 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000848 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
849 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000850 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000851 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000852 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000853 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
854 // If this is a forward declaration of an interface, warn.
855 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000856 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000857 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000858 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000859 } else {
860 // We did not find anything with the name ClassName; try to correct for
861 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000862 TypoCorrection Corrected = CorrectTypo(
863 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
864 NULL, NULL, false, CTC_NoKeywords);
865 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000866 // Suggest the (potentially) correct interface name. However, put the
867 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000868 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000869 // provide a code-modification hint or use the typo name for recovery,
870 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000871 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000872 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000873 << ClassName << CorrectedName;
874 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
875 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000876 IDecl = 0;
877 } else {
878 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
879 }
Chris Lattner4d391482007-12-12 07:09:47 +0000880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner4d391482007-12-12 07:09:47 +0000882 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000883 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000884 if (SuperClassname) {
885 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000886 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
887 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000888 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000889 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
890 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000891 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000892 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000893 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000894 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 Diag(SuperClassLoc, diag::err_undef_superclass)
896 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000897 else if (IDecl && IDecl->getSuperClass() != SDecl) {
898 // This implementation and its interface do not have the same
899 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000900 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000901 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000902 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000903 }
904 }
905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Chris Lattner4d391482007-12-12 07:09:47 +0000907 if (!IDecl) {
908 // Legacy case of @implementation with no corresponding @interface.
909 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000910
Mike Stump390b4cc2009-05-16 07:39:55 +0000911 // FIXME: Do we support attributes on the @implementation? If so we should
912 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000913 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000914 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000915 IDecl->setSuperClass(SDecl);
916 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000917
918 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000919 } else {
920 // Mark the interface as being completed, even if it was just as
921 // @class ....;
922 // declaration; the user cannot reopen it.
923 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
926 ObjCImplementationDecl* IMPDecl =
927 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000928 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Anders Carlsson15281452008-11-04 16:57:32 +0000930 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000931 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Chris Lattner4d391482007-12-12 07:09:47 +0000933 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000934 if (IDecl->getImplementation()) {
935 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000936 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000937 Diag(IDecl->getImplementation()->getLocation(),
938 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000939 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000940 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000941 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000942 // Warn on implementating deprecated class under
943 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000944 DiagnoseObjCImplementedDeprecations(*this,
945 dyn_cast<NamedDecl>(IDecl),
946 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000947 }
John McCalld226f652010-08-21 09:40:31 +0000948 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000949}
950
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000951void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
952 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000953 SourceLocation RBrace) {
954 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000955 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000956 if (!IDecl)
957 return;
958 /// Check case of non-existing @interface decl.
959 /// (legacy objective-c @implementation decl without an @interface decl).
960 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000961 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000962 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000963 // Add ivar's to class's DeclContext.
964 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000965 ivars[i]->setLexicalDeclContext(ImpDecl);
966 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000967 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000968 }
969
Chris Lattner4d391482007-12-12 07:09:47 +0000970 return;
971 }
972 // If implementation has empty ivar list, just return.
973 if (numIvars == 0)
974 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattner4d391482007-12-12 07:09:47 +0000976 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000977 if (LangOpts.ObjCNonFragileABI2) {
978 if (ImpDecl->getSuperClass())
979 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
980 for (unsigned i = 0; i < numIvars; i++) {
981 ObjCIvarDecl* ImplIvar = ivars[i];
982 if (const ObjCIvarDecl *ClsIvar =
983 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
984 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
985 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
986 continue;
987 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000988 // Instance ivar to Implementation's DeclContext.
989 ImplIvar->setLexicalDeclContext(ImpDecl);
990 IDecl->makeDeclVisibleInContext(ImplIvar, false);
991 ImpDecl->addDecl(ImplIvar);
992 }
993 return;
994 }
Chris Lattner4d391482007-12-12 07:09:47 +0000995 // Check interface's Ivar list against those in the implementation.
996 // names and types must match.
997 //
Chris Lattner4d391482007-12-12 07:09:47 +0000998 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000999 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001000 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1001 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001002 ObjCIvarDecl* ImplIvar = ivars[j++];
1003 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001004 assert (ImplIvar && "missing implementation ivar");
1005 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Steve Naroffca331292009-03-03 14:49:36 +00001007 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +00001008 if (Context.getCanonicalType(ImplIvar->getType()) !=
1009 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001010 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001011 << ImplIvar->getIdentifier()
1012 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001013 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +00001014 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
1015 Expr *ImplBitWidth = ImplIvar->getBitWidth();
1016 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001017 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
1018 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +00001019 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
1020 << ImplIvar->getIdentifier();
1021 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023 }
Steve Naroffca331292009-03-03 14:49:36 +00001024 // Make sure the names are identical.
1025 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001026 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001027 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001028 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001029 }
1030 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Chris Lattner609e4c72007-12-12 18:11:49 +00001033 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001034 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001035 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001036 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001037}
1038
Steve Naroff3c2eb662008-02-10 21:38:56 +00001039void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001040 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001041 // No point warning no definition of method which is 'unavailable'.
1042 if (method->hasAttr<UnavailableAttr>())
1043 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001044 if (!IncompleteImpl) {
1045 Diag(ImpLoc, diag::warn_incomplete_impl);
1046 IncompleteImpl = true;
1047 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001048 if (DiagID == diag::warn_unimplemented_protocol_method)
1049 Diag(ImpLoc, DiagID) << method->getDeclName();
1050 else
1051 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001052}
1053
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001054/// Determines if type B can be substituted for type A. Returns true if we can
1055/// guarantee that anything that the user will do to an object of type A can
1056/// also be done to an object of type B. This is trivially true if the two
1057/// types are the same, or if B is a subclass of A. It becomes more complex
1058/// in cases where protocols are involved.
1059///
1060/// Object types in Objective-C describe the minimum requirements for an
1061/// object, rather than providing a complete description of a type. For
1062/// example, if A is a subclass of B, then B* may refer to an instance of A.
1063/// The principle of substitutability means that we may use an instance of A
1064/// anywhere that we may use an instance of B - it will implement all of the
1065/// ivars of B and all of the methods of B.
1066///
1067/// This substitutability is important when type checking methods, because
1068/// the implementation may have stricter type definitions than the interface.
1069/// The interface specifies minimum requirements, but the implementation may
1070/// have more accurate ones. For example, a method may privately accept
1071/// instances of B, but only publish that it accepts instances of A. Any
1072/// object passed to it will be type checked against B, and so will implicitly
1073/// by a valid A*. Similarly, a method may return a subclass of the class that
1074/// it is declared as returning.
1075///
1076/// This is most important when considering subclassing. A method in a
1077/// subclass must accept any object as an argument that its superclass's
1078/// implementation accepts. It may, however, accept a more general type
1079/// without breaking substitutability (i.e. you can still use the subclass
1080/// anywhere that you can use the superclass, but not vice versa). The
1081/// converse requirement applies to return types: the return type for a
1082/// subclass method must be a valid object of the kind that the superclass
1083/// advertises, but it may be specified more accurately. This avoids the need
1084/// for explicit down-casting by callers.
1085///
1086/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001087static bool isObjCTypeSubstitutable(ASTContext &Context,
1088 const ObjCObjectPointerType *A,
1089 const ObjCObjectPointerType *B,
1090 bool rejectId) {
1091 // Reject a protocol-unqualified id.
1092 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001093
1094 // If B is a qualified id, then A must also be a qualified id and it must
1095 // implement all of the protocols in B. It may not be a qualified class.
1096 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1097 // stricter definition so it is not substitutable for id<A>.
1098 if (B->isObjCQualifiedIdType()) {
1099 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001100 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1101 QualType(B,0),
1102 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001103 }
1104
1105 /*
1106 // id is a special type that bypasses type checking completely. We want a
1107 // warning when it is used in one place but not another.
1108 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1109
1110
1111 // If B is a qualified id, then A must also be a qualified id (which it isn't
1112 // if we've got this far)
1113 if (B->isObjCQualifiedIdType()) return false;
1114 */
1115
1116 // Now we know that A and B are (potentially-qualified) class types. The
1117 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001118 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001119}
1120
John McCall10302c02010-10-28 02:34:38 +00001121static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1122 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1123}
1124
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001125static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001126 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001127 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001128 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001129 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001130 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001131 if (IsProtocolMethodDecl &&
1132 (MethodDecl->getObjCDeclQualifier() !=
1133 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001134 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001135 S.Diag(MethodImpl->getLocation(),
1136 (IsOverridingMode ?
1137 diag::warn_conflicting_overriding_ret_type_modifiers
1138 : diag::warn_conflicting_ret_type_modifiers))
1139 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001140 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1141 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1142 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1143 }
1144 else
1145 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001146 }
1147
John McCall10302c02010-10-28 02:34:38 +00001148 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001149 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001150 return true;
1151 if (!Warn)
1152 return false;
John McCall10302c02010-10-28 02:34:38 +00001153
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001154 unsigned DiagID =
1155 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1156 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001157
1158 // Mismatches between ObjC pointers go into a different warning
1159 // category, and sometimes they're even completely whitelisted.
1160 if (const ObjCObjectPointerType *ImplPtrTy =
1161 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1162 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001163 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001164 // Allow non-matching return types as long as they don't violate
1165 // the principle of substitutability. Specifically, we permit
1166 // return types that are subclasses of the declared return type,
1167 // or that are more-qualified versions of the declared type.
1168 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001169 return false;
John McCall10302c02010-10-28 02:34:38 +00001170
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001171 DiagID =
1172 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1173 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001174 }
1175 }
1176
1177 S.Diag(MethodImpl->getLocation(), DiagID)
1178 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001179 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001180 << MethodImpl->getResultType()
1181 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001182 S.Diag(MethodDecl->getLocation(),
1183 IsOverridingMode ? diag::note_previous_declaration
1184 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001185 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001186 return false;
John McCall10302c02010-10-28 02:34:38 +00001187}
1188
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001189static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001190 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001191 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001192 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001193 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001194 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001195 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001196 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001197 if (IsProtocolMethodDecl &&
1198 (ImplVar->getObjCDeclQualifier() !=
1199 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001200 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001201 if (IsOverridingMode)
1202 S.Diag(ImplVar->getLocation(),
1203 diag::warn_conflicting_overriding_param_modifiers)
1204 << getTypeRange(ImplVar->getTypeSourceInfo())
1205 << MethodImpl->getDeclName();
1206 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001207 diag::warn_conflicting_param_modifiers)
1208 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001209 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001210 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1211 << getTypeRange(IfaceVar->getTypeSourceInfo());
1212 }
1213 else
1214 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001215 }
1216
John McCall10302c02010-10-28 02:34:38 +00001217 QualType ImplTy = ImplVar->getType();
1218 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001219
John McCall10302c02010-10-28 02:34:38 +00001220 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001221 return true;
1222
1223 if (!Warn)
1224 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001225 unsigned DiagID =
1226 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1227 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001228
1229 // Mismatches between ObjC pointers go into a different warning
1230 // category, and sometimes they're even completely whitelisted.
1231 if (const ObjCObjectPointerType *ImplPtrTy =
1232 ImplTy->getAs<ObjCObjectPointerType>()) {
1233 if (const ObjCObjectPointerType *IfacePtrTy =
1234 IfaceTy->getAs<ObjCObjectPointerType>()) {
1235 // Allow non-matching argument types as long as they don't
1236 // violate the principle of substitutability. Specifically, the
1237 // implementation must accept any objects that the superclass
1238 // accepts, however it may also accept others.
1239 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001240 return false;
John McCall10302c02010-10-28 02:34:38 +00001241
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001242 DiagID =
1243 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1244 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001245 }
1246 }
1247
1248 S.Diag(ImplVar->getLocation(), DiagID)
1249 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001250 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1251 S.Diag(IfaceVar->getLocation(),
1252 (IsOverridingMode ? diag::note_previous_declaration
1253 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001254 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001255 return false;
John McCall10302c02010-10-28 02:34:38 +00001256}
John McCallf85e1932011-06-15 23:02:42 +00001257
1258/// In ARC, check whether the conventional meanings of the two methods
1259/// match. If they don't, it's a hard error.
1260static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1261 ObjCMethodDecl *decl) {
1262 ObjCMethodFamily implFamily = impl->getMethodFamily();
1263 ObjCMethodFamily declFamily = decl->getMethodFamily();
1264 if (implFamily == declFamily) return false;
1265
1266 // Since conventions are sorted by selector, the only possibility is
1267 // that the types differ enough to cause one selector or the other
1268 // to fall out of the family.
1269 assert(implFamily == OMF_None || declFamily == OMF_None);
1270
1271 // No further diagnostics required on invalid declarations.
1272 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1273
1274 const ObjCMethodDecl *unmatched = impl;
1275 ObjCMethodFamily family = declFamily;
1276 unsigned errorID = diag::err_arc_lost_method_convention;
1277 unsigned noteID = diag::note_arc_lost_method_convention;
1278 if (declFamily == OMF_None) {
1279 unmatched = decl;
1280 family = implFamily;
1281 errorID = diag::err_arc_gained_method_convention;
1282 noteID = diag::note_arc_gained_method_convention;
1283 }
1284
1285 // Indexes into a %select clause in the diagnostic.
1286 enum FamilySelector {
1287 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1288 };
1289 FamilySelector familySelector = FamilySelector();
1290
1291 switch (family) {
1292 case OMF_None: llvm_unreachable("logic error, no method convention");
1293 case OMF_retain:
1294 case OMF_release:
1295 case OMF_autorelease:
1296 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001297 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001298 case OMF_retainCount:
1299 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001300 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001301 // Mismatches for these methods don't change ownership
1302 // conventions, so we don't care.
1303 return false;
1304
1305 case OMF_init: familySelector = F_init; break;
1306 case OMF_alloc: familySelector = F_alloc; break;
1307 case OMF_copy: familySelector = F_copy; break;
1308 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1309 case OMF_new: familySelector = F_new; break;
1310 }
1311
1312 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1313 ReasonSelector reasonSelector;
1314
1315 // The only reason these methods don't fall within their families is
1316 // due to unusual result types.
1317 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1318 reasonSelector = R_UnrelatedReturn;
1319 } else {
1320 reasonSelector = R_NonObjectReturn;
1321 }
1322
1323 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1324 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1325
1326 return true;
1327}
John McCall10302c02010-10-28 02:34:38 +00001328
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001329void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001330 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001331 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001332 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001333 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001334 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001335 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1336 return;
1337
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001338 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001339 IsProtocolMethodDecl, IsOverridingMode,
1340 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Chris Lattner3aff9192009-04-11 19:58:42 +00001342 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001343 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001344 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001345 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1346 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001347 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001348
Fariborz Jahanian21121902011-08-08 18:03:17 +00001349 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001350 if (IsOverridingMode)
1351 Diag(ImpMethodDecl->getLocation(),
1352 diag::warn_conflicting_overriding_variadic);
1353 else
1354 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001355 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001356 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001357}
1358
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001359/// WarnExactTypedMethods - This routine issues a warning if method
1360/// implementation declaration matches exactly that of its declaration.
1361void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1362 ObjCMethodDecl *MethodDecl,
1363 bool IsProtocolMethodDecl) {
1364 // don't issue warning when protocol method is optional because primary
1365 // class is not required to implement it and it is safe for protocol
1366 // to implement it.
1367 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1368 return;
1369 // don't issue warning when primary class's method is
1370 // depecated/unavailable.
1371 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1372 MethodDecl->hasAttr<DeprecatedAttr>())
1373 return;
1374
1375 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1376 IsProtocolMethodDecl, false, false);
1377 if (match)
1378 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1379 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1380 IM != EM; ++IM, ++IF) {
1381 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1382 *IM, *IF,
1383 IsProtocolMethodDecl, false, false);
1384 if (!match)
1385 break;
1386 }
1387 if (match)
1388 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001389 if (match)
1390 match = !(MethodDecl->isClassMethod() &&
1391 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001392
1393 if (match) {
1394 Diag(ImpMethodDecl->getLocation(),
1395 diag::warn_category_method_impl_match);
1396 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1397 }
1398}
1399
Mike Stump390b4cc2009-05-16 07:39:55 +00001400/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1401/// improve the efficiency of selector lookups and type checking by associating
1402/// with each protocol / interface / category the flattened instance tables. If
1403/// we used an immutable set to keep the table then it wouldn't add significant
1404/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001405
Steve Naroffefe7f362008-02-08 22:06:17 +00001406/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001407/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001408void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1409 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001410 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001411 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001412 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001413 ObjCContainerDecl *CDecl) {
1414 ObjCInterfaceDecl *IDecl;
1415 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1416 IDecl = C->getClassInterface();
1417 else
1418 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1419 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1420
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001421 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001422 ObjCInterfaceDecl *NSIDecl = 0;
1423 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001424 // check to see if class implements forwardInvocation method and objects
1425 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001426 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001427 // Under such conditions, which means that every method possible is
1428 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001429 // found" warnings.
1430 // FIXME: Use a general GetUnarySelector method for this.
1431 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1432 Selector fISelector = Context.Selectors.getSelector(1, &II);
1433 if (InsMap.count(fISelector))
1434 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1435 // need be implemented in the implementation.
1436 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1437 }
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001439 // If a method lookup fails locally we still need to look and see if
1440 // the method was implemented by a base class or an inherited
1441 // protocol. This lookup is slow, but occurs rarely in correct code
1442 // and otherwise would terminate in a warning.
1443
Chris Lattner4d391482007-12-12 07:09:47 +00001444 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001445 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001446 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001447 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001448 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001449 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001450 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001451 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001452 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001453 // Ugly, but necessary. Method declared in protcol might have
1454 // have been synthesized due to a property declared in the class which
1455 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001456 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001457 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001458 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001459 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001460 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001461 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001462 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001463 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001464 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1465 << PDecl->getDeclName();
1466 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001467 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001468 }
1469 }
Chris Lattner4d391482007-12-12 07:09:47 +00001470 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001471 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001472 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001473 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001474 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001475 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1476 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001477 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001478 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001479 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1480 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001481 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001482 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001483 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1484 PDecl->getDeclName();
1485 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001486 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001487 }
Chris Lattner780f3292008-07-21 21:32:27 +00001488 // Check on this protocols's referenced protocols, recursively.
1489 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1490 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001491 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001492}
1493
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001494/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001495/// or protocol against those declared in their implementations.
1496///
1497void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1498 const llvm::DenseSet<Selector> &ClsMap,
1499 llvm::DenseSet<Selector> &InsMapSeen,
1500 llvm::DenseSet<Selector> &ClsMapSeen,
1501 ObjCImplDecl* IMPDecl,
1502 ObjCContainerDecl* CDecl,
1503 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001504 bool ImmediateClass,
1505 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001506 // Check and see if instance methods in class interface have been
1507 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001508 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1509 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001510 if (InsMapSeen.count((*I)->getSelector()))
1511 continue;
1512 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001513 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001514 !InsMap.count((*I)->getSelector())) {
1515 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001516 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1517 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001518 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001519 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001520 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001521 IMPDecl->getInstanceMethod((*I)->getSelector());
1522 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1523 "Expected to find the method through lookup as well");
1524 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001525 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001526 if (ImpMethodDecl) {
1527 if (!WarnExactMatch)
1528 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1529 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001530 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001531 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1532 isa<ObjCProtocolDecl>(CDecl));
1533 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001534 }
1535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001537 // Check and see if class methods in class interface have been
1538 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001539 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001540 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001541 if (ClsMapSeen.count((*I)->getSelector()))
1542 continue;
1543 ClsMapSeen.insert((*I)->getSelector());
1544 if (!ClsMap.count((*I)->getSelector())) {
1545 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001546 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1547 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001548 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001549 ObjCMethodDecl *ImpMethodDecl =
1550 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001551 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1552 "Expected to find the method through lookup as well");
1553 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001554 if (!WarnExactMatch)
1555 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1556 isa<ObjCProtocolDecl>(CDecl));
1557 else
1558 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1559 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001560 }
1561 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001562
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001563 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001564 // Also methods in class extensions need be looked at next.
1565 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1566 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1567 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1568 IMPDecl,
1569 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001570 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001571
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001572 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001573 for (ObjCInterfaceDecl::all_protocol_iterator
1574 PI = I->all_referenced_protocol_begin(),
1575 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001576 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1577 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001578 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001579
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001580 // FIXME. For now, we are not checking for extact match of methods
1581 // in category implementation and its primary class's super class.
1582 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001583 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001584 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001585 I->getSuperClass(), IncompleteImpl, false);
1586 }
1587}
1588
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001589/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1590/// category matches with those implemented in its primary class and
1591/// warns each time an exact match is found.
1592void Sema::CheckCategoryVsClassMethodMatches(
1593 ObjCCategoryImplDecl *CatIMPDecl) {
1594 llvm::DenseSet<Selector> InsMap, ClsMap;
1595
1596 for (ObjCImplementationDecl::instmeth_iterator
1597 I = CatIMPDecl->instmeth_begin(),
1598 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1599 InsMap.insert((*I)->getSelector());
1600
1601 for (ObjCImplementationDecl::classmeth_iterator
1602 I = CatIMPDecl->classmeth_begin(),
1603 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1604 ClsMap.insert((*I)->getSelector());
1605 if (InsMap.empty() && ClsMap.empty())
1606 return;
1607
1608 // Get category's primary class.
1609 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1610 if (!CatDecl)
1611 return;
1612 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1613 if (!IDecl)
1614 return;
1615 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1616 bool IncompleteImpl = false;
1617 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1618 CatIMPDecl, IDecl,
1619 IncompleteImpl, false, true /*WarnExactMatch*/);
1620}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001621
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001622void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001623 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001624 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001625 llvm::DenseSet<Selector> InsMap;
1626 // Check and see if instance methods in class interface have been
1627 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001628 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001629 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001630 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001632 // Check and see if properties declared in the interface have either 1)
1633 // an implementation or 2) there is a @synthesize/@dynamic implementation
1634 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001635 if (isa<ObjCInterfaceDecl>(CDecl) &&
1636 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001637 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001638
Chris Lattner4d391482007-12-12 07:09:47 +00001639 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001640 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001641 I = IMPDecl->classmeth_begin(),
1642 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001643 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001645 // Check for type conflict of methods declared in a class/protocol and
1646 // its implementation; if any.
1647 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001648 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1649 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001650 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001651
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001652 // check all methods implemented in category against those declared
1653 // in its primary class.
1654 if (ObjCCategoryImplDecl *CatDecl =
1655 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1656 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Chris Lattner4d391482007-12-12 07:09:47 +00001658 // Check the protocol list for unimplemented methods in the @implementation
1659 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001660 // Check and see if class methods in class interface have been
1661 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Chris Lattnercddc8882009-03-01 00:56:52 +00001663 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001664 for (ObjCInterfaceDecl::all_protocol_iterator
1665 PI = I->all_referenced_protocol_begin(),
1666 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001667 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001668 InsMap, ClsMap, I);
1669 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001670 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1671 Categories; Categories = Categories->getNextClassExtension())
1672 ImplMethodsVsClassMethods(S, IMPDecl,
1673 const_cast<ObjCCategoryDecl*>(Categories),
1674 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001675 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001676 // For extended class, unimplemented methods in its protocols will
1677 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001678 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001679 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1680 E = C->protocol_end(); PI != E; ++PI)
1681 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001682 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001683 // Report unimplemented properties in the category as well.
1684 // When reporting on missing setter/getters, do not report when
1685 // setter/getter is implemented in category's primary class
1686 // implementation.
1687 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1688 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1689 for (ObjCImplementationDecl::instmeth_iterator
1690 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1691 InsMap.insert((*I)->getSelector());
1692 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001693 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001694 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001695 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001696 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001697}
1698
Mike Stump1eb44332009-09-09 15:08:12 +00001699/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001700Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001701Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001702 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001703 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001704 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001705 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001706 for (unsigned i = 0; i != NumElts; ++i) {
1707 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001708 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001709 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001710 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001711 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001712 // Maybe we will complain about the shadowed template parameter.
1713 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1714 // Just pretend that we didn't see the previous declaration.
1715 PrevDecl = 0;
1716 }
1717
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001718 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001719 // GCC apparently allows the following idiom:
1720 //
1721 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1722 // @class XCElementToggler;
1723 //
Mike Stump1eb44332009-09-09 15:08:12 +00001724 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001725 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001726 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001727 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001728 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001729 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001730 // a forward class declaration matching a typedef name of a class refers
1731 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001732 if (const ObjCObjectType *OI =
1733 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1734 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001735 }
Chris Lattner4d391482007-12-12 07:09:47 +00001736 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001737 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1738 if (!IDecl) { // Not already seen? Make a forward decl.
1739 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1740 IdentList[i], IdentLocs[i], true);
1741
1742 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1743 // the current DeclContext. This prevents clients that walk DeclContext
1744 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1745 // declared later (if at all). We also take care to explicitly make
1746 // sure this declaration is visible for name lookup.
1747 PushOnScopeChains(IDecl, TUScope, false);
1748 CurContext->makeDeclVisibleInContext(IDecl, true);
1749 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001750 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1751 IDecl, IdentLocs[i]);
1752 CurContext->addDecl(CDecl);
1753 CheckObjCDeclScope(CDecl);
1754 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001755 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001756
1757 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001758}
1759
John McCall0f4c4c42011-06-16 01:15:19 +00001760static bool tryMatchRecordTypes(ASTContext &Context,
1761 Sema::MethodMatchStrategy strategy,
1762 const Type *left, const Type *right);
1763
John McCallf85e1932011-06-15 23:02:42 +00001764static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1765 QualType leftQT, QualType rightQT) {
1766 const Type *left =
1767 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1768 const Type *right =
1769 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1770
1771 if (left == right) return true;
1772
1773 // If we're doing a strict match, the types have to match exactly.
1774 if (strategy == Sema::MMS_strict) return false;
1775
1776 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1777
1778 // Otherwise, use this absurdly complicated algorithm to try to
1779 // validate the basic, low-level compatibility of the two types.
1780
1781 // As a minimum, require the sizes and alignments to match.
1782 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1783 return false;
1784
1785 // Consider all the kinds of non-dependent canonical types:
1786 // - functions and arrays aren't possible as return and parameter types
1787
1788 // - vector types of equal size can be arbitrarily mixed
1789 if (isa<VectorType>(left)) return isa<VectorType>(right);
1790 if (isa<VectorType>(right)) return false;
1791
1792 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001793 // - structs, unions, and Objective-C objects must match more-or-less
1794 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001795 // - everything else should be a scalar
1796 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001797 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001798
John McCall1d9b3b22011-09-09 05:25:32 +00001799 // Make scalars agree in kind, except count bools as chars, and group
1800 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001801 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1802 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1803 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1804 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001805 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1806 leftSK = Type::STK_ObjCObjectPointer;
1807 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1808 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001809
1810 // Note that data member pointers and function member pointers don't
1811 // intermix because of the size differences.
1812
1813 return (leftSK == rightSK);
1814}
Chris Lattner4d391482007-12-12 07:09:47 +00001815
John McCall0f4c4c42011-06-16 01:15:19 +00001816static bool tryMatchRecordTypes(ASTContext &Context,
1817 Sema::MethodMatchStrategy strategy,
1818 const Type *lt, const Type *rt) {
1819 assert(lt && rt && lt != rt);
1820
1821 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1822 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1823 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1824
1825 // Require union-hood to match.
1826 if (left->isUnion() != right->isUnion()) return false;
1827
1828 // Require an exact match if either is non-POD.
1829 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1830 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1831 return false;
1832
1833 // Require size and alignment to match.
1834 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1835
1836 // Require fields to match.
1837 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1838 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1839 for (; li != le && ri != re; ++li, ++ri) {
1840 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1841 return false;
1842 }
1843 return (li == le && ri == re);
1844}
1845
Chris Lattner4d391482007-12-12 07:09:47 +00001846/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1847/// returns true, or false, accordingly.
1848/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001849bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1850 const ObjCMethodDecl *right,
1851 MethodMatchStrategy strategy) {
1852 if (!matchTypes(Context, strategy,
1853 left->getResultType(), right->getResultType()))
1854 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001855
John McCallf85e1932011-06-15 23:02:42 +00001856 if (getLangOptions().ObjCAutoRefCount &&
1857 (left->hasAttr<NSReturnsRetainedAttr>()
1858 != right->hasAttr<NSReturnsRetainedAttr>() ||
1859 left->hasAttr<NSConsumesSelfAttr>()
1860 != right->hasAttr<NSConsumesSelfAttr>()))
1861 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
John McCallf85e1932011-06-15 23:02:42 +00001863 ObjCMethodDecl::param_iterator
1864 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001865
John McCallf85e1932011-06-15 23:02:42 +00001866 for (; li != le; ++li, ++ri) {
1867 assert(ri != right->param_end() && "Param mismatch");
1868 ParmVarDecl *lparm = *li, *rparm = *ri;
1869
1870 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1871 return false;
1872
1873 if (getLangOptions().ObjCAutoRefCount &&
1874 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1875 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001876 }
1877 return true;
1878}
1879
Sebastian Redldb9d2142010-08-02 23:18:59 +00001880/// \brief Read the contents of the method pool for a given selector from
1881/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001882///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001883/// This routine should only be called once, when the method pool has no entry
1884/// for this selector.
1885Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001886 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001887 assert(MethodPool.find(Sel) == MethodPool.end() &&
1888 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001889
1890 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001891 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Sebastian Redldb9d2142010-08-02 23:18:59 +00001893 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001894}
1895
Sebastian Redldb9d2142010-08-02 23:18:59 +00001896void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1897 bool instance) {
1898 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1899 if (Pos == MethodPool.end()) {
1900 if (ExternalSource)
1901 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001902 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001903 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1904 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001905 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001906 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001907 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001908 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001909 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001910 Entry.Method = Method;
1911 Entry.Next = 0;
1912 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001913 }
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Chris Lattnerb25df352009-03-04 05:16:45 +00001915 // We've seen a method with this name, see if we have already seen this type
1916 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001917 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1918 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1919
1920 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001921 ObjCMethodDecl *PrevObjCMethod = List->Method;
1922 PrevObjCMethod->setDefined(impl);
1923 // If a method is deprecated, push it in the global pool.
1924 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001925 if (Method->isDeprecated()) {
1926 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001927 List->Method = Method;
1928 }
1929 // If new method is unavailable, push it into global pool
1930 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001931 if (Method->isUnavailable()) {
1932 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001933 List->Method = Method;
1934 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001935 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001936 }
John McCallf85e1932011-06-15 23:02:42 +00001937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Chris Lattnerb25df352009-03-04 05:16:45 +00001939 // We have a new signature for an existing method - add it.
1940 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001941 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1942 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001943}
1944
John McCallf85e1932011-06-15 23:02:42 +00001945/// Determines if this is an "acceptable" loose mismatch in the global
1946/// method pool. This exists mostly as a hack to get around certain
1947/// global mismatches which we can't afford to make warnings / errors.
1948/// Really, what we want is a way to take a method out of the global
1949/// method pool.
1950static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1951 ObjCMethodDecl *other) {
1952 if (!chosen->isInstanceMethod())
1953 return false;
1954
1955 Selector sel = chosen->getSelector();
1956 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1957 return false;
1958
1959 // Don't complain about mismatches for -length if the method we
1960 // chose has an integral result type.
1961 return (chosen->getResultType()->isIntegerType());
1962}
1963
Sebastian Redldb9d2142010-08-02 23:18:59 +00001964ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001965 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001966 bool warn, bool instance) {
1967 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1968 if (Pos == MethodPool.end()) {
1969 if (ExternalSource)
1970 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001971 else
1972 return 0;
1973 }
1974
Sebastian Redldb9d2142010-08-02 23:18:59 +00001975 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Sebastian Redldb9d2142010-08-02 23:18:59 +00001977 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001978 bool issueDiagnostic = false, issueError = false;
1979
1980 // We support a warning which complains about *any* difference in
1981 // method signature.
1982 bool strictSelectorMatch =
1983 (receiverIdOrClass && warn &&
1984 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1985 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00001986 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001987 if (strictSelectorMatch)
1988 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001989 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1990 MMS_strict)) {
1991 issueDiagnostic = true;
1992 break;
1993 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001994 }
1995
John McCallf85e1932011-06-15 23:02:42 +00001996 // If we didn't see any strict differences, we won't see any loose
1997 // differences. In ARC, however, we also need to check for loose
1998 // mismatches, because most of them are errors.
1999 if (!strictSelectorMatch ||
2000 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002001 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002002 // This checks if the methods differ in type mismatch.
2003 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2004 MMS_loose) &&
2005 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2006 issueDiagnostic = true;
2007 if (getLangOptions().ObjCAutoRefCount)
2008 issueError = true;
2009 break;
2010 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002011 }
2012
John McCallf85e1932011-06-15 23:02:42 +00002013 if (issueDiagnostic) {
2014 if (issueError)
2015 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2016 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002017 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2018 else
2019 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002020
2021 Diag(MethList.Method->getLocStart(),
2022 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002023 << MethList.Method->getSourceRange();
2024 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2025 Diag(Next->Method->getLocStart(), diag::note_also_found)
2026 << Next->Method->getSourceRange();
2027 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002028 }
2029 return MethList.Method;
2030}
2031
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002032ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002033 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2034 if (Pos == MethodPool.end())
2035 return 0;
2036
2037 GlobalMethods &Methods = Pos->second;
2038
2039 if (Methods.first.Method && Methods.first.Method->isDefined())
2040 return Methods.first.Method;
2041 if (Methods.second.Method && Methods.second.Method->isDefined())
2042 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002043 return 0;
2044}
2045
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002046/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2047/// identical selector names in current and its super classes and issues
2048/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002049void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2050 ObjCMethodDecl *Method,
2051 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002052 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2053 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002055 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002056 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002057 SD->lookupMethod(Method->getSelector(), IsInstance);
2058 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002059 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002060 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002061 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002062 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2063 E = Method->param_end();
2064 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2065 for (; ParamI != E; ++ParamI, ++PrevI) {
2066 // Number of parameters are the same and is guaranteed by selector match.
2067 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2068 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2069 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002070 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002071 // respective argument type in the super class method, issue warning;
2072 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002073 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002074 << T1 << T2;
2075 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2076 return;
2077 }
2078 }
2079 ID = SD;
2080 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002081}
2082
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002083/// DiagnoseDuplicateIvars -
2084/// Check for duplicate ivars in the entire class at the start of
2085/// @implementation. This becomes necesssary because class extension can
2086/// add ivars to a class in random order which will not be known until
2087/// class's @implementation is seen.
2088void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2089 ObjCInterfaceDecl *SID) {
2090 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2091 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2092 ObjCIvarDecl* Ivar = (*IVI);
2093 if (Ivar->isInvalidDecl())
2094 continue;
2095 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2096 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2097 if (prevIvar) {
2098 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2099 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2100 Ivar->setInvalidDecl();
2101 }
2102 }
2103 }
2104}
2105
Steve Naroffa56f6162007-12-18 01:30:32 +00002106// Note: For class/category implemenations, allMethods/allProperties is
2107// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002108void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002109 Decl **allMethods, unsigned allNum,
2110 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002111 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002112
2113 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002114 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002115 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2116 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002117
Mike Stump1eb44332009-09-09 15:08:12 +00002118 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002119 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2120 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002121 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002122
Ted Kremenek782f2f52010-01-07 01:20:12 +00002123 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2124 // FIXME: This is wrong. We shouldn't be pretending that there is
2125 // an '@end' in the declaration.
2126 SourceLocation L = ClassDecl->getLocation();
2127 AtEnd.setBegin(L);
2128 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002129 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002130 }
2131
Steve Naroff0701bbb2009-01-08 17:28:14 +00002132 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2133 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2134 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2135
Chris Lattner4d391482007-12-12 07:09:47 +00002136 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002137 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002138 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002139
2140 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002141 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002142 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002143 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002144 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002145 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002146 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002147 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002148 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002149 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002150 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002151 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002152 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002153 InsMap[Method->getSelector()] = Method;
2154 /// The following allows us to typecheck messages to "id".
2155 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002156 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002157 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002158 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002159 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002160 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002161 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002162 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002163 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002164 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002165 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002166 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002167 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002168 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002169 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002170 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002171 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002172 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002173 /// The following allows us to typecheck messages to "Class".
2174 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002175 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002176 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002177 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002178 }
2179 }
2180 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002181 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002182 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002183 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002184 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002185 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002186 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002187 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002188 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002189 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002190
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002191 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002192 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002193 if (C->IsClassExtension()) {
2194 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2195 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002196 }
Chris Lattner4d391482007-12-12 07:09:47 +00002197 }
Steve Naroff09c47192009-01-09 15:36:25 +00002198 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002199 if (CDecl->getIdentifier())
2200 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2201 // user-defined setter/getter. It also synthesizes setter/getter methods
2202 // and adds them to the DeclContext and global method pools.
2203 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2204 E = CDecl->prop_end();
2205 I != E; ++I)
2206 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002207 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002208 }
2209 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002210 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002211 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002212 // Any property declared in a class extension might have user
2213 // declared setter or getter in current class extension or one
2214 // of the other class extensions. Mark them as synthesized as
2215 // property will be synthesized when property with same name is
2216 // seen in the @implementation.
2217 for (const ObjCCategoryDecl *ClsExtDecl =
2218 IDecl->getFirstClassExtension();
2219 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2220 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2221 E = ClsExtDecl->prop_end(); I != E; ++I) {
2222 ObjCPropertyDecl *Property = (*I);
2223 // Skip over properties declared @dynamic
2224 if (const ObjCPropertyImplDecl *PIDecl
2225 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2226 if (PIDecl->getPropertyImplementation()
2227 == ObjCPropertyImplDecl::Dynamic)
2228 continue;
2229
2230 for (const ObjCCategoryDecl *CExtDecl =
2231 IDecl->getFirstClassExtension();
2232 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2233 if (ObjCMethodDecl *GetterMethod =
2234 CExtDecl->getInstanceMethod(Property->getGetterName()))
2235 GetterMethod->setSynthesized(true);
2236 if (!Property->isReadOnly())
2237 if (ObjCMethodDecl *SetterMethod =
2238 CExtDecl->getInstanceMethod(Property->getSetterName()))
2239 SetterMethod->setSynthesized(true);
2240 }
2241 }
2242 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002243 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002244 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002245 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002246
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002247 if (LangOpts.ObjCNonFragileABI2)
2248 while (IDecl->getSuperClass()) {
2249 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2250 IDecl = IDecl->getSuperClass();
2251 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002252 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002253 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002254 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002255 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002256 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002257
Chris Lattner4d391482007-12-12 07:09:47 +00002258 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002259 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002260 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002261 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002262 Categories; Categories = Categories->getNextClassCategory()) {
2263 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002264 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002265 break;
2266 }
2267 }
2268 }
2269 }
Chris Lattner682bf922009-03-29 16:50:03 +00002270 if (isInterfaceDeclKind) {
2271 // Reject invalid vardecls.
2272 for (unsigned i = 0; i != tuvNum; i++) {
2273 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2274 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2275 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002276 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002277 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002278 }
Chris Lattner682bf922009-03-29 16:50:03 +00002279 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002280 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002281 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002282}
2283
2284
2285/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2286/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002287static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002288CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002289 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002290}
2291
Ted Kremenek422bae72010-04-18 04:59:38 +00002292static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002293bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002294 // The 'ibaction' attribute is allowed on method definitions because of
2295 // how the IBAction macro is used on both method declarations and definitions.
2296 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002297 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2298 if ((*i)->getKind() != attr::IBAction)
2299 return true;
2300 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002301}
2302
Douglas Gregore97179c2011-09-08 01:46:34 +00002303namespace {
2304 /// \brief Describes the compatibility of a result type with its method.
2305 enum ResultTypeCompatibilityKind {
2306 RTC_Compatible,
2307 RTC_Incompatible,
2308 RTC_Unknown
2309 };
2310}
2311
Douglas Gregor926df6c2011-06-11 01:09:30 +00002312/// \brief Check whether the declared result type of the given Objective-C
2313/// method declaration is compatible with the method's class.
2314///
Douglas Gregore97179c2011-09-08 01:46:34 +00002315static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002316CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2317 ObjCInterfaceDecl *CurrentClass) {
2318 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002319
2320 // If an Objective-C method inherits its related result type, then its
2321 // declared result type must be compatible with its own class type. The
2322 // declared result type is compatible if:
2323 if (const ObjCObjectPointerType *ResultObjectType
2324 = ResultType->getAs<ObjCObjectPointerType>()) {
2325 // - it is id or qualified id, or
2326 if (ResultObjectType->isObjCIdType() ||
2327 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002328 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002329
2330 if (CurrentClass) {
2331 if (ObjCInterfaceDecl *ResultClass
2332 = ResultObjectType->getInterfaceDecl()) {
2333 // - it is the same as the method's class type, or
2334 if (CurrentClass == ResultClass)
Douglas Gregore97179c2011-09-08 01:46:34 +00002335 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002336
2337 // - it is a superclass of the method's class type
2338 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002339 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002340 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002341 } else {
2342 // Any Objective-C pointer type might be acceptable for a protocol
2343 // method; we just don't know.
2344 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002345 }
2346 }
2347
Douglas Gregore97179c2011-09-08 01:46:34 +00002348 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002349}
2350
John McCall6c2c2502011-07-22 02:45:48 +00002351namespace {
2352/// A helper class for searching for methods which a particular method
2353/// overrides.
2354class OverrideSearch {
2355 Sema &S;
2356 ObjCMethodDecl *Method;
2357 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2358 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2359 bool Recursive;
2360
2361public:
2362 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2363 Selector selector = method->getSelector();
2364
2365 // Bypass this search if we've never seen an instance/class method
2366 // with this selector before.
2367 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2368 if (it == S.MethodPool.end()) {
2369 if (!S.ExternalSource) return;
2370 it = S.ReadMethodPool(selector);
2371 }
2372 ObjCMethodList &list =
2373 method->isInstanceMethod() ? it->second.first : it->second.second;
2374 if (!list.Method) return;
2375
2376 ObjCContainerDecl *container
2377 = cast<ObjCContainerDecl>(method->getDeclContext());
2378
2379 // Prevent the search from reaching this container again. This is
2380 // important with categories, which override methods from the
2381 // interface and each other.
2382 Searched.insert(container);
2383 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002384 }
John McCall6c2c2502011-07-22 02:45:48 +00002385
2386 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2387 iterator begin() const { return Overridden.begin(); }
2388 iterator end() const { return Overridden.end(); }
2389
2390private:
2391 void searchFromContainer(ObjCContainerDecl *container) {
2392 if (container->isInvalidDecl()) return;
2393
2394 switch (container->getDeclKind()) {
2395#define OBJCCONTAINER(type, base) \
2396 case Decl::type: \
2397 searchFrom(cast<type##Decl>(container)); \
2398 break;
2399#define ABSTRACT_DECL(expansion)
2400#define DECL(type, base) \
2401 case Decl::type:
2402#include "clang/AST/DeclNodes.inc"
2403 llvm_unreachable("not an ObjC container!");
2404 }
2405 }
2406
2407 void searchFrom(ObjCProtocolDecl *protocol) {
2408 // A method in a protocol declaration overrides declarations from
2409 // referenced ("parent") protocols.
2410 search(protocol->getReferencedProtocols());
2411 }
2412
2413 void searchFrom(ObjCCategoryDecl *category) {
2414 // A method in a category declaration overrides declarations from
2415 // the main class and from protocols the category references.
2416 search(category->getClassInterface());
2417 search(category->getReferencedProtocols());
2418 }
2419
2420 void searchFrom(ObjCCategoryImplDecl *impl) {
2421 // A method in a category definition that has a category
2422 // declaration overrides declarations from the category
2423 // declaration.
2424 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2425 search(category);
2426
2427 // Otherwise it overrides declarations from the class.
2428 } else {
2429 search(impl->getClassInterface());
2430 }
2431 }
2432
2433 void searchFrom(ObjCInterfaceDecl *iface) {
2434 // A method in a class declaration overrides declarations from
2435
2436 // - categories,
2437 for (ObjCCategoryDecl *category = iface->getCategoryList();
2438 category; category = category->getNextClassCategory())
2439 search(category);
2440
2441 // - the super class, and
2442 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2443 search(super);
2444
2445 // - any referenced protocols.
2446 search(iface->getReferencedProtocols());
2447 }
2448
2449 void searchFrom(ObjCImplementationDecl *impl) {
2450 // A method in a class implementation overrides declarations from
2451 // the class interface.
2452 search(impl->getClassInterface());
2453 }
2454
2455
2456 void search(const ObjCProtocolList &protocols) {
2457 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2458 i != e; ++i)
2459 search(*i);
2460 }
2461
2462 void search(ObjCContainerDecl *container) {
2463 // Abort if we've already searched this container.
2464 if (!Searched.insert(container)) return;
2465
2466 // Check for a method in this container which matches this selector.
2467 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2468 Method->isInstanceMethod());
2469
2470 // If we find one, record it and bail out.
2471 if (meth) {
2472 Overridden.insert(meth);
2473 return;
2474 }
2475
2476 // Otherwise, search for methods that a hypothetical method here
2477 // would have overridden.
2478
2479 // Note that we're now in a recursive case.
2480 Recursive = true;
2481
2482 searchFromContainer(container);
2483 }
2484};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002485}
2486
John McCalld226f652010-08-21 09:40:31 +00002487Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002488 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002489 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002490 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002491 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002492 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002493 Selector Sel,
2494 // optional arguments. The number of types/arguments is obtained
2495 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002496 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002497 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002498 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002499 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002500 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002501 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002502 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002503 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002504 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002505 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2506 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002507 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002508
Douglas Gregore97179c2011-09-08 01:46:34 +00002509 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002510 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002511 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002512 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Steve Naroffccef3712009-02-20 22:59:16 +00002514 // Methods cannot return interface types. All ObjC objects are
2515 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002516 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002517 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2518 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002519 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002520 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002521
2522 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002523 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002524 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002525 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002526 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002527 }
Mike Stump1eb44332009-09-09 15:08:12 +00002528
2529 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002530 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, SelectorLocs, Sel,
2531 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002532 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002533 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002534 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002535 /*isSynthesized=*/false,
2536 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002537 MethodDeclKind == tok::objc_optional
2538 ? ObjCMethodDecl::Optional
2539 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002540 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002541
Chris Lattner5f9e2722011-07-23 10:55:15 +00002542 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002543
Chris Lattner7db638d2009-04-11 19:42:43 +00002544 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002545 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002546 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002547
Chris Lattnere294d3f2009-04-11 18:57:04 +00002548 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002549 ArgType = Context.getObjCIdType();
2550 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002551 } else {
John McCall58e46772009-10-23 21:48:59 +00002552 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002553 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002554 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002555 }
Mike Stump1eb44332009-09-09 15:08:12 +00002556
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002557 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2558 LookupOrdinaryName, ForRedeclaration);
2559 LookupName(R, S);
2560 if (R.isSingleResult()) {
2561 NamedDecl *PrevDecl = R.getFoundDecl();
2562 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002563 Diag(ArgInfo[i].NameLoc,
2564 (MethodDefinition ? diag::warn_method_param_redefinition
2565 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002566 << ArgInfo[i].Name;
2567 Diag(PrevDecl->getLocation(),
2568 diag::note_previous_declaration);
2569 }
2570 }
2571
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002572 SourceLocation StartLoc = DI
2573 ? DI->getTypeLoc().getBeginLoc()
2574 : ArgInfo[i].NameLoc;
2575
John McCall81ef3e62011-04-23 02:46:06 +00002576 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2577 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2578 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002579
John McCall70798862011-05-02 00:30:12 +00002580 Param->setObjCMethodScopeInfo(i);
2581
Chris Lattner0ed844b2008-04-04 06:12:32 +00002582 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002583 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002585 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002586 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002588 S->AddDecl(Param);
2589 IdResolver.AddDecl(Param);
2590
Chris Lattner0ed844b2008-04-04 06:12:32 +00002591 Params.push_back(Param);
2592 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002593
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002594 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002595 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002596 QualType ArgType = Param->getType();
2597 if (ArgType.isNull())
2598 ArgType = Context.getObjCIdType();
2599 else
2600 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002601 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002602 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002603 Diag(Param->getLocation(),
2604 diag::err_object_cannot_be_passed_returned_by_value)
2605 << 1 << ArgType;
2606 Param->setInvalidDecl();
2607 }
2608 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002609
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002610 Params.push_back(Param);
2611 }
2612
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +00002613 ObjCMethod->setMethodParams(Context, Params.data(), Params.size());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002614 ObjCMethod->setObjCDeclQualifier(
2615 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002616
2617 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002618 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002620 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002621 const ObjCMethodDecl *PrevMethod = 0;
2622 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002623 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002624 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2625 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002626 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002627 PrevMethod = ImpDecl->getClassMethod(Sel);
2628 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002629 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002630
Sean Huntcf807c42010-08-18 23:23:40 +00002631 if (ObjCMethod->hasAttrs() &&
2632 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002633 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002634 } else {
2635 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002636 }
John McCall6c2c2502011-07-22 02:45:48 +00002637
Chris Lattner4d391482007-12-12 07:09:47 +00002638 if (PrevMethod) {
2639 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002640 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002641 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002642 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002643 }
John McCall54abf7d2009-11-04 02:18:39 +00002644
Douglas Gregor926df6c2011-06-11 01:09:30 +00002645 // If this Objective-C method does not have a related result type, but we
2646 // are allowed to infer related result types, try to do so based on the
2647 // method family.
2648 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2649 if (!CurrentClass) {
2650 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2651 CurrentClass = Cat->getClassInterface();
2652 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2653 CurrentClass = Impl->getClassInterface();
2654 else if (ObjCCategoryImplDecl *CatImpl
2655 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2656 CurrentClass = CatImpl->getClassInterface();
2657 }
John McCall6c2c2502011-07-22 02:45:48 +00002658
Douglas Gregore97179c2011-09-08 01:46:34 +00002659 ResultTypeCompatibilityKind RTC
2660 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002661
2662 // Search for overridden methods and merge information down from them.
2663 OverrideSearch overrides(*this, ObjCMethod);
2664 for (OverrideSearch::iterator
2665 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2666 ObjCMethodDecl *overridden = *i;
2667
2668 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002669 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002670 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002671
2672 // Then merge the declarations.
2673 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002674
2675 // Check for overriding methods
2676 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2677 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2678 WarnConflictingTypedMethods(ObjCMethod, overridden,
2679 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2680 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002681 }
2682
John McCallf85e1932011-06-15 23:02:42 +00002683 bool ARCError = false;
2684 if (getLangOptions().ObjCAutoRefCount)
2685 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2686
Douglas Gregore97179c2011-09-08 01:46:34 +00002687 // Infer the related result type when possible.
2688 if (!ARCError && RTC == RTC_Compatible &&
2689 !ObjCMethod->hasRelatedResultType() &&
2690 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002691 bool InferRelatedResultType = false;
2692 switch (ObjCMethod->getMethodFamily()) {
2693 case OMF_None:
2694 case OMF_copy:
2695 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002696 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002697 case OMF_mutableCopy:
2698 case OMF_release:
2699 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002700 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002701 break;
2702
2703 case OMF_alloc:
2704 case OMF_new:
2705 InferRelatedResultType = ObjCMethod->isClassMethod();
2706 break;
2707
2708 case OMF_init:
2709 case OMF_autorelease:
2710 case OMF_retain:
2711 case OMF_self:
2712 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2713 break;
2714 }
2715
John McCall6c2c2502011-07-22 02:45:48 +00002716 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002717 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002718 }
2719
John McCalld226f652010-08-21 09:40:31 +00002720 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002721}
2722
Chris Lattnercc98eac2008-12-17 07:13:27 +00002723bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002724 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002725 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002726 // Following is also an error. But it is caused by a missing @end
2727 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002728 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2729 return false;
2730 }
2731
Anders Carlsson15281452008-11-04 16:57:32 +00002732 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2733 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002734
Anders Carlsson15281452008-11-04 16:57:32 +00002735 return true;
2736}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002737
Chris Lattnercc98eac2008-12-17 07:13:27 +00002738/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2739/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002740void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002741 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002742 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002743 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002744 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002745 if (!Class) {
2746 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2747 return;
2748 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002749 if (LangOpts.ObjCNonFragileABI) {
2750 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2751 return;
2752 }
Mike Stump1eb44332009-09-09 15:08:12 +00002753
Chris Lattnercc98eac2008-12-17 07:13:27 +00002754 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002755 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002756 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002757 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002758 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002759 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002760 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002761 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2762 /*FIXME: StartL=*/ID->getLocation(),
2763 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002764 ID->getIdentifier(), ID->getType(),
2765 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002766 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002767 }
Mike Stump1eb44332009-09-09 15:08:12 +00002768
Chris Lattnercc98eac2008-12-17 07:13:27 +00002769 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002770 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002771 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002772 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002773 if (getLangOptions().CPlusPlus)
2774 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002775 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002776 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002777 }
2778}
2779
Douglas Gregor160b5632010-04-26 17:32:49 +00002780/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002781VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2782 SourceLocation StartLoc,
2783 SourceLocation IdLoc,
2784 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002785 bool Invalid) {
2786 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2787 // duration shall not be qualified by an address-space qualifier."
2788 // Since all parameters have automatic store duration, they can not have
2789 // an address space.
2790 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002791 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002792 Invalid = true;
2793 }
2794
2795 // An @catch parameter must be an unqualified object pointer type;
2796 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2797 if (Invalid) {
2798 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002799 } else if (T->isDependentType()) {
2800 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002801 } else if (!T->isObjCObjectPointerType()) {
2802 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002803 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002804 } else if (T->isObjCQualifiedIdType()) {
2805 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002806 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002807 }
2808
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002809 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2810 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002811 New->setExceptionVariable(true);
2812
Douglas Gregor160b5632010-04-26 17:32:49 +00002813 if (Invalid)
2814 New->setInvalidDecl();
2815 return New;
2816}
2817
John McCalld226f652010-08-21 09:40:31 +00002818Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002819 const DeclSpec &DS = D.getDeclSpec();
2820
2821 // We allow the "register" storage class on exception variables because
2822 // GCC did, but we drop it completely. Any other storage class is an error.
2823 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2824 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2825 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2826 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2827 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2828 << DS.getStorageClassSpec();
2829 }
2830 if (D.getDeclSpec().isThreadSpecified())
2831 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2832 D.getMutableDeclSpec().ClearStorageClassSpecs();
2833
2834 DiagnoseFunctionSpecifiers(D);
2835
2836 // Check that there are no default arguments inside the type of this
2837 // exception object (C++ only).
2838 if (getLangOptions().CPlusPlus)
2839 CheckExtraCXXDefaultArguments(D);
2840
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002841 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002842 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002843
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002844 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2845 D.getSourceRange().getBegin(),
2846 D.getIdentifierLoc(),
2847 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002848 D.isInvalidType());
2849
2850 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2851 if (D.getCXXScopeSpec().isSet()) {
2852 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2853 << D.getCXXScopeSpec().getRange();
2854 New->setInvalidDecl();
2855 }
2856
2857 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002858 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002859 if (D.getIdentifier())
2860 IdResolver.AddDecl(New);
2861
2862 ProcessDeclAttributes(S, New, D);
2863
2864 if (New->hasAttr<BlocksAttr>())
2865 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002866 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002867}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002868
2869/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002870/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002871void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002872 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002873 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2874 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002875 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002876 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002877 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002878 }
2879}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002880
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002881void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002882 // Load referenced selectors from the external source.
2883 if (ExternalSource) {
2884 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2885 ExternalSource->ReadReferencedSelectors(Sels);
2886 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2887 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2888 }
2889
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002890 // Warning will be issued only when selector table is
2891 // generated (which means there is at lease one implementation
2892 // in the TU). This is to match gcc's behavior.
2893 if (ReferencedSelectors.empty() ||
2894 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002895 return;
2896 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2897 ReferencedSelectors.begin(),
2898 E = ReferencedSelectors.end(); S != E; ++S) {
2899 Selector Sel = (*S).first;
2900 if (!LookupImplementedMethodInGlobalPool(Sel))
2901 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2902 }
2903 return;
2904}