blob: 6947d7e211aff5ed28210acff03bbee272a4c0f9 [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 Gregor2e5c15b2011-12-15 05:27:12 +0000371 if (ObjCInterfaceDecl *Def = IDecl->getDefinition()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000372 IDecl->setInvalidDecl();
373 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000374 Diag(Def->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
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000395 if (AttrList)
396 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000397 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000398 } else {
399 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
400 ClassName, ClassLoc);
401 if (AttrList)
402 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
403
404 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000407 if (!IDecl->hasDefinition())
408 IDecl->startDefinition();
409
Chris Lattner4d391482007-12-12 07:09:47 +0000410 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000411 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000412 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
413 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000414
415 if (!PrevDecl) {
416 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000417 TypoCorrection Corrected = CorrectTypo(
418 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
419 NULL, NULL, false, CTC_NoKeywords);
420 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregor60ef3082011-12-15 00:29:59 +0000421 if (declaresSameEntity(PrevDecl, IDecl)) {
Douglas Gregora38c4732011-12-01 15:37:53 +0000422 // Don't correct to the class we're defining.
423 PrevDecl = 0;
424 } else {
425 Diag(SuperLoc, diag::err_undef_superclass_suggest)
426 << SuperName << ClassName << PrevDecl->getDeclName();
427 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
428 << PrevDecl->getDeclName();
429 }
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000430 }
431 }
432
Douglas Gregor60ef3082011-12-15 00:29:59 +0000433 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000434 Diag(SuperLoc, diag::err_recursive_superclass)
435 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
436 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000437 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000438 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000439 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000440
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000441 // Diagnose classes that inherit from deprecated classes.
442 if (SuperClassDecl)
443 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000445 if (PrevDecl && SuperClassDecl == 0) {
446 // The previous declaration was not a class decl. Check if we have a
447 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000448 if (const TypedefNameDecl *TDecl =
449 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000450 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000451 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000452 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
453 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000454 }
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000457 // This handles the following case:
458 //
459 // typedef int SuperClass;
460 // @interface MyClass : SuperClass {} @end
461 //
462 if (!SuperClassDecl) {
463 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
464 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000465 }
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Richard Smith162e1c12011-04-15 14:24:37 +0000468 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000469 if (!SuperClassDecl)
470 Diag(SuperLoc, diag::err_undef_superclass)
471 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000472 else if (RequireCompleteType(SuperLoc,
473 Context.getObjCInterfaceType(SuperClassDecl),
474 PDiag(diag::err_forward_superclass)
475 << SuperClassDecl->getDeclName()
476 << ClassName
477 << SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000478 SuperClassDecl = 0;
479 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000480 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000481 IDecl->setSuperClass(SuperClassDecl);
482 IDecl->setSuperClassLoc(SuperLoc);
483 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000484 }
Chris Lattner4d391482007-12-12 07:09:47 +0000485 } else { // we have a root class.
486 IDecl->setLocEnd(ClassLoc);
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Sebastian Redl0b17c612010-08-13 00:28:03 +0000489 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000490 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000491 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000492 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000493 IDecl->setLocEnd(EndProtoLoc);
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Anders Carlsson15281452008-11-04 16:57:32 +0000496 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000497 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000498}
499
500/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000501/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000502Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
503 IdentifierInfo *AliasName,
504 SourceLocation AliasLocation,
505 IdentifierInfo *ClassName,
506 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000507 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000508 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000509 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000510 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000511 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000512 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000513 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000514 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000515 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000516 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000517 }
518 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000519 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000520 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000521 if (const TypedefNameDecl *TDecl =
522 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000523 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000524 if (T->isObjCObjectType()) {
525 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000526 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000527 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000528 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000529 }
530 }
531 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000532 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
533 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000534 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000535 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000536 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000537 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000540 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000541 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000542 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Anders Carlsson15281452008-11-04 16:57:32 +0000544 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000545 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000546
John McCalld226f652010-08-21 09:40:31 +0000547 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000548}
549
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000550bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000551 IdentifierInfo *PName,
552 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000553 const ObjCList<ObjCProtocolDecl> &PList) {
554
555 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000556 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
557 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000558 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
559 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000560 if (PDecl->getIdentifier() == PName) {
561 Diag(Ploc, diag::err_protocol_has_circular_dependency);
562 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000563 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000564 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000565 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
566 PDecl->getLocation(), PDecl->getReferencedProtocols()))
567 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000568 }
569 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000570 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000571}
572
John McCalld226f652010-08-21 09:40:31 +0000573Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000574Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
575 IdentifierInfo *ProtocolName,
576 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000577 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000578 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000579 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000580 SourceLocation EndProtoLoc,
581 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000582 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000583 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000584 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000585 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000586 if (PDecl) {
587 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000588 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000589 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000590 Diag(PDecl->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000592 // Create a new one; the other may be in a different DeclContex, (e.g.
593 // this one may be in a LinkageSpecDecl while the other is not) which
594 // will break invariants.
595 // We will not add it to scope chains to ignore it as the warning says.
596 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
597 ProtocolLoc, AtProtoInterfaceLoc,
598 /*isForwardDecl=*/false);
599
600 } else {
601 ObjCList<ObjCProtocolDecl> PList;
602 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
603 err = CheckForwardProtocolDeclarationForCircularDependency(
604 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
605
606 // Make sure the cached decl gets a valid start location.
607 PDecl->setAtStartLoc(AtProtoInterfaceLoc);
608 PDecl->setLocation(ProtocolLoc);
609 // Since this ObjCProtocolDecl was created by a forward declaration,
610 // we now add it to the DeclContext since it wasn't added before
611 PDecl->setLexicalDeclContext(CurContext);
612 CurContext->addDecl(PDecl);
613 PDecl->completedForwardDecl();
614 }
Chris Lattner439e71f2008-03-16 01:25:17 +0000615 } else {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000616 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000617 ProtocolLoc, AtProtoInterfaceLoc,
618 /*isForwardDecl=*/false);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000619 PushOnScopeChains(PDecl, TUScope);
Chris Lattnercca59d72008-03-16 01:23:04 +0000620 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000621 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000622 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000623 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000624 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000625 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
626 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000627 PDecl->setLocEnd(EndProtoLoc);
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
630 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000631 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000632}
633
634/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000635/// issues an error if they are not declared. It returns list of
636/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000637void
Chris Lattnere13b9592008-07-26 04:03:38 +0000638Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000639 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000640 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000641 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000642 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000643 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
644 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000645 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000646 TypoCorrection Corrected = CorrectTypo(
647 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
648 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
649 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000650 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000651 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000652 Diag(PDecl->getLocation(), diag::note_previous_decl)
653 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000654 }
655 }
656
657 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000658 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000659 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000660 continue;
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000663 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000664
665 // If this is a forward declaration and we are supposed to warn in this
666 // case, do it.
667 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000668 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000669 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000670 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000671 }
672}
673
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000674/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000675/// a class method in its extension.
676///
Mike Stump1eb44332009-09-09 15:08:12 +0000677void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000678 ObjCInterfaceDecl *ID) {
679 if (!ID)
680 return; // Possibly due to previous error
681
682 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000683 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
684 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000685 ObjCMethodDecl *MD = *i;
686 MethodMap[MD->getSelector()] = MD;
687 }
688
689 if (MethodMap.empty())
690 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000691 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
692 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000693 ObjCMethodDecl *Method = *i;
694 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
695 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
696 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
697 << Method->getDeclName();
698 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
699 }
700 }
701}
702
Chris Lattner58fe03b2009-04-12 08:43:13 +0000703/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000704Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000705Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000706 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000707 unsigned NumElts,
708 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000709 SmallVector<ObjCProtocolDecl*, 32> Protocols;
710 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Chris Lattner4d391482007-12-12 07:09:47 +0000712 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000713 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000714 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000715 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000716 if (PDecl == 0) { // Not already seen?
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000717 PDecl = ObjCProtocolDecl::Create(Context, CurContext, Ident,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000718 IdentList[i].second, AtProtocolLoc,
719 /*isForwardDecl=*/true);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000720 PushOnScopeChains(PDecl, TUScope, false);
721 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000722 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000723 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000724 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +0000725 if (!isNew) {
726 if (ASTMutationListener *L = Context.getASTMutationListener())
727 L->UpdatedAttributeList(PDecl);
728 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000729 }
Chris Lattner4d391482007-12-12 07:09:47 +0000730 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000731 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
734 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000735 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000736 Protocols.data(), Protocols.size(),
737 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000738 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000739 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000740 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000741}
742
John McCalld226f652010-08-21 09:40:31 +0000743Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000744ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
745 IdentifierInfo *ClassName, SourceLocation ClassLoc,
746 IdentifierInfo *CategoryName,
747 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000748 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000749 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000750 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000751 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000752 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000753 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000754
755 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000756
757 if (!IDecl
758 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
759 PDiag(diag::err_category_forward_interface)
760 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000761 // Create an invalid ObjCCategoryDecl to serve as context for
762 // the enclosing method declarations. We mark the decl invalid
763 // to make it clear that this isn't a valid AST.
764 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000765 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000766 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000767
768 if (!IDecl)
769 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000770 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000771 }
772
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000773 if (!CategoryName && IDecl->getImplementation()) {
774 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
775 Diag(IDecl->getImplementation()->getLocation(),
776 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000777 }
778
Fariborz Jahanian25760612010-02-15 21:55:26 +0000779 if (CategoryName) {
780 /// Check for duplicate interface declaration for this category
781 ObjCCategoryDecl *CDeclChain;
782 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
783 CDeclChain = CDeclChain->getNextClassCategory()) {
784 if (CDeclChain->getIdentifier() == CategoryName) {
785 // Class extensions can be declared multiple times.
786 Diag(CategoryLoc, diag::warn_dup_category_def)
787 << ClassName << CategoryName;
788 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
789 break;
790 }
Chris Lattner70f19542009-02-16 21:26:43 +0000791 }
792 }
Chris Lattner70f19542009-02-16 21:26:43 +0000793
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000794 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
795 ClassLoc, CategoryLoc, CategoryName, IDecl);
796 // FIXME: PushOnScopeChains?
797 CurContext->addDecl(CDecl);
798
Chris Lattner4d391482007-12-12 07:09:47 +0000799 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000800 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000801 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000802 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000803 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000804 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000805 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Anders Carlsson15281452008-11-04 16:57:32 +0000808 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000809 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000810}
811
812/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000813/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000814/// object.
John McCalld226f652010-08-21 09:40:31 +0000815Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000816 SourceLocation AtCatImplLoc,
817 IdentifierInfo *ClassName, SourceLocation ClassLoc,
818 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000819 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000820 ObjCCategoryDecl *CatIDecl = 0;
821 if (IDecl) {
822 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
823 if (!CatIDecl) {
824 // Category @implementation with no corresponding @interface.
825 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000826 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
827 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000828 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000829 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000830 }
831 }
832
Mike Stump1eb44332009-09-09 15:08:12 +0000833 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000834 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000835 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000836 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000837 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000838 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000839 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000840 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
841 diag::err_undef_interface)) {
842 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000843 }
Chris Lattner4d391482007-12-12 07:09:47 +0000844
Douglas Gregord0434102009-01-09 00:49:46 +0000845 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000846 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000847
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000848 // If the interface is deprecated/unavailable, warn/error about it.
849 if (IDecl)
850 DiagnoseUseOfDecl(IDecl, ClassLoc);
851
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000852 /// Check that CatName, category name, is not used in another implementation.
853 if (CatIDecl) {
854 if (CatIDecl->getImplementation()) {
855 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
856 << CatName;
857 Diag(CatIDecl->getImplementation()->getLocation(),
858 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000859 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000860 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000861 // Warn on implementating category of deprecated class under
862 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000863 DiagnoseObjCImplementedDeprecations(*this,
864 dyn_cast<NamedDecl>(IDecl),
865 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000866 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000867 }
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Anders Carlsson15281452008-11-04 16:57:32 +0000869 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000870 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000871}
872
John McCalld226f652010-08-21 09:40:31 +0000873Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000874 SourceLocation AtClassImplLoc,
875 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000876 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000877 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000878 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000879 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000880 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000881 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
882 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000883 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000884 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000885 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000886 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregorb3029962011-11-14 22:10:01 +0000887 if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
888 diag::warn_undef_interface))
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000889 IDecl = 0;
Douglas Gregor95ff7422010-01-04 17:27:12 +0000890 } else {
891 // We did not find anything with the name ClassName; try to correct for
892 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000893 TypoCorrection Corrected = CorrectTypo(
894 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
895 NULL, NULL, false, CTC_NoKeywords);
896 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000897 // Suggest the (potentially) correct interface name. However, put the
898 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000899 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000900 // provide a code-modification hint or use the typo name for recovery,
901 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000902 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000903 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000904 << ClassName << CorrectedName;
905 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
906 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000907 IDecl = 0;
908 } else {
909 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
910 }
Chris Lattner4d391482007-12-12 07:09:47 +0000911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattner4d391482007-12-12 07:09:47 +0000913 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000914 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000915 if (SuperClassname) {
916 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000917 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
918 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000919 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000920 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
921 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000922 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000923 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000924 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000925 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000926 Diag(SuperClassLoc, diag::err_undef_superclass)
927 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000928 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000929 // This implementation and its interface do not have the same
930 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000931 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000932 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000933 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000934 }
935 }
936 }
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Chris Lattner4d391482007-12-12 07:09:47 +0000938 if (!IDecl) {
939 // Legacy case of @implementation with no corresponding @interface.
940 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000941
Mike Stump390b4cc2009-05-16 07:39:55 +0000942 // FIXME: Do we support attributes on the @implementation? If so we should
943 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000944 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000945 ClassName, ClassLoc, false, true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000946 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000947 IDecl->setSuperClass(SDecl);
948 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000949
950 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000951 } else {
952 // Mark the interface as being completed, even if it was just as
953 // @class ....;
954 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000955 if (!IDecl->hasDefinition())
956 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
959 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000960 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
961 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Anders Carlsson15281452008-11-04 16:57:32 +0000963 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000964 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Chris Lattner4d391482007-12-12 07:09:47 +0000966 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000967 if (IDecl->getImplementation()) {
968 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000969 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000970 Diag(IDecl->getImplementation()->getLocation(),
971 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000972 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000973 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000974 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000975 // Warn on implementating deprecated class under
976 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000977 DiagnoseObjCImplementedDeprecations(*this,
978 dyn_cast<NamedDecl>(IDecl),
979 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000980 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000981 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000982}
983
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000984void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
985 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000986 SourceLocation RBrace) {
987 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000988 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000989 if (!IDecl)
990 return;
991 /// Check case of non-existing @interface decl.
992 /// (legacy objective-c @implementation decl without an @interface decl).
993 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000994 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000995 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000996 // Add ivar's to class's DeclContext.
997 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000998 ivars[i]->setLexicalDeclContext(ImpDecl);
999 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001000 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001001 }
1002
Chris Lattner4d391482007-12-12 07:09:47 +00001003 return;
1004 }
1005 // If implementation has empty ivar list, just return.
1006 if (numIvars == 0)
1007 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattner4d391482007-12-12 07:09:47 +00001009 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001010 if (LangOpts.ObjCNonFragileABI2) {
1011 if (ImpDecl->getSuperClass())
1012 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1013 for (unsigned i = 0; i < numIvars; i++) {
1014 ObjCIvarDecl* ImplIvar = ivars[i];
1015 if (const ObjCIvarDecl *ClsIvar =
1016 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1017 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1018 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1019 continue;
1020 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001021 // Instance ivar to Implementation's DeclContext.
1022 ImplIvar->setLexicalDeclContext(ImpDecl);
1023 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1024 ImpDecl->addDecl(ImplIvar);
1025 }
1026 return;
1027 }
Chris Lattner4d391482007-12-12 07:09:47 +00001028 // Check interface's Ivar list against those in the implementation.
1029 // names and types must match.
1030 //
Chris Lattner4d391482007-12-12 07:09:47 +00001031 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001032 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001033 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1034 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001035 ObjCIvarDecl* ImplIvar = ivars[j++];
1036 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001037 assert (ImplIvar && "missing implementation ivar");
1038 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Steve Naroffca331292009-03-03 14:49:36 +00001040 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001041 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001042 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001043 << ImplIvar->getIdentifier()
1044 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001045 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001046 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1047 ImplIvar->getBitWidthValue(Context) !=
1048 ClsIvar->getBitWidthValue(Context)) {
1049 Diag(ImplIvar->getBitWidth()->getLocStart(),
1050 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1051 Diag(ClsIvar->getBitWidth()->getLocStart(),
1052 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001053 }
Steve Naroffca331292009-03-03 14:49:36 +00001054 // Make sure the names are identical.
1055 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001056 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001057 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001058 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001059 }
1060 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Chris Lattner609e4c72007-12-12 18:11:49 +00001063 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001064 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001065 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001066 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001067}
1068
Steve Naroff3c2eb662008-02-10 21:38:56 +00001069void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001070 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001071 // No point warning no definition of method which is 'unavailable'.
1072 if (method->hasAttr<UnavailableAttr>())
1073 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001074 if (!IncompleteImpl) {
1075 Diag(ImpLoc, diag::warn_incomplete_impl);
1076 IncompleteImpl = true;
1077 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001078 if (DiagID == diag::warn_unimplemented_protocol_method)
1079 Diag(ImpLoc, DiagID) << method->getDeclName();
1080 else
1081 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001082}
1083
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001084/// Determines if type B can be substituted for type A. Returns true if we can
1085/// guarantee that anything that the user will do to an object of type A can
1086/// also be done to an object of type B. This is trivially true if the two
1087/// types are the same, or if B is a subclass of A. It becomes more complex
1088/// in cases where protocols are involved.
1089///
1090/// Object types in Objective-C describe the minimum requirements for an
1091/// object, rather than providing a complete description of a type. For
1092/// example, if A is a subclass of B, then B* may refer to an instance of A.
1093/// The principle of substitutability means that we may use an instance of A
1094/// anywhere that we may use an instance of B - it will implement all of the
1095/// ivars of B and all of the methods of B.
1096///
1097/// This substitutability is important when type checking methods, because
1098/// the implementation may have stricter type definitions than the interface.
1099/// The interface specifies minimum requirements, but the implementation may
1100/// have more accurate ones. For example, a method may privately accept
1101/// instances of B, but only publish that it accepts instances of A. Any
1102/// object passed to it will be type checked against B, and so will implicitly
1103/// by a valid A*. Similarly, a method may return a subclass of the class that
1104/// it is declared as returning.
1105///
1106/// This is most important when considering subclassing. A method in a
1107/// subclass must accept any object as an argument that its superclass's
1108/// implementation accepts. It may, however, accept a more general type
1109/// without breaking substitutability (i.e. you can still use the subclass
1110/// anywhere that you can use the superclass, but not vice versa). The
1111/// converse requirement applies to return types: the return type for a
1112/// subclass method must be a valid object of the kind that the superclass
1113/// advertises, but it may be specified more accurately. This avoids the need
1114/// for explicit down-casting by callers.
1115///
1116/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001117static bool isObjCTypeSubstitutable(ASTContext &Context,
1118 const ObjCObjectPointerType *A,
1119 const ObjCObjectPointerType *B,
1120 bool rejectId) {
1121 // Reject a protocol-unqualified id.
1122 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001123
1124 // If B is a qualified id, then A must also be a qualified id and it must
1125 // implement all of the protocols in B. It may not be a qualified class.
1126 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1127 // stricter definition so it is not substitutable for id<A>.
1128 if (B->isObjCQualifiedIdType()) {
1129 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001130 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1131 QualType(B,0),
1132 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001133 }
1134
1135 /*
1136 // id is a special type that bypasses type checking completely. We want a
1137 // warning when it is used in one place but not another.
1138 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1139
1140
1141 // If B is a qualified id, then A must also be a qualified id (which it isn't
1142 // if we've got this far)
1143 if (B->isObjCQualifiedIdType()) return false;
1144 */
1145
1146 // Now we know that A and B are (potentially-qualified) class types. The
1147 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001148 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001149}
1150
John McCall10302c02010-10-28 02:34:38 +00001151static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1152 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1153}
1154
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001155static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001156 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001157 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001158 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001159 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001160 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001161 if (IsProtocolMethodDecl &&
1162 (MethodDecl->getObjCDeclQualifier() !=
1163 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001164 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001165 S.Diag(MethodImpl->getLocation(),
1166 (IsOverridingMode ?
1167 diag::warn_conflicting_overriding_ret_type_modifiers
1168 : diag::warn_conflicting_ret_type_modifiers))
1169 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001170 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1171 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1172 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1173 }
1174 else
1175 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001176 }
1177
John McCall10302c02010-10-28 02:34:38 +00001178 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001179 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001180 return true;
1181 if (!Warn)
1182 return false;
John McCall10302c02010-10-28 02:34:38 +00001183
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001184 unsigned DiagID =
1185 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1186 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001187
1188 // Mismatches between ObjC pointers go into a different warning
1189 // category, and sometimes they're even completely whitelisted.
1190 if (const ObjCObjectPointerType *ImplPtrTy =
1191 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1192 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001193 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001194 // Allow non-matching return types as long as they don't violate
1195 // the principle of substitutability. Specifically, we permit
1196 // return types that are subclasses of the declared return type,
1197 // or that are more-qualified versions of the declared type.
1198 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001199 return false;
John McCall10302c02010-10-28 02:34:38 +00001200
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001201 DiagID =
1202 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1203 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001204 }
1205 }
1206
1207 S.Diag(MethodImpl->getLocation(), DiagID)
1208 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001209 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001210 << MethodImpl->getResultType()
1211 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001212 S.Diag(MethodDecl->getLocation(),
1213 IsOverridingMode ? diag::note_previous_declaration
1214 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001215 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001216 return false;
John McCall10302c02010-10-28 02:34:38 +00001217}
1218
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001219static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001220 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001221 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001222 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001223 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001224 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001225 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001226 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001227 if (IsProtocolMethodDecl &&
1228 (ImplVar->getObjCDeclQualifier() !=
1229 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001230 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001231 if (IsOverridingMode)
1232 S.Diag(ImplVar->getLocation(),
1233 diag::warn_conflicting_overriding_param_modifiers)
1234 << getTypeRange(ImplVar->getTypeSourceInfo())
1235 << MethodImpl->getDeclName();
1236 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001237 diag::warn_conflicting_param_modifiers)
1238 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001239 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001240 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1241 << getTypeRange(IfaceVar->getTypeSourceInfo());
1242 }
1243 else
1244 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001245 }
1246
John McCall10302c02010-10-28 02:34:38 +00001247 QualType ImplTy = ImplVar->getType();
1248 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001249
John McCall10302c02010-10-28 02:34:38 +00001250 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001251 return true;
1252
1253 if (!Warn)
1254 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001255 unsigned DiagID =
1256 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1257 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001258
1259 // Mismatches between ObjC pointers go into a different warning
1260 // category, and sometimes they're even completely whitelisted.
1261 if (const ObjCObjectPointerType *ImplPtrTy =
1262 ImplTy->getAs<ObjCObjectPointerType>()) {
1263 if (const ObjCObjectPointerType *IfacePtrTy =
1264 IfaceTy->getAs<ObjCObjectPointerType>()) {
1265 // Allow non-matching argument types as long as they don't
1266 // violate the principle of substitutability. Specifically, the
1267 // implementation must accept any objects that the superclass
1268 // accepts, however it may also accept others.
1269 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001270 return false;
John McCall10302c02010-10-28 02:34:38 +00001271
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001272 DiagID =
1273 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1274 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001275 }
1276 }
1277
1278 S.Diag(ImplVar->getLocation(), DiagID)
1279 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001280 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1281 S.Diag(IfaceVar->getLocation(),
1282 (IsOverridingMode ? diag::note_previous_declaration
1283 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001284 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001285 return false;
John McCall10302c02010-10-28 02:34:38 +00001286}
John McCallf85e1932011-06-15 23:02:42 +00001287
1288/// In ARC, check whether the conventional meanings of the two methods
1289/// match. If they don't, it's a hard error.
1290static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1291 ObjCMethodDecl *decl) {
1292 ObjCMethodFamily implFamily = impl->getMethodFamily();
1293 ObjCMethodFamily declFamily = decl->getMethodFamily();
1294 if (implFamily == declFamily) return false;
1295
1296 // Since conventions are sorted by selector, the only possibility is
1297 // that the types differ enough to cause one selector or the other
1298 // to fall out of the family.
1299 assert(implFamily == OMF_None || declFamily == OMF_None);
1300
1301 // No further diagnostics required on invalid declarations.
1302 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1303
1304 const ObjCMethodDecl *unmatched = impl;
1305 ObjCMethodFamily family = declFamily;
1306 unsigned errorID = diag::err_arc_lost_method_convention;
1307 unsigned noteID = diag::note_arc_lost_method_convention;
1308 if (declFamily == OMF_None) {
1309 unmatched = decl;
1310 family = implFamily;
1311 errorID = diag::err_arc_gained_method_convention;
1312 noteID = diag::note_arc_gained_method_convention;
1313 }
1314
1315 // Indexes into a %select clause in the diagnostic.
1316 enum FamilySelector {
1317 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1318 };
1319 FamilySelector familySelector = FamilySelector();
1320
1321 switch (family) {
1322 case OMF_None: llvm_unreachable("logic error, no method convention");
1323 case OMF_retain:
1324 case OMF_release:
1325 case OMF_autorelease:
1326 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001327 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001328 case OMF_retainCount:
1329 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001330 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001331 // Mismatches for these methods don't change ownership
1332 // conventions, so we don't care.
1333 return false;
1334
1335 case OMF_init: familySelector = F_init; break;
1336 case OMF_alloc: familySelector = F_alloc; break;
1337 case OMF_copy: familySelector = F_copy; break;
1338 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1339 case OMF_new: familySelector = F_new; break;
1340 }
1341
1342 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1343 ReasonSelector reasonSelector;
1344
1345 // The only reason these methods don't fall within their families is
1346 // due to unusual result types.
1347 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1348 reasonSelector = R_UnrelatedReturn;
1349 } else {
1350 reasonSelector = R_NonObjectReturn;
1351 }
1352
1353 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1354 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1355
1356 return true;
1357}
John McCall10302c02010-10-28 02:34:38 +00001358
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001359void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001360 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001361 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001362 if (getLangOptions().ObjCAutoRefCount &&
1363 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1364 return;
1365
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001366 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001367 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001368 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Chris Lattner3aff9192009-04-11 19:58:42 +00001370 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001371 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001372 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001373 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001374 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001375 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001376
Fariborz Jahanian21121902011-08-08 18:03:17 +00001377 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001378 Diag(ImpMethodDecl->getLocation(),
1379 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001380 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001381 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001382}
1383
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001384void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1385 ObjCMethodDecl *Overridden,
1386 bool IsProtocolMethodDecl) {
1387
1388 CheckMethodOverrideReturn(*this, Method, Overridden,
1389 IsProtocolMethodDecl, true,
1390 true);
1391
1392 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1393 IF = Overridden->param_begin(), EM = Method->param_end();
1394 IM != EM; ++IM, ++IF) {
1395 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1396 IsProtocolMethodDecl, true, true);
1397 }
1398
1399 if (Method->isVariadic() != Overridden->isVariadic()) {
1400 Diag(Method->getLocation(),
1401 diag::warn_conflicting_overriding_variadic);
1402 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1403 }
1404}
1405
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001406/// WarnExactTypedMethods - This routine issues a warning if method
1407/// implementation declaration matches exactly that of its declaration.
1408void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1409 ObjCMethodDecl *MethodDecl,
1410 bool IsProtocolMethodDecl) {
1411 // don't issue warning when protocol method is optional because primary
1412 // class is not required to implement it and it is safe for protocol
1413 // to implement it.
1414 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1415 return;
1416 // don't issue warning when primary class's method is
1417 // depecated/unavailable.
1418 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1419 MethodDecl->hasAttr<DeprecatedAttr>())
1420 return;
1421
1422 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1423 IsProtocolMethodDecl, false, false);
1424 if (match)
1425 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1426 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1427 IM != EM; ++IM, ++IF) {
1428 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1429 *IM, *IF,
1430 IsProtocolMethodDecl, false, false);
1431 if (!match)
1432 break;
1433 }
1434 if (match)
1435 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001436 if (match)
1437 match = !(MethodDecl->isClassMethod() &&
1438 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001439
1440 if (match) {
1441 Diag(ImpMethodDecl->getLocation(),
1442 diag::warn_category_method_impl_match);
1443 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1444 }
1445}
1446
Mike Stump390b4cc2009-05-16 07:39:55 +00001447/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1448/// improve the efficiency of selector lookups and type checking by associating
1449/// with each protocol / interface / category the flattened instance tables. If
1450/// we used an immutable set to keep the table then it wouldn't add significant
1451/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001452
Steve Naroffefe7f362008-02-08 22:06:17 +00001453/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001454/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001455void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1456 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001457 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001458 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001459 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001460 ObjCContainerDecl *CDecl) {
1461 ObjCInterfaceDecl *IDecl;
1462 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1463 IDecl = C->getClassInterface();
1464 else
1465 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1466 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1467
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001468 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001469 ObjCInterfaceDecl *NSIDecl = 0;
1470 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001471 // check to see if class implements forwardInvocation method and objects
1472 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001473 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001474 // Under such conditions, which means that every method possible is
1475 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001476 // found" warnings.
1477 // FIXME: Use a general GetUnarySelector method for this.
1478 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1479 Selector fISelector = Context.Selectors.getSelector(1, &II);
1480 if (InsMap.count(fISelector))
1481 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1482 // need be implemented in the implementation.
1483 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1484 }
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001486 // If a method lookup fails locally we still need to look and see if
1487 // the method was implemented by a base class or an inherited
1488 // protocol. This lookup is slow, but occurs rarely in correct code
1489 // and otherwise would terminate in a warning.
1490
Chris Lattner4d391482007-12-12 07:09:47 +00001491 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001492 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001493 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001494 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001495 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001496 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001497 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001498 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001499 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001500 // Ugly, but necessary. Method declared in protcol might have
1501 // have been synthesized due to a property declared in the class which
1502 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001503 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001504 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001505 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001506 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001507 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001508 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001509 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001510 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001511 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1512 << PDecl->getDeclName();
1513 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001514 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001515 }
1516 }
Chris Lattner4d391482007-12-12 07:09:47 +00001517 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001518 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001519 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001520 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001521 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001522 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1523 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001524 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001525 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001526 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1527 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001528 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001529 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001530 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1531 PDecl->getDeclName();
1532 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001533 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001534 }
Chris Lattner780f3292008-07-21 21:32:27 +00001535 // Check on this protocols's referenced protocols, recursively.
1536 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1537 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001538 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001539}
1540
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001541/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001542/// or protocol against those declared in their implementations.
1543///
1544void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1545 const llvm::DenseSet<Selector> &ClsMap,
1546 llvm::DenseSet<Selector> &InsMapSeen,
1547 llvm::DenseSet<Selector> &ClsMapSeen,
1548 ObjCImplDecl* IMPDecl,
1549 ObjCContainerDecl* CDecl,
1550 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001551 bool ImmediateClass,
1552 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001553 // Check and see if instance methods in class interface have been
1554 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001555 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1556 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001557 if (InsMapSeen.count((*I)->getSelector()))
1558 continue;
1559 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001560 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001561 !InsMap.count((*I)->getSelector())) {
1562 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001563 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1564 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001565 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001566 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001567 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001568 IMPDecl->getInstanceMethod((*I)->getSelector());
1569 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1570 "Expected to find the method through lookup as well");
1571 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001572 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001573 if (ImpMethodDecl) {
1574 if (!WarnExactMatch)
1575 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1576 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001577 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001578 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1579 isa<ObjCProtocolDecl>(CDecl));
1580 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001581 }
1582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001584 // Check and see if class methods in class interface have been
1585 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001586 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001587 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001588 if (ClsMapSeen.count((*I)->getSelector()))
1589 continue;
1590 ClsMapSeen.insert((*I)->getSelector());
1591 if (!ClsMap.count((*I)->getSelector())) {
1592 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001593 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1594 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001595 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001596 ObjCMethodDecl *ImpMethodDecl =
1597 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001598 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1599 "Expected to find the method through lookup as well");
1600 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001601 if (!WarnExactMatch)
1602 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1603 isa<ObjCProtocolDecl>(CDecl));
1604 else
1605 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1606 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001607 }
1608 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001609
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001610 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001611 // Also methods in class extensions need be looked at next.
1612 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1613 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1614 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1615 IMPDecl,
1616 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001617 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001618
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001619 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001620 for (ObjCInterfaceDecl::all_protocol_iterator
1621 PI = I->all_referenced_protocol_begin(),
1622 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001623 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1624 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001625 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001626
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001627 // FIXME. For now, we are not checking for extact match of methods
1628 // in category implementation and its primary class's super class.
1629 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001630 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001631 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001632 I->getSuperClass(), IncompleteImpl, false);
1633 }
1634}
1635
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001636/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1637/// category matches with those implemented in its primary class and
1638/// warns each time an exact match is found.
1639void Sema::CheckCategoryVsClassMethodMatches(
1640 ObjCCategoryImplDecl *CatIMPDecl) {
1641 llvm::DenseSet<Selector> InsMap, ClsMap;
1642
1643 for (ObjCImplementationDecl::instmeth_iterator
1644 I = CatIMPDecl->instmeth_begin(),
1645 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1646 InsMap.insert((*I)->getSelector());
1647
1648 for (ObjCImplementationDecl::classmeth_iterator
1649 I = CatIMPDecl->classmeth_begin(),
1650 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1651 ClsMap.insert((*I)->getSelector());
1652 if (InsMap.empty() && ClsMap.empty())
1653 return;
1654
1655 // Get category's primary class.
1656 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1657 if (!CatDecl)
1658 return;
1659 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1660 if (!IDecl)
1661 return;
1662 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1663 bool IncompleteImpl = false;
1664 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1665 CatIMPDecl, IDecl,
1666 IncompleteImpl, false, true /*WarnExactMatch*/);
1667}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001668
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001669void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001670 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001671 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001672 llvm::DenseSet<Selector> InsMap;
1673 // Check and see if instance methods in class interface have been
1674 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001675 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001676 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001677 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001679 // Check and see if properties declared in the interface have either 1)
1680 // an implementation or 2) there is a @synthesize/@dynamic implementation
1681 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001682 if (isa<ObjCInterfaceDecl>(CDecl) &&
1683 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001684 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001685
Chris Lattner4d391482007-12-12 07:09:47 +00001686 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001687 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001688 I = IMPDecl->classmeth_begin(),
1689 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001690 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001692 // Check for type conflict of methods declared in a class/protocol and
1693 // its implementation; if any.
1694 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001695 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1696 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001697 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001698
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001699 // check all methods implemented in category against those declared
1700 // in its primary class.
1701 if (ObjCCategoryImplDecl *CatDecl =
1702 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1703 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Chris Lattner4d391482007-12-12 07:09:47 +00001705 // Check the protocol list for unimplemented methods in the @implementation
1706 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001707 // Check and see if class methods in class interface have been
1708 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Chris Lattnercddc8882009-03-01 00:56:52 +00001710 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001711 for (ObjCInterfaceDecl::all_protocol_iterator
1712 PI = I->all_referenced_protocol_begin(),
1713 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001714 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001715 InsMap, ClsMap, I);
1716 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001717 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1718 Categories; Categories = Categories->getNextClassExtension())
1719 ImplMethodsVsClassMethods(S, IMPDecl,
1720 const_cast<ObjCCategoryDecl*>(Categories),
1721 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001722 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001723 // For extended class, unimplemented methods in its protocols will
1724 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001725 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001726 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1727 E = C->protocol_end(); PI != E; ++PI)
1728 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001729 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001730 // Report unimplemented properties in the category as well.
1731 // When reporting on missing setter/getters, do not report when
1732 // setter/getter is implemented in category's primary class
1733 // implementation.
1734 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1735 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1736 for (ObjCImplementationDecl::instmeth_iterator
1737 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1738 InsMap.insert((*I)->getSelector());
1739 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001740 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001741 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001742 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001743 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001744}
1745
Mike Stump1eb44332009-09-09 15:08:12 +00001746/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001747Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001748Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001749 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001750 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001751 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001752 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001753 for (unsigned i = 0; i != NumElts; ++i) {
1754 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001755 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001756 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001757 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001758 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001759 // Maybe we will complain about the shadowed template parameter.
1760 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1761 // Just pretend that we didn't see the previous declaration.
1762 PrevDecl = 0;
1763 }
1764
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001765 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001766 // GCC apparently allows the following idiom:
1767 //
1768 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1769 // @class XCElementToggler;
1770 //
Mike Stump1eb44332009-09-09 15:08:12 +00001771 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001772 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001773 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001774 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001775 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001776 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001777 // a forward class declaration matching a typedef name of a class refers
1778 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001779 if (const ObjCObjectType *OI =
1780 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1781 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001782 }
Chris Lattner4d391482007-12-12 07:09:47 +00001783 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001784 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1785 if (!IDecl) { // Not already seen? Make a forward decl.
1786 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1787 IdentList[i], IdentLocs[i], true);
1788
1789 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1790 // the current DeclContext. This prevents clients that walk DeclContext
1791 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1792 // declared later (if at all). We also take care to explicitly make
1793 // sure this declaration is visible for name lookup.
1794 PushOnScopeChains(IDecl, TUScope, false);
1795 CurContext->makeDeclVisibleInContext(IDecl, true);
1796 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001797 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1798 IDecl, IdentLocs[i]);
1799 CurContext->addDecl(CDecl);
1800 CheckObjCDeclScope(CDecl);
1801 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001802 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001803
1804 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001805}
1806
John McCall0f4c4c42011-06-16 01:15:19 +00001807static bool tryMatchRecordTypes(ASTContext &Context,
1808 Sema::MethodMatchStrategy strategy,
1809 const Type *left, const Type *right);
1810
John McCallf85e1932011-06-15 23:02:42 +00001811static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1812 QualType leftQT, QualType rightQT) {
1813 const Type *left =
1814 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1815 const Type *right =
1816 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1817
1818 if (left == right) return true;
1819
1820 // If we're doing a strict match, the types have to match exactly.
1821 if (strategy == Sema::MMS_strict) return false;
1822
1823 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1824
1825 // Otherwise, use this absurdly complicated algorithm to try to
1826 // validate the basic, low-level compatibility of the two types.
1827
1828 // As a minimum, require the sizes and alignments to match.
1829 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1830 return false;
1831
1832 // Consider all the kinds of non-dependent canonical types:
1833 // - functions and arrays aren't possible as return and parameter types
1834
1835 // - vector types of equal size can be arbitrarily mixed
1836 if (isa<VectorType>(left)) return isa<VectorType>(right);
1837 if (isa<VectorType>(right)) return false;
1838
1839 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001840 // - structs, unions, and Objective-C objects must match more-or-less
1841 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001842 // - everything else should be a scalar
1843 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001844 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001845
John McCall1d9b3b22011-09-09 05:25:32 +00001846 // Make scalars agree in kind, except count bools as chars, and group
1847 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001848 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1849 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1850 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1851 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001852 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1853 leftSK = Type::STK_ObjCObjectPointer;
1854 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1855 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001856
1857 // Note that data member pointers and function member pointers don't
1858 // intermix because of the size differences.
1859
1860 return (leftSK == rightSK);
1861}
Chris Lattner4d391482007-12-12 07:09:47 +00001862
John McCall0f4c4c42011-06-16 01:15:19 +00001863static bool tryMatchRecordTypes(ASTContext &Context,
1864 Sema::MethodMatchStrategy strategy,
1865 const Type *lt, const Type *rt) {
1866 assert(lt && rt && lt != rt);
1867
1868 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1869 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1870 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1871
1872 // Require union-hood to match.
1873 if (left->isUnion() != right->isUnion()) return false;
1874
1875 // Require an exact match if either is non-POD.
1876 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1877 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1878 return false;
1879
1880 // Require size and alignment to match.
1881 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1882
1883 // Require fields to match.
1884 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1885 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1886 for (; li != le && ri != re; ++li, ++ri) {
1887 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1888 return false;
1889 }
1890 return (li == le && ri == re);
1891}
1892
Chris Lattner4d391482007-12-12 07:09:47 +00001893/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1894/// returns true, or false, accordingly.
1895/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001896bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1897 const ObjCMethodDecl *right,
1898 MethodMatchStrategy strategy) {
1899 if (!matchTypes(Context, strategy,
1900 left->getResultType(), right->getResultType()))
1901 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001902
John McCallf85e1932011-06-15 23:02:42 +00001903 if (getLangOptions().ObjCAutoRefCount &&
1904 (left->hasAttr<NSReturnsRetainedAttr>()
1905 != right->hasAttr<NSReturnsRetainedAttr>() ||
1906 left->hasAttr<NSConsumesSelfAttr>()
1907 != right->hasAttr<NSConsumesSelfAttr>()))
1908 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001910 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001911 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001912
John McCallf85e1932011-06-15 23:02:42 +00001913 for (; li != le; ++li, ++ri) {
1914 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001915 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001916
1917 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1918 return false;
1919
1920 if (getLangOptions().ObjCAutoRefCount &&
1921 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1922 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001923 }
1924 return true;
1925}
1926
Sebastian Redldb9d2142010-08-02 23:18:59 +00001927/// \brief Read the contents of the method pool for a given selector from
1928/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001929///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001930/// This routine should only be called once, when the method pool has no entry
1931/// for this selector.
1932Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001933 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001934 assert(MethodPool.find(Sel) == MethodPool.end() &&
1935 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001936
1937 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001938 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Sebastian Redldb9d2142010-08-02 23:18:59 +00001940 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001941}
1942
Sebastian Redldb9d2142010-08-02 23:18:59 +00001943void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1944 bool instance) {
1945 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1946 if (Pos == MethodPool.end()) {
1947 if (ExternalSource)
1948 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001949 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001950 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1951 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001952 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001953 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001954 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001955 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001956 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001957 Entry.Method = Method;
1958 Entry.Next = 0;
1959 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001960 }
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Chris Lattnerb25df352009-03-04 05:16:45 +00001962 // We've seen a method with this name, see if we have already seen this type
1963 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001964 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1965 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1966
1967 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001968 ObjCMethodDecl *PrevObjCMethod = List->Method;
1969 PrevObjCMethod->setDefined(impl);
1970 // If a method is deprecated, push it in the global pool.
1971 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001972 if (Method->isDeprecated()) {
1973 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001974 List->Method = Method;
1975 }
1976 // If new method is unavailable, push it into global pool
1977 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001978 if (Method->isUnavailable()) {
1979 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001980 List->Method = Method;
1981 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001982 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001983 }
John McCallf85e1932011-06-15 23:02:42 +00001984 }
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Chris Lattnerb25df352009-03-04 05:16:45 +00001986 // We have a new signature for an existing method - add it.
1987 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001988 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1989 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001990}
1991
John McCallf85e1932011-06-15 23:02:42 +00001992/// Determines if this is an "acceptable" loose mismatch in the global
1993/// method pool. This exists mostly as a hack to get around certain
1994/// global mismatches which we can't afford to make warnings / errors.
1995/// Really, what we want is a way to take a method out of the global
1996/// method pool.
1997static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1998 ObjCMethodDecl *other) {
1999 if (!chosen->isInstanceMethod())
2000 return false;
2001
2002 Selector sel = chosen->getSelector();
2003 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2004 return false;
2005
2006 // Don't complain about mismatches for -length if the method we
2007 // chose has an integral result type.
2008 return (chosen->getResultType()->isIntegerType());
2009}
2010
Sebastian Redldb9d2142010-08-02 23:18:59 +00002011ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002012 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002013 bool warn, bool instance) {
2014 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2015 if (Pos == MethodPool.end()) {
2016 if (ExternalSource)
2017 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002018 else
2019 return 0;
2020 }
2021
Sebastian Redldb9d2142010-08-02 23:18:59 +00002022 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Sebastian Redldb9d2142010-08-02 23:18:59 +00002024 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002025 bool issueDiagnostic = false, issueError = false;
2026
2027 // We support a warning which complains about *any* difference in
2028 // method signature.
2029 bool strictSelectorMatch =
2030 (receiverIdOrClass && warn &&
2031 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2032 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002033 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002034 if (strictSelectorMatch)
2035 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002036 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2037 MMS_strict)) {
2038 issueDiagnostic = true;
2039 break;
2040 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002041 }
2042
John McCallf85e1932011-06-15 23:02:42 +00002043 // If we didn't see any strict differences, we won't see any loose
2044 // differences. In ARC, however, we also need to check for loose
2045 // mismatches, because most of them are errors.
2046 if (!strictSelectorMatch ||
2047 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002048 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002049 // This checks if the methods differ in type mismatch.
2050 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2051 MMS_loose) &&
2052 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2053 issueDiagnostic = true;
2054 if (getLangOptions().ObjCAutoRefCount)
2055 issueError = true;
2056 break;
2057 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002058 }
2059
John McCallf85e1932011-06-15 23:02:42 +00002060 if (issueDiagnostic) {
2061 if (issueError)
2062 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2063 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002064 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2065 else
2066 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002067
2068 Diag(MethList.Method->getLocStart(),
2069 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002070 << MethList.Method->getSourceRange();
2071 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2072 Diag(Next->Method->getLocStart(), diag::note_also_found)
2073 << Next->Method->getSourceRange();
2074 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002075 }
2076 return MethList.Method;
2077}
2078
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002079ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002080 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2081 if (Pos == MethodPool.end())
2082 return 0;
2083
2084 GlobalMethods &Methods = Pos->second;
2085
2086 if (Methods.first.Method && Methods.first.Method->isDefined())
2087 return Methods.first.Method;
2088 if (Methods.second.Method && Methods.second.Method->isDefined())
2089 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002090 return 0;
2091}
2092
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002093/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2094/// identical selector names in current and its super classes and issues
2095/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002096void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2097 ObjCMethodDecl *Method,
2098 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002099 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2100 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002101
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002102 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002103 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002104 SD->lookupMethod(Method->getSelector(), IsInstance);
2105 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002106 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002107 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002108 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002109 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2110 E = Method->param_end();
2111 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2112 for (; ParamI != E; ++ParamI, ++PrevI) {
2113 // Number of parameters are the same and is guaranteed by selector match.
2114 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2115 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2116 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002117 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002118 // respective argument type in the super class method, issue warning;
2119 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002120 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002121 << T1 << T2;
2122 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2123 return;
2124 }
2125 }
2126 ID = SD;
2127 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002128}
2129
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002130/// DiagnoseDuplicateIvars -
2131/// Check for duplicate ivars in the entire class at the start of
2132/// @implementation. This becomes necesssary because class extension can
2133/// add ivars to a class in random order which will not be known until
2134/// class's @implementation is seen.
2135void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2136 ObjCInterfaceDecl *SID) {
2137 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2138 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2139 ObjCIvarDecl* Ivar = (*IVI);
2140 if (Ivar->isInvalidDecl())
2141 continue;
2142 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2143 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2144 if (prevIvar) {
2145 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2146 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2147 Ivar->setInvalidDecl();
2148 }
2149 }
2150 }
2151}
2152
Erik Verbruggend64251f2011-12-06 09:25:23 +00002153Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2154 switch (CurContext->getDeclKind()) {
2155 case Decl::ObjCInterface:
2156 return Sema::OCK_Interface;
2157 case Decl::ObjCProtocol:
2158 return Sema::OCK_Protocol;
2159 case Decl::ObjCCategory:
2160 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2161 return Sema::OCK_ClassExtension;
2162 else
2163 return Sema::OCK_Category;
2164 case Decl::ObjCImplementation:
2165 return Sema::OCK_Implementation;
2166 case Decl::ObjCCategoryImpl:
2167 return Sema::OCK_CategoryImplementation;
2168
2169 default:
2170 return Sema::OCK_None;
2171 }
2172}
2173
Steve Naroffa56f6162007-12-18 01:30:32 +00002174// Note: For class/category implemenations, allMethods/allProperties is
2175// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002176Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2177 Decl **allMethods, unsigned allNum,
2178 Decl **allProperties, unsigned pNum,
2179 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002180
Erik Verbruggend64251f2011-12-06 09:25:23 +00002181 if (getObjCContainerKind() == Sema::OCK_None)
2182 return 0;
2183
2184 assert(AtEnd.isValid() && "Invalid location for '@end'");
2185
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002186 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2187 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002188
Mike Stump1eb44332009-09-09 15:08:12 +00002189 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002190 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2191 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002192 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002193
Steve Naroff0701bbb2009-01-08 17:28:14 +00002194 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2195 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2196 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2197
Chris Lattner4d391482007-12-12 07:09:47 +00002198 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002199 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002200 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002201
2202 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002203 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002204 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002205 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002206 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002207 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002208 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002209 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002210 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002211 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002212 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002213 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002214 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002215 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002216 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002217 if (!Context.getSourceManager().isInSystemHeader(
2218 Method->getLocation()))
2219 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2220 << Method->getDeclName();
2221 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2222 }
Chris Lattner4d391482007-12-12 07:09:47 +00002223 InsMap[Method->getSelector()] = Method;
2224 /// The following allows us to typecheck messages to "id".
2225 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002226 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002227 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002228 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002229 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002230 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002231 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002232 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002233 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002234 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002235 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002236 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002237 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002238 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002239 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002240 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002241 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002242 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002243 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002244 if (!Context.getSourceManager().isInSystemHeader(
2245 Method->getLocation()))
2246 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2247 << Method->getDeclName();
2248 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2249 }
Chris Lattner4d391482007-12-12 07:09:47 +00002250 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002251 /// The following allows us to typecheck messages to "Class".
2252 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002253 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002254 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002255 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002256 }
2257 }
2258 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002259 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002260 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002261 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002262 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002263 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002264 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002265 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002266 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002267 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002268
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002269 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002270 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002271 if (C->IsClassExtension()) {
2272 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2273 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002274 }
Chris Lattner4d391482007-12-12 07:09:47 +00002275 }
Steve Naroff09c47192009-01-09 15:36:25 +00002276 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002277 if (CDecl->getIdentifier())
2278 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2279 // user-defined setter/getter. It also synthesizes setter/getter methods
2280 // and adds them to the DeclContext and global method pools.
2281 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2282 E = CDecl->prop_end();
2283 I != E; ++I)
2284 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002285 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002286 }
2287 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002288 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002289 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002290 // Any property declared in a class extension might have user
2291 // declared setter or getter in current class extension or one
2292 // of the other class extensions. Mark them as synthesized as
2293 // property will be synthesized when property with same name is
2294 // seen in the @implementation.
2295 for (const ObjCCategoryDecl *ClsExtDecl =
2296 IDecl->getFirstClassExtension();
2297 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2298 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2299 E = ClsExtDecl->prop_end(); I != E; ++I) {
2300 ObjCPropertyDecl *Property = (*I);
2301 // Skip over properties declared @dynamic
2302 if (const ObjCPropertyImplDecl *PIDecl
2303 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2304 if (PIDecl->getPropertyImplementation()
2305 == ObjCPropertyImplDecl::Dynamic)
2306 continue;
2307
2308 for (const ObjCCategoryDecl *CExtDecl =
2309 IDecl->getFirstClassExtension();
2310 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2311 if (ObjCMethodDecl *GetterMethod =
2312 CExtDecl->getInstanceMethod(Property->getGetterName()))
2313 GetterMethod->setSynthesized(true);
2314 if (!Property->isReadOnly())
2315 if (ObjCMethodDecl *SetterMethod =
2316 CExtDecl->getInstanceMethod(Property->getSetterName()))
2317 SetterMethod->setSynthesized(true);
2318 }
2319 }
2320 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002321 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002322 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002323 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002324
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002325 if (LangOpts.ObjCNonFragileABI2)
2326 while (IDecl->getSuperClass()) {
2327 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2328 IDecl = IDecl->getSuperClass();
2329 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002330 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002331 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002332 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002333 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002334 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Chris Lattner4d391482007-12-12 07:09:47 +00002336 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002337 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002338 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002339 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002340 Categories; Categories = Categories->getNextClassCategory()) {
2341 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002342 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002343 break;
2344 }
2345 }
2346 }
2347 }
Chris Lattner682bf922009-03-29 16:50:03 +00002348 if (isInterfaceDeclKind) {
2349 // Reject invalid vardecls.
2350 for (unsigned i = 0; i != tuvNum; i++) {
2351 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2352 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2353 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002354 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002355 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002356 }
Chris Lattner682bf922009-03-29 16:50:03 +00002357 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002358 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002359 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002360
2361 for (unsigned i = 0; i != tuvNum; i++) {
2362 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002363 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2364 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002365 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2366 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002367
2368 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002369}
2370
2371
2372/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2373/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002374static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002375CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002376 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002377}
2378
Ted Kremenek422bae72010-04-18 04:59:38 +00002379static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002380bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2381 const AttrVec &A) {
2382 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002383 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002384 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002385 return false;
2386
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002387 // method declared in interface has no attribute.
2388 // But implementation has attributes. This is invalid
2389 if (!IMD->hasAttrs())
2390 return true;
2391
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002392 const AttrVec &D = IMD->getAttrs();
2393 if (D.size() != A.size())
2394 return true;
2395
2396 // attributes on method declaration and definition must match exactly.
2397 // Note that we have at most a couple of attributes on methods, so this
2398 // n*n search is good enough.
2399 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2400 bool match = false;
2401 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2402 if ((*i)->getKind() == (*i1)->getKind()) {
2403 match = true;
2404 break;
2405 }
2406 }
2407 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002408 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002409 }
Sean Huntcf807c42010-08-18 23:23:40 +00002410 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002411}
2412
Douglas Gregore97179c2011-09-08 01:46:34 +00002413namespace {
2414 /// \brief Describes the compatibility of a result type with its method.
2415 enum ResultTypeCompatibilityKind {
2416 RTC_Compatible,
2417 RTC_Incompatible,
2418 RTC_Unknown
2419 };
2420}
2421
Douglas Gregor926df6c2011-06-11 01:09:30 +00002422/// \brief Check whether the declared result type of the given Objective-C
2423/// method declaration is compatible with the method's class.
2424///
Douglas Gregore97179c2011-09-08 01:46:34 +00002425static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002426CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2427 ObjCInterfaceDecl *CurrentClass) {
2428 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002429
2430 // If an Objective-C method inherits its related result type, then its
2431 // declared result type must be compatible with its own class type. The
2432 // declared result type is compatible if:
2433 if (const ObjCObjectPointerType *ResultObjectType
2434 = ResultType->getAs<ObjCObjectPointerType>()) {
2435 // - it is id or qualified id, or
2436 if (ResultObjectType->isObjCIdType() ||
2437 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002438 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002439
2440 if (CurrentClass) {
2441 if (ObjCInterfaceDecl *ResultClass
2442 = ResultObjectType->getInterfaceDecl()) {
2443 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002444 if (declaresSameEntity(CurrentClass, ResultClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002445 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002446
2447 // - it is a superclass of the method's class type
2448 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002449 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002450 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002451 } else {
2452 // Any Objective-C pointer type might be acceptable for a protocol
2453 // method; we just don't know.
2454 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002455 }
2456 }
2457
Douglas Gregore97179c2011-09-08 01:46:34 +00002458 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002459}
2460
John McCall6c2c2502011-07-22 02:45:48 +00002461namespace {
2462/// A helper class for searching for methods which a particular method
2463/// overrides.
2464class OverrideSearch {
2465 Sema &S;
2466 ObjCMethodDecl *Method;
2467 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2468 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2469 bool Recursive;
2470
2471public:
2472 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2473 Selector selector = method->getSelector();
2474
2475 // Bypass this search if we've never seen an instance/class method
2476 // with this selector before.
2477 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2478 if (it == S.MethodPool.end()) {
2479 if (!S.ExternalSource) return;
2480 it = S.ReadMethodPool(selector);
2481 }
2482 ObjCMethodList &list =
2483 method->isInstanceMethod() ? it->second.first : it->second.second;
2484 if (!list.Method) return;
2485
2486 ObjCContainerDecl *container
2487 = cast<ObjCContainerDecl>(method->getDeclContext());
2488
2489 // Prevent the search from reaching this container again. This is
2490 // important with categories, which override methods from the
2491 // interface and each other.
2492 Searched.insert(container);
2493 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002494 }
John McCall6c2c2502011-07-22 02:45:48 +00002495
2496 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2497 iterator begin() const { return Overridden.begin(); }
2498 iterator end() const { return Overridden.end(); }
2499
2500private:
2501 void searchFromContainer(ObjCContainerDecl *container) {
2502 if (container->isInvalidDecl()) return;
2503
2504 switch (container->getDeclKind()) {
2505#define OBJCCONTAINER(type, base) \
2506 case Decl::type: \
2507 searchFrom(cast<type##Decl>(container)); \
2508 break;
2509#define ABSTRACT_DECL(expansion)
2510#define DECL(type, base) \
2511 case Decl::type:
2512#include "clang/AST/DeclNodes.inc"
2513 llvm_unreachable("not an ObjC container!");
2514 }
2515 }
2516
2517 void searchFrom(ObjCProtocolDecl *protocol) {
2518 // A method in a protocol declaration overrides declarations from
2519 // referenced ("parent") protocols.
2520 search(protocol->getReferencedProtocols());
2521 }
2522
2523 void searchFrom(ObjCCategoryDecl *category) {
2524 // A method in a category declaration overrides declarations from
2525 // the main class and from protocols the category references.
2526 search(category->getClassInterface());
2527 search(category->getReferencedProtocols());
2528 }
2529
2530 void searchFrom(ObjCCategoryImplDecl *impl) {
2531 // A method in a category definition that has a category
2532 // declaration overrides declarations from the category
2533 // declaration.
2534 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2535 search(category);
2536
2537 // Otherwise it overrides declarations from the class.
2538 } else {
2539 search(impl->getClassInterface());
2540 }
2541 }
2542
2543 void searchFrom(ObjCInterfaceDecl *iface) {
2544 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002545 if (!iface->hasDefinition())
2546 return;
2547
John McCall6c2c2502011-07-22 02:45:48 +00002548 // - categories,
2549 for (ObjCCategoryDecl *category = iface->getCategoryList();
2550 category; category = category->getNextClassCategory())
2551 search(category);
2552
2553 // - the super class, and
2554 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2555 search(super);
2556
2557 // - any referenced protocols.
2558 search(iface->getReferencedProtocols());
2559 }
2560
2561 void searchFrom(ObjCImplementationDecl *impl) {
2562 // A method in a class implementation overrides declarations from
2563 // the class interface.
2564 search(impl->getClassInterface());
2565 }
2566
2567
2568 void search(const ObjCProtocolList &protocols) {
2569 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2570 i != e; ++i)
2571 search(*i);
2572 }
2573
2574 void search(ObjCContainerDecl *container) {
2575 // Abort if we've already searched this container.
2576 if (!Searched.insert(container)) return;
2577
2578 // Check for a method in this container which matches this selector.
2579 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2580 Method->isInstanceMethod());
2581
2582 // If we find one, record it and bail out.
2583 if (meth) {
2584 Overridden.insert(meth);
2585 return;
2586 }
2587
2588 // Otherwise, search for methods that a hypothetical method here
2589 // would have overridden.
2590
2591 // Note that we're now in a recursive case.
2592 Recursive = true;
2593
2594 searchFromContainer(container);
2595 }
2596};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002597}
2598
John McCalld226f652010-08-21 09:40:31 +00002599Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002600 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002601 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002602 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002603 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002604 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002605 Selector Sel,
2606 // optional arguments. The number of types/arguments is obtained
2607 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002608 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002609 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002610 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002611 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002612 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002613 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002614 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002615 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002616 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002617 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2618 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002619 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Douglas Gregore97179c2011-09-08 01:46:34 +00002621 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002622 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002623 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002624 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002625
Steve Naroffccef3712009-02-20 22:59:16 +00002626 // Methods cannot return interface types. All ObjC objects are
2627 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002628 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002629 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2630 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002631 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002632 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002633
2634 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002635 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002636 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002637 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002638 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002639 }
Mike Stump1eb44332009-09-09 15:08:12 +00002640
2641 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002642 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002643 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002644 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002645 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002646 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002647 /*isSynthesized=*/false,
2648 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002649 MethodDeclKind == tok::objc_optional
2650 ? ObjCMethodDecl::Optional
2651 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002652 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002653
Chris Lattner5f9e2722011-07-23 10:55:15 +00002654 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002655
Chris Lattner7db638d2009-04-11 19:42:43 +00002656 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002657 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002658 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Chris Lattnere294d3f2009-04-11 18:57:04 +00002660 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002661 ArgType = Context.getObjCIdType();
2662 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002663 } else {
John McCall58e46772009-10-23 21:48:59 +00002664 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002665 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002666 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002667 }
Mike Stump1eb44332009-09-09 15:08:12 +00002668
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002669 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2670 LookupOrdinaryName, ForRedeclaration);
2671 LookupName(R, S);
2672 if (R.isSingleResult()) {
2673 NamedDecl *PrevDecl = R.getFoundDecl();
2674 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002675 Diag(ArgInfo[i].NameLoc,
2676 (MethodDefinition ? diag::warn_method_param_redefinition
2677 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002678 << ArgInfo[i].Name;
2679 Diag(PrevDecl->getLocation(),
2680 diag::note_previous_declaration);
2681 }
2682 }
2683
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002684 SourceLocation StartLoc = DI
2685 ? DI->getTypeLoc().getBeginLoc()
2686 : ArgInfo[i].NameLoc;
2687
John McCall81ef3e62011-04-23 02:46:06 +00002688 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2689 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2690 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002691
John McCall70798862011-05-02 00:30:12 +00002692 Param->setObjCMethodScopeInfo(i);
2693
Chris Lattner0ed844b2008-04-04 06:12:32 +00002694 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002695 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002697 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002698 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002699
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002700 S->AddDecl(Param);
2701 IdResolver.AddDecl(Param);
2702
Chris Lattner0ed844b2008-04-04 06:12:32 +00002703 Params.push_back(Param);
2704 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002705
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002706 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002707 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002708 QualType ArgType = Param->getType();
2709 if (ArgType.isNull())
2710 ArgType = Context.getObjCIdType();
2711 else
2712 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002713 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002714 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002715 Diag(Param->getLocation(),
2716 diag::err_object_cannot_be_passed_returned_by_value)
2717 << 1 << ArgType;
2718 Param->setInvalidDecl();
2719 }
2720 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002721
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002722 Params.push_back(Param);
2723 }
2724
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002725 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002726 ObjCMethod->setObjCDeclQualifier(
2727 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002728
2729 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002730 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002731
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002732 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002733 const ObjCMethodDecl *PrevMethod = 0;
2734 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002735 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002736 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2737 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002738 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002739 PrevMethod = ImpDecl->getClassMethod(Sel);
2740 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002741 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002742
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002743 ObjCMethodDecl *IMD = 0;
2744 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2745 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2746 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002747 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002748 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002749 Diag(EndLoc, diag::warn_attribute_method_def);
Fariborz Jahanianec236782011-12-06 00:02:41 +00002750 Diag(IMD->getLocation(), diag::note_method_declared_at);
2751 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002752 } else {
2753 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002754 }
John McCall6c2c2502011-07-22 02:45:48 +00002755
Chris Lattner4d391482007-12-12 07:09:47 +00002756 if (PrevMethod) {
2757 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002758 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002759 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002760 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002761 }
John McCall54abf7d2009-11-04 02:18:39 +00002762
Douglas Gregor926df6c2011-06-11 01:09:30 +00002763 // If this Objective-C method does not have a related result type, but we
2764 // are allowed to infer related result types, try to do so based on the
2765 // method family.
2766 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2767 if (!CurrentClass) {
2768 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2769 CurrentClass = Cat->getClassInterface();
2770 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2771 CurrentClass = Impl->getClassInterface();
2772 else if (ObjCCategoryImplDecl *CatImpl
2773 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2774 CurrentClass = CatImpl->getClassInterface();
2775 }
John McCall6c2c2502011-07-22 02:45:48 +00002776
Douglas Gregore97179c2011-09-08 01:46:34 +00002777 ResultTypeCompatibilityKind RTC
2778 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002779
2780 // Search for overridden methods and merge information down from them.
2781 OverrideSearch overrides(*this, ObjCMethod);
2782 for (OverrideSearch::iterator
2783 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2784 ObjCMethodDecl *overridden = *i;
2785
2786 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002787 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002788 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002789
2790 // Then merge the declarations.
2791 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002792
2793 // Check for overriding methods
2794 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002795 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2796 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2797 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002798 }
2799
John McCallf85e1932011-06-15 23:02:42 +00002800 bool ARCError = false;
2801 if (getLangOptions().ObjCAutoRefCount)
2802 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2803
Douglas Gregore97179c2011-09-08 01:46:34 +00002804 // Infer the related result type when possible.
2805 if (!ARCError && RTC == RTC_Compatible &&
2806 !ObjCMethod->hasRelatedResultType() &&
2807 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002808 bool InferRelatedResultType = false;
2809 switch (ObjCMethod->getMethodFamily()) {
2810 case OMF_None:
2811 case OMF_copy:
2812 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002813 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002814 case OMF_mutableCopy:
2815 case OMF_release:
2816 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002817 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002818 break;
2819
2820 case OMF_alloc:
2821 case OMF_new:
2822 InferRelatedResultType = ObjCMethod->isClassMethod();
2823 break;
2824
2825 case OMF_init:
2826 case OMF_autorelease:
2827 case OMF_retain:
2828 case OMF_self:
2829 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2830 break;
2831 }
2832
John McCall6c2c2502011-07-22 02:45:48 +00002833 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002834 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002835 }
2836
John McCalld226f652010-08-21 09:40:31 +00002837 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002838}
2839
Chris Lattnercc98eac2008-12-17 07:13:27 +00002840bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002841 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002842 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002843 // Following is also an error. But it is caused by a missing @end
2844 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002845 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2846 return false;
2847 }
2848
Anders Carlsson15281452008-11-04 16:57:32 +00002849 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2850 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002851
Anders Carlsson15281452008-11-04 16:57:32 +00002852 return true;
2853}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002854
Chris Lattnercc98eac2008-12-17 07:13:27 +00002855/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2856/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002857void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002858 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002859 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002860 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002861 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002862 if (!Class) {
2863 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2864 return;
2865 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002866 if (LangOpts.ObjCNonFragileABI) {
2867 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2868 return;
2869 }
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Chris Lattnercc98eac2008-12-17 07:13:27 +00002871 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002872 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002873 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002874 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002875 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002876 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002877 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002878 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2879 /*FIXME: StartL=*/ID->getLocation(),
2880 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002881 ID->getIdentifier(), ID->getType(),
2882 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002883 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002884 }
Mike Stump1eb44332009-09-09 15:08:12 +00002885
Chris Lattnercc98eac2008-12-17 07:13:27 +00002886 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002887 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002888 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002889 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002890 if (getLangOptions().CPlusPlus)
2891 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002892 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002893 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002894 }
2895}
2896
Douglas Gregor160b5632010-04-26 17:32:49 +00002897/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002898VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2899 SourceLocation StartLoc,
2900 SourceLocation IdLoc,
2901 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002902 bool Invalid) {
2903 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2904 // duration shall not be qualified by an address-space qualifier."
2905 // Since all parameters have automatic store duration, they can not have
2906 // an address space.
2907 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002908 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002909 Invalid = true;
2910 }
2911
2912 // An @catch parameter must be an unqualified object pointer type;
2913 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2914 if (Invalid) {
2915 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002916 } else if (T->isDependentType()) {
2917 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002918 } else if (!T->isObjCObjectPointerType()) {
2919 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002920 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002921 } else if (T->isObjCQualifiedIdType()) {
2922 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002923 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002924 }
2925
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002926 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2927 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002928 New->setExceptionVariable(true);
2929
Douglas Gregor9aab9c42011-12-10 01:22:52 +00002930 // In ARC, infer 'retaining' for variables of retainable type.
2931 if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(New))
2932 Invalid = true;
2933
Douglas Gregor160b5632010-04-26 17:32:49 +00002934 if (Invalid)
2935 New->setInvalidDecl();
2936 return New;
2937}
2938
John McCalld226f652010-08-21 09:40:31 +00002939Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002940 const DeclSpec &DS = D.getDeclSpec();
2941
2942 // We allow the "register" storage class on exception variables because
2943 // GCC did, but we drop it completely. Any other storage class is an error.
2944 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2945 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2946 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2947 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2948 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2949 << DS.getStorageClassSpec();
2950 }
2951 if (D.getDeclSpec().isThreadSpecified())
2952 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2953 D.getMutableDeclSpec().ClearStorageClassSpecs();
2954
2955 DiagnoseFunctionSpecifiers(D);
2956
2957 // Check that there are no default arguments inside the type of this
2958 // exception object (C++ only).
2959 if (getLangOptions().CPlusPlus)
2960 CheckExtraCXXDefaultArguments(D);
2961
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002962 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002963 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002964
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002965 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2966 D.getSourceRange().getBegin(),
2967 D.getIdentifierLoc(),
2968 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002969 D.isInvalidType());
2970
2971 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2972 if (D.getCXXScopeSpec().isSet()) {
2973 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2974 << D.getCXXScopeSpec().getRange();
2975 New->setInvalidDecl();
2976 }
2977
2978 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002979 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002980 if (D.getIdentifier())
2981 IdResolver.AddDecl(New);
2982
2983 ProcessDeclAttributes(S, New, D);
2984
2985 if (New->hasAttr<BlocksAttr>())
2986 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002987 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002988}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002989
2990/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002991/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002992void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002993 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002994 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2995 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002996 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002997 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002998 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002999 }
3000}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003001
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003002void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003003 // Load referenced selectors from the external source.
3004 if (ExternalSource) {
3005 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3006 ExternalSource->ReadReferencedSelectors(Sels);
3007 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3008 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3009 }
3010
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003011 // Warning will be issued only when selector table is
3012 // generated (which means there is at lease one implementation
3013 // in the TU). This is to match gcc's behavior.
3014 if (ReferencedSelectors.empty() ||
3015 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003016 return;
3017 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3018 ReferencedSelectors.begin(),
3019 E = ReferencedSelectors.end(); S != E; ++S) {
3020 Selector Sel = (*S).first;
3021 if (!LookupImplementedMethodInGlobalPool(Sel))
3022 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3023 }
3024 return;
3025}