blob: 661c580cc97b1a83d1dabae2cd8cc731d1d23db6 [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.
Douglas Gregor7723fec2011-12-15 20:29:51 +000063 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000064 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
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000350namespace {
351
352// Callback to only accept typo corrections that are Objective-C classes.
353// If an ObjCInterfaceDecl* is given to the constructor, then the validation
354// function will reject corrections to that class.
355class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
356 public:
357 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
358 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
359 : CurrentIDecl(IDecl) {}
360
361 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
362 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
363 return ID && !declaresSameEntity(ID, CurrentIDecl);
364 }
365
366 private:
367 ObjCInterfaceDecl *CurrentIDecl;
368};
369
370}
371
John McCalld226f652010-08-21 09:40:31 +0000372Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000373ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
374 IdentifierInfo *ClassName, SourceLocation ClassLoc,
375 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000376 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000377 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000378 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000379 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Chris Lattner4d391482007-12-12 07:09:47 +0000381 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000382 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000383 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000384
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000385 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000386 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000387 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Douglas Gregor7723fec2011-12-15 20:29:51 +0000390 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000391 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000392 ObjCInterfaceDecl *IDecl
393 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000394 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000395
Douglas Gregor7723fec2011-12-15 20:29:51 +0000396 if (PrevIDecl) {
397 // Class already seen. Was it a definition?
398 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
399 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
400 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000401 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000402 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000403 }
Chris Lattner4d391482007-12-12 07:09:47 +0000404 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000405
406 if (AttrList)
407 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
408 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregor7723fec2011-12-15 20:29:51 +0000410 // Start the definition of this class. If we're in a redefinition case, there
411 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000412 if (!IDecl->hasDefinition())
413 IDecl->startDefinition();
414
Chris Lattner4d391482007-12-12 07:09:47 +0000415 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000416 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000417 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
418 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000419
420 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000421 // Try to correct for a typo in the superclass name without correcting
422 // to the class we're defining.
423 ObjCInterfaceValidatorCCC Validator(IDecl);
424 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000425 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000426 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000427 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
428 Diag(SuperLoc, diag::err_undef_superclass_suggest)
429 << SuperName << ClassName << PrevDecl->getDeclName();
430 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
431 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000432 }
433 }
434
Douglas Gregor60ef3082011-12-15 00:29:59 +0000435 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000436 Diag(SuperLoc, diag::err_recursive_superclass)
437 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000438 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000439 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000440 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000441 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000442
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000443 // Diagnose classes that inherit from deprecated classes.
444 if (SuperClassDecl)
445 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000447 if (PrevDecl && SuperClassDecl == 0) {
448 // The previous declaration was not a class decl. Check if we have a
449 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000450 if (const TypedefNameDecl *TDecl =
451 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000452 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000453 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000454 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
455 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000456 }
457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000459 // This handles the following case:
460 //
461 // typedef int SuperClass;
462 // @interface MyClass : SuperClass {} @end
463 //
464 if (!SuperClassDecl) {
465 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
466 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000467 }
468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Richard Smith162e1c12011-04-15 14:24:37 +0000470 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000471 if (!SuperClassDecl)
472 Diag(SuperLoc, diag::err_undef_superclass)
473 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000474 else if (RequireCompleteType(SuperLoc,
475 Context.getObjCInterfaceType(SuperClassDecl),
476 PDiag(diag::err_forward_superclass)
477 << SuperClassDecl->getDeclName()
478 << ClassName
479 << SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000480 SuperClassDecl = 0;
481 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000482 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000483 IDecl->setSuperClass(SuperClassDecl);
484 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000485 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000486 }
Chris Lattner4d391482007-12-12 07:09:47 +0000487 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000488 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Sebastian Redl0b17c612010-08-13 00:28:03 +0000491 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000492 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000493 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000494 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000495 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Anders Carlsson15281452008-11-04 16:57:32 +0000498 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000499 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000500}
501
502/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000503/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000504Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
505 IdentifierInfo *AliasName,
506 SourceLocation AliasLocation,
507 IdentifierInfo *ClassName,
508 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000509 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000510 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000511 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000512 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000513 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000514 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000515 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000516 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000517 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000518 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000519 }
520 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000521 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000522 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000523 if (const TypedefNameDecl *TDecl =
524 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000525 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000526 if (T->isObjCObjectType()) {
527 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000528 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000529 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000530 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000531 }
532 }
533 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000534 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
535 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000536 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000537 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000538 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000539 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000542 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000543 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000544 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Anders Carlsson15281452008-11-04 16:57:32 +0000546 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000547 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000548
John McCalld226f652010-08-21 09:40:31 +0000549 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000550}
551
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000552bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000553 IdentifierInfo *PName,
554 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000555 const ObjCList<ObjCProtocolDecl> &PList) {
556
557 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000558 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
559 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000560 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
561 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000562 if (PDecl->getIdentifier() == PName) {
563 Diag(Ploc, diag::err_protocol_has_circular_dependency);
564 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000565 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000566 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000567
568 if (!PDecl->hasDefinition())
569 continue;
570
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000571 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
572 PDecl->getLocation(), PDecl->getReferencedProtocols()))
573 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000574 }
575 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000576 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000577}
578
John McCalld226f652010-08-21 09:40:31 +0000579Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000580Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
581 IdentifierInfo *ProtocolName,
582 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000583 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000584 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000585 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000586 SourceLocation EndProtoLoc,
587 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000588 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000589 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000590 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000591 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
592 ForRedeclaration);
593 ObjCProtocolDecl *PDecl = 0;
594 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
595 // If we already have a definition, complain.
596 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
597 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregor27c6da22012-01-01 20:30:41 +0000599 // Create a new protocol that is completely distinct from previous
600 // declarations, and do not make this protocol available for name lookup.
601 // That way, we'll end up completely ignoring the duplicate.
602 // FIXME: Can we turn this into an error?
603 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
604 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000605 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000606 PDecl->startDefinition();
607 } else {
608 if (PrevDecl) {
609 // Check for circular dependencies among protocol declarations. This can
610 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000611 ObjCList<ObjCProtocolDecl> PList;
612 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
613 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000614 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000615 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000616
617 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000618 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000619 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000620 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000621
Douglas Gregor6e378de2009-04-23 23:18:26 +0000622 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000623 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000624 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000625
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000626 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000627 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000628
629 // Merge attributes from previous declarations.
630 if (PrevDecl)
631 mergeDeclAttributes(PDecl, PrevDecl);
632
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000633 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000634 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000635 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
636 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
639 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000640 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000641}
642
643/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000644/// issues an error if they are not declared. It returns list of
645/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000646void
Chris Lattnere13b9592008-07-26 04:03:38 +0000647Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000648 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000649 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000650 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000651 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000652 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
653 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000654 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000655 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000656 TypoCorrection Corrected = CorrectTypo(
657 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000658 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000659 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000660 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000661 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000662 Diag(PDecl->getLocation(), diag::note_previous_decl)
663 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000664 }
665 }
666
667 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000668 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000669 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000670 continue;
671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000673 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000674
675 // If this is a forward declaration and we are supposed to warn in this
676 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000677 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000678 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000679 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000680 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000681 }
682}
683
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000684/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000685/// a class method in its extension.
686///
Mike Stump1eb44332009-09-09 15:08:12 +0000687void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000688 ObjCInterfaceDecl *ID) {
689 if (!ID)
690 return; // Possibly due to previous error
691
692 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000693 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
694 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000695 ObjCMethodDecl *MD = *i;
696 MethodMap[MD->getSelector()] = MD;
697 }
698
699 if (MethodMap.empty())
700 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000701 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
702 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000703 ObjCMethodDecl *Method = *i;
704 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
705 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
706 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
707 << Method->getDeclName();
708 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
709 }
710 }
711}
712
Chris Lattner58fe03b2009-04-12 08:43:13 +0000713/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000714Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000715Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000716 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000717 unsigned NumElts,
718 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000719 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000720 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000721 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000722 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
723 ForRedeclaration);
724 ObjCProtocolDecl *PDecl
725 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
726 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000727 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000728
729 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000730 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000731
Douglas Gregor3937f872012-01-01 20:33:24 +0000732 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000733 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000734
735 if (PrevDecl)
736 mergeDeclAttributes(PDecl, PrevDecl);
737
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000738 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000739 }
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000741 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000742}
743
John McCalld226f652010-08-21 09:40:31 +0000744Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000745ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
746 IdentifierInfo *ClassName, SourceLocation ClassLoc,
747 IdentifierInfo *CategoryName,
748 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000749 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000750 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000751 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000752 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000753 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000754 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000755
756 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000757
758 if (!IDecl
759 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
760 PDiag(diag::err_category_forward_interface)
761 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000762 // Create an invalid ObjCCategoryDecl to serve as context for
763 // the enclosing method declarations. We mark the decl invalid
764 // to make it clear that this isn't a valid AST.
765 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000766 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000767 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000768
769 if (!IDecl)
770 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000771 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000772 }
773
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000774 if (!CategoryName && IDecl->getImplementation()) {
775 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
776 Diag(IDecl->getImplementation()->getLocation(),
777 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000778 }
779
Fariborz Jahanian25760612010-02-15 21:55:26 +0000780 if (CategoryName) {
781 /// Check for duplicate interface declaration for this category
782 ObjCCategoryDecl *CDeclChain;
783 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
784 CDeclChain = CDeclChain->getNextClassCategory()) {
785 if (CDeclChain->getIdentifier() == CategoryName) {
786 // Class extensions can be declared multiple times.
787 Diag(CategoryLoc, diag::warn_dup_category_def)
788 << ClassName << CategoryName;
789 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
790 break;
791 }
Chris Lattner70f19542009-02-16 21:26:43 +0000792 }
793 }
Chris Lattner70f19542009-02-16 21:26:43 +0000794
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000795 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
796 ClassLoc, CategoryLoc, CategoryName, IDecl);
797 // FIXME: PushOnScopeChains?
798 CurContext->addDecl(CDecl);
799
Chris Lattner4d391482007-12-12 07:09:47 +0000800 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000801 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000802 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000803 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000804 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000805 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000806 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Anders Carlsson15281452008-11-04 16:57:32 +0000809 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000810 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000811}
812
813/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000814/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000815/// object.
John McCalld226f652010-08-21 09:40:31 +0000816Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000817 SourceLocation AtCatImplLoc,
818 IdentifierInfo *ClassName, SourceLocation ClassLoc,
819 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000820 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000821 ObjCCategoryDecl *CatIDecl = 0;
822 if (IDecl) {
823 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
824 if (!CatIDecl) {
825 // Category @implementation with no corresponding @interface.
826 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000827 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
828 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000829 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000830 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000831 }
832 }
833
Mike Stump1eb44332009-09-09 15:08:12 +0000834 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000835 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000836 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000837 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000838 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000839 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000840 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000841 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
842 diag::err_undef_interface)) {
843 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000844 }
Chris Lattner4d391482007-12-12 07:09:47 +0000845
Douglas Gregord0434102009-01-09 00:49:46 +0000846 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000847 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000848
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000849 // If the interface is deprecated/unavailable, warn/error about it.
850 if (IDecl)
851 DiagnoseUseOfDecl(IDecl, ClassLoc);
852
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000853 /// Check that CatName, category name, is not used in another implementation.
854 if (CatIDecl) {
855 if (CatIDecl->getImplementation()) {
856 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
857 << CatName;
858 Diag(CatIDecl->getImplementation()->getLocation(),
859 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000860 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000861 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000862 // Warn on implementating category of deprecated class under
863 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000864 DiagnoseObjCImplementedDeprecations(*this,
865 dyn_cast<NamedDecl>(IDecl),
866 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000867 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Anders Carlsson15281452008-11-04 16:57:32 +0000870 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000871 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000872}
873
John McCalld226f652010-08-21 09:40:31 +0000874Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000875 SourceLocation AtClassImplLoc,
876 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000877 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000878 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000879 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000880 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000881 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000882 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
883 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000884 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000885 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000886 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000887 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000888 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
889 diag::warn_undef_interface);
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.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000893 ObjCInterfaceValidatorCCC Validator;
894 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000895 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000896 NULL, Validator)) {
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.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000902 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000903 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000904 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000905 << ClassName << CorrectedName;
906 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
907 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000908 IDecl = 0;
909 } else {
910 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
911 }
Chris Lattner4d391482007-12-12 07:09:47 +0000912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Chris Lattner4d391482007-12-12 07:09:47 +0000914 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000915 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000916 if (SuperClassname) {
917 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000918 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
919 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000920 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000921 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
922 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000923 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000924 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000925 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000926 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000927 Diag(SuperClassLoc, diag::err_undef_superclass)
928 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000929 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000930 // This implementation and its interface do not have the same
931 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000932 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000933 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000934 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000935 }
936 }
937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Chris Lattner4d391482007-12-12 07:09:47 +0000939 if (!IDecl) {
940 // Legacy case of @implementation with no corresponding @interface.
941 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000942
Mike Stump390b4cc2009-05-16 07:39:55 +0000943 // FIXME: Do we support attributes on the @implementation? If so we should
944 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000945 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000946 ClassName, /*PrevDecl=*/0, ClassLoc,
947 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000948 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000949 if (SDecl) {
950 IDecl->setSuperClass(SDecl);
951 IDecl->setSuperClassLoc(SuperClassLoc);
952 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
953 } else {
954 IDecl->setEndOfDefinitionLoc(ClassLoc);
955 }
956
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000957 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000958 } else {
959 // Mark the interface as being completed, even if it was just as
960 // @class ....;
961 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000962 if (!IDecl->hasDefinition())
963 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
966 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000967 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
968 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Anders Carlsson15281452008-11-04 16:57:32 +0000970 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000971 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattner4d391482007-12-12 07:09:47 +0000973 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000974 if (IDecl->getImplementation()) {
975 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000976 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000977 Diag(IDecl->getImplementation()->getLocation(),
978 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000979 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000980 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000981 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000982 // Warn on implementating deprecated class under
983 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000984 DiagnoseObjCImplementedDeprecations(*this,
985 dyn_cast<NamedDecl>(IDecl),
986 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000987 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000988 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000989}
990
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +0000991Sema::DeclGroupPtrTy
992Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
993 SmallVector<Decl *, 64> DeclsInGroup;
994 DeclsInGroup.reserve(Decls.size() + 1);
995
996 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
997 Decl *Dcl = Decls[i];
998 if (!Dcl)
999 continue;
1000 if (Dcl->getDeclContext()->isFileContext())
1001 Dcl->setTopLevelDeclInObjCContainer();
1002 DeclsInGroup.push_back(Dcl);
1003 }
1004
1005 DeclsInGroup.push_back(ObjCImpDecl);
1006
1007 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1008}
1009
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001010void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1011 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001012 SourceLocation RBrace) {
1013 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001014 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001015 if (!IDecl)
1016 return;
1017 /// Check case of non-existing @interface decl.
1018 /// (legacy objective-c @implementation decl without an @interface decl).
1019 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001020 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001021 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001022 // Add ivar's to class's DeclContext.
1023 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001024 ivars[i]->setLexicalDeclContext(ImpDecl);
1025 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001026 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001027 }
1028
Chris Lattner4d391482007-12-12 07:09:47 +00001029 return;
1030 }
1031 // If implementation has empty ivar list, just return.
1032 if (numIvars == 0)
1033 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Chris Lattner4d391482007-12-12 07:09:47 +00001035 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001036 if (LangOpts.ObjCNonFragileABI2) {
1037 if (ImpDecl->getSuperClass())
1038 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1039 for (unsigned i = 0; i < numIvars; i++) {
1040 ObjCIvarDecl* ImplIvar = ivars[i];
1041 if (const ObjCIvarDecl *ClsIvar =
1042 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1043 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1044 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1045 continue;
1046 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001047 // Instance ivar to Implementation's DeclContext.
1048 ImplIvar->setLexicalDeclContext(ImpDecl);
1049 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1050 ImpDecl->addDecl(ImplIvar);
1051 }
1052 return;
1053 }
Chris Lattner4d391482007-12-12 07:09:47 +00001054 // Check interface's Ivar list against those in the implementation.
1055 // names and types must match.
1056 //
Chris Lattner4d391482007-12-12 07:09:47 +00001057 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001058 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001059 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1060 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001061 ObjCIvarDecl* ImplIvar = ivars[j++];
1062 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001063 assert (ImplIvar && "missing implementation ivar");
1064 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Steve Naroffca331292009-03-03 14:49:36 +00001066 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001067 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001068 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001069 << ImplIvar->getIdentifier()
1070 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001071 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001072 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1073 ImplIvar->getBitWidthValue(Context) !=
1074 ClsIvar->getBitWidthValue(Context)) {
1075 Diag(ImplIvar->getBitWidth()->getLocStart(),
1076 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1077 Diag(ClsIvar->getBitWidth()->getLocStart(),
1078 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001079 }
Steve Naroffca331292009-03-03 14:49:36 +00001080 // Make sure the names are identical.
1081 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001082 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001083 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001084 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001085 }
1086 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner609e4c72007-12-12 18:11:49 +00001089 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001090 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001091 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001092 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001093}
1094
Steve Naroff3c2eb662008-02-10 21:38:56 +00001095void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001096 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001097 // No point warning no definition of method which is 'unavailable'.
1098 if (method->hasAttr<UnavailableAttr>())
1099 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001100 if (!IncompleteImpl) {
1101 Diag(ImpLoc, diag::warn_incomplete_impl);
1102 IncompleteImpl = true;
1103 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001104 if (DiagID == diag::warn_unimplemented_protocol_method)
1105 Diag(ImpLoc, DiagID) << method->getDeclName();
1106 else
1107 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001108}
1109
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001110/// Determines if type B can be substituted for type A. Returns true if we can
1111/// guarantee that anything that the user will do to an object of type A can
1112/// also be done to an object of type B. This is trivially true if the two
1113/// types are the same, or if B is a subclass of A. It becomes more complex
1114/// in cases where protocols are involved.
1115///
1116/// Object types in Objective-C describe the minimum requirements for an
1117/// object, rather than providing a complete description of a type. For
1118/// example, if A is a subclass of B, then B* may refer to an instance of A.
1119/// The principle of substitutability means that we may use an instance of A
1120/// anywhere that we may use an instance of B - it will implement all of the
1121/// ivars of B and all of the methods of B.
1122///
1123/// This substitutability is important when type checking methods, because
1124/// the implementation may have stricter type definitions than the interface.
1125/// The interface specifies minimum requirements, but the implementation may
1126/// have more accurate ones. For example, a method may privately accept
1127/// instances of B, but only publish that it accepts instances of A. Any
1128/// object passed to it will be type checked against B, and so will implicitly
1129/// by a valid A*. Similarly, a method may return a subclass of the class that
1130/// it is declared as returning.
1131///
1132/// This is most important when considering subclassing. A method in a
1133/// subclass must accept any object as an argument that its superclass's
1134/// implementation accepts. It may, however, accept a more general type
1135/// without breaking substitutability (i.e. you can still use the subclass
1136/// anywhere that you can use the superclass, but not vice versa). The
1137/// converse requirement applies to return types: the return type for a
1138/// subclass method must be a valid object of the kind that the superclass
1139/// advertises, but it may be specified more accurately. This avoids the need
1140/// for explicit down-casting by callers.
1141///
1142/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001143static bool isObjCTypeSubstitutable(ASTContext &Context,
1144 const ObjCObjectPointerType *A,
1145 const ObjCObjectPointerType *B,
1146 bool rejectId) {
1147 // Reject a protocol-unqualified id.
1148 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001149
1150 // If B is a qualified id, then A must also be a qualified id and it must
1151 // implement all of the protocols in B. It may not be a qualified class.
1152 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1153 // stricter definition so it is not substitutable for id<A>.
1154 if (B->isObjCQualifiedIdType()) {
1155 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001156 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1157 QualType(B,0),
1158 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001159 }
1160
1161 /*
1162 // id is a special type that bypasses type checking completely. We want a
1163 // warning when it is used in one place but not another.
1164 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1165
1166
1167 // If B is a qualified id, then A must also be a qualified id (which it isn't
1168 // if we've got this far)
1169 if (B->isObjCQualifiedIdType()) return false;
1170 */
1171
1172 // Now we know that A and B are (potentially-qualified) class types. The
1173 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001174 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001175}
1176
John McCall10302c02010-10-28 02:34:38 +00001177static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1178 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1179}
1180
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001181static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001182 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001183 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001184 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001185 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001186 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001187 if (IsProtocolMethodDecl &&
1188 (MethodDecl->getObjCDeclQualifier() !=
1189 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001190 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001191 S.Diag(MethodImpl->getLocation(),
1192 (IsOverridingMode ?
1193 diag::warn_conflicting_overriding_ret_type_modifiers
1194 : diag::warn_conflicting_ret_type_modifiers))
1195 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001196 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1197 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1198 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1199 }
1200 else
1201 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001202 }
1203
John McCall10302c02010-10-28 02:34:38 +00001204 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001205 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001206 return true;
1207 if (!Warn)
1208 return false;
John McCall10302c02010-10-28 02:34:38 +00001209
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001210 unsigned DiagID =
1211 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1212 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001213
1214 // Mismatches between ObjC pointers go into a different warning
1215 // category, and sometimes they're even completely whitelisted.
1216 if (const ObjCObjectPointerType *ImplPtrTy =
1217 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1218 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001219 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001220 // Allow non-matching return types as long as they don't violate
1221 // the principle of substitutability. Specifically, we permit
1222 // return types that are subclasses of the declared return type,
1223 // or that are more-qualified versions of the declared type.
1224 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001225 return false;
John McCall10302c02010-10-28 02:34:38 +00001226
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001227 DiagID =
1228 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1229 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001230 }
1231 }
1232
1233 S.Diag(MethodImpl->getLocation(), DiagID)
1234 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001235 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001236 << MethodImpl->getResultType()
1237 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001238 S.Diag(MethodDecl->getLocation(),
1239 IsOverridingMode ? diag::note_previous_declaration
1240 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001241 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001242 return false;
John McCall10302c02010-10-28 02:34:38 +00001243}
1244
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001245static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001246 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001247 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001248 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001249 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001250 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001251 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001252 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001253 if (IsProtocolMethodDecl &&
1254 (ImplVar->getObjCDeclQualifier() !=
1255 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001256 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001257 if (IsOverridingMode)
1258 S.Diag(ImplVar->getLocation(),
1259 diag::warn_conflicting_overriding_param_modifiers)
1260 << getTypeRange(ImplVar->getTypeSourceInfo())
1261 << MethodImpl->getDeclName();
1262 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001263 diag::warn_conflicting_param_modifiers)
1264 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001265 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001266 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1267 << getTypeRange(IfaceVar->getTypeSourceInfo());
1268 }
1269 else
1270 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001271 }
1272
John McCall10302c02010-10-28 02:34:38 +00001273 QualType ImplTy = ImplVar->getType();
1274 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001275
John McCall10302c02010-10-28 02:34:38 +00001276 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001277 return true;
1278
1279 if (!Warn)
1280 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001281 unsigned DiagID =
1282 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1283 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001284
1285 // Mismatches between ObjC pointers go into a different warning
1286 // category, and sometimes they're even completely whitelisted.
1287 if (const ObjCObjectPointerType *ImplPtrTy =
1288 ImplTy->getAs<ObjCObjectPointerType>()) {
1289 if (const ObjCObjectPointerType *IfacePtrTy =
1290 IfaceTy->getAs<ObjCObjectPointerType>()) {
1291 // Allow non-matching argument types as long as they don't
1292 // violate the principle of substitutability. Specifically, the
1293 // implementation must accept any objects that the superclass
1294 // accepts, however it may also accept others.
1295 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001296 return false;
John McCall10302c02010-10-28 02:34:38 +00001297
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001298 DiagID =
1299 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1300 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001301 }
1302 }
1303
1304 S.Diag(ImplVar->getLocation(), DiagID)
1305 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001306 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1307 S.Diag(IfaceVar->getLocation(),
1308 (IsOverridingMode ? diag::note_previous_declaration
1309 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001310 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001311 return false;
John McCall10302c02010-10-28 02:34:38 +00001312}
John McCallf85e1932011-06-15 23:02:42 +00001313
1314/// In ARC, check whether the conventional meanings of the two methods
1315/// match. If they don't, it's a hard error.
1316static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1317 ObjCMethodDecl *decl) {
1318 ObjCMethodFamily implFamily = impl->getMethodFamily();
1319 ObjCMethodFamily declFamily = decl->getMethodFamily();
1320 if (implFamily == declFamily) return false;
1321
1322 // Since conventions are sorted by selector, the only possibility is
1323 // that the types differ enough to cause one selector or the other
1324 // to fall out of the family.
1325 assert(implFamily == OMF_None || declFamily == OMF_None);
1326
1327 // No further diagnostics required on invalid declarations.
1328 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1329
1330 const ObjCMethodDecl *unmatched = impl;
1331 ObjCMethodFamily family = declFamily;
1332 unsigned errorID = diag::err_arc_lost_method_convention;
1333 unsigned noteID = diag::note_arc_lost_method_convention;
1334 if (declFamily == OMF_None) {
1335 unmatched = decl;
1336 family = implFamily;
1337 errorID = diag::err_arc_gained_method_convention;
1338 noteID = diag::note_arc_gained_method_convention;
1339 }
1340
1341 // Indexes into a %select clause in the diagnostic.
1342 enum FamilySelector {
1343 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1344 };
1345 FamilySelector familySelector = FamilySelector();
1346
1347 switch (family) {
1348 case OMF_None: llvm_unreachable("logic error, no method convention");
1349 case OMF_retain:
1350 case OMF_release:
1351 case OMF_autorelease:
1352 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001353 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001354 case OMF_retainCount:
1355 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001356 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001357 // Mismatches for these methods don't change ownership
1358 // conventions, so we don't care.
1359 return false;
1360
1361 case OMF_init: familySelector = F_init; break;
1362 case OMF_alloc: familySelector = F_alloc; break;
1363 case OMF_copy: familySelector = F_copy; break;
1364 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1365 case OMF_new: familySelector = F_new; break;
1366 }
1367
1368 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1369 ReasonSelector reasonSelector;
1370
1371 // The only reason these methods don't fall within their families is
1372 // due to unusual result types.
1373 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1374 reasonSelector = R_UnrelatedReturn;
1375 } else {
1376 reasonSelector = R_NonObjectReturn;
1377 }
1378
1379 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1380 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1381
1382 return true;
1383}
John McCall10302c02010-10-28 02:34:38 +00001384
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001385void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001386 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001387 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001388 if (getLangOptions().ObjCAutoRefCount &&
1389 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1390 return;
1391
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001392 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001393 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001394 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Chris Lattner3aff9192009-04-11 19:58:42 +00001396 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001397 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001398 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001399 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001400 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001401 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001402
Fariborz Jahanian21121902011-08-08 18:03:17 +00001403 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001404 Diag(ImpMethodDecl->getLocation(),
1405 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001406 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001407 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001408}
1409
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001410void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1411 ObjCMethodDecl *Overridden,
1412 bool IsProtocolMethodDecl) {
1413
1414 CheckMethodOverrideReturn(*this, Method, Overridden,
1415 IsProtocolMethodDecl, true,
1416 true);
1417
1418 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1419 IF = Overridden->param_begin(), EM = Method->param_end();
1420 IM != EM; ++IM, ++IF) {
1421 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1422 IsProtocolMethodDecl, true, true);
1423 }
1424
1425 if (Method->isVariadic() != Overridden->isVariadic()) {
1426 Diag(Method->getLocation(),
1427 diag::warn_conflicting_overriding_variadic);
1428 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1429 }
1430}
1431
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001432/// WarnExactTypedMethods - This routine issues a warning if method
1433/// implementation declaration matches exactly that of its declaration.
1434void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1435 ObjCMethodDecl *MethodDecl,
1436 bool IsProtocolMethodDecl) {
1437 // don't issue warning when protocol method is optional because primary
1438 // class is not required to implement it and it is safe for protocol
1439 // to implement it.
1440 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1441 return;
1442 // don't issue warning when primary class's method is
1443 // depecated/unavailable.
1444 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1445 MethodDecl->hasAttr<DeprecatedAttr>())
1446 return;
1447
1448 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1449 IsProtocolMethodDecl, false, false);
1450 if (match)
1451 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1452 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1453 IM != EM; ++IM, ++IF) {
1454 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1455 *IM, *IF,
1456 IsProtocolMethodDecl, false, false);
1457 if (!match)
1458 break;
1459 }
1460 if (match)
1461 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001462 if (match)
1463 match = !(MethodDecl->isClassMethod() &&
1464 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001465
1466 if (match) {
1467 Diag(ImpMethodDecl->getLocation(),
1468 diag::warn_category_method_impl_match);
1469 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1470 }
1471}
1472
Mike Stump390b4cc2009-05-16 07:39:55 +00001473/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1474/// improve the efficiency of selector lookups and type checking by associating
1475/// with each protocol / interface / category the flattened instance tables. If
1476/// we used an immutable set to keep the table then it wouldn't add significant
1477/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001478
Steve Naroffefe7f362008-02-08 22:06:17 +00001479/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001480/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001481void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1482 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001483 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001484 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001485 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001486 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001487 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1488 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1489 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001490 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1491
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001492 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001493 ObjCInterfaceDecl *NSIDecl = 0;
1494 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001495 // check to see if class implements forwardInvocation method and objects
1496 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001497 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001498 // Under such conditions, which means that every method possible is
1499 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001500 // found" warnings.
1501 // FIXME: Use a general GetUnarySelector method for this.
1502 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1503 Selector fISelector = Context.Selectors.getSelector(1, &II);
1504 if (InsMap.count(fISelector))
1505 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1506 // need be implemented in the implementation.
1507 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001510 // If a method lookup fails locally we still need to look and see if
1511 // the method was implemented by a base class or an inherited
1512 // protocol. This lookup is slow, but occurs rarely in correct code
1513 // and otherwise would terminate in a warning.
1514
Chris Lattner4d391482007-12-12 07:09:47 +00001515 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001516 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001517 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001518 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001519 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001520 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001521 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001522 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001523 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001524 // If a method is not implemented in the category implementation but
1525 // has been declared in its primary class, superclass,
1526 // or in one of their protocols, no need to issue the warning.
1527 // This is because method will be implemented in the primary class
1528 // or one of its super class implementation.
1529
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001530 // Ugly, but necessary. Method declared in protcol might have
1531 // have been synthesized due to a property declared in the class which
1532 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001533 if (ObjCMethodDecl *MethodInClass =
1534 IDecl->lookupInstanceMethod(method->getSelector(),
1535 true /*noCategoryLookup*/))
1536 if (C || MethodInClass->isSynthesized())
1537 continue;
1538 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1539 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1540 != DiagnosticsEngine::Ignored) {
1541 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1542 Diag(method->getLocation(), diag::note_method_declared_at);
1543 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1544 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001545 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001546 }
1547 }
Chris Lattner4d391482007-12-12 07:09:47 +00001548 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001549 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001550 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001551 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001552 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001553 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1554 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001555 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001556 // See above comment for instance method lookups.
1557 if (C && IDecl->lookupClassMethod(method->getSelector(),
1558 true /*noCategoryLookup*/))
1559 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001560 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001561 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1562 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001563 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001564 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001565 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1566 PDecl->getDeclName();
1567 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001568 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001569 }
Chris Lattner780f3292008-07-21 21:32:27 +00001570 // Check on this protocols's referenced protocols, recursively.
1571 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1572 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001573 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001574}
1575
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001576/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001577/// or protocol against those declared in their implementations.
1578///
1579void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1580 const llvm::DenseSet<Selector> &ClsMap,
1581 llvm::DenseSet<Selector> &InsMapSeen,
1582 llvm::DenseSet<Selector> &ClsMapSeen,
1583 ObjCImplDecl* IMPDecl,
1584 ObjCContainerDecl* CDecl,
1585 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001586 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001587 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001588 // Check and see if instance methods in class interface have been
1589 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001590 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1591 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001592 if (InsMapSeen.count((*I)->getSelector()))
1593 continue;
1594 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001595 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001596 !InsMap.count((*I)->getSelector())) {
1597 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001598 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1599 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001600 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001601 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001602 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001603 IMPDecl->getInstanceMethod((*I)->getSelector());
1604 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1605 "Expected to find the method through lookup as well");
1606 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001607 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001608 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001609 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001610 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1611 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001612 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001613 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001614 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001615 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001616 }
1617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001619 // Check and see if class methods in class interface have been
1620 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001621 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001622 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001623 if (ClsMapSeen.count((*I)->getSelector()))
1624 continue;
1625 ClsMapSeen.insert((*I)->getSelector());
1626 if (!ClsMap.count((*I)->getSelector())) {
1627 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001628 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1629 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001630 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001631 ObjCMethodDecl *ImpMethodDecl =
1632 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001633 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1634 "Expected to find the method through lookup as well");
1635 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001636 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001637 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1638 isa<ObjCProtocolDecl>(CDecl));
1639 else
1640 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001641 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001642 }
1643 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001644
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001645 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001646 // Also methods in class extensions need be looked at next.
1647 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1648 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1649 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1650 IMPDecl,
1651 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001652 IncompleteImpl, false,
1653 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001654
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001655 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001656 for (ObjCInterfaceDecl::all_protocol_iterator
1657 PI = I->all_referenced_protocol_begin(),
1658 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001659 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1660 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001661 (*PI), IncompleteImpl, false,
1662 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001663
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001664 // FIXME. For now, we are not checking for extact match of methods
1665 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001666 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001667 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001668 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001669 I->getSuperClass(), IncompleteImpl, false);
1670 }
1671}
1672
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001673/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1674/// category matches with those implemented in its primary class and
1675/// warns each time an exact match is found.
1676void Sema::CheckCategoryVsClassMethodMatches(
1677 ObjCCategoryImplDecl *CatIMPDecl) {
1678 llvm::DenseSet<Selector> InsMap, ClsMap;
1679
1680 for (ObjCImplementationDecl::instmeth_iterator
1681 I = CatIMPDecl->instmeth_begin(),
1682 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1683 InsMap.insert((*I)->getSelector());
1684
1685 for (ObjCImplementationDecl::classmeth_iterator
1686 I = CatIMPDecl->classmeth_begin(),
1687 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1688 ClsMap.insert((*I)->getSelector());
1689 if (InsMap.empty() && ClsMap.empty())
1690 return;
1691
1692 // Get category's primary class.
1693 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1694 if (!CatDecl)
1695 return;
1696 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1697 if (!IDecl)
1698 return;
1699 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1700 bool IncompleteImpl = false;
1701 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1702 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001703 IncompleteImpl, false,
1704 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001705}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001706
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001707void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001708 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001709 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001710 llvm::DenseSet<Selector> InsMap;
1711 // Check and see if instance methods in class interface have been
1712 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001713 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001714 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001715 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001717 // Check and see if properties declared in the interface have either 1)
1718 // an implementation or 2) there is a @synthesize/@dynamic implementation
1719 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001720 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1721 if (!(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2) ||
Ted Kremenek71207fc2012-01-05 22:47:47 +00001722 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001723 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001724
Chris Lattner4d391482007-12-12 07:09:47 +00001725 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001726 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001727 I = IMPDecl->classmeth_begin(),
1728 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001729 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001731 // Check for type conflict of methods declared in a class/protocol and
1732 // its implementation; if any.
1733 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001734 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1735 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001736 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001737
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001738 // check all methods implemented in category against those declared
1739 // in its primary class.
1740 if (ObjCCategoryImplDecl *CatDecl =
1741 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1742 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Chris Lattner4d391482007-12-12 07:09:47 +00001744 // Check the protocol list for unimplemented methods in the @implementation
1745 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001746 // Check and see if class methods in class interface have been
1747 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Chris Lattnercddc8882009-03-01 00:56:52 +00001749 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001750 for (ObjCInterfaceDecl::all_protocol_iterator
1751 PI = I->all_referenced_protocol_begin(),
1752 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001753 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001754 InsMap, ClsMap, I);
1755 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001756 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1757 Categories; Categories = Categories->getNextClassExtension())
1758 ImplMethodsVsClassMethods(S, IMPDecl,
1759 const_cast<ObjCCategoryDecl*>(Categories),
1760 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001761 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001762 // For extended class, unimplemented methods in its protocols will
1763 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001764 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001765 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1766 E = C->protocol_end(); PI != E; ++PI)
1767 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001768 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001769 // Report unimplemented properties in the category as well.
1770 // When reporting on missing setter/getters, do not report when
1771 // setter/getter is implemented in category's primary class
1772 // implementation.
1773 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1774 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1775 for (ObjCImplementationDecl::instmeth_iterator
1776 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1777 InsMap.insert((*I)->getSelector());
1778 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001779 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001780 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001781 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001782 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001783}
1784
Mike Stump1eb44332009-09-09 15:08:12 +00001785/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001786Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001787Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001788 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001789 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001790 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001791 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001792 for (unsigned i = 0; i != NumElts; ++i) {
1793 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001794 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001795 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001796 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001797 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001798 // Maybe we will complain about the shadowed template parameter.
1799 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1800 // Just pretend that we didn't see the previous declaration.
1801 PrevDecl = 0;
1802 }
1803
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001804 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001805 // GCC apparently allows the following idiom:
1806 //
1807 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1808 // @class XCElementToggler;
1809 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001810 // Here we have chosen to ignore the forward class declaration
1811 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001812 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001813 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001814 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001815 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001816 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001817 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001818 // to the underlying class. Just ignore the forward class with a warning
1819 // as this will force the intended behavior which is to lookup the typedef
1820 // name.
1821 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1822 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1823 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1824 continue;
1825 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001826 }
Chris Lattner4d391482007-12-12 07:09:47 +00001827 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001828
1829 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001830 ObjCInterfaceDecl *PrevIDecl
1831 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001832 ObjCInterfaceDecl *IDecl
1833 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001834 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001835 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001836
Douglas Gregor7723fec2011-12-15 20:29:51 +00001837 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001838 CheckObjCDeclScope(IDecl);
1839 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001840 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001841
1842 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001843}
1844
John McCall0f4c4c42011-06-16 01:15:19 +00001845static bool tryMatchRecordTypes(ASTContext &Context,
1846 Sema::MethodMatchStrategy strategy,
1847 const Type *left, const Type *right);
1848
John McCallf85e1932011-06-15 23:02:42 +00001849static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1850 QualType leftQT, QualType rightQT) {
1851 const Type *left =
1852 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1853 const Type *right =
1854 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1855
1856 if (left == right) return true;
1857
1858 // If we're doing a strict match, the types have to match exactly.
1859 if (strategy == Sema::MMS_strict) return false;
1860
1861 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1862
1863 // Otherwise, use this absurdly complicated algorithm to try to
1864 // validate the basic, low-level compatibility of the two types.
1865
1866 // As a minimum, require the sizes and alignments to match.
1867 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1868 return false;
1869
1870 // Consider all the kinds of non-dependent canonical types:
1871 // - functions and arrays aren't possible as return and parameter types
1872
1873 // - vector types of equal size can be arbitrarily mixed
1874 if (isa<VectorType>(left)) return isa<VectorType>(right);
1875 if (isa<VectorType>(right)) return false;
1876
1877 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001878 // - structs, unions, and Objective-C objects must match more-or-less
1879 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001880 // - everything else should be a scalar
1881 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001882 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001883
John McCall1d9b3b22011-09-09 05:25:32 +00001884 // Make scalars agree in kind, except count bools as chars, and group
1885 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001886 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1887 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1888 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1889 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001890 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1891 leftSK = Type::STK_ObjCObjectPointer;
1892 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1893 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001894
1895 // Note that data member pointers and function member pointers don't
1896 // intermix because of the size differences.
1897
1898 return (leftSK == rightSK);
1899}
Chris Lattner4d391482007-12-12 07:09:47 +00001900
John McCall0f4c4c42011-06-16 01:15:19 +00001901static bool tryMatchRecordTypes(ASTContext &Context,
1902 Sema::MethodMatchStrategy strategy,
1903 const Type *lt, const Type *rt) {
1904 assert(lt && rt && lt != rt);
1905
1906 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1907 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1908 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1909
1910 // Require union-hood to match.
1911 if (left->isUnion() != right->isUnion()) return false;
1912
1913 // Require an exact match if either is non-POD.
1914 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1915 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1916 return false;
1917
1918 // Require size and alignment to match.
1919 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1920
1921 // Require fields to match.
1922 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1923 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1924 for (; li != le && ri != re; ++li, ++ri) {
1925 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1926 return false;
1927 }
1928 return (li == le && ri == re);
1929}
1930
Chris Lattner4d391482007-12-12 07:09:47 +00001931/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1932/// returns true, or false, accordingly.
1933/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001934bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1935 const ObjCMethodDecl *right,
1936 MethodMatchStrategy strategy) {
1937 if (!matchTypes(Context, strategy,
1938 left->getResultType(), right->getResultType()))
1939 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001940
John McCallf85e1932011-06-15 23:02:42 +00001941 if (getLangOptions().ObjCAutoRefCount &&
1942 (left->hasAttr<NSReturnsRetainedAttr>()
1943 != right->hasAttr<NSReturnsRetainedAttr>() ||
1944 left->hasAttr<NSConsumesSelfAttr>()
1945 != right->hasAttr<NSConsumesSelfAttr>()))
1946 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001948 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001949 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001950
John McCallf85e1932011-06-15 23:02:42 +00001951 for (; li != le; ++li, ++ri) {
1952 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001953 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001954
1955 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1956 return false;
1957
1958 if (getLangOptions().ObjCAutoRefCount &&
1959 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1960 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001961 }
1962 return true;
1963}
1964
Douglas Gregor5ac4b692012-01-25 00:49:42 +00001965void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00001966 // If the list is empty, make it a singleton list.
1967 if (List->Method == 0) {
1968 List->Method = Method;
1969 List->Next = 0;
1970 return;
1971 }
1972
1973 // We've seen a method with this name, see if we have already seen this type
1974 // signature.
1975 ObjCMethodList *Previous = List;
1976 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00001977 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00001978 continue;
1979
1980 ObjCMethodDecl *PrevObjCMethod = List->Method;
1981
1982 // Propagate the 'defined' bit.
1983 if (Method->isDefined())
1984 PrevObjCMethod->setDefined(true);
1985
1986 // If a method is deprecated, push it in the global pool.
1987 // This is used for better diagnostics.
1988 if (Method->isDeprecated()) {
1989 if (!PrevObjCMethod->isDeprecated())
1990 List->Method = Method;
1991 }
1992 // If new method is unavailable, push it into global pool
1993 // unless previous one is deprecated.
1994 if (Method->isUnavailable()) {
1995 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
1996 List->Method = Method;
1997 }
1998
1999 return;
2000 }
2001
2002 // We have a new signature for an existing method - add it.
2003 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002004 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002005 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2006}
2007
Sebastian Redldb9d2142010-08-02 23:18:59 +00002008/// \brief Read the contents of the method pool for a given selector from
2009/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002010void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002011 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002012 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002013}
2014
Sebastian Redldb9d2142010-08-02 23:18:59 +00002015void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
2016 bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002017 if (ExternalSource)
2018 ReadMethodPool(Method->getSelector());
2019
Sebastian Redldb9d2142010-08-02 23:18:59 +00002020 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002021 if (Pos == MethodPool.end())
2022 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2023 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002024
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002025 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002026
Sebastian Redldb9d2142010-08-02 23:18:59 +00002027 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002028 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002029}
2030
John McCallf85e1932011-06-15 23:02:42 +00002031/// Determines if this is an "acceptable" loose mismatch in the global
2032/// method pool. This exists mostly as a hack to get around certain
2033/// global mismatches which we can't afford to make warnings / errors.
2034/// Really, what we want is a way to take a method out of the global
2035/// method pool.
2036static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2037 ObjCMethodDecl *other) {
2038 if (!chosen->isInstanceMethod())
2039 return false;
2040
2041 Selector sel = chosen->getSelector();
2042 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2043 return false;
2044
2045 // Don't complain about mismatches for -length if the method we
2046 // chose has an integral result type.
2047 return (chosen->getResultType()->isIntegerType());
2048}
2049
Sebastian Redldb9d2142010-08-02 23:18:59 +00002050ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002051 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002052 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002053 if (ExternalSource)
2054 ReadMethodPool(Sel);
2055
Sebastian Redldb9d2142010-08-02 23:18:59 +00002056 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002057 if (Pos == MethodPool.end())
2058 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002059
Sebastian Redldb9d2142010-08-02 23:18:59 +00002060 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Sebastian Redldb9d2142010-08-02 23:18:59 +00002062 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002063 bool issueDiagnostic = false, issueError = false;
2064
2065 // We support a warning which complains about *any* difference in
2066 // method signature.
2067 bool strictSelectorMatch =
2068 (receiverIdOrClass && warn &&
2069 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2070 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002071 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002072 if (strictSelectorMatch)
2073 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002074 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2075 MMS_strict)) {
2076 issueDiagnostic = true;
2077 break;
2078 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002079 }
2080
John McCallf85e1932011-06-15 23:02:42 +00002081 // If we didn't see any strict differences, we won't see any loose
2082 // differences. In ARC, however, we also need to check for loose
2083 // mismatches, because most of them are errors.
2084 if (!strictSelectorMatch ||
2085 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002086 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002087 // This checks if the methods differ in type mismatch.
2088 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2089 MMS_loose) &&
2090 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2091 issueDiagnostic = true;
2092 if (getLangOptions().ObjCAutoRefCount)
2093 issueError = true;
2094 break;
2095 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002096 }
2097
John McCallf85e1932011-06-15 23:02:42 +00002098 if (issueDiagnostic) {
2099 if (issueError)
2100 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2101 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002102 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2103 else
2104 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002105
2106 Diag(MethList.Method->getLocStart(),
2107 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002108 << MethList.Method->getSourceRange();
2109 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2110 Diag(Next->Method->getLocStart(), diag::note_also_found)
2111 << Next->Method->getSourceRange();
2112 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002113 }
2114 return MethList.Method;
2115}
2116
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002117ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002118 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2119 if (Pos == MethodPool.end())
2120 return 0;
2121
2122 GlobalMethods &Methods = Pos->second;
2123
2124 if (Methods.first.Method && Methods.first.Method->isDefined())
2125 return Methods.first.Method;
2126 if (Methods.second.Method && Methods.second.Method->isDefined())
2127 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002128 return 0;
2129}
2130
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002131/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2132/// identical selector names in current and its super classes and issues
2133/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002134void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2135 ObjCMethodDecl *Method,
2136 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002137 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2138 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002139
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002140 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002141 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002142 SD->lookupMethod(Method->getSelector(), IsInstance);
2143 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002144 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002145 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002146 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002147 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2148 E = Method->param_end();
2149 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2150 for (; ParamI != E; ++ParamI, ++PrevI) {
2151 // Number of parameters are the same and is guaranteed by selector match.
2152 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2153 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2154 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002155 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002156 // respective argument type in the super class method, issue warning;
2157 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002158 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002159 << T1 << T2;
2160 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2161 return;
2162 }
2163 }
2164 ID = SD;
2165 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002166}
2167
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002168/// DiagnoseDuplicateIvars -
2169/// Check for duplicate ivars in the entire class at the start of
2170/// @implementation. This becomes necesssary because class extension can
2171/// add ivars to a class in random order which will not be known until
2172/// class's @implementation is seen.
2173void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2174 ObjCInterfaceDecl *SID) {
2175 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2176 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2177 ObjCIvarDecl* Ivar = (*IVI);
2178 if (Ivar->isInvalidDecl())
2179 continue;
2180 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2181 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2182 if (prevIvar) {
2183 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2184 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2185 Ivar->setInvalidDecl();
2186 }
2187 }
2188 }
2189}
2190
Erik Verbruggend64251f2011-12-06 09:25:23 +00002191Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2192 switch (CurContext->getDeclKind()) {
2193 case Decl::ObjCInterface:
2194 return Sema::OCK_Interface;
2195 case Decl::ObjCProtocol:
2196 return Sema::OCK_Protocol;
2197 case Decl::ObjCCategory:
2198 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2199 return Sema::OCK_ClassExtension;
2200 else
2201 return Sema::OCK_Category;
2202 case Decl::ObjCImplementation:
2203 return Sema::OCK_Implementation;
2204 case Decl::ObjCCategoryImpl:
2205 return Sema::OCK_CategoryImplementation;
2206
2207 default:
2208 return Sema::OCK_None;
2209 }
2210}
2211
Steve Naroffa56f6162007-12-18 01:30:32 +00002212// Note: For class/category implemenations, allMethods/allProperties is
2213// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002214Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2215 Decl **allMethods, unsigned allNum,
2216 Decl **allProperties, unsigned pNum,
2217 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002218
Erik Verbruggend64251f2011-12-06 09:25:23 +00002219 if (getObjCContainerKind() == Sema::OCK_None)
2220 return 0;
2221
2222 assert(AtEnd.isValid() && "Invalid location for '@end'");
2223
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002224 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2225 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002226
Mike Stump1eb44332009-09-09 15:08:12 +00002227 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002228 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2229 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002230 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002231
Steve Naroff0701bbb2009-01-08 17:28:14 +00002232 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2233 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2234 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2235
Chris Lattner4d391482007-12-12 07:09:47 +00002236 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002237 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002238 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002239
2240 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002241 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002242 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002243 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002244 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002245 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002246 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002247 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002248 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002249 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002250 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002251 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002252 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002253 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002254 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002255 if (!Context.getSourceManager().isInSystemHeader(
2256 Method->getLocation()))
2257 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2258 << Method->getDeclName();
2259 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2260 }
Chris Lattner4d391482007-12-12 07:09:47 +00002261 InsMap[Method->getSelector()] = Method;
2262 /// The following allows us to typecheck messages to "id".
2263 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002264 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002265 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002266 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002267 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002268 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002269 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002270 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002271 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002272 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002273 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002274 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002275 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002276 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002277 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002278 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002279 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002280 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002281 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002282 if (!Context.getSourceManager().isInSystemHeader(
2283 Method->getLocation()))
2284 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2285 << Method->getDeclName();
2286 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2287 }
Chris Lattner4d391482007-12-12 07:09:47 +00002288 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002289 /// The following allows us to typecheck messages to "Class".
2290 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002291 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002292 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002293 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002294 }
2295 }
2296 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002297 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002298 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002299 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002300 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002301 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002302 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002303 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002304 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002305 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002306
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002307 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002308 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002309 if (C->IsClassExtension()) {
2310 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2311 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002312 }
Chris Lattner4d391482007-12-12 07:09:47 +00002313 }
Steve Naroff09c47192009-01-09 15:36:25 +00002314 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002315 if (CDecl->getIdentifier())
2316 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2317 // user-defined setter/getter. It also synthesizes setter/getter methods
2318 // and adds them to the DeclContext and global method pools.
2319 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2320 E = CDecl->prop_end();
2321 I != E; ++I)
2322 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002323 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002324 }
2325 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002326 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002327 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002328 // Any property declared in a class extension might have user
2329 // declared setter or getter in current class extension or one
2330 // of the other class extensions. Mark them as synthesized as
2331 // property will be synthesized when property with same name is
2332 // seen in the @implementation.
2333 for (const ObjCCategoryDecl *ClsExtDecl =
2334 IDecl->getFirstClassExtension();
2335 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2336 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2337 E = ClsExtDecl->prop_end(); I != E; ++I) {
2338 ObjCPropertyDecl *Property = (*I);
2339 // Skip over properties declared @dynamic
2340 if (const ObjCPropertyImplDecl *PIDecl
2341 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2342 if (PIDecl->getPropertyImplementation()
2343 == ObjCPropertyImplDecl::Dynamic)
2344 continue;
2345
2346 for (const ObjCCategoryDecl *CExtDecl =
2347 IDecl->getFirstClassExtension();
2348 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2349 if (ObjCMethodDecl *GetterMethod =
2350 CExtDecl->getInstanceMethod(Property->getGetterName()))
2351 GetterMethod->setSynthesized(true);
2352 if (!Property->isReadOnly())
2353 if (ObjCMethodDecl *SetterMethod =
2354 CExtDecl->getInstanceMethod(Property->getSetterName()))
2355 SetterMethod->setSynthesized(true);
2356 }
2357 }
2358 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002359 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002360 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002361 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002362
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002363 if (LangOpts.ObjCNonFragileABI2)
2364 while (IDecl->getSuperClass()) {
2365 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2366 IDecl = IDecl->getSuperClass();
2367 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002368 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002369 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002370 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002371 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002372 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002373
Chris Lattner4d391482007-12-12 07:09:47 +00002374 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002375 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002376 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002377 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002378 Categories; Categories = Categories->getNextClassCategory()) {
2379 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002380 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002381 break;
2382 }
2383 }
2384 }
2385 }
Chris Lattner682bf922009-03-29 16:50:03 +00002386 if (isInterfaceDeclKind) {
2387 // Reject invalid vardecls.
2388 for (unsigned i = 0; i != tuvNum; i++) {
2389 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2390 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2391 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002392 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002393 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002394 }
Chris Lattner682bf922009-03-29 16:50:03 +00002395 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002396 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002397 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002398
2399 for (unsigned i = 0; i != tuvNum; i++) {
2400 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002401 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2402 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002403 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2404 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002405
2406 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002407}
2408
2409
2410/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2411/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002412static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002413CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002414 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002415}
2416
Ted Kremenek422bae72010-04-18 04:59:38 +00002417static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002418bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2419 const AttrVec &A) {
2420 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002421 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002422 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002423 return false;
2424
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002425 // method declared in interface has no attribute.
2426 // But implementation has attributes. This is invalid
2427 if (!IMD->hasAttrs())
2428 return true;
2429
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002430 const AttrVec &D = IMD->getAttrs();
2431 if (D.size() != A.size())
2432 return true;
2433
2434 // attributes on method declaration and definition must match exactly.
2435 // Note that we have at most a couple of attributes on methods, so this
2436 // n*n search is good enough.
2437 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2438 bool match = false;
2439 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2440 if ((*i)->getKind() == (*i1)->getKind()) {
2441 match = true;
2442 break;
2443 }
2444 }
2445 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002446 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002447 }
Sean Huntcf807c42010-08-18 23:23:40 +00002448 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002449}
2450
Douglas Gregore97179c2011-09-08 01:46:34 +00002451namespace {
2452 /// \brief Describes the compatibility of a result type with its method.
2453 enum ResultTypeCompatibilityKind {
2454 RTC_Compatible,
2455 RTC_Incompatible,
2456 RTC_Unknown
2457 };
2458}
2459
Douglas Gregor926df6c2011-06-11 01:09:30 +00002460/// \brief Check whether the declared result type of the given Objective-C
2461/// method declaration is compatible with the method's class.
2462///
Douglas Gregore97179c2011-09-08 01:46:34 +00002463static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002464CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2465 ObjCInterfaceDecl *CurrentClass) {
2466 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002467
2468 // If an Objective-C method inherits its related result type, then its
2469 // declared result type must be compatible with its own class type. The
2470 // declared result type is compatible if:
2471 if (const ObjCObjectPointerType *ResultObjectType
2472 = ResultType->getAs<ObjCObjectPointerType>()) {
2473 // - it is id or qualified id, or
2474 if (ResultObjectType->isObjCIdType() ||
2475 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002476 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002477
2478 if (CurrentClass) {
2479 if (ObjCInterfaceDecl *ResultClass
2480 = ResultObjectType->getInterfaceDecl()) {
2481 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002482 if (declaresSameEntity(CurrentClass, ResultClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002483 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002484
2485 // - it is a superclass of the method's class type
2486 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002487 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002488 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002489 } else {
2490 // Any Objective-C pointer type might be acceptable for a protocol
2491 // method; we just don't know.
2492 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002493 }
2494 }
2495
Douglas Gregore97179c2011-09-08 01:46:34 +00002496 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002497}
2498
John McCall6c2c2502011-07-22 02:45:48 +00002499namespace {
2500/// A helper class for searching for methods which a particular method
2501/// overrides.
2502class OverrideSearch {
2503 Sema &S;
2504 ObjCMethodDecl *Method;
2505 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2506 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2507 bool Recursive;
2508
2509public:
2510 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2511 Selector selector = method->getSelector();
2512
2513 // Bypass this search if we've never seen an instance/class method
2514 // with this selector before.
2515 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2516 if (it == S.MethodPool.end()) {
2517 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002518 S.ReadMethodPool(selector);
2519
2520 it = S.MethodPool.find(selector);
2521 if (it == S.MethodPool.end())
2522 return;
John McCall6c2c2502011-07-22 02:45:48 +00002523 }
2524 ObjCMethodList &list =
2525 method->isInstanceMethod() ? it->second.first : it->second.second;
2526 if (!list.Method) return;
2527
2528 ObjCContainerDecl *container
2529 = cast<ObjCContainerDecl>(method->getDeclContext());
2530
2531 // Prevent the search from reaching this container again. This is
2532 // important with categories, which override methods from the
2533 // interface and each other.
2534 Searched.insert(container);
2535 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002536 }
John McCall6c2c2502011-07-22 02:45:48 +00002537
2538 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2539 iterator begin() const { return Overridden.begin(); }
2540 iterator end() const { return Overridden.end(); }
2541
2542private:
2543 void searchFromContainer(ObjCContainerDecl *container) {
2544 if (container->isInvalidDecl()) return;
2545
2546 switch (container->getDeclKind()) {
2547#define OBJCCONTAINER(type, base) \
2548 case Decl::type: \
2549 searchFrom(cast<type##Decl>(container)); \
2550 break;
2551#define ABSTRACT_DECL(expansion)
2552#define DECL(type, base) \
2553 case Decl::type:
2554#include "clang/AST/DeclNodes.inc"
2555 llvm_unreachable("not an ObjC container!");
2556 }
2557 }
2558
2559 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002560 if (!protocol->hasDefinition())
2561 return;
2562
John McCall6c2c2502011-07-22 02:45:48 +00002563 // A method in a protocol declaration overrides declarations from
2564 // referenced ("parent") protocols.
2565 search(protocol->getReferencedProtocols());
2566 }
2567
2568 void searchFrom(ObjCCategoryDecl *category) {
2569 // A method in a category declaration overrides declarations from
2570 // the main class and from protocols the category references.
2571 search(category->getClassInterface());
2572 search(category->getReferencedProtocols());
2573 }
2574
2575 void searchFrom(ObjCCategoryImplDecl *impl) {
2576 // A method in a category definition that has a category
2577 // declaration overrides declarations from the category
2578 // declaration.
2579 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2580 search(category);
2581
2582 // Otherwise it overrides declarations from the class.
2583 } else {
2584 search(impl->getClassInterface());
2585 }
2586 }
2587
2588 void searchFrom(ObjCInterfaceDecl *iface) {
2589 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002590 if (!iface->hasDefinition())
2591 return;
2592
John McCall6c2c2502011-07-22 02:45:48 +00002593 // - categories,
2594 for (ObjCCategoryDecl *category = iface->getCategoryList();
2595 category; category = category->getNextClassCategory())
2596 search(category);
2597
2598 // - the super class, and
2599 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2600 search(super);
2601
2602 // - any referenced protocols.
2603 search(iface->getReferencedProtocols());
2604 }
2605
2606 void searchFrom(ObjCImplementationDecl *impl) {
2607 // A method in a class implementation overrides declarations from
2608 // the class interface.
2609 search(impl->getClassInterface());
2610 }
2611
2612
2613 void search(const ObjCProtocolList &protocols) {
2614 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2615 i != e; ++i)
2616 search(*i);
2617 }
2618
2619 void search(ObjCContainerDecl *container) {
2620 // Abort if we've already searched this container.
2621 if (!Searched.insert(container)) return;
2622
2623 // Check for a method in this container which matches this selector.
2624 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2625 Method->isInstanceMethod());
2626
2627 // If we find one, record it and bail out.
2628 if (meth) {
2629 Overridden.insert(meth);
2630 return;
2631 }
2632
2633 // Otherwise, search for methods that a hypothetical method here
2634 // would have overridden.
2635
2636 // Note that we're now in a recursive case.
2637 Recursive = true;
2638
2639 searchFromContainer(container);
2640 }
2641};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002642}
2643
John McCalld226f652010-08-21 09:40:31 +00002644Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002645 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002646 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002647 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002648 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002649 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002650 Selector Sel,
2651 // optional arguments. The number of types/arguments is obtained
2652 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002653 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002654 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002655 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002656 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002657 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002658 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002659 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002660 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002661 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002662 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2663 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002664 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002665
Douglas Gregore97179c2011-09-08 01:46:34 +00002666 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002667 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002668 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002669 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002670
Steve Naroffccef3712009-02-20 22:59:16 +00002671 // Methods cannot return interface types. All ObjC objects are
2672 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002673 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002674 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2675 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002676 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002677 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002678
2679 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002680 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002681 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002682 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002683 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002684 }
Mike Stump1eb44332009-09-09 15:08:12 +00002685
2686 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002687 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002688 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002689 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002690 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002691 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002692 /*isSynthesized=*/false,
2693 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002694 MethodDeclKind == tok::objc_optional
2695 ? ObjCMethodDecl::Optional
2696 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002697 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Chris Lattner5f9e2722011-07-23 10:55:15 +00002699 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002700
Chris Lattner7db638d2009-04-11 19:42:43 +00002701 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002702 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002703 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Chris Lattnere294d3f2009-04-11 18:57:04 +00002705 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002706 ArgType = Context.getObjCIdType();
2707 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002708 } else {
John McCall58e46772009-10-23 21:48:59 +00002709 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002710 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002711 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002712 }
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002714 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2715 LookupOrdinaryName, ForRedeclaration);
2716 LookupName(R, S);
2717 if (R.isSingleResult()) {
2718 NamedDecl *PrevDecl = R.getFoundDecl();
2719 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002720 Diag(ArgInfo[i].NameLoc,
2721 (MethodDefinition ? diag::warn_method_param_redefinition
2722 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002723 << ArgInfo[i].Name;
2724 Diag(PrevDecl->getLocation(),
2725 diag::note_previous_declaration);
2726 }
2727 }
2728
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002729 SourceLocation StartLoc = DI
2730 ? DI->getTypeLoc().getBeginLoc()
2731 : ArgInfo[i].NameLoc;
2732
John McCall81ef3e62011-04-23 02:46:06 +00002733 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2734 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2735 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002736
John McCall70798862011-05-02 00:30:12 +00002737 Param->setObjCMethodScopeInfo(i);
2738
Chris Lattner0ed844b2008-04-04 06:12:32 +00002739 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002740 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002741
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002742 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002743 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002745 if (Param->hasAttr<BlocksAttr>()) {
2746 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2747 Param->setInvalidDecl();
2748 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002749 S->AddDecl(Param);
2750 IdResolver.AddDecl(Param);
2751
Chris Lattner0ed844b2008-04-04 06:12:32 +00002752 Params.push_back(Param);
2753 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002754
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002755 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002756 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002757 QualType ArgType = Param->getType();
2758 if (ArgType.isNull())
2759 ArgType = Context.getObjCIdType();
2760 else
2761 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002762 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002763 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002764 Diag(Param->getLocation(),
2765 diag::err_object_cannot_be_passed_returned_by_value)
2766 << 1 << ArgType;
2767 Param->setInvalidDecl();
2768 }
2769 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002770
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002771 Params.push_back(Param);
2772 }
2773
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002774 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002775 ObjCMethod->setObjCDeclQualifier(
2776 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002777
2778 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002779 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002780
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002781 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002782 const ObjCMethodDecl *PrevMethod = 0;
2783 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002784 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002785 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2786 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002787 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002788 PrevMethod = ImpDecl->getClassMethod(Sel);
2789 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002790 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002791
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002792 ObjCMethodDecl *IMD = 0;
2793 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2794 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2795 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002796 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002797 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002798 SourceLocation MethodLoc = IMD->getLocation();
2799 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2800 Diag(EndLoc, diag::warn_attribute_method_def);
2801 Diag(MethodLoc, diag::note_method_declared_at);
2802 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002803 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002804 } else {
2805 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002806 }
John McCall6c2c2502011-07-22 02:45:48 +00002807
Chris Lattner4d391482007-12-12 07:09:47 +00002808 if (PrevMethod) {
2809 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002810 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002811 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002812 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002813 }
John McCall54abf7d2009-11-04 02:18:39 +00002814
Douglas Gregor926df6c2011-06-11 01:09:30 +00002815 // If this Objective-C method does not have a related result type, but we
2816 // are allowed to infer related result types, try to do so based on the
2817 // method family.
2818 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2819 if (!CurrentClass) {
2820 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2821 CurrentClass = Cat->getClassInterface();
2822 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2823 CurrentClass = Impl->getClassInterface();
2824 else if (ObjCCategoryImplDecl *CatImpl
2825 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2826 CurrentClass = CatImpl->getClassInterface();
2827 }
John McCall6c2c2502011-07-22 02:45:48 +00002828
Douglas Gregore97179c2011-09-08 01:46:34 +00002829 ResultTypeCompatibilityKind RTC
2830 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002831
2832 // Search for overridden methods and merge information down from them.
2833 OverrideSearch overrides(*this, ObjCMethod);
2834 for (OverrideSearch::iterator
2835 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2836 ObjCMethodDecl *overridden = *i;
2837
2838 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002839 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002840 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002841
2842 // Then merge the declarations.
2843 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002844
2845 // Check for overriding methods
2846 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002847 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2848 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2849 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002850 }
2851
John McCallf85e1932011-06-15 23:02:42 +00002852 bool ARCError = false;
2853 if (getLangOptions().ObjCAutoRefCount)
2854 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2855
Douglas Gregore97179c2011-09-08 01:46:34 +00002856 // Infer the related result type when possible.
2857 if (!ARCError && RTC == RTC_Compatible &&
2858 !ObjCMethod->hasRelatedResultType() &&
2859 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002860 bool InferRelatedResultType = false;
2861 switch (ObjCMethod->getMethodFamily()) {
2862 case OMF_None:
2863 case OMF_copy:
2864 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002865 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002866 case OMF_mutableCopy:
2867 case OMF_release:
2868 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002869 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002870 break;
2871
2872 case OMF_alloc:
2873 case OMF_new:
2874 InferRelatedResultType = ObjCMethod->isClassMethod();
2875 break;
2876
2877 case OMF_init:
2878 case OMF_autorelease:
2879 case OMF_retain:
2880 case OMF_self:
2881 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2882 break;
2883 }
2884
John McCall6c2c2502011-07-22 02:45:48 +00002885 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002886 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002887 }
2888
John McCalld226f652010-08-21 09:40:31 +00002889 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002890}
2891
Chris Lattnercc98eac2008-12-17 07:13:27 +00002892bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002893 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002894 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002895 // Following is also an error. But it is caused by a missing @end
2896 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002897 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2898 return false;
2899 }
2900
Anders Carlsson15281452008-11-04 16:57:32 +00002901 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2902 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Anders Carlsson15281452008-11-04 16:57:32 +00002904 return true;
2905}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002906
Chris Lattnercc98eac2008-12-17 07:13:27 +00002907/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2908/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002909void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002910 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002911 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002912 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002913 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002914 if (!Class) {
2915 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2916 return;
2917 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002918 if (LangOpts.ObjCNonFragileABI) {
2919 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2920 return;
2921 }
Mike Stump1eb44332009-09-09 15:08:12 +00002922
Chris Lattnercc98eac2008-12-17 07:13:27 +00002923 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002924 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002925 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002926 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002927 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002928 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002929 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002930 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2931 /*FIXME: StartL=*/ID->getLocation(),
2932 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002933 ID->getIdentifier(), ID->getType(),
2934 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002935 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002936 }
Mike Stump1eb44332009-09-09 15:08:12 +00002937
Chris Lattnercc98eac2008-12-17 07:13:27 +00002938 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002939 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002940 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002941 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002942 if (getLangOptions().CPlusPlus)
2943 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002944 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002945 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002946 }
2947}
2948
Douglas Gregor160b5632010-04-26 17:32:49 +00002949/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002950VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2951 SourceLocation StartLoc,
2952 SourceLocation IdLoc,
2953 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002954 bool Invalid) {
2955 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2956 // duration shall not be qualified by an address-space qualifier."
2957 // Since all parameters have automatic store duration, they can not have
2958 // an address space.
2959 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002960 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002961 Invalid = true;
2962 }
2963
2964 // An @catch parameter must be an unqualified object pointer type;
2965 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2966 if (Invalid) {
2967 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002968 } else if (T->isDependentType()) {
2969 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002970 } else if (!T->isObjCObjectPointerType()) {
2971 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002972 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002973 } else if (T->isObjCQualifiedIdType()) {
2974 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002975 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002976 }
2977
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002978 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2979 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002980 New->setExceptionVariable(true);
2981
Douglas Gregor9aab9c42011-12-10 01:22:52 +00002982 // In ARC, infer 'retaining' for variables of retainable type.
2983 if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(New))
2984 Invalid = true;
2985
Douglas Gregor160b5632010-04-26 17:32:49 +00002986 if (Invalid)
2987 New->setInvalidDecl();
2988 return New;
2989}
2990
John McCalld226f652010-08-21 09:40:31 +00002991Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002992 const DeclSpec &DS = D.getDeclSpec();
2993
2994 // We allow the "register" storage class on exception variables because
2995 // GCC did, but we drop it completely. Any other storage class is an error.
2996 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2997 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2998 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2999 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3000 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3001 << DS.getStorageClassSpec();
3002 }
3003 if (D.getDeclSpec().isThreadSpecified())
3004 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3005 D.getMutableDeclSpec().ClearStorageClassSpecs();
3006
3007 DiagnoseFunctionSpecifiers(D);
3008
3009 // Check that there are no default arguments inside the type of this
3010 // exception object (C++ only).
3011 if (getLangOptions().CPlusPlus)
3012 CheckExtraCXXDefaultArguments(D);
3013
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003014 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003015 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003016
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003017 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3018 D.getSourceRange().getBegin(),
3019 D.getIdentifierLoc(),
3020 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003021 D.isInvalidType());
3022
3023 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3024 if (D.getCXXScopeSpec().isSet()) {
3025 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3026 << D.getCXXScopeSpec().getRange();
3027 New->setInvalidDecl();
3028 }
3029
3030 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003031 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003032 if (D.getIdentifier())
3033 IdResolver.AddDecl(New);
3034
3035 ProcessDeclAttributes(S, New, D);
3036
3037 if (New->hasAttr<BlocksAttr>())
3038 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003039 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003040}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003041
3042/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003043/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003044void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003045 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003046 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3047 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003048 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003049 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003050 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003051 }
3052}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003053
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003054void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003055 // Load referenced selectors from the external source.
3056 if (ExternalSource) {
3057 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3058 ExternalSource->ReadReferencedSelectors(Sels);
3059 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3060 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3061 }
3062
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003063 // Warning will be issued only when selector table is
3064 // generated (which means there is at lease one implementation
3065 // in the TU). This is to match gcc's behavior.
3066 if (ReferencedSelectors.empty() ||
3067 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003068 return;
3069 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3070 ReferencedSelectors.begin(),
3071 E = ReferencedSelectors.end(); S != E; ++S) {
3072 Selector Sel = (*S).first;
3073 if (!LookupImplementedMethodInGlobalPool(Sel))
3074 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3075 }
3076 return;
3077}