blob: ebb0cb5463ed381e7af6303f656e35b03f076565 [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"
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +000024#include "clang/AST/ASTMutationListener.h"
John McCallf85e1932011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000027#include "llvm/ADT/DenseSet.h"
28
Chris Lattner4d391482007-12-12 07:09:47 +000029using namespace clang;
30
John McCallf85e1932011-06-15 23:02:42 +000031/// Check whether the given method, which must be in the 'init'
32/// family, is a valid member of that family.
33///
34/// \param receiverTypeIfCall - if null, check this as if declaring it;
35/// if non-null, check this as if making a call to it with the given
36/// receiver type
37///
38/// \return true to indicate that there was an error and appropriate
39/// actions were taken
40bool Sema::checkInitMethod(ObjCMethodDecl *method,
41 QualType receiverTypeIfCall) {
42 if (method->isInvalidDecl()) return true;
43
44 // This castAs is safe: methods that don't return an object
45 // pointer won't be inferred as inits and will reject an explicit
46 // objc_method_family(init).
47
48 // We ignore protocols here. Should we? What about Class?
49
50 const ObjCObjectType *result = method->getResultType()
51 ->castAs<ObjCObjectPointerType>()->getObjectType();
52
53 if (result->isObjCId()) {
54 return false;
55 } else if (result->isObjCClass()) {
56 // fall through: always an error
57 } else {
58 ObjCInterfaceDecl *resultClass = result->getInterface();
59 assert(resultClass && "unexpected object type!");
60
61 // It's okay for the result type to still be a forward declaration
62 // if we're checking an interface declaration.
63 if (resultClass->isForwardDecl()) {
64 if (receiverTypeIfCall.isNull() &&
65 !isa<ObjCImplementationDecl>(method->getDeclContext()))
66 return false;
67
68 // Otherwise, we try to compare class types.
69 } else {
70 // If this method was declared in a protocol, we can't check
71 // anything unless we have a receiver type that's an interface.
72 const ObjCInterfaceDecl *receiverClass = 0;
73 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
74 if (receiverTypeIfCall.isNull())
75 return false;
76
77 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
78 ->getInterfaceDecl();
79
80 // This can be null for calls to e.g. id<Foo>.
81 if (!receiverClass) return false;
82 } else {
83 receiverClass = method->getClassInterface();
84 assert(receiverClass && "method not associated with a class!");
85 }
86
87 // If either class is a subclass of the other, it's fine.
88 if (receiverClass->isSuperClassOf(resultClass) ||
89 resultClass->isSuperClassOf(receiverClass))
90 return false;
91 }
92 }
93
94 SourceLocation loc = method->getLocation();
95
96 // If we're in a system header, and this is not a call, just make
97 // the method unusable.
98 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
99 method->addAttr(new (Context) UnavailableAttr(loc, Context,
100 "init method returns a type unrelated to its receiver type"));
101 return true;
102 }
103
104 // Otherwise, it's an error.
105 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
106 method->setInvalidDecl();
107 return true;
108}
109
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000110void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000111 const ObjCMethodDecl *Overridden,
112 bool IsImplementation) {
113 if (Overridden->hasRelatedResultType() &&
114 !NewMethod->hasRelatedResultType()) {
115 // This can only happen when the method follows a naming convention that
116 // implies a related result type, and the original (overridden) method has
117 // a suitable return type, but the new (overriding) method does not have
118 // a suitable return type.
119 QualType ResultType = NewMethod->getResultType();
120 SourceRange ResultTypeRange;
121 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
124
125 // Figure out which class this method is part of, if any.
126 ObjCInterfaceDecl *CurrentClass
127 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
128 if (!CurrentClass) {
129 DeclContext *DC = NewMethod->getDeclContext();
130 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
131 CurrentClass = Cat->getClassInterface();
132 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
133 CurrentClass = Impl->getClassInterface();
134 else if (ObjCCategoryImplDecl *CatImpl
135 = dyn_cast<ObjCCategoryImplDecl>(DC))
136 CurrentClass = CatImpl->getClassInterface();
137 }
138
139 if (CurrentClass) {
140 Diag(NewMethod->getLocation(),
141 diag::warn_related_result_type_compatibility_class)
142 << Context.getObjCInterfaceType(CurrentClass)
143 << ResultType
144 << ResultTypeRange;
145 } else {
146 Diag(NewMethod->getLocation(),
147 diag::warn_related_result_type_compatibility_protocol)
148 << ResultType
149 << ResultTypeRange;
150 }
151
Douglas Gregore97179c2011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
154 diag::note_related_result_type_overridden_family)
155 << Family;
156 else
157 Diag(Overridden->getLocation(),
158 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000159 }
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000160 if (getLangOptions().ObjCAutoRefCount) {
161 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
162 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
163 Diag(NewMethod->getLocation(),
164 diag::err_nsreturns_retained_attribute_mismatch) << 1;
165 Diag(Overridden->getLocation(), diag::note_previous_decl)
166 << "method";
167 }
168 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
169 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
170 Diag(NewMethod->getLocation(),
171 diag::err_nsreturns_retained_attribute_mismatch) << 0;
172 Diag(Overridden->getLocation(), diag::note_previous_decl)
173 << "method";
174 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000175 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin();
176 for (ObjCMethodDecl::param_iterator
177 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000178 ni != ne; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000179 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000180 ParmVarDecl *newDecl = (*ni);
181 if (newDecl->hasAttr<NSConsumedAttr>() !=
182 oldDecl->hasAttr<NSConsumedAttr>()) {
183 Diag(newDecl->getLocation(),
184 diag::err_nsconsumed_attribute_mismatch);
185 Diag(oldDecl->getLocation(), diag::note_previous_decl)
186 << "parameter";
187 }
188 }
189 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000190}
191
John McCallf85e1932011-06-15 23:02:42 +0000192/// \brief Check a method declaration for compatibility with the Objective-C
193/// ARC conventions.
194static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
195 ObjCMethodFamily family = method->getMethodFamily();
196 switch (family) {
197 case OMF_None:
198 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000199 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000200 case OMF_retain:
201 case OMF_release:
202 case OMF_autorelease:
203 case OMF_retainCount:
204 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000205 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000206 return false;
207
208 case OMF_init:
209 // If the method doesn't obey the init rules, don't bother annotating it.
210 if (S.checkInitMethod(method, QualType()))
211 return true;
212
213 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
214 S.Context));
215
216 // Don't add a second copy of this attribute, but otherwise don't
217 // let it be suppressed.
218 if (method->hasAttr<NSReturnsRetainedAttr>())
219 return false;
220 break;
221
222 case OMF_alloc:
223 case OMF_copy:
224 case OMF_mutableCopy:
225 case OMF_new:
226 if (method->hasAttr<NSReturnsRetainedAttr>() ||
227 method->hasAttr<NSReturnsNotRetainedAttr>() ||
228 method->hasAttr<NSReturnsAutoreleasedAttr>())
229 return false;
230 break;
231 }
232
233 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
234 S.Context));
235 return false;
236}
237
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000238static void DiagnoseObjCImplementedDeprecations(Sema &S,
239 NamedDecl *ND,
240 SourceLocation ImplLoc,
241 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000242 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000243 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000244 if (select == 0)
245 S.Diag(ND->getLocation(), diag::note_method_declared_at);
246 else
247 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
248 }
249}
250
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000251/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
252/// pool.
253void Sema::AddAnyMethodToGlobalPool(Decl *D) {
254 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
255
256 // If we don't have a valid method decl, simply return.
257 if (!MDecl)
258 return;
259 if (MDecl->isInstanceMethod())
260 AddInstanceMethodToGlobalPool(MDecl, true);
261 else
262 AddFactoryMethodToGlobalPool(MDecl, true);
263}
264
Steve Naroffebf64432009-02-28 16:59:13 +0000265/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000266/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000267void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000268 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000269 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Steve Naroff394f3f42008-07-25 17:57:26 +0000271 // If we don't have a valid method decl, simply return.
272 if (!MDecl)
273 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000274
Chris Lattner4d391482007-12-12 07:09:47 +0000275 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000276 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000277 PushFunctionScope();
278
Chris Lattner4d391482007-12-12 07:09:47 +0000279 // Create Decl objects for each parameter, entrring them in the scope for
280 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000281
282 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000283 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Daniel Dunbar451318c2008-08-26 06:07:48 +0000285 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
286 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000287
Chris Lattner8123a952008-04-10 02:22:51 +0000288 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000289 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000290 E = MDecl->param_end(); PI != E; ++PI) {
291 ParmVarDecl *Param = (*PI);
292 if (!Param->isInvalidDecl() &&
293 RequireCompleteType(Param->getLocation(), Param->getType(),
294 diag::err_typecheck_decl_incomplete_type))
295 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000296 if ((*PI)->getIdentifier())
297 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000298 }
John McCallf85e1932011-06-15 23:02:42 +0000299
300 // In ARC, disallow definition of retain/release/autorelease/retainCount
301 if (getLangOptions().ObjCAutoRefCount) {
302 switch (MDecl->getMethodFamily()) {
303 case OMF_retain:
304 case OMF_retainCount:
305 case OMF_release:
306 case OMF_autorelease:
307 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
308 << MDecl->getSelector();
309 break;
310
311 case OMF_None:
312 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000313 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000314 case OMF_alloc:
315 case OMF_init:
316 case OMF_mutableCopy:
317 case OMF_copy:
318 case OMF_new:
319 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000320 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000321 break;
322 }
323 }
324
Nico Weber9a1ecf02011-08-22 17:25:57 +0000325 // Warn on deprecated methods under -Wdeprecated-implementations,
326 // and prepare for warning on missing super calls.
327 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000328 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000329 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000330 DiagnoseObjCImplementedDeprecations(*this,
331 dyn_cast<NamedDecl>(IMD),
332 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000333
Nico Weber80cb6e62011-08-28 22:35:17 +0000334 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000335 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
336 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
337 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000338 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000339 ObjCShouldCallSuperDealloc =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000340 !(Context.getLangOptions().ObjCAutoRefCount ||
341 Context.getLangOptions().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000342 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000343 ObjCShouldCallSuperFinalize =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000344 Context.getLangOptions().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000345 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000346 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000347 }
Chris Lattner4d391482007-12-12 07:09:47 +0000348}
349
John McCalld226f652010-08-21 09:40:31 +0000350Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000351ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
352 IdentifierInfo *ClassName, SourceLocation ClassLoc,
353 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000354 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000355 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000356 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000357 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattner4d391482007-12-12 07:09:47 +0000359 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000360 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000361 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000362
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000363 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000364 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000365 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000368 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
369 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000370 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000371 if (!IDecl->isForwardDecl()) {
372 IDecl->setInvalidDecl();
373 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
374 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000375
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000376 // Create a new one; the other may be in a different DeclContex, (e.g.
377 // this one may be in a LinkageSpecDecl while the other is not) which
378 // will break invariants.
379 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
380 ClassName, ClassLoc);
381 if (AttrList)
382 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
383 PushOnScopeChains(IDecl, TUScope);
384
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000385 } else {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000386 IDecl->setLocation(ClassLoc);
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000387 IDecl->setAtStartLoc(AtInterfaceLoc);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000388
389 // Since this ObjCInterfaceDecl was created by a forward declaration,
390 // we now add it to the DeclContext since it wasn't added before
391 // (see ActOnForwardClassDeclaration).
392 IDecl->setLexicalDeclContext(CurContext);
393 CurContext->addDecl(IDecl);
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000394
395 IDecl->completedForwardDecl();
396
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000397 if (AttrList)
398 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000399 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000400 } else {
401 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
402 ClassName, ClassLoc);
403 if (AttrList)
404 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
405
406 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattner4d391482007-12-12 07:09:47 +0000409 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000410 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000411 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
412 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000413
414 if (!PrevDecl) {
415 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000416 TypoCorrection Corrected = CorrectTypo(
417 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
418 NULL, NULL, false, CTC_NoKeywords);
419 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000420 Diag(SuperLoc, diag::err_undef_superclass_suggest)
421 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000422 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
423 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000424 }
425 }
426
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000427 if (PrevDecl == IDecl) {
428 Diag(SuperLoc, diag::err_recursive_superclass)
429 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
430 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000431 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000432 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000433 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000434
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000435 // Diagnose classes that inherit from deprecated classes.
436 if (SuperClassDecl)
437 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000439 if (PrevDecl && SuperClassDecl == 0) {
440 // The previous declaration was not a class decl. Check if we have a
441 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000442 if (const TypedefNameDecl *TDecl =
443 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000444 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000445 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000446 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
447 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000448 }
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000451 // This handles the following case:
452 //
453 // typedef int SuperClass;
454 // @interface MyClass : SuperClass {} @end
455 //
456 if (!SuperClassDecl) {
457 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
458 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000459 }
460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Richard Smith162e1c12011-04-15 14:24:37 +0000462 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000463 if (!SuperClassDecl)
464 Diag(SuperLoc, diag::err_undef_superclass)
465 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000466 else if (RequireCompleteType(SuperLoc,
467 Context.getObjCInterfaceType(SuperClassDecl),
468 PDiag(diag::err_forward_superclass)
469 << SuperClassDecl->getDeclName()
470 << ClassName
471 << SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000472 SuperClassDecl = 0;
473 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000474 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000475 IDecl->setSuperClass(SuperClassDecl);
476 IDecl->setSuperClassLoc(SuperLoc);
477 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000478 }
Chris Lattner4d391482007-12-12 07:09:47 +0000479 } else { // we have a root class.
480 IDecl->setLocEnd(ClassLoc);
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Sebastian Redl0b17c612010-08-13 00:28:03 +0000483 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000484 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000485 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000486 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000487 IDecl->setLocEnd(EndProtoLoc);
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Anders Carlsson15281452008-11-04 16:57:32 +0000490 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000491 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000492}
493
494/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000495/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000496Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
497 IdentifierInfo *AliasName,
498 SourceLocation AliasLocation,
499 IdentifierInfo *ClassName,
500 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000501 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000502 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000503 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000504 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000505 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000506 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000507 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000508 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000509 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000510 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000511 }
512 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000513 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000514 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000515 if (const TypedefNameDecl *TDecl =
516 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000517 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000518 if (T->isObjCObjectType()) {
519 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000520 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000521 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000522 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000523 }
524 }
525 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000526 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
527 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000528 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000529 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000530 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000531 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000534 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000535 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000536 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Anders Carlsson15281452008-11-04 16:57:32 +0000538 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000539 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000540
John McCalld226f652010-08-21 09:40:31 +0000541 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000542}
543
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000544bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000545 IdentifierInfo *PName,
546 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000547 const ObjCList<ObjCProtocolDecl> &PList) {
548
549 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000550 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
551 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000552 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
553 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000554 if (PDecl->getIdentifier() == PName) {
555 Diag(Ploc, diag::err_protocol_has_circular_dependency);
556 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000557 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000558 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000559 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
560 PDecl->getLocation(), PDecl->getReferencedProtocols()))
561 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000562 }
563 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000564 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000565}
566
John McCalld226f652010-08-21 09:40:31 +0000567Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000568Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
569 IdentifierInfo *ProtocolName,
570 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000571 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000572 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000573 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000574 SourceLocation EndProtoLoc,
575 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000576 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000577 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000578 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000579 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000580 if (PDecl) {
581 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000582 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000583 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000584 Diag(PDecl->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000586 // Create a new one; the other may be in a different DeclContex, (e.g.
587 // this one may be in a LinkageSpecDecl while the other is not) which
588 // will break invariants.
589 // We will not add it to scope chains to ignore it as the warning says.
590 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
591 ProtocolLoc, AtProtoInterfaceLoc,
592 /*isForwardDecl=*/false);
593
594 } else {
595 ObjCList<ObjCProtocolDecl> PList;
596 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
597 err = CheckForwardProtocolDeclarationForCircularDependency(
598 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
599
600 // Make sure the cached decl gets a valid start location.
601 PDecl->setAtStartLoc(AtProtoInterfaceLoc);
602 PDecl->setLocation(ProtocolLoc);
603 // Since this ObjCProtocolDecl was created by a forward declaration,
604 // we now add it to the DeclContext since it wasn't added before
605 PDecl->setLexicalDeclContext(CurContext);
606 CurContext->addDecl(PDecl);
607 PDecl->completedForwardDecl();
608 }
Chris Lattner439e71f2008-03-16 01:25:17 +0000609 } else {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000610 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000611 ProtocolLoc, AtProtoInterfaceLoc,
612 /*isForwardDecl=*/false);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000613 PushOnScopeChains(PDecl, TUScope);
Chris Lattnercca59d72008-03-16 01:23:04 +0000614 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000615 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000616 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000617 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000618 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000619 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
620 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000621 PDecl->setLocEnd(EndProtoLoc);
622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
624 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000625 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000626}
627
628/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000629/// issues an error if they are not declared. It returns list of
630/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000631void
Chris Lattnere13b9592008-07-26 04:03:38 +0000632Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000633 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000634 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000635 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000636 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000637 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
638 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000639 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000640 TypoCorrection Corrected = CorrectTypo(
641 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
642 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
643 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000644 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000645 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000646 Diag(PDecl->getLocation(), diag::note_previous_decl)
647 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000648 }
649 }
650
651 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000652 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000653 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000654 continue;
655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000657 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000658
659 // If this is a forward declaration and we are supposed to warn in this
660 // case, do it.
661 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000662 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000663 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000664 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000665 }
666}
667
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000668/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000669/// a class method in its extension.
670///
Mike Stump1eb44332009-09-09 15:08:12 +0000671void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000672 ObjCInterfaceDecl *ID) {
673 if (!ID)
674 return; // Possibly due to previous error
675
676 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000677 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
678 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000679 ObjCMethodDecl *MD = *i;
680 MethodMap[MD->getSelector()] = MD;
681 }
682
683 if (MethodMap.empty())
684 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000685 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
686 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000687 ObjCMethodDecl *Method = *i;
688 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
689 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
690 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
691 << Method->getDeclName();
692 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
693 }
694 }
695}
696
Chris Lattner58fe03b2009-04-12 08:43:13 +0000697/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000698Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000699Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000700 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000701 unsigned NumElts,
702 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000703 SmallVector<ObjCProtocolDecl*, 32> Protocols;
704 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Chris Lattner4d391482007-12-12 07:09:47 +0000706 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000707 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000708 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000709 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000710 if (PDecl == 0) { // Not already seen?
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000711 PDecl = ObjCProtocolDecl::Create(Context, CurContext, Ident,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000712 IdentList[i].second, AtProtocolLoc,
713 /*isForwardDecl=*/true);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000714 PushOnScopeChains(PDecl, TUScope, false);
715 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000716 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000717 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000718 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +0000719 if (!isNew) {
720 if (ASTMutationListener *L = Context.getASTMutationListener())
721 L->UpdatedAttributeList(PDecl);
722 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000723 }
Chris Lattner4d391482007-12-12 07:09:47 +0000724 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000725 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000726 }
Mike Stump1eb44332009-09-09 15:08:12 +0000727
728 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000729 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000730 Protocols.data(), Protocols.size(),
731 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000732 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000733 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000734 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000735}
736
John McCalld226f652010-08-21 09:40:31 +0000737Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000738ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
739 IdentifierInfo *ClassName, SourceLocation ClassLoc,
740 IdentifierInfo *CategoryName,
741 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000742 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000743 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000744 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000745 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000746 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000747 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000748
749 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000750
751 if (!IDecl
752 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
753 PDiag(diag::err_category_forward_interface)
754 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000755 // Create an invalid ObjCCategoryDecl to serve as context for
756 // the enclosing method declarations. We mark the decl invalid
757 // to make it clear that this isn't a valid AST.
758 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000759 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000760 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000761
762 if (!IDecl)
763 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000764 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000765 }
766
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000767 if (!CategoryName && IDecl->getImplementation()) {
768 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
769 Diag(IDecl->getImplementation()->getLocation(),
770 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000771 }
772
Fariborz Jahanian25760612010-02-15 21:55:26 +0000773 if (CategoryName) {
774 /// Check for duplicate interface declaration for this category
775 ObjCCategoryDecl *CDeclChain;
776 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
777 CDeclChain = CDeclChain->getNextClassCategory()) {
778 if (CDeclChain->getIdentifier() == CategoryName) {
779 // Class extensions can be declared multiple times.
780 Diag(CategoryLoc, diag::warn_dup_category_def)
781 << ClassName << CategoryName;
782 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
783 break;
784 }
Chris Lattner70f19542009-02-16 21:26:43 +0000785 }
786 }
Chris Lattner70f19542009-02-16 21:26:43 +0000787
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000788 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
789 ClassLoc, CategoryLoc, CategoryName, IDecl);
790 // FIXME: PushOnScopeChains?
791 CurContext->addDecl(CDecl);
792
Chris Lattner4d391482007-12-12 07:09:47 +0000793 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000794 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000795 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000796 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000797 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000798 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000799 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000800 }
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Anders Carlsson15281452008-11-04 16:57:32 +0000802 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000803 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000804}
805
806/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000808/// object.
John McCalld226f652010-08-21 09:40:31 +0000809Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000810 SourceLocation AtCatImplLoc,
811 IdentifierInfo *ClassName, SourceLocation ClassLoc,
812 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000813 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000814 ObjCCategoryDecl *CatIDecl = 0;
815 if (IDecl) {
816 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
817 if (!CatIDecl) {
818 // Category @implementation with no corresponding @interface.
819 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000820 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
821 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000822 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000823 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000824 }
825 }
826
Mike Stump1eb44332009-09-09 15:08:12 +0000827 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000828 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
829 ClassLoc, AtCatImplLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000830 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000831 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000832 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000833 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000834 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
835 diag::err_undef_interface)) {
836 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000837 }
Chris Lattner4d391482007-12-12 07:09:47 +0000838
Douglas Gregord0434102009-01-09 00:49:46 +0000839 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000840 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000841
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000842 // If the interface is deprecated/unavailable, warn/error about it.
843 if (IDecl)
844 DiagnoseUseOfDecl(IDecl, ClassLoc);
845
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000846 /// Check that CatName, category name, is not used in another implementation.
847 if (CatIDecl) {
848 if (CatIDecl->getImplementation()) {
849 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
850 << CatName;
851 Diag(CatIDecl->getImplementation()->getLocation(),
852 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000853 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000854 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000855 // Warn on implementating category of deprecated class under
856 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000857 DiagnoseObjCImplementedDeprecations(*this,
858 dyn_cast<NamedDecl>(IDecl),
859 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000860 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Anders Carlsson15281452008-11-04 16:57:32 +0000863 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000864 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000865}
866
John McCalld226f652010-08-21 09:40:31 +0000867Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000868 SourceLocation AtClassImplLoc,
869 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000870 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000871 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000872 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000873 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000874 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000875 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
876 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000877 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000878 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000879 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000880 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregorb3029962011-11-14 22:10:01 +0000881 if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
882 diag::warn_undef_interface))
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000883 IDecl = 0;
Douglas Gregor95ff7422010-01-04 17:27:12 +0000884 } else {
885 // We did not find anything with the name ClassName; try to correct for
886 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000887 TypoCorrection Corrected = CorrectTypo(
888 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
889 NULL, NULL, false, CTC_NoKeywords);
890 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000891 // Suggest the (potentially) correct interface name. However, put the
892 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000893 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000894 // provide a code-modification hint or use the typo name for recovery,
895 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000896 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000897 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000898 << ClassName << CorrectedName;
899 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
900 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000901 IDecl = 0;
902 } else {
903 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
904 }
Chris Lattner4d391482007-12-12 07:09:47 +0000905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Chris Lattner4d391482007-12-12 07:09:47 +0000907 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000908 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000909 if (SuperClassname) {
910 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000911 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
912 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000913 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000914 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
915 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000916 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000917 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000918 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000919 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000920 Diag(SuperClassLoc, diag::err_undef_superclass)
921 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000922 else if (IDecl && IDecl->getSuperClass() != SDecl) {
923 // This implementation and its interface do not have the same
924 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000925 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000926 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000927 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000928 }
929 }
930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattner4d391482007-12-12 07:09:47 +0000932 if (!IDecl) {
933 // Legacy case of @implementation with no corresponding @interface.
934 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000935
Mike Stump390b4cc2009-05-16 07:39:55 +0000936 // FIXME: Do we support attributes on the @implementation? If so we should
937 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000938 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000939 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000940 IDecl->setSuperClass(SDecl);
941 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000942
943 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000944 } else {
945 // Mark the interface as being completed, even if it was just as
946 // @class ....;
947 // declaration; the user cannot reopen it.
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000948 if (IDecl->isForwardDecl())
949 IDecl->completedForwardDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
952 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000953 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
954 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Anders Carlsson15281452008-11-04 16:57:32 +0000956 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000957 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Chris Lattner4d391482007-12-12 07:09:47 +0000959 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000960 if (IDecl->getImplementation()) {
961 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000962 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000963 Diag(IDecl->getImplementation()->getLocation(),
964 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000965 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000966 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000967 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000968 // Warn on implementating deprecated class under
969 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000970 DiagnoseObjCImplementedDeprecations(*this,
971 dyn_cast<NamedDecl>(IDecl),
972 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000973 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000974 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000975}
976
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000977void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
978 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000979 SourceLocation RBrace) {
980 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000981 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000982 if (!IDecl)
983 return;
984 /// Check case of non-existing @interface decl.
985 /// (legacy objective-c @implementation decl without an @interface decl).
986 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000987 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000988 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000989 // Add ivar's to class's DeclContext.
990 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000991 ivars[i]->setLexicalDeclContext(ImpDecl);
992 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000993 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000994 }
995
Chris Lattner4d391482007-12-12 07:09:47 +0000996 return;
997 }
998 // If implementation has empty ivar list, just return.
999 if (numIvars == 0)
1000 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Chris Lattner4d391482007-12-12 07:09:47 +00001002 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001003 if (LangOpts.ObjCNonFragileABI2) {
1004 if (ImpDecl->getSuperClass())
1005 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1006 for (unsigned i = 0; i < numIvars; i++) {
1007 ObjCIvarDecl* ImplIvar = ivars[i];
1008 if (const ObjCIvarDecl *ClsIvar =
1009 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1010 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1011 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1012 continue;
1013 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001014 // Instance ivar to Implementation's DeclContext.
1015 ImplIvar->setLexicalDeclContext(ImpDecl);
1016 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1017 ImpDecl->addDecl(ImplIvar);
1018 }
1019 return;
1020 }
Chris Lattner4d391482007-12-12 07:09:47 +00001021 // Check interface's Ivar list against those in the implementation.
1022 // names and types must match.
1023 //
Chris Lattner4d391482007-12-12 07:09:47 +00001024 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001025 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001026 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1027 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028 ObjCIvarDecl* ImplIvar = ivars[j++];
1029 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001030 assert (ImplIvar && "missing implementation ivar");
1031 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Steve Naroffca331292009-03-03 14:49:36 +00001033 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001034 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001035 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001036 << ImplIvar->getIdentifier()
1037 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001038 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001039 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1040 ImplIvar->getBitWidthValue(Context) !=
1041 ClsIvar->getBitWidthValue(Context)) {
1042 Diag(ImplIvar->getBitWidth()->getLocStart(),
1043 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1044 Diag(ClsIvar->getBitWidth()->getLocStart(),
1045 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001046 }
Steve Naroffca331292009-03-03 14:49:36 +00001047 // Make sure the names are identical.
1048 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001049 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001050 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001051 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001052 }
1053 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Chris Lattner609e4c72007-12-12 18:11:49 +00001056 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001057 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001058 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001059 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001060}
1061
Steve Naroff3c2eb662008-02-10 21:38:56 +00001062void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001063 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001064 // No point warning no definition of method which is 'unavailable'.
1065 if (method->hasAttr<UnavailableAttr>())
1066 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001067 if (!IncompleteImpl) {
1068 Diag(ImpLoc, diag::warn_incomplete_impl);
1069 IncompleteImpl = true;
1070 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001071 if (DiagID == diag::warn_unimplemented_protocol_method)
1072 Diag(ImpLoc, DiagID) << method->getDeclName();
1073 else
1074 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001075}
1076
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001077/// Determines if type B can be substituted for type A. Returns true if we can
1078/// guarantee that anything that the user will do to an object of type A can
1079/// also be done to an object of type B. This is trivially true if the two
1080/// types are the same, or if B is a subclass of A. It becomes more complex
1081/// in cases where protocols are involved.
1082///
1083/// Object types in Objective-C describe the minimum requirements for an
1084/// object, rather than providing a complete description of a type. For
1085/// example, if A is a subclass of B, then B* may refer to an instance of A.
1086/// The principle of substitutability means that we may use an instance of A
1087/// anywhere that we may use an instance of B - it will implement all of the
1088/// ivars of B and all of the methods of B.
1089///
1090/// This substitutability is important when type checking methods, because
1091/// the implementation may have stricter type definitions than the interface.
1092/// The interface specifies minimum requirements, but the implementation may
1093/// have more accurate ones. For example, a method may privately accept
1094/// instances of B, but only publish that it accepts instances of A. Any
1095/// object passed to it will be type checked against B, and so will implicitly
1096/// by a valid A*. Similarly, a method may return a subclass of the class that
1097/// it is declared as returning.
1098///
1099/// This is most important when considering subclassing. A method in a
1100/// subclass must accept any object as an argument that its superclass's
1101/// implementation accepts. It may, however, accept a more general type
1102/// without breaking substitutability (i.e. you can still use the subclass
1103/// anywhere that you can use the superclass, but not vice versa). The
1104/// converse requirement applies to return types: the return type for a
1105/// subclass method must be a valid object of the kind that the superclass
1106/// advertises, but it may be specified more accurately. This avoids the need
1107/// for explicit down-casting by callers.
1108///
1109/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001110static bool isObjCTypeSubstitutable(ASTContext &Context,
1111 const ObjCObjectPointerType *A,
1112 const ObjCObjectPointerType *B,
1113 bool rejectId) {
1114 // Reject a protocol-unqualified id.
1115 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001116
1117 // If B is a qualified id, then A must also be a qualified id and it must
1118 // implement all of the protocols in B. It may not be a qualified class.
1119 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1120 // stricter definition so it is not substitutable for id<A>.
1121 if (B->isObjCQualifiedIdType()) {
1122 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001123 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1124 QualType(B,0),
1125 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001126 }
1127
1128 /*
1129 // id is a special type that bypasses type checking completely. We want a
1130 // warning when it is used in one place but not another.
1131 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1132
1133
1134 // If B is a qualified id, then A must also be a qualified id (which it isn't
1135 // if we've got this far)
1136 if (B->isObjCQualifiedIdType()) return false;
1137 */
1138
1139 // Now we know that A and B are (potentially-qualified) class types. The
1140 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001141 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001142}
1143
John McCall10302c02010-10-28 02:34:38 +00001144static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1145 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1146}
1147
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001148static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001149 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001150 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001151 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001152 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001153 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001154 if (IsProtocolMethodDecl &&
1155 (MethodDecl->getObjCDeclQualifier() !=
1156 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001157 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001158 S.Diag(MethodImpl->getLocation(),
1159 (IsOverridingMode ?
1160 diag::warn_conflicting_overriding_ret_type_modifiers
1161 : diag::warn_conflicting_ret_type_modifiers))
1162 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001163 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1164 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1165 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1166 }
1167 else
1168 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001169 }
1170
John McCall10302c02010-10-28 02:34:38 +00001171 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001172 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001173 return true;
1174 if (!Warn)
1175 return false;
John McCall10302c02010-10-28 02:34:38 +00001176
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001177 unsigned DiagID =
1178 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1179 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001180
1181 // Mismatches between ObjC pointers go into a different warning
1182 // category, and sometimes they're even completely whitelisted.
1183 if (const ObjCObjectPointerType *ImplPtrTy =
1184 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1185 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001186 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001187 // Allow non-matching return types as long as they don't violate
1188 // the principle of substitutability. Specifically, we permit
1189 // return types that are subclasses of the declared return type,
1190 // or that are more-qualified versions of the declared type.
1191 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001192 return false;
John McCall10302c02010-10-28 02:34:38 +00001193
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001194 DiagID =
1195 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1196 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001197 }
1198 }
1199
1200 S.Diag(MethodImpl->getLocation(), DiagID)
1201 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001202 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001203 << MethodImpl->getResultType()
1204 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001205 S.Diag(MethodDecl->getLocation(),
1206 IsOverridingMode ? diag::note_previous_declaration
1207 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001208 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001209 return false;
John McCall10302c02010-10-28 02:34:38 +00001210}
1211
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001212static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001213 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001214 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001215 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001216 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001217 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001218 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001219 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001220 if (IsProtocolMethodDecl &&
1221 (ImplVar->getObjCDeclQualifier() !=
1222 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001223 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001224 if (IsOverridingMode)
1225 S.Diag(ImplVar->getLocation(),
1226 diag::warn_conflicting_overriding_param_modifiers)
1227 << getTypeRange(ImplVar->getTypeSourceInfo())
1228 << MethodImpl->getDeclName();
1229 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001230 diag::warn_conflicting_param_modifiers)
1231 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001232 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001233 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1234 << getTypeRange(IfaceVar->getTypeSourceInfo());
1235 }
1236 else
1237 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001238 }
1239
John McCall10302c02010-10-28 02:34:38 +00001240 QualType ImplTy = ImplVar->getType();
1241 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001242
John McCall10302c02010-10-28 02:34:38 +00001243 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001244 return true;
1245
1246 if (!Warn)
1247 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001248 unsigned DiagID =
1249 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1250 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001251
1252 // Mismatches between ObjC pointers go into a different warning
1253 // category, and sometimes they're even completely whitelisted.
1254 if (const ObjCObjectPointerType *ImplPtrTy =
1255 ImplTy->getAs<ObjCObjectPointerType>()) {
1256 if (const ObjCObjectPointerType *IfacePtrTy =
1257 IfaceTy->getAs<ObjCObjectPointerType>()) {
1258 // Allow non-matching argument types as long as they don't
1259 // violate the principle of substitutability. Specifically, the
1260 // implementation must accept any objects that the superclass
1261 // accepts, however it may also accept others.
1262 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001263 return false;
John McCall10302c02010-10-28 02:34:38 +00001264
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001265 DiagID =
1266 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1267 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001268 }
1269 }
1270
1271 S.Diag(ImplVar->getLocation(), DiagID)
1272 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001273 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1274 S.Diag(IfaceVar->getLocation(),
1275 (IsOverridingMode ? diag::note_previous_declaration
1276 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001277 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001278 return false;
John McCall10302c02010-10-28 02:34:38 +00001279}
John McCallf85e1932011-06-15 23:02:42 +00001280
1281/// In ARC, check whether the conventional meanings of the two methods
1282/// match. If they don't, it's a hard error.
1283static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1284 ObjCMethodDecl *decl) {
1285 ObjCMethodFamily implFamily = impl->getMethodFamily();
1286 ObjCMethodFamily declFamily = decl->getMethodFamily();
1287 if (implFamily == declFamily) return false;
1288
1289 // Since conventions are sorted by selector, the only possibility is
1290 // that the types differ enough to cause one selector or the other
1291 // to fall out of the family.
1292 assert(implFamily == OMF_None || declFamily == OMF_None);
1293
1294 // No further diagnostics required on invalid declarations.
1295 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1296
1297 const ObjCMethodDecl *unmatched = impl;
1298 ObjCMethodFamily family = declFamily;
1299 unsigned errorID = diag::err_arc_lost_method_convention;
1300 unsigned noteID = diag::note_arc_lost_method_convention;
1301 if (declFamily == OMF_None) {
1302 unmatched = decl;
1303 family = implFamily;
1304 errorID = diag::err_arc_gained_method_convention;
1305 noteID = diag::note_arc_gained_method_convention;
1306 }
1307
1308 // Indexes into a %select clause in the diagnostic.
1309 enum FamilySelector {
1310 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1311 };
1312 FamilySelector familySelector = FamilySelector();
1313
1314 switch (family) {
1315 case OMF_None: llvm_unreachable("logic error, no method convention");
1316 case OMF_retain:
1317 case OMF_release:
1318 case OMF_autorelease:
1319 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001320 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001321 case OMF_retainCount:
1322 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001323 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001324 // Mismatches for these methods don't change ownership
1325 // conventions, so we don't care.
1326 return false;
1327
1328 case OMF_init: familySelector = F_init; break;
1329 case OMF_alloc: familySelector = F_alloc; break;
1330 case OMF_copy: familySelector = F_copy; break;
1331 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1332 case OMF_new: familySelector = F_new; break;
1333 }
1334
1335 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1336 ReasonSelector reasonSelector;
1337
1338 // The only reason these methods don't fall within their families is
1339 // due to unusual result types.
1340 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1341 reasonSelector = R_UnrelatedReturn;
1342 } else {
1343 reasonSelector = R_NonObjectReturn;
1344 }
1345
1346 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1347 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1348
1349 return true;
1350}
John McCall10302c02010-10-28 02:34:38 +00001351
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001352void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001353 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001354 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001355 if (getLangOptions().ObjCAutoRefCount &&
1356 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1357 return;
1358
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001359 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001360 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001361 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Chris Lattner3aff9192009-04-11 19:58:42 +00001363 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001364 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001365 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001366 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001367 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001368 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001369
Fariborz Jahanian21121902011-08-08 18:03:17 +00001370 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001371 Diag(ImpMethodDecl->getLocation(),
1372 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001373 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001374 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001375}
1376
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001377void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1378 ObjCMethodDecl *Overridden,
1379 bool IsProtocolMethodDecl) {
1380
1381 CheckMethodOverrideReturn(*this, Method, Overridden,
1382 IsProtocolMethodDecl, true,
1383 true);
1384
1385 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1386 IF = Overridden->param_begin(), EM = Method->param_end();
1387 IM != EM; ++IM, ++IF) {
1388 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1389 IsProtocolMethodDecl, true, true);
1390 }
1391
1392 if (Method->isVariadic() != Overridden->isVariadic()) {
1393 Diag(Method->getLocation(),
1394 diag::warn_conflicting_overriding_variadic);
1395 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1396 }
1397}
1398
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001399/// WarnExactTypedMethods - This routine issues a warning if method
1400/// implementation declaration matches exactly that of its declaration.
1401void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1402 ObjCMethodDecl *MethodDecl,
1403 bool IsProtocolMethodDecl) {
1404 // don't issue warning when protocol method is optional because primary
1405 // class is not required to implement it and it is safe for protocol
1406 // to implement it.
1407 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1408 return;
1409 // don't issue warning when primary class's method is
1410 // depecated/unavailable.
1411 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1412 MethodDecl->hasAttr<DeprecatedAttr>())
1413 return;
1414
1415 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1416 IsProtocolMethodDecl, false, false);
1417 if (match)
1418 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1419 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1420 IM != EM; ++IM, ++IF) {
1421 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1422 *IM, *IF,
1423 IsProtocolMethodDecl, false, false);
1424 if (!match)
1425 break;
1426 }
1427 if (match)
1428 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001429 if (match)
1430 match = !(MethodDecl->isClassMethod() &&
1431 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001432
1433 if (match) {
1434 Diag(ImpMethodDecl->getLocation(),
1435 diag::warn_category_method_impl_match);
1436 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1437 }
1438}
1439
Mike Stump390b4cc2009-05-16 07:39:55 +00001440/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1441/// improve the efficiency of selector lookups and type checking by associating
1442/// with each protocol / interface / category the flattened instance tables. If
1443/// we used an immutable set to keep the table then it wouldn't add significant
1444/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001445
Steve Naroffefe7f362008-02-08 22:06:17 +00001446/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001447/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001448void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1449 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001450 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001451 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001452 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001453 ObjCContainerDecl *CDecl) {
1454 ObjCInterfaceDecl *IDecl;
1455 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1456 IDecl = C->getClassInterface();
1457 else
1458 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1459 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1460
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001461 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001462 ObjCInterfaceDecl *NSIDecl = 0;
1463 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001464 // check to see if class implements forwardInvocation method and objects
1465 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001466 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001467 // Under such conditions, which means that every method possible is
1468 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001469 // found" warnings.
1470 // FIXME: Use a general GetUnarySelector method for this.
1471 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1472 Selector fISelector = Context.Selectors.getSelector(1, &II);
1473 if (InsMap.count(fISelector))
1474 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1475 // need be implemented in the implementation.
1476 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1477 }
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001479 // If a method lookup fails locally we still need to look and see if
1480 // the method was implemented by a base class or an inherited
1481 // protocol. This lookup is slow, but occurs rarely in correct code
1482 // and otherwise would terminate in a warning.
1483
Chris Lattner4d391482007-12-12 07:09:47 +00001484 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001485 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001486 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001487 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001488 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001489 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001490 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001491 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001492 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001493 // Ugly, but necessary. Method declared in protcol might have
1494 // have been synthesized due to a property declared in the class which
1495 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001496 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001497 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001498 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001499 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001500 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001501 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001502 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001503 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001504 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1505 << PDecl->getDeclName();
1506 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001507 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001508 }
1509 }
Chris Lattner4d391482007-12-12 07:09:47 +00001510 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001511 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001512 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001513 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001515 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1516 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001517 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001518 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001519 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1520 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001521 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001522 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001523 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1524 PDecl->getDeclName();
1525 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001526 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001527 }
Chris Lattner780f3292008-07-21 21:32:27 +00001528 // Check on this protocols's referenced protocols, recursively.
1529 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1530 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001531 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001532}
1533
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001534/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001535/// or protocol against those declared in their implementations.
1536///
1537void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1538 const llvm::DenseSet<Selector> &ClsMap,
1539 llvm::DenseSet<Selector> &InsMapSeen,
1540 llvm::DenseSet<Selector> &ClsMapSeen,
1541 ObjCImplDecl* IMPDecl,
1542 ObjCContainerDecl* CDecl,
1543 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001544 bool ImmediateClass,
1545 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001546 // Check and see if instance methods in class interface have been
1547 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001548 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1549 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001550 if (InsMapSeen.count((*I)->getSelector()))
1551 continue;
1552 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001553 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001554 !InsMap.count((*I)->getSelector())) {
1555 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001556 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1557 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001558 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001559 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001560 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001561 IMPDecl->getInstanceMethod((*I)->getSelector());
1562 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1563 "Expected to find the method through lookup as well");
1564 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001565 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001566 if (ImpMethodDecl) {
1567 if (!WarnExactMatch)
1568 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1569 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001570 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001571 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1572 isa<ObjCProtocolDecl>(CDecl));
1573 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001574 }
1575 }
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001577 // Check and see if class methods in class interface have been
1578 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001579 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001580 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001581 if (ClsMapSeen.count((*I)->getSelector()))
1582 continue;
1583 ClsMapSeen.insert((*I)->getSelector());
1584 if (!ClsMap.count((*I)->getSelector())) {
1585 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001586 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1587 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001588 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001589 ObjCMethodDecl *ImpMethodDecl =
1590 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001591 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1592 "Expected to find the method through lookup as well");
1593 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001594 if (!WarnExactMatch)
1595 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1596 isa<ObjCProtocolDecl>(CDecl));
1597 else
1598 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1599 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001600 }
1601 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001602
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001603 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001604 // Also methods in class extensions need be looked at next.
1605 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1606 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1607 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1608 IMPDecl,
1609 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001610 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001611
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001612 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001613 for (ObjCInterfaceDecl::all_protocol_iterator
1614 PI = I->all_referenced_protocol_begin(),
1615 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001616 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1617 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001618 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001619
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001620 // FIXME. For now, we are not checking for extact match of methods
1621 // in category implementation and its primary class's super class.
1622 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001623 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001624 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001625 I->getSuperClass(), IncompleteImpl, false);
1626 }
1627}
1628
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001629/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1630/// category matches with those implemented in its primary class and
1631/// warns each time an exact match is found.
1632void Sema::CheckCategoryVsClassMethodMatches(
1633 ObjCCategoryImplDecl *CatIMPDecl) {
1634 llvm::DenseSet<Selector> InsMap, ClsMap;
1635
1636 for (ObjCImplementationDecl::instmeth_iterator
1637 I = CatIMPDecl->instmeth_begin(),
1638 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1639 InsMap.insert((*I)->getSelector());
1640
1641 for (ObjCImplementationDecl::classmeth_iterator
1642 I = CatIMPDecl->classmeth_begin(),
1643 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1644 ClsMap.insert((*I)->getSelector());
1645 if (InsMap.empty() && ClsMap.empty())
1646 return;
1647
1648 // Get category's primary class.
1649 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1650 if (!CatDecl)
1651 return;
1652 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1653 if (!IDecl)
1654 return;
1655 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1656 bool IncompleteImpl = false;
1657 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1658 CatIMPDecl, IDecl,
1659 IncompleteImpl, false, true /*WarnExactMatch*/);
1660}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001661
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001662void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001663 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001664 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001665 llvm::DenseSet<Selector> InsMap;
1666 // Check and see if instance methods in class interface have been
1667 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001668 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001669 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001670 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001672 // Check and see if properties declared in the interface have either 1)
1673 // an implementation or 2) there is a @synthesize/@dynamic implementation
1674 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001675 if (isa<ObjCInterfaceDecl>(CDecl) &&
1676 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001677 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001678
Chris Lattner4d391482007-12-12 07:09:47 +00001679 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001680 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001681 I = IMPDecl->classmeth_begin(),
1682 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001683 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001685 // Check for type conflict of methods declared in a class/protocol and
1686 // its implementation; if any.
1687 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001688 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1689 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001690 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001691
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001692 // check all methods implemented in category against those declared
1693 // in its primary class.
1694 if (ObjCCategoryImplDecl *CatDecl =
1695 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1696 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Chris Lattner4d391482007-12-12 07:09:47 +00001698 // Check the protocol list for unimplemented methods in the @implementation
1699 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001700 // Check and see if class methods in class interface have been
1701 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Chris Lattnercddc8882009-03-01 00:56:52 +00001703 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001704 for (ObjCInterfaceDecl::all_protocol_iterator
1705 PI = I->all_referenced_protocol_begin(),
1706 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001707 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001708 InsMap, ClsMap, I);
1709 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001710 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1711 Categories; Categories = Categories->getNextClassExtension())
1712 ImplMethodsVsClassMethods(S, IMPDecl,
1713 const_cast<ObjCCategoryDecl*>(Categories),
1714 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001715 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001716 // For extended class, unimplemented methods in its protocols will
1717 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001718 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001719 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1720 E = C->protocol_end(); PI != E; ++PI)
1721 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001722 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001723 // Report unimplemented properties in the category as well.
1724 // When reporting on missing setter/getters, do not report when
1725 // setter/getter is implemented in category's primary class
1726 // implementation.
1727 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1728 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1729 for (ObjCImplementationDecl::instmeth_iterator
1730 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1731 InsMap.insert((*I)->getSelector());
1732 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001733 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001734 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001735 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001736 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001737}
1738
Mike Stump1eb44332009-09-09 15:08:12 +00001739/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001740Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001741Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001742 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001743 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001744 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001745 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001746 for (unsigned i = 0; i != NumElts; ++i) {
1747 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001748 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001749 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001750 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001751 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001752 // Maybe we will complain about the shadowed template parameter.
1753 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1754 // Just pretend that we didn't see the previous declaration.
1755 PrevDecl = 0;
1756 }
1757
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001758 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001759 // GCC apparently allows the following idiom:
1760 //
1761 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1762 // @class XCElementToggler;
1763 //
Mike Stump1eb44332009-09-09 15:08:12 +00001764 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001765 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001766 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001767 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001768 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001769 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001770 // a forward class declaration matching a typedef name of a class refers
1771 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001772 if (const ObjCObjectType *OI =
1773 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1774 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001775 }
Chris Lattner4d391482007-12-12 07:09:47 +00001776 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001777 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1778 if (!IDecl) { // Not already seen? Make a forward decl.
1779 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1780 IdentList[i], IdentLocs[i], true);
1781
1782 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1783 // the current DeclContext. This prevents clients that walk DeclContext
1784 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1785 // declared later (if at all). We also take care to explicitly make
1786 // sure this declaration is visible for name lookup.
1787 PushOnScopeChains(IDecl, TUScope, false);
1788 CurContext->makeDeclVisibleInContext(IDecl, true);
1789 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001790 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1791 IDecl, IdentLocs[i]);
1792 CurContext->addDecl(CDecl);
1793 CheckObjCDeclScope(CDecl);
1794 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001795 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001796
1797 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001798}
1799
John McCall0f4c4c42011-06-16 01:15:19 +00001800static bool tryMatchRecordTypes(ASTContext &Context,
1801 Sema::MethodMatchStrategy strategy,
1802 const Type *left, const Type *right);
1803
John McCallf85e1932011-06-15 23:02:42 +00001804static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1805 QualType leftQT, QualType rightQT) {
1806 const Type *left =
1807 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1808 const Type *right =
1809 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1810
1811 if (left == right) return true;
1812
1813 // If we're doing a strict match, the types have to match exactly.
1814 if (strategy == Sema::MMS_strict) return false;
1815
1816 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1817
1818 // Otherwise, use this absurdly complicated algorithm to try to
1819 // validate the basic, low-level compatibility of the two types.
1820
1821 // As a minimum, require the sizes and alignments to match.
1822 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1823 return false;
1824
1825 // Consider all the kinds of non-dependent canonical types:
1826 // - functions and arrays aren't possible as return and parameter types
1827
1828 // - vector types of equal size can be arbitrarily mixed
1829 if (isa<VectorType>(left)) return isa<VectorType>(right);
1830 if (isa<VectorType>(right)) return false;
1831
1832 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001833 // - structs, unions, and Objective-C objects must match more-or-less
1834 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001835 // - everything else should be a scalar
1836 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001837 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001838
John McCall1d9b3b22011-09-09 05:25:32 +00001839 // Make scalars agree in kind, except count bools as chars, and group
1840 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001841 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1842 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1843 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1844 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001845 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1846 leftSK = Type::STK_ObjCObjectPointer;
1847 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1848 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001849
1850 // Note that data member pointers and function member pointers don't
1851 // intermix because of the size differences.
1852
1853 return (leftSK == rightSK);
1854}
Chris Lattner4d391482007-12-12 07:09:47 +00001855
John McCall0f4c4c42011-06-16 01:15:19 +00001856static bool tryMatchRecordTypes(ASTContext &Context,
1857 Sema::MethodMatchStrategy strategy,
1858 const Type *lt, const Type *rt) {
1859 assert(lt && rt && lt != rt);
1860
1861 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1862 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1863 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1864
1865 // Require union-hood to match.
1866 if (left->isUnion() != right->isUnion()) return false;
1867
1868 // Require an exact match if either is non-POD.
1869 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1870 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1871 return false;
1872
1873 // Require size and alignment to match.
1874 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1875
1876 // Require fields to match.
1877 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1878 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1879 for (; li != le && ri != re; ++li, ++ri) {
1880 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1881 return false;
1882 }
1883 return (li == le && ri == re);
1884}
1885
Chris Lattner4d391482007-12-12 07:09:47 +00001886/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1887/// returns true, or false, accordingly.
1888/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001889bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1890 const ObjCMethodDecl *right,
1891 MethodMatchStrategy strategy) {
1892 if (!matchTypes(Context, strategy,
1893 left->getResultType(), right->getResultType()))
1894 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
John McCallf85e1932011-06-15 23:02:42 +00001896 if (getLangOptions().ObjCAutoRefCount &&
1897 (left->hasAttr<NSReturnsRetainedAttr>()
1898 != right->hasAttr<NSReturnsRetainedAttr>() ||
1899 left->hasAttr<NSConsumesSelfAttr>()
1900 != right->hasAttr<NSConsumesSelfAttr>()))
1901 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001903 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001904 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001905
John McCallf85e1932011-06-15 23:02:42 +00001906 for (; li != le; ++li, ++ri) {
1907 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001908 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001909
1910 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1911 return false;
1912
1913 if (getLangOptions().ObjCAutoRefCount &&
1914 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1915 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001916 }
1917 return true;
1918}
1919
Sebastian Redldb9d2142010-08-02 23:18:59 +00001920/// \brief Read the contents of the method pool for a given selector from
1921/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001922///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001923/// This routine should only be called once, when the method pool has no entry
1924/// for this selector.
1925Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001926 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001927 assert(MethodPool.find(Sel) == MethodPool.end() &&
1928 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001929
1930 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001931 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Sebastian Redldb9d2142010-08-02 23:18:59 +00001933 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001934}
1935
Sebastian Redldb9d2142010-08-02 23:18:59 +00001936void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1937 bool instance) {
1938 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1939 if (Pos == MethodPool.end()) {
1940 if (ExternalSource)
1941 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001942 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001943 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1944 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001945 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001946 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001947 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001948 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001949 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001950 Entry.Method = Method;
1951 Entry.Next = 0;
1952 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001953 }
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Chris Lattnerb25df352009-03-04 05:16:45 +00001955 // We've seen a method with this name, see if we have already seen this type
1956 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001957 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1958 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1959
1960 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001961 ObjCMethodDecl *PrevObjCMethod = List->Method;
1962 PrevObjCMethod->setDefined(impl);
1963 // If a method is deprecated, push it in the global pool.
1964 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001965 if (Method->isDeprecated()) {
1966 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001967 List->Method = Method;
1968 }
1969 // If new method is unavailable, push it into global pool
1970 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001971 if (Method->isUnavailable()) {
1972 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001973 List->Method = Method;
1974 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001975 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001976 }
John McCallf85e1932011-06-15 23:02:42 +00001977 }
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Chris Lattnerb25df352009-03-04 05:16:45 +00001979 // We have a new signature for an existing method - add it.
1980 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001981 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1982 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001983}
1984
John McCallf85e1932011-06-15 23:02:42 +00001985/// Determines if this is an "acceptable" loose mismatch in the global
1986/// method pool. This exists mostly as a hack to get around certain
1987/// global mismatches which we can't afford to make warnings / errors.
1988/// Really, what we want is a way to take a method out of the global
1989/// method pool.
1990static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1991 ObjCMethodDecl *other) {
1992 if (!chosen->isInstanceMethod())
1993 return false;
1994
1995 Selector sel = chosen->getSelector();
1996 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1997 return false;
1998
1999 // Don't complain about mismatches for -length if the method we
2000 // chose has an integral result type.
2001 return (chosen->getResultType()->isIntegerType());
2002}
2003
Sebastian Redldb9d2142010-08-02 23:18:59 +00002004ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002005 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002006 bool warn, bool instance) {
2007 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2008 if (Pos == MethodPool.end()) {
2009 if (ExternalSource)
2010 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002011 else
2012 return 0;
2013 }
2014
Sebastian Redldb9d2142010-08-02 23:18:59 +00002015 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Sebastian Redldb9d2142010-08-02 23:18:59 +00002017 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002018 bool issueDiagnostic = false, issueError = false;
2019
2020 // We support a warning which complains about *any* difference in
2021 // method signature.
2022 bool strictSelectorMatch =
2023 (receiverIdOrClass && warn &&
2024 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2025 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002026 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002027 if (strictSelectorMatch)
2028 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002029 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2030 MMS_strict)) {
2031 issueDiagnostic = true;
2032 break;
2033 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002034 }
2035
John McCallf85e1932011-06-15 23:02:42 +00002036 // If we didn't see any strict differences, we won't see any loose
2037 // differences. In ARC, however, we also need to check for loose
2038 // mismatches, because most of them are errors.
2039 if (!strictSelectorMatch ||
2040 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002041 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002042 // This checks if the methods differ in type mismatch.
2043 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2044 MMS_loose) &&
2045 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2046 issueDiagnostic = true;
2047 if (getLangOptions().ObjCAutoRefCount)
2048 issueError = true;
2049 break;
2050 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002051 }
2052
John McCallf85e1932011-06-15 23:02:42 +00002053 if (issueDiagnostic) {
2054 if (issueError)
2055 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2056 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002057 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2058 else
2059 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002060
2061 Diag(MethList.Method->getLocStart(),
2062 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002063 << MethList.Method->getSourceRange();
2064 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2065 Diag(Next->Method->getLocStart(), diag::note_also_found)
2066 << Next->Method->getSourceRange();
2067 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002068 }
2069 return MethList.Method;
2070}
2071
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002072ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002073 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2074 if (Pos == MethodPool.end())
2075 return 0;
2076
2077 GlobalMethods &Methods = Pos->second;
2078
2079 if (Methods.first.Method && Methods.first.Method->isDefined())
2080 return Methods.first.Method;
2081 if (Methods.second.Method && Methods.second.Method->isDefined())
2082 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002083 return 0;
2084}
2085
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002086/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2087/// identical selector names in current and its super classes and issues
2088/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002089void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2090 ObjCMethodDecl *Method,
2091 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002092 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2093 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002095 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002096 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002097 SD->lookupMethod(Method->getSelector(), IsInstance);
2098 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002099 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002100 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002101 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002102 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2103 E = Method->param_end();
2104 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2105 for (; ParamI != E; ++ParamI, ++PrevI) {
2106 // Number of parameters are the same and is guaranteed by selector match.
2107 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2108 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2109 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002110 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002111 // respective argument type in the super class method, issue warning;
2112 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002113 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002114 << T1 << T2;
2115 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2116 return;
2117 }
2118 }
2119 ID = SD;
2120 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002121}
2122
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002123/// DiagnoseDuplicateIvars -
2124/// Check for duplicate ivars in the entire class at the start of
2125/// @implementation. This becomes necesssary because class extension can
2126/// add ivars to a class in random order which will not be known until
2127/// class's @implementation is seen.
2128void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2129 ObjCInterfaceDecl *SID) {
2130 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2131 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2132 ObjCIvarDecl* Ivar = (*IVI);
2133 if (Ivar->isInvalidDecl())
2134 continue;
2135 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2136 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2137 if (prevIvar) {
2138 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2139 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2140 Ivar->setInvalidDecl();
2141 }
2142 }
2143 }
2144}
2145
Steve Naroffa56f6162007-12-18 01:30:32 +00002146// Note: For class/category implemenations, allMethods/allProperties is
2147// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002148void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002149 Decl **allMethods, unsigned allNum,
2150 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002151 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002152
2153 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002154 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002155 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2156 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002157
Mike Stump1eb44332009-09-09 15:08:12 +00002158 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002159 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2160 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002161 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002162
Ted Kremenek782f2f52010-01-07 01:20:12 +00002163 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2164 // FIXME: This is wrong. We shouldn't be pretending that there is
2165 // an '@end' in the declaration.
Argyrios Kyrtzidis1104d9b2011-10-27 00:09:29 +00002166 SourceLocation L = OCD->getAtStartLoc();
Ted Kremenek782f2f52010-01-07 01:20:12 +00002167 AtEnd.setBegin(L);
2168 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002169 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002170 }
2171
Steve Naroff0701bbb2009-01-08 17:28:14 +00002172 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2173 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2174 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2175
Chris Lattner4d391482007-12-12 07:09:47 +00002176 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002177 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002178 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002179
2180 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002181 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002182 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002183 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002184 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002185 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002186 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002187 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002188 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002189 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002190 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002191 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002192 } else {
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +00002193 if (PrevMethod)
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002194 Method->setAsRedeclaration(PrevMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002195 InsMap[Method->getSelector()] = Method;
2196 /// The following allows us to typecheck messages to "id".
2197 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002198 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002199 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002200 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002201 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002202 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002203 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002204 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002205 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002206 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002207 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002208 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002209 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002210 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002211 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002212 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002213 } else {
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +00002214 if (PrevMethod)
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002215 Method->setAsRedeclaration(PrevMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002216 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002217 /// The following allows us to typecheck messages to "Class".
2218 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002219 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002220 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002221 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002222 }
2223 }
2224 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002225 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002226 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002227 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002228 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002229 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002230 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002231 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002232 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002233 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002234
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002235 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002236 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002237 if (C->IsClassExtension()) {
2238 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2239 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002240 }
Chris Lattner4d391482007-12-12 07:09:47 +00002241 }
Steve Naroff09c47192009-01-09 15:36:25 +00002242 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002243 if (CDecl->getIdentifier())
2244 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2245 // user-defined setter/getter. It also synthesizes setter/getter methods
2246 // and adds them to the DeclContext and global method pools.
2247 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2248 E = CDecl->prop_end();
2249 I != E; ++I)
2250 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002251 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002252 }
2253 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002254 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002255 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002256 // Any property declared in a class extension might have user
2257 // declared setter or getter in current class extension or one
2258 // of the other class extensions. Mark them as synthesized as
2259 // property will be synthesized when property with same name is
2260 // seen in the @implementation.
2261 for (const ObjCCategoryDecl *ClsExtDecl =
2262 IDecl->getFirstClassExtension();
2263 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2264 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2265 E = ClsExtDecl->prop_end(); I != E; ++I) {
2266 ObjCPropertyDecl *Property = (*I);
2267 // Skip over properties declared @dynamic
2268 if (const ObjCPropertyImplDecl *PIDecl
2269 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2270 if (PIDecl->getPropertyImplementation()
2271 == ObjCPropertyImplDecl::Dynamic)
2272 continue;
2273
2274 for (const ObjCCategoryDecl *CExtDecl =
2275 IDecl->getFirstClassExtension();
2276 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2277 if (ObjCMethodDecl *GetterMethod =
2278 CExtDecl->getInstanceMethod(Property->getGetterName()))
2279 GetterMethod->setSynthesized(true);
2280 if (!Property->isReadOnly())
2281 if (ObjCMethodDecl *SetterMethod =
2282 CExtDecl->getInstanceMethod(Property->getSetterName()))
2283 SetterMethod->setSynthesized(true);
2284 }
2285 }
2286 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002287 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002288 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002289 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002290
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002291 if (LangOpts.ObjCNonFragileABI2)
2292 while (IDecl->getSuperClass()) {
2293 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2294 IDecl = IDecl->getSuperClass();
2295 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002296 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002297 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002298 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002299 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002300 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002301
Chris Lattner4d391482007-12-12 07:09:47 +00002302 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002303 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002304 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002305 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002306 Categories; Categories = Categories->getNextClassCategory()) {
2307 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002308 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002309 break;
2310 }
2311 }
2312 }
2313 }
Chris Lattner682bf922009-03-29 16:50:03 +00002314 if (isInterfaceDeclKind) {
2315 // Reject invalid vardecls.
2316 for (unsigned i = 0; i != tuvNum; i++) {
2317 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2318 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2319 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002320 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002321 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002322 }
Chris Lattner682bf922009-03-29 16:50:03 +00002323 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002324 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002325 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002326
2327 for (unsigned i = 0; i != tuvNum; i++) {
2328 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2329 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2330 }
Chris Lattner4d391482007-12-12 07:09:47 +00002331}
2332
2333
2334/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2335/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002336static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002337CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002338 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002339}
2340
Ted Kremenek422bae72010-04-18 04:59:38 +00002341static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002342bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2343 const AttrVec &A) {
2344 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002345 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002346 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002347 return false;
2348
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002349 // method declared in interface has no attribute.
2350 // But implementation has attributes. This is invalid
2351 if (!IMD->hasAttrs())
2352 return true;
2353
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002354 const AttrVec &D = IMD->getAttrs();
2355 if (D.size() != A.size())
2356 return true;
2357
2358 // attributes on method declaration and definition must match exactly.
2359 // Note that we have at most a couple of attributes on methods, so this
2360 // n*n search is good enough.
2361 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2362 bool match = false;
2363 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2364 if ((*i)->getKind() == (*i1)->getKind()) {
2365 match = true;
2366 break;
2367 }
2368 }
2369 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002370 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002371 }
Sean Huntcf807c42010-08-18 23:23:40 +00002372 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002373}
2374
Douglas Gregore97179c2011-09-08 01:46:34 +00002375namespace {
2376 /// \brief Describes the compatibility of a result type with its method.
2377 enum ResultTypeCompatibilityKind {
2378 RTC_Compatible,
2379 RTC_Incompatible,
2380 RTC_Unknown
2381 };
2382}
2383
Douglas Gregor926df6c2011-06-11 01:09:30 +00002384/// \brief Check whether the declared result type of the given Objective-C
2385/// method declaration is compatible with the method's class.
2386///
Douglas Gregore97179c2011-09-08 01:46:34 +00002387static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002388CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2389 ObjCInterfaceDecl *CurrentClass) {
2390 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002391
2392 // If an Objective-C method inherits its related result type, then its
2393 // declared result type must be compatible with its own class type. The
2394 // declared result type is compatible if:
2395 if (const ObjCObjectPointerType *ResultObjectType
2396 = ResultType->getAs<ObjCObjectPointerType>()) {
2397 // - it is id or qualified id, or
2398 if (ResultObjectType->isObjCIdType() ||
2399 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002400 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002401
2402 if (CurrentClass) {
2403 if (ObjCInterfaceDecl *ResultClass
2404 = ResultObjectType->getInterfaceDecl()) {
2405 // - it is the same as the method's class type, or
2406 if (CurrentClass == ResultClass)
Douglas Gregore97179c2011-09-08 01:46:34 +00002407 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002408
2409 // - it is a superclass of the method's class type
2410 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002411 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002412 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002413 } else {
2414 // Any Objective-C pointer type might be acceptable for a protocol
2415 // method; we just don't know.
2416 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002417 }
2418 }
2419
Douglas Gregore97179c2011-09-08 01:46:34 +00002420 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002421}
2422
John McCall6c2c2502011-07-22 02:45:48 +00002423namespace {
2424/// A helper class for searching for methods which a particular method
2425/// overrides.
2426class OverrideSearch {
2427 Sema &S;
2428 ObjCMethodDecl *Method;
2429 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2430 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2431 bool Recursive;
2432
2433public:
2434 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2435 Selector selector = method->getSelector();
2436
2437 // Bypass this search if we've never seen an instance/class method
2438 // with this selector before.
2439 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2440 if (it == S.MethodPool.end()) {
2441 if (!S.ExternalSource) return;
2442 it = S.ReadMethodPool(selector);
2443 }
2444 ObjCMethodList &list =
2445 method->isInstanceMethod() ? it->second.first : it->second.second;
2446 if (!list.Method) return;
2447
2448 ObjCContainerDecl *container
2449 = cast<ObjCContainerDecl>(method->getDeclContext());
2450
2451 // Prevent the search from reaching this container again. This is
2452 // important with categories, which override methods from the
2453 // interface and each other.
2454 Searched.insert(container);
2455 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002456 }
John McCall6c2c2502011-07-22 02:45:48 +00002457
2458 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2459 iterator begin() const { return Overridden.begin(); }
2460 iterator end() const { return Overridden.end(); }
2461
2462private:
2463 void searchFromContainer(ObjCContainerDecl *container) {
2464 if (container->isInvalidDecl()) return;
2465
2466 switch (container->getDeclKind()) {
2467#define OBJCCONTAINER(type, base) \
2468 case Decl::type: \
2469 searchFrom(cast<type##Decl>(container)); \
2470 break;
2471#define ABSTRACT_DECL(expansion)
2472#define DECL(type, base) \
2473 case Decl::type:
2474#include "clang/AST/DeclNodes.inc"
2475 llvm_unreachable("not an ObjC container!");
2476 }
2477 }
2478
2479 void searchFrom(ObjCProtocolDecl *protocol) {
2480 // A method in a protocol declaration overrides declarations from
2481 // referenced ("parent") protocols.
2482 search(protocol->getReferencedProtocols());
2483 }
2484
2485 void searchFrom(ObjCCategoryDecl *category) {
2486 // A method in a category declaration overrides declarations from
2487 // the main class and from protocols the category references.
2488 search(category->getClassInterface());
2489 search(category->getReferencedProtocols());
2490 }
2491
2492 void searchFrom(ObjCCategoryImplDecl *impl) {
2493 // A method in a category definition that has a category
2494 // declaration overrides declarations from the category
2495 // declaration.
2496 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2497 search(category);
2498
2499 // Otherwise it overrides declarations from the class.
2500 } else {
2501 search(impl->getClassInterface());
2502 }
2503 }
2504
2505 void searchFrom(ObjCInterfaceDecl *iface) {
2506 // A method in a class declaration overrides declarations from
2507
2508 // - categories,
2509 for (ObjCCategoryDecl *category = iface->getCategoryList();
2510 category; category = category->getNextClassCategory())
2511 search(category);
2512
2513 // - the super class, and
2514 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2515 search(super);
2516
2517 // - any referenced protocols.
2518 search(iface->getReferencedProtocols());
2519 }
2520
2521 void searchFrom(ObjCImplementationDecl *impl) {
2522 // A method in a class implementation overrides declarations from
2523 // the class interface.
2524 search(impl->getClassInterface());
2525 }
2526
2527
2528 void search(const ObjCProtocolList &protocols) {
2529 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2530 i != e; ++i)
2531 search(*i);
2532 }
2533
2534 void search(ObjCContainerDecl *container) {
2535 // Abort if we've already searched this container.
2536 if (!Searched.insert(container)) return;
2537
2538 // Check for a method in this container which matches this selector.
2539 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2540 Method->isInstanceMethod());
2541
2542 // If we find one, record it and bail out.
2543 if (meth) {
2544 Overridden.insert(meth);
2545 return;
2546 }
2547
2548 // Otherwise, search for methods that a hypothetical method here
2549 // would have overridden.
2550
2551 // Note that we're now in a recursive case.
2552 Recursive = true;
2553
2554 searchFromContainer(container);
2555 }
2556};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002557}
2558
John McCalld226f652010-08-21 09:40:31 +00002559Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002560 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002561 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002562 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002563 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002564 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002565 Selector Sel,
2566 // optional arguments. The number of types/arguments is obtained
2567 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002568 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002569 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002570 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002571 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002572 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002573 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002574 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002575 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002576 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002577 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2578 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002579 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002580
Douglas Gregore97179c2011-09-08 01:46:34 +00002581 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002582 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002583 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002584 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Steve Naroffccef3712009-02-20 22:59:16 +00002586 // Methods cannot return interface types. All ObjC objects are
2587 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002588 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002589 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2590 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002591 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002592 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002593
2594 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002595 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002596 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002597 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002598 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002599 }
Mike Stump1eb44332009-09-09 15:08:12 +00002600
2601 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002602 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002603 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002604 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002605 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002606 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002607 /*isSynthesized=*/false,
2608 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002609 MethodDeclKind == tok::objc_optional
2610 ? ObjCMethodDecl::Optional
2611 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002612 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002613
Chris Lattner5f9e2722011-07-23 10:55:15 +00002614 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002615
Chris Lattner7db638d2009-04-11 19:42:43 +00002616 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002617 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002618 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Chris Lattnere294d3f2009-04-11 18:57:04 +00002620 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002621 ArgType = Context.getObjCIdType();
2622 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002623 } else {
John McCall58e46772009-10-23 21:48:59 +00002624 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002625 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002626 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002627 }
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002629 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2630 LookupOrdinaryName, ForRedeclaration);
2631 LookupName(R, S);
2632 if (R.isSingleResult()) {
2633 NamedDecl *PrevDecl = R.getFoundDecl();
2634 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002635 Diag(ArgInfo[i].NameLoc,
2636 (MethodDefinition ? diag::warn_method_param_redefinition
2637 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002638 << ArgInfo[i].Name;
2639 Diag(PrevDecl->getLocation(),
2640 diag::note_previous_declaration);
2641 }
2642 }
2643
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002644 SourceLocation StartLoc = DI
2645 ? DI->getTypeLoc().getBeginLoc()
2646 : ArgInfo[i].NameLoc;
2647
John McCall81ef3e62011-04-23 02:46:06 +00002648 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2649 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2650 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002651
John McCall70798862011-05-02 00:30:12 +00002652 Param->setObjCMethodScopeInfo(i);
2653
Chris Lattner0ed844b2008-04-04 06:12:32 +00002654 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002655 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002656
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002657 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002658 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002660 S->AddDecl(Param);
2661 IdResolver.AddDecl(Param);
2662
Chris Lattner0ed844b2008-04-04 06:12:32 +00002663 Params.push_back(Param);
2664 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002665
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002666 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002667 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002668 QualType ArgType = Param->getType();
2669 if (ArgType.isNull())
2670 ArgType = Context.getObjCIdType();
2671 else
2672 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002673 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002674 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002675 Diag(Param->getLocation(),
2676 diag::err_object_cannot_be_passed_returned_by_value)
2677 << 1 << ArgType;
2678 Param->setInvalidDecl();
2679 }
2680 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002681
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002682 Params.push_back(Param);
2683 }
2684
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002685 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002686 ObjCMethod->setObjCDeclQualifier(
2687 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002688
2689 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002690 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002692 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002693 const ObjCMethodDecl *PrevMethod = 0;
2694 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002695 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002696 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2697 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002698 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002699 PrevMethod = ImpDecl->getClassMethod(Sel);
2700 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002701 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002702
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002703 ObjCMethodDecl *IMD = 0;
2704 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2705 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2706 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002707 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002708 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002709 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002710 } else {
2711 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002712 }
John McCall6c2c2502011-07-22 02:45:48 +00002713
Chris Lattner4d391482007-12-12 07:09:47 +00002714 if (PrevMethod) {
2715 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002716 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002717 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002718 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002719 }
John McCall54abf7d2009-11-04 02:18:39 +00002720
Douglas Gregor926df6c2011-06-11 01:09:30 +00002721 // If this Objective-C method does not have a related result type, but we
2722 // are allowed to infer related result types, try to do so based on the
2723 // method family.
2724 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2725 if (!CurrentClass) {
2726 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2727 CurrentClass = Cat->getClassInterface();
2728 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2729 CurrentClass = Impl->getClassInterface();
2730 else if (ObjCCategoryImplDecl *CatImpl
2731 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2732 CurrentClass = CatImpl->getClassInterface();
2733 }
John McCall6c2c2502011-07-22 02:45:48 +00002734
Douglas Gregore97179c2011-09-08 01:46:34 +00002735 ResultTypeCompatibilityKind RTC
2736 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002737
2738 // Search for overridden methods and merge information down from them.
2739 OverrideSearch overrides(*this, ObjCMethod);
2740 for (OverrideSearch::iterator
2741 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2742 ObjCMethodDecl *overridden = *i;
2743
2744 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002745 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002746 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002747
2748 // Then merge the declarations.
2749 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002750
2751 // Check for overriding methods
2752 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002753 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2754 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2755 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002756 }
2757
John McCallf85e1932011-06-15 23:02:42 +00002758 bool ARCError = false;
2759 if (getLangOptions().ObjCAutoRefCount)
2760 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2761
Douglas Gregore97179c2011-09-08 01:46:34 +00002762 // Infer the related result type when possible.
2763 if (!ARCError && RTC == RTC_Compatible &&
2764 !ObjCMethod->hasRelatedResultType() &&
2765 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002766 bool InferRelatedResultType = false;
2767 switch (ObjCMethod->getMethodFamily()) {
2768 case OMF_None:
2769 case OMF_copy:
2770 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002771 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002772 case OMF_mutableCopy:
2773 case OMF_release:
2774 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002775 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002776 break;
2777
2778 case OMF_alloc:
2779 case OMF_new:
2780 InferRelatedResultType = ObjCMethod->isClassMethod();
2781 break;
2782
2783 case OMF_init:
2784 case OMF_autorelease:
2785 case OMF_retain:
2786 case OMF_self:
2787 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2788 break;
2789 }
2790
John McCall6c2c2502011-07-22 02:45:48 +00002791 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002792 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002793 }
2794
John McCalld226f652010-08-21 09:40:31 +00002795 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002796}
2797
Chris Lattnercc98eac2008-12-17 07:13:27 +00002798bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002799 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002800 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002801 // Following is also an error. But it is caused by a missing @end
2802 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002803 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2804 return false;
2805 }
2806
Anders Carlsson15281452008-11-04 16:57:32 +00002807 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2808 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002809
Anders Carlsson15281452008-11-04 16:57:32 +00002810 return true;
2811}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002812
Chris Lattnercc98eac2008-12-17 07:13:27 +00002813/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2814/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002815void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002816 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002817 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002818 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002819 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002820 if (!Class) {
2821 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2822 return;
2823 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002824 if (LangOpts.ObjCNonFragileABI) {
2825 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2826 return;
2827 }
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Chris Lattnercc98eac2008-12-17 07:13:27 +00002829 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002830 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002831 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002832 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002833 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002834 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002835 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002836 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2837 /*FIXME: StartL=*/ID->getLocation(),
2838 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002839 ID->getIdentifier(), ID->getType(),
2840 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002841 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002842 }
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Chris Lattnercc98eac2008-12-17 07:13:27 +00002844 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002845 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002846 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002847 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002848 if (getLangOptions().CPlusPlus)
2849 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002850 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002851 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002852 }
2853}
2854
Douglas Gregor160b5632010-04-26 17:32:49 +00002855/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002856VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2857 SourceLocation StartLoc,
2858 SourceLocation IdLoc,
2859 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002860 bool Invalid) {
2861 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2862 // duration shall not be qualified by an address-space qualifier."
2863 // Since all parameters have automatic store duration, they can not have
2864 // an address space.
2865 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002866 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002867 Invalid = true;
2868 }
2869
2870 // An @catch parameter must be an unqualified object pointer type;
2871 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2872 if (Invalid) {
2873 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002874 } else if (T->isDependentType()) {
2875 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002876 } else if (!T->isObjCObjectPointerType()) {
2877 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002878 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002879 } else if (T->isObjCQualifiedIdType()) {
2880 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002881 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002882 }
2883
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002884 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2885 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002886 New->setExceptionVariable(true);
2887
Douglas Gregor160b5632010-04-26 17:32:49 +00002888 if (Invalid)
2889 New->setInvalidDecl();
2890 return New;
2891}
2892
John McCalld226f652010-08-21 09:40:31 +00002893Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002894 const DeclSpec &DS = D.getDeclSpec();
2895
2896 // We allow the "register" storage class on exception variables because
2897 // GCC did, but we drop it completely. Any other storage class is an error.
2898 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2899 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2900 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2901 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2902 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2903 << DS.getStorageClassSpec();
2904 }
2905 if (D.getDeclSpec().isThreadSpecified())
2906 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2907 D.getMutableDeclSpec().ClearStorageClassSpecs();
2908
2909 DiagnoseFunctionSpecifiers(D);
2910
2911 // Check that there are no default arguments inside the type of this
2912 // exception object (C++ only).
2913 if (getLangOptions().CPlusPlus)
2914 CheckExtraCXXDefaultArguments(D);
2915
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002916 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002917 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002918
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002919 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2920 D.getSourceRange().getBegin(),
2921 D.getIdentifierLoc(),
2922 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002923 D.isInvalidType());
2924
2925 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2926 if (D.getCXXScopeSpec().isSet()) {
2927 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2928 << D.getCXXScopeSpec().getRange();
2929 New->setInvalidDecl();
2930 }
2931
2932 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002933 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002934 if (D.getIdentifier())
2935 IdResolver.AddDecl(New);
2936
2937 ProcessDeclAttributes(S, New, D);
2938
2939 if (New->hasAttr<BlocksAttr>())
2940 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002941 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002942}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002943
2944/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002945/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002946void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002947 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002948 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2949 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002950 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002951 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002952 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002953 }
2954}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002955
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002956void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002957 // Load referenced selectors from the external source.
2958 if (ExternalSource) {
2959 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2960 ExternalSource->ReadReferencedSelectors(Sels);
2961 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2962 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2963 }
2964
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002965 // Warning will be issued only when selector table is
2966 // generated (which means there is at lease one implementation
2967 // in the TU). This is to match gcc's behavior.
2968 if (ReferencedSelectors.empty() ||
2969 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002970 return;
2971 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2972 ReferencedSelectors.begin(),
2973 E = ReferencedSelectors.end(); S != E; ++S) {
2974 Selector Sel = (*S).first;
2975 if (!LookupImplementedMethodInGlobalPool(Sel))
2976 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2977 }
2978 return;
2979}