blob: e80cc824823f62f3b46b36e77f7a9793fe6dfc08 [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"
Patrick Beardb2f68202012-04-06 18:12:22 +000027#include "clang/Lex/Preprocessor.h"
John McCall50df6ae2010-08-25 07:03:20 +000028#include "llvm/ADT/DenseSet.h"
29
Chris Lattner4d391482007-12-12 07:09:47 +000030using namespace clang;
31
John McCallf85e1932011-06-15 23:02:42 +000032/// Check whether the given method, which must be in the 'init'
33/// family, is a valid member of that family.
34///
35/// \param receiverTypeIfCall - if null, check this as if declaring it;
36/// if non-null, check this as if making a call to it with the given
37/// receiver type
38///
39/// \return true to indicate that there was an error and appropriate
40/// actions were taken
41bool Sema::checkInitMethod(ObjCMethodDecl *method,
42 QualType receiverTypeIfCall) {
43 if (method->isInvalidDecl()) return true;
44
45 // This castAs is safe: methods that don't return an object
46 // pointer won't be inferred as inits and will reject an explicit
47 // objc_method_family(init).
48
49 // We ignore protocols here. Should we? What about Class?
50
51 const ObjCObjectType *result = method->getResultType()
52 ->castAs<ObjCObjectPointerType>()->getObjectType();
53
54 if (result->isObjCId()) {
55 return false;
56 } else if (result->isObjCClass()) {
57 // fall through: always an error
58 } else {
59 ObjCInterfaceDecl *resultClass = result->getInterface();
60 assert(resultClass && "unexpected object type!");
61
62 // It's okay for the result type to still be a forward declaration
63 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000064 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000065 if (receiverTypeIfCall.isNull() &&
66 !isa<ObjCImplementationDecl>(method->getDeclContext()))
67 return false;
68
69 // Otherwise, we try to compare class types.
70 } else {
71 // If this method was declared in a protocol, we can't check
72 // anything unless we have a receiver type that's an interface.
73 const ObjCInterfaceDecl *receiverClass = 0;
74 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75 if (receiverTypeIfCall.isNull())
76 return false;
77
78 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79 ->getInterfaceDecl();
80
81 // This can be null for calls to e.g. id<Foo>.
82 if (!receiverClass) return false;
83 } else {
84 receiverClass = method->getClassInterface();
85 assert(receiverClass && "method not associated with a class!");
86 }
87
88 // If either class is a subclass of the other, it's fine.
89 if (receiverClass->isSuperClassOf(resultClass) ||
90 resultClass->isSuperClassOf(receiverClass))
91 return false;
92 }
93 }
94
95 SourceLocation loc = method->getLocation();
96
97 // If we're in a system header, and this is not a call, just make
98 // the method unusable.
99 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100 method->addAttr(new (Context) UnavailableAttr(loc, Context,
101 "init method returns a type unrelated to its receiver type"));
102 return true;
103 }
104
105 // Otherwise, it's an error.
106 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
107 method->setInvalidDecl();
108 return true;
109}
110
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000111void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000112 const ObjCMethodDecl *Overridden,
113 bool IsImplementation) {
114 if (Overridden->hasRelatedResultType() &&
115 !NewMethod->hasRelatedResultType()) {
116 // This can only happen when the method follows a naming convention that
117 // implies a related result type, and the original (overridden) method has
118 // a suitable return type, but the new (overriding) method does not have
119 // a suitable return type.
120 QualType ResultType = NewMethod->getResultType();
121 SourceRange ResultTypeRange;
122 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000123 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000124 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
125
126 // Figure out which class this method is part of, if any.
127 ObjCInterfaceDecl *CurrentClass
128 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
129 if (!CurrentClass) {
130 DeclContext *DC = NewMethod->getDeclContext();
131 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
132 CurrentClass = Cat->getClassInterface();
133 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
134 CurrentClass = Impl->getClassInterface();
135 else if (ObjCCategoryImplDecl *CatImpl
136 = dyn_cast<ObjCCategoryImplDecl>(DC))
137 CurrentClass = CatImpl->getClassInterface();
138 }
139
140 if (CurrentClass) {
141 Diag(NewMethod->getLocation(),
142 diag::warn_related_result_type_compatibility_class)
143 << Context.getObjCInterfaceType(CurrentClass)
144 << ResultType
145 << ResultTypeRange;
146 } else {
147 Diag(NewMethod->getLocation(),
148 diag::warn_related_result_type_compatibility_protocol)
149 << ResultType
150 << ResultTypeRange;
151 }
152
Douglas Gregore97179c2011-09-08 01:46:34 +0000153 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
154 Diag(Overridden->getLocation(),
155 diag::note_related_result_type_overridden_family)
156 << Family;
157 else
158 Diag(Overridden->getLocation(),
159 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000160 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000161 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000162 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
163 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
164 Diag(NewMethod->getLocation(),
165 diag::err_nsreturns_retained_attribute_mismatch) << 1;
166 Diag(Overridden->getLocation(), diag::note_previous_decl)
167 << "method";
168 }
169 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
170 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
171 Diag(NewMethod->getLocation(),
172 diag::err_nsreturns_retained_attribute_mismatch) << 0;
173 Diag(Overridden->getLocation(), diag::note_previous_decl)
174 << "method";
175 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
177 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000178 for (ObjCMethodDecl::param_iterator
179 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000180 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000181 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000182 ParmVarDecl *newDecl = (*ni);
183 if (newDecl->hasAttr<NSConsumedAttr>() !=
184 oldDecl->hasAttr<NSConsumedAttr>()) {
185 Diag(newDecl->getLocation(),
186 diag::err_nsconsumed_attribute_mismatch);
187 Diag(oldDecl->getLocation(), diag::note_previous_decl)
188 << "parameter";
189 }
190 }
191 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000192}
193
John McCallf85e1932011-06-15 23:02:42 +0000194/// \brief Check a method declaration for compatibility with the Objective-C
195/// ARC conventions.
196static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
197 ObjCMethodFamily family = method->getMethodFamily();
198 switch (family) {
199 case OMF_None:
Nico Weber80cb6e62011-08-28 22:35:17 +0000200 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000201 case OMF_retain:
202 case OMF_release:
203 case OMF_autorelease:
204 case OMF_retainCount:
205 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000206 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000207 return false;
208
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000209 case OMF_dealloc:
210 if (!S.Context.hasSameType(method->getResultType(), S.Context.VoidTy)) {
211 SourceRange ResultTypeRange;
212 if (const TypeSourceInfo *ResultTypeInfo
213 = method->getResultTypeSourceInfo())
214 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
215 if (ResultTypeRange.isInvalid())
216 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
217 << method->getResultType()
218 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
219 else
220 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
221 << method->getResultType()
222 << FixItHint::CreateReplacement(ResultTypeRange, "void");
223 return true;
224 }
225 return false;
226
John McCallf85e1932011-06-15 23:02:42 +0000227 case OMF_init:
228 // If the method doesn't obey the init rules, don't bother annotating it.
229 if (S.checkInitMethod(method, QualType()))
230 return true;
231
232 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
233 S.Context));
234
235 // Don't add a second copy of this attribute, but otherwise don't
236 // let it be suppressed.
237 if (method->hasAttr<NSReturnsRetainedAttr>())
238 return false;
239 break;
240
241 case OMF_alloc:
242 case OMF_copy:
243 case OMF_mutableCopy:
244 case OMF_new:
245 if (method->hasAttr<NSReturnsRetainedAttr>() ||
246 method->hasAttr<NSReturnsNotRetainedAttr>() ||
247 method->hasAttr<NSReturnsAutoreleasedAttr>())
248 return false;
249 break;
250 }
251
252 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
253 S.Context));
254 return false;
255}
256
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000257static void DiagnoseObjCImplementedDeprecations(Sema &S,
258 NamedDecl *ND,
259 SourceLocation ImplLoc,
260 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000261 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000262 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000263 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000264 S.Diag(ND->getLocation(), diag::note_method_declared_at)
265 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000266 else
267 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
268 }
269}
270
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000271/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
272/// pool.
273void Sema::AddAnyMethodToGlobalPool(Decl *D) {
274 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
275
276 // If we don't have a valid method decl, simply return.
277 if (!MDecl)
278 return;
279 if (MDecl->isInstanceMethod())
280 AddInstanceMethodToGlobalPool(MDecl, true);
281 else
282 AddFactoryMethodToGlobalPool(MDecl, true);
283}
284
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000285/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
286/// and user declared, in the method definition's AST.
287void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
288 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000289 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000290
Steve Naroff394f3f42008-07-25 17:57:26 +0000291 // If we don't have a valid method decl, simply return.
292 if (!MDecl)
293 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000294
Chris Lattner4d391482007-12-12 07:09:47 +0000295 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000296 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000297 PushFunctionScope();
298
Chris Lattner4d391482007-12-12 07:09:47 +0000299 // Create Decl objects for each parameter, entrring them in the scope for
300 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000301
302 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000303 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Daniel Dunbar451318c2008-08-26 06:07:48 +0000305 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
306 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000307
Chris Lattner8123a952008-04-10 02:22:51 +0000308 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000309 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000310 E = MDecl->param_end(); PI != E; ++PI) {
311 ParmVarDecl *Param = (*PI);
312 if (!Param->isInvalidDecl() &&
313 RequireCompleteType(Param->getLocation(), Param->getType(),
314 diag::err_typecheck_decl_incomplete_type))
315 Param->setInvalidDecl();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000316
Chris Lattner89951a82009-02-20 18:43:26 +0000317 if ((*PI)->getIdentifier())
318 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000319 }
John McCallf85e1932011-06-15 23:02:42 +0000320
321 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000322 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000323 switch (MDecl->getMethodFamily()) {
324 case OMF_retain:
325 case OMF_retainCount:
326 case OMF_release:
327 case OMF_autorelease:
328 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
329 << MDecl->getSelector();
330 break;
331
332 case OMF_None:
333 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000334 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000335 case OMF_alloc:
336 case OMF_init:
337 case OMF_mutableCopy:
338 case OMF_copy:
339 case OMF_new:
340 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000341 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000342 break;
343 }
344 }
345
Nico Weber9a1ecf02011-08-22 17:25:57 +0000346 // Warn on deprecated methods under -Wdeprecated-implementations,
347 // and prepare for warning on missing super calls.
348 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000349 ObjCMethodDecl *IMD =
350 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
351
352 if (IMD)
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000353 DiagnoseObjCImplementedDeprecations(*this,
354 dyn_cast<NamedDecl>(IMD),
355 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000356
Nico Weber80cb6e62011-08-28 22:35:17 +0000357 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000358 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
359 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
360 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000361 if (IC->getSuperClass()) {
Eli Friedman95aac152012-08-01 21:02:59 +0000362 getCurFunction()->ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000363 !(Context.getLangOpts().ObjCAutoRefCount ||
364 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Fariborz Jahanian84101132012-09-07 23:46:23 +0000365 MDecl->getMethodFamily() == OMF_dealloc;
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000366 if (!getCurFunction()->ObjCShouldCallSuperDealloc) {
367 IMD = IC->getSuperClass()->lookupMethod(MDecl->getSelector(),
368 MDecl->isInstanceMethod());
Fariborz Jahanian84101132012-09-07 23:46:23 +0000369 getCurFunction()->ObjCShouldCallSuperDealloc =
370 (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000371 }
Eli Friedman95aac152012-08-01 21:02:59 +0000372 getCurFunction()->ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000373 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000374 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000375 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000376 }
Chris Lattner4d391482007-12-12 07:09:47 +0000377}
378
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000379namespace {
380
381// Callback to only accept typo corrections that are Objective-C classes.
382// If an ObjCInterfaceDecl* is given to the constructor, then the validation
383// function will reject corrections to that class.
384class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
385 public:
386 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
387 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
388 : CurrentIDecl(IDecl) {}
389
390 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
391 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
392 return ID && !declaresSameEntity(ID, CurrentIDecl);
393 }
394
395 private:
396 ObjCInterfaceDecl *CurrentIDecl;
397};
398
399}
400
John McCalld226f652010-08-21 09:40:31 +0000401Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000402ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
403 IdentifierInfo *ClassName, SourceLocation ClassLoc,
404 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000405 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000406 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000407 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000408 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner4d391482007-12-12 07:09:47 +0000410 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000411 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000412 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000413
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000414 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000415 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000416 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor7723fec2011-12-15 20:29:51 +0000419 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000420 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000421 ObjCInterfaceDecl *IDecl
422 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000423 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000424
Douglas Gregor7723fec2011-12-15 20:29:51 +0000425 if (PrevIDecl) {
426 // Class already seen. Was it a definition?
427 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
428 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
429 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000430 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000431 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000432 }
Chris Lattner4d391482007-12-12 07:09:47 +0000433 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000434
435 if (AttrList)
436 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
437 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Douglas Gregor7723fec2011-12-15 20:29:51 +0000439 // Start the definition of this class. If we're in a redefinition case, there
440 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000441 if (!IDecl->hasDefinition())
442 IDecl->startDefinition();
443
Chris Lattner4d391482007-12-12 07:09:47 +0000444 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000445 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000446 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
447 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000448
449 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000450 // Try to correct for a typo in the superclass name without correcting
451 // to the class we're defining.
452 ObjCInterfaceValidatorCCC Validator(IDecl);
453 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000454 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000455 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000456 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
457 Diag(SuperLoc, diag::err_undef_superclass_suggest)
458 << SuperName << ClassName << PrevDecl->getDeclName();
459 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
460 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000461 }
462 }
463
Douglas Gregor60ef3082011-12-15 00:29:59 +0000464 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000465 Diag(SuperLoc, diag::err_recursive_superclass)
466 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000467 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000468 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000469 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000470 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000471
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000472 // Diagnose classes that inherit from deprecated classes.
473 if (SuperClassDecl)
474 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000476 if (PrevDecl && SuperClassDecl == 0) {
477 // The previous declaration was not a class decl. Check if we have a
478 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000479 if (const TypedefNameDecl *TDecl =
480 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000481 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000482 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000483 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
484 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000485 }
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000488 // This handles the following case:
489 //
490 // typedef int SuperClass;
491 // @interface MyClass : SuperClass {} @end
492 //
493 if (!SuperClassDecl) {
494 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
495 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000496 }
497 }
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Richard Smith162e1c12011-04-15 14:24:37 +0000499 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000500 if (!SuperClassDecl)
501 Diag(SuperLoc, diag::err_undef_superclass)
502 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000503 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000504 Context.getObjCInterfaceType(SuperClassDecl),
505 diag::err_forward_superclass,
506 SuperClassDecl->getDeclName(),
507 ClassName,
508 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000509 SuperClassDecl = 0;
510 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000511 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000512 IDecl->setSuperClass(SuperClassDecl);
513 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000514 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000515 }
Chris Lattner4d391482007-12-12 07:09:47 +0000516 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000517 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Sebastian Redl0b17c612010-08-13 00:28:03 +0000520 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000521 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000522 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000523 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000524 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Anders Carlsson15281452008-11-04 16:57:32 +0000527 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000528 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000529}
530
Richard Smithde01b7a2012-08-08 23:32:13 +0000531/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000532/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000533Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
534 IdentifierInfo *AliasName,
535 SourceLocation AliasLocation,
536 IdentifierInfo *ClassName,
537 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000538 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000539 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000540 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000541 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000542 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000543 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000544 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000545 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000546 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000547 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000548 }
549 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000550 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000551 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000552 if (const TypedefNameDecl *TDecl =
553 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000554 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000555 if (T->isObjCObjectType()) {
556 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000557 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000558 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000559 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000560 }
561 }
562 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000563 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
564 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000565 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000566 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000567 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000568 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000571 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000572 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000573 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Anders Carlsson15281452008-11-04 16:57:32 +0000575 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000576 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000577
John McCalld226f652010-08-21 09:40:31 +0000578 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000579}
580
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000581bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000582 IdentifierInfo *PName,
583 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000584 const ObjCList<ObjCProtocolDecl> &PList) {
585
586 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000587 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
588 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000589 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
590 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000591 if (PDecl->getIdentifier() == PName) {
592 Diag(Ploc, diag::err_protocol_has_circular_dependency);
593 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000594 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000595 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000596
597 if (!PDecl->hasDefinition())
598 continue;
599
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000600 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
601 PDecl->getLocation(), PDecl->getReferencedProtocols()))
602 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000603 }
604 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000605 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000606}
607
John McCalld226f652010-08-21 09:40:31 +0000608Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000609Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
610 IdentifierInfo *ProtocolName,
611 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000612 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000613 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000614 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000615 SourceLocation EndProtoLoc,
616 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000617 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000618 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000619 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000620 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
621 ForRedeclaration);
622 ObjCProtocolDecl *PDecl = 0;
623 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
624 // If we already have a definition, complain.
625 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
626 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Douglas Gregor27c6da22012-01-01 20:30:41 +0000628 // Create a new protocol that is completely distinct from previous
629 // declarations, and do not make this protocol available for name lookup.
630 // That way, we'll end up completely ignoring the duplicate.
631 // FIXME: Can we turn this into an error?
632 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
633 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000634 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000635 PDecl->startDefinition();
636 } else {
637 if (PrevDecl) {
638 // Check for circular dependencies among protocol declarations. This can
639 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000640 ObjCList<ObjCProtocolDecl> PList;
641 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
642 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000643 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000644 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000645
646 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000647 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000648 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000649 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000650
Douglas Gregor6e378de2009-04-23 23:18:26 +0000651 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000652 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000653 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000654
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000655 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000656 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000657
658 // Merge attributes from previous declarations.
659 if (PrevDecl)
660 mergeDeclAttributes(PDecl, PrevDecl);
661
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000662 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000663 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000664 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000665 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000666 }
Mike Stump1eb44332009-09-09 15:08:12 +0000667
668 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000669 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000670}
671
672/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000673/// issues an error if they are not declared. It returns list of
674/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000675void
Chris Lattnere13b9592008-07-26 04:03:38 +0000676Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000677 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000678 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000679 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000680 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000681 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
682 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000683 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000684 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000685 TypoCorrection Corrected = CorrectTypo(
686 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000687 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000688 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000689 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000690 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000691 Diag(PDecl->getLocation(), diag::note_previous_decl)
692 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000693 }
694 }
695
696 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000697 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000698 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000699 continue;
700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000702 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000703
704 // If this is a forward declaration and we are supposed to warn in this
705 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000706 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000707 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000708 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000709 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000710 }
711}
712
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000713/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000714/// a class method in its extension.
715///
Mike Stump1eb44332009-09-09 15:08:12 +0000716void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000717 ObjCInterfaceDecl *ID) {
718 if (!ID)
719 return; // Possibly due to previous error
720
721 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000722 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
723 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000724 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000725 MethodMap[MD->getSelector()] = MD;
726 }
727
728 if (MethodMap.empty())
729 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000730 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
731 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000732 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000733 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
734 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
735 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
736 << Method->getDeclName();
737 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
738 }
739 }
740}
741
James Dennett1dfbd922012-06-14 21:40:34 +0000742/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000743Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000744Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000745 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000746 unsigned NumElts,
747 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000748 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000749 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000750 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000751 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
752 ForRedeclaration);
753 ObjCProtocolDecl *PDecl
754 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
755 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000756 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000757
758 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000759 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000760
Douglas Gregor3937f872012-01-01 20:33:24 +0000761 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000762 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000763
764 if (PrevDecl)
765 mergeDeclAttributes(PDecl, PrevDecl);
766
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000767 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000770 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000771}
772
John McCalld226f652010-08-21 09:40:31 +0000773Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000774ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
775 IdentifierInfo *ClassName, SourceLocation ClassLoc,
776 IdentifierInfo *CategoryName,
777 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000778 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000779 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000780 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000781 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000782 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000783 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000784
785 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000786
787 if (!IDecl
788 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000789 diag::err_category_forward_interface,
790 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000791 // Create an invalid ObjCCategoryDecl to serve as context for
792 // the enclosing method declarations. We mark the decl invalid
793 // to make it clear that this isn't a valid AST.
794 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000795 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000796 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000797 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000798
799 if (!IDecl)
800 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000801 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000802 }
803
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000804 if (!CategoryName && IDecl->getImplementation()) {
805 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
806 Diag(IDecl->getImplementation()->getLocation(),
807 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000808 }
809
Fariborz Jahanian25760612010-02-15 21:55:26 +0000810 if (CategoryName) {
811 /// Check for duplicate interface declaration for this category
812 ObjCCategoryDecl *CDeclChain;
813 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
814 CDeclChain = CDeclChain->getNextClassCategory()) {
815 if (CDeclChain->getIdentifier() == CategoryName) {
816 // Class extensions can be declared multiple times.
817 Diag(CategoryLoc, diag::warn_dup_category_def)
818 << ClassName << CategoryName;
819 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
820 break;
821 }
Chris Lattner70f19542009-02-16 21:26:43 +0000822 }
823 }
Chris Lattner70f19542009-02-16 21:26:43 +0000824
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000825 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
826 ClassLoc, CategoryLoc, CategoryName, IDecl);
827 // FIXME: PushOnScopeChains?
828 CurContext->addDecl(CDecl);
829
Chris Lattner4d391482007-12-12 07:09:47 +0000830 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000831 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000832 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000833 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000834 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000835 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000836 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Anders Carlsson15281452008-11-04 16:57:32 +0000839 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000840 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000841}
842
843/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000844/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000845/// object.
John McCalld226f652010-08-21 09:40:31 +0000846Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000847 SourceLocation AtCatImplLoc,
848 IdentifierInfo *ClassName, SourceLocation ClassLoc,
849 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000850 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000851 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000852 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000853 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
854 if (!CatIDecl) {
855 // Category @implementation with no corresponding @interface.
856 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000857 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
858 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000859 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000860 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000861 }
862 }
863
Mike Stump1eb44332009-09-09 15:08:12 +0000864 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000865 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000866 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000867 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000868 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000869 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000870 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000871 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
872 diag::err_undef_interface)) {
873 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000874 }
Chris Lattner4d391482007-12-12 07:09:47 +0000875
Douglas Gregord0434102009-01-09 00:49:46 +0000876 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000877 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000878
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000879 // If the interface is deprecated/unavailable, warn/error about it.
880 if (IDecl)
881 DiagnoseUseOfDecl(IDecl, ClassLoc);
882
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000883 /// Check that CatName, category name, is not used in another implementation.
884 if (CatIDecl) {
885 if (CatIDecl->getImplementation()) {
886 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
887 << CatName;
888 Diag(CatIDecl->getImplementation()->getLocation(),
889 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000890 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000891 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000892 // Warn on implementating category of deprecated class under
893 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000894 DiagnoseObjCImplementedDeprecations(*this,
895 dyn_cast<NamedDecl>(IDecl),
896 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000897 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Anders Carlsson15281452008-11-04 16:57:32 +0000900 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000901 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000902}
903
John McCalld226f652010-08-21 09:40:31 +0000904Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000905 SourceLocation AtClassImplLoc,
906 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000907 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000908 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000909 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000910 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000911 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000912 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
913 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000914 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000915 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000916 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000917 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000918 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
919 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000920 } else {
921 // We did not find anything with the name ClassName; try to correct for
922 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000923 ObjCInterfaceValidatorCCC Validator;
924 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000925 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000926 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000927 // Suggest the (potentially) correct interface name. However, put the
928 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000929 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000930 // provide a code-modification hint or use the typo name for recovery,
931 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000932 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000933 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000934 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000935 << ClassName << CorrectedName;
936 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
937 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000938 IDecl = 0;
939 } else {
940 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
941 }
Chris Lattner4d391482007-12-12 07:09:47 +0000942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Chris Lattner4d391482007-12-12 07:09:47 +0000944 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000945 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000946 if (SuperClassname) {
947 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000948 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
949 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000950 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000951 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
952 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000953 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000954 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000955 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000956 if (SDecl && !SDecl->hasDefinition())
957 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000958 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000959 Diag(SuperClassLoc, diag::err_undef_superclass)
960 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000961 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000962 // This implementation and its interface do not have the same
963 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000964 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000965 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000966 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000967 }
968 }
969 }
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Chris Lattner4d391482007-12-12 07:09:47 +0000971 if (!IDecl) {
972 // Legacy case of @implementation with no corresponding @interface.
973 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000974
Mike Stump390b4cc2009-05-16 07:39:55 +0000975 // FIXME: Do we support attributes on the @implementation? If so we should
976 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000977 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000978 ClassName, /*PrevDecl=*/0, ClassLoc,
979 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000980 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000981 if (SDecl) {
982 IDecl->setSuperClass(SDecl);
983 IDecl->setSuperClassLoc(SuperClassLoc);
984 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
985 } else {
986 IDecl->setEndOfDefinitionLoc(ClassLoc);
987 }
988
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000989 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000990 } else {
991 // Mark the interface as being completed, even if it was just as
992 // @class ....;
993 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000994 if (!IDecl->hasDefinition())
995 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
998 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000999 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1000 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Anders Carlsson15281452008-11-04 16:57:32 +00001002 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001003 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Chris Lattner4d391482007-12-12 07:09:47 +00001005 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001006 if (IDecl->getImplementation()) {
1007 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001008 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001009 Diag(IDecl->getImplementation()->getLocation(),
1010 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001011 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001012 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001013 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001014 // Warn on implementating deprecated class under
1015 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001016 DiagnoseObjCImplementedDeprecations(*this,
1017 dyn_cast<NamedDecl>(IDecl),
1018 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001019 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001020 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001021}
1022
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001023Sema::DeclGroupPtrTy
1024Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1025 SmallVector<Decl *, 64> DeclsInGroup;
1026 DeclsInGroup.reserve(Decls.size() + 1);
1027
1028 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1029 Decl *Dcl = Decls[i];
1030 if (!Dcl)
1031 continue;
1032 if (Dcl->getDeclContext()->isFileContext())
1033 Dcl->setTopLevelDeclInObjCContainer();
1034 DeclsInGroup.push_back(Dcl);
1035 }
1036
1037 DeclsInGroup.push_back(ObjCImpDecl);
1038
1039 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1040}
1041
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001042void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1043 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001044 SourceLocation RBrace) {
1045 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001046 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001047 if (!IDecl)
1048 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001049 /// Check case of non-existing \@interface decl.
1050 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001051 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001052 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001053 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001054 // Add ivar's to class's DeclContext.
1055 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001056 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001057 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001058 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001059 }
1060
Chris Lattner4d391482007-12-12 07:09:47 +00001061 return;
1062 }
1063 // If implementation has empty ivar list, just return.
1064 if (numIvars == 0)
1065 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Chris Lattner4d391482007-12-12 07:09:47 +00001067 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001068 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001069 if (ImpDecl->getSuperClass())
1070 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1071 for (unsigned i = 0; i < numIvars; i++) {
1072 ObjCIvarDecl* ImplIvar = ivars[i];
1073 if (const ObjCIvarDecl *ClsIvar =
1074 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1075 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1076 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1077 continue;
1078 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001079 // Instance ivar to Implementation's DeclContext.
1080 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001081 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001082 ImpDecl->addDecl(ImplIvar);
1083 }
1084 return;
1085 }
Chris Lattner4d391482007-12-12 07:09:47 +00001086 // Check interface's Ivar list against those in the implementation.
1087 // names and types must match.
1088 //
Chris Lattner4d391482007-12-12 07:09:47 +00001089 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001090 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001091 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1092 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001093 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001094 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001095 assert (ImplIvar && "missing implementation ivar");
1096 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Steve Naroffca331292009-03-03 14:49:36 +00001098 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001099 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001100 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001101 << ImplIvar->getIdentifier()
1102 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001103 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001104 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1105 ImplIvar->getBitWidthValue(Context) !=
1106 ClsIvar->getBitWidthValue(Context)) {
1107 Diag(ImplIvar->getBitWidth()->getLocStart(),
1108 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1109 Diag(ClsIvar->getBitWidth()->getLocStart(),
1110 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001111 }
Steve Naroffca331292009-03-03 14:49:36 +00001112 // Make sure the names are identical.
1113 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001114 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001115 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001116 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001117 }
1118 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001119 }
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Chris Lattner609e4c72007-12-12 18:11:49 +00001121 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001122 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001123 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001124 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001125}
1126
Steve Naroff3c2eb662008-02-10 21:38:56 +00001127void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001128 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001129 // No point warning no definition of method which is 'unavailable'.
1130 if (method->hasAttr<UnavailableAttr>())
1131 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001132 if (!IncompleteImpl) {
1133 Diag(ImpLoc, diag::warn_incomplete_impl);
1134 IncompleteImpl = true;
1135 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001136 if (DiagID == diag::warn_unimplemented_protocol_method)
1137 Diag(ImpLoc, DiagID) << method->getDeclName();
1138 else
1139 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001140}
1141
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001142/// Determines if type B can be substituted for type A. Returns true if we can
1143/// guarantee that anything that the user will do to an object of type A can
1144/// also be done to an object of type B. This is trivially true if the two
1145/// types are the same, or if B is a subclass of A. It becomes more complex
1146/// in cases where protocols are involved.
1147///
1148/// Object types in Objective-C describe the minimum requirements for an
1149/// object, rather than providing a complete description of a type. For
1150/// example, if A is a subclass of B, then B* may refer to an instance of A.
1151/// The principle of substitutability means that we may use an instance of A
1152/// anywhere that we may use an instance of B - it will implement all of the
1153/// ivars of B and all of the methods of B.
1154///
1155/// This substitutability is important when type checking methods, because
1156/// the implementation may have stricter type definitions than the interface.
1157/// The interface specifies minimum requirements, but the implementation may
1158/// have more accurate ones. For example, a method may privately accept
1159/// instances of B, but only publish that it accepts instances of A. Any
1160/// object passed to it will be type checked against B, and so will implicitly
1161/// by a valid A*. Similarly, a method may return a subclass of the class that
1162/// it is declared as returning.
1163///
1164/// This is most important when considering subclassing. A method in a
1165/// subclass must accept any object as an argument that its superclass's
1166/// implementation accepts. It may, however, accept a more general type
1167/// without breaking substitutability (i.e. you can still use the subclass
1168/// anywhere that you can use the superclass, but not vice versa). The
1169/// converse requirement applies to return types: the return type for a
1170/// subclass method must be a valid object of the kind that the superclass
1171/// advertises, but it may be specified more accurately. This avoids the need
1172/// for explicit down-casting by callers.
1173///
1174/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001175static bool isObjCTypeSubstitutable(ASTContext &Context,
1176 const ObjCObjectPointerType *A,
1177 const ObjCObjectPointerType *B,
1178 bool rejectId) {
1179 // Reject a protocol-unqualified id.
1180 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001181
1182 // If B is a qualified id, then A must also be a qualified id and it must
1183 // implement all of the protocols in B. It may not be a qualified class.
1184 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1185 // stricter definition so it is not substitutable for id<A>.
1186 if (B->isObjCQualifiedIdType()) {
1187 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001188 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1189 QualType(B,0),
1190 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001191 }
1192
1193 /*
1194 // id is a special type that bypasses type checking completely. We want a
1195 // warning when it is used in one place but not another.
1196 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1197
1198
1199 // If B is a qualified id, then A must also be a qualified id (which it isn't
1200 // if we've got this far)
1201 if (B->isObjCQualifiedIdType()) return false;
1202 */
1203
1204 // Now we know that A and B are (potentially-qualified) class types. The
1205 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001206 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001207}
1208
John McCall10302c02010-10-28 02:34:38 +00001209static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1210 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1211}
1212
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001213static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001214 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001215 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001216 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001217 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001218 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001219 if (IsProtocolMethodDecl &&
1220 (MethodDecl->getObjCDeclQualifier() !=
1221 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001222 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001223 S.Diag(MethodImpl->getLocation(),
1224 (IsOverridingMode ?
1225 diag::warn_conflicting_overriding_ret_type_modifiers
1226 : diag::warn_conflicting_ret_type_modifiers))
1227 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001228 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1229 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1230 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1231 }
1232 else
1233 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001234 }
1235
John McCall10302c02010-10-28 02:34:38 +00001236 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001237 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001238 return true;
1239 if (!Warn)
1240 return false;
John McCall10302c02010-10-28 02:34:38 +00001241
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001242 unsigned DiagID =
1243 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1244 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001245
1246 // Mismatches between ObjC pointers go into a different warning
1247 // category, and sometimes they're even completely whitelisted.
1248 if (const ObjCObjectPointerType *ImplPtrTy =
1249 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1250 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001251 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001252 // Allow non-matching return types as long as they don't violate
1253 // the principle of substitutability. Specifically, we permit
1254 // return types that are subclasses of the declared return type,
1255 // or that are more-qualified versions of the declared type.
1256 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001257 return false;
John McCall10302c02010-10-28 02:34:38 +00001258
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001259 DiagID =
1260 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1261 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001262 }
1263 }
1264
1265 S.Diag(MethodImpl->getLocation(), DiagID)
1266 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001267 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001268 << MethodImpl->getResultType()
1269 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001270 S.Diag(MethodDecl->getLocation(),
1271 IsOverridingMode ? diag::note_previous_declaration
1272 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001273 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001274 return false;
John McCall10302c02010-10-28 02:34:38 +00001275}
1276
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001277static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001278 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001279 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001280 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001281 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001282 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001283 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001284 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001285 if (IsProtocolMethodDecl &&
1286 (ImplVar->getObjCDeclQualifier() !=
1287 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001288 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001289 if (IsOverridingMode)
1290 S.Diag(ImplVar->getLocation(),
1291 diag::warn_conflicting_overriding_param_modifiers)
1292 << getTypeRange(ImplVar->getTypeSourceInfo())
1293 << MethodImpl->getDeclName();
1294 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001295 diag::warn_conflicting_param_modifiers)
1296 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001297 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001298 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1299 << getTypeRange(IfaceVar->getTypeSourceInfo());
1300 }
1301 else
1302 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001303 }
1304
John McCall10302c02010-10-28 02:34:38 +00001305 QualType ImplTy = ImplVar->getType();
1306 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001307
John McCall10302c02010-10-28 02:34:38 +00001308 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001309 return true;
1310
1311 if (!Warn)
1312 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001313 unsigned DiagID =
1314 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1315 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001316
1317 // Mismatches between ObjC pointers go into a different warning
1318 // category, and sometimes they're even completely whitelisted.
1319 if (const ObjCObjectPointerType *ImplPtrTy =
1320 ImplTy->getAs<ObjCObjectPointerType>()) {
1321 if (const ObjCObjectPointerType *IfacePtrTy =
1322 IfaceTy->getAs<ObjCObjectPointerType>()) {
1323 // Allow non-matching argument types as long as they don't
1324 // violate the principle of substitutability. Specifically, the
1325 // implementation must accept any objects that the superclass
1326 // accepts, however it may also accept others.
1327 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001328 return false;
John McCall10302c02010-10-28 02:34:38 +00001329
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001330 DiagID =
1331 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1332 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001333 }
1334 }
1335
1336 S.Diag(ImplVar->getLocation(), DiagID)
1337 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001338 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1339 S.Diag(IfaceVar->getLocation(),
1340 (IsOverridingMode ? diag::note_previous_declaration
1341 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001342 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001343 return false;
John McCall10302c02010-10-28 02:34:38 +00001344}
John McCallf85e1932011-06-15 23:02:42 +00001345
1346/// In ARC, check whether the conventional meanings of the two methods
1347/// match. If they don't, it's a hard error.
1348static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1349 ObjCMethodDecl *decl) {
1350 ObjCMethodFamily implFamily = impl->getMethodFamily();
1351 ObjCMethodFamily declFamily = decl->getMethodFamily();
1352 if (implFamily == declFamily) return false;
1353
1354 // Since conventions are sorted by selector, the only possibility is
1355 // that the types differ enough to cause one selector or the other
1356 // to fall out of the family.
1357 assert(implFamily == OMF_None || declFamily == OMF_None);
1358
1359 // No further diagnostics required on invalid declarations.
1360 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1361
1362 const ObjCMethodDecl *unmatched = impl;
1363 ObjCMethodFamily family = declFamily;
1364 unsigned errorID = diag::err_arc_lost_method_convention;
1365 unsigned noteID = diag::note_arc_lost_method_convention;
1366 if (declFamily == OMF_None) {
1367 unmatched = decl;
1368 family = implFamily;
1369 errorID = diag::err_arc_gained_method_convention;
1370 noteID = diag::note_arc_gained_method_convention;
1371 }
1372
1373 // Indexes into a %select clause in the diagnostic.
1374 enum FamilySelector {
1375 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1376 };
1377 FamilySelector familySelector = FamilySelector();
1378
1379 switch (family) {
1380 case OMF_None: llvm_unreachable("logic error, no method convention");
1381 case OMF_retain:
1382 case OMF_release:
1383 case OMF_autorelease:
1384 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001385 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001386 case OMF_retainCount:
1387 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001388 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001389 // Mismatches for these methods don't change ownership
1390 // conventions, so we don't care.
1391 return false;
1392
1393 case OMF_init: familySelector = F_init; break;
1394 case OMF_alloc: familySelector = F_alloc; break;
1395 case OMF_copy: familySelector = F_copy; break;
1396 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1397 case OMF_new: familySelector = F_new; break;
1398 }
1399
1400 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1401 ReasonSelector reasonSelector;
1402
1403 // The only reason these methods don't fall within their families is
1404 // due to unusual result types.
1405 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1406 reasonSelector = R_UnrelatedReturn;
1407 } else {
1408 reasonSelector = R_NonObjectReturn;
1409 }
1410
1411 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1412 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1413
1414 return true;
1415}
John McCall10302c02010-10-28 02:34:38 +00001416
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001417void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001418 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001419 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001420 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001421 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1422 return;
1423
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001424 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001425 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001426 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Chris Lattner3aff9192009-04-11 19:58:42 +00001428 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001429 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1430 EF = MethodDecl->param_end();
1431 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001432 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001433 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001434 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001435
Fariborz Jahanian21121902011-08-08 18:03:17 +00001436 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001437 Diag(ImpMethodDecl->getLocation(),
1438 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001439 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001440 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001441}
1442
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001443void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1444 ObjCMethodDecl *Overridden,
1445 bool IsProtocolMethodDecl) {
1446
1447 CheckMethodOverrideReturn(*this, Method, Overridden,
1448 IsProtocolMethodDecl, true,
1449 true);
1450
1451 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001452 IF = Overridden->param_begin(), EM = Method->param_end(),
1453 EF = Overridden->param_end();
1454 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001455 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1456 IsProtocolMethodDecl, true, true);
1457 }
1458
1459 if (Method->isVariadic() != Overridden->isVariadic()) {
1460 Diag(Method->getLocation(),
1461 diag::warn_conflicting_overriding_variadic);
1462 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1463 }
1464}
1465
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001466/// WarnExactTypedMethods - This routine issues a warning if method
1467/// implementation declaration matches exactly that of its declaration.
1468void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1469 ObjCMethodDecl *MethodDecl,
1470 bool IsProtocolMethodDecl) {
1471 // don't issue warning when protocol method is optional because primary
1472 // class is not required to implement it and it is safe for protocol
1473 // to implement it.
1474 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1475 return;
1476 // don't issue warning when primary class's method is
1477 // depecated/unavailable.
1478 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1479 MethodDecl->hasAttr<DeprecatedAttr>())
1480 return;
1481
1482 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1483 IsProtocolMethodDecl, false, false);
1484 if (match)
1485 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001486 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1487 EF = MethodDecl->param_end();
1488 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001489 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1490 *IM, *IF,
1491 IsProtocolMethodDecl, false, false);
1492 if (!match)
1493 break;
1494 }
1495 if (match)
1496 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001497 if (match)
1498 match = !(MethodDecl->isClassMethod() &&
1499 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001500
1501 if (match) {
1502 Diag(ImpMethodDecl->getLocation(),
1503 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001504 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1505 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001506 }
1507}
1508
Mike Stump390b4cc2009-05-16 07:39:55 +00001509/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1510/// improve the efficiency of selector lookups and type checking by associating
1511/// with each protocol / interface / category the flattened instance tables. If
1512/// we used an immutable set to keep the table then it wouldn't add significant
1513/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001514
Steve Naroffefe7f362008-02-08 22:06:17 +00001515/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001516/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001517void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1518 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001519 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001520 const SelectorSet &InsMap,
1521 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001522 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001523 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1524 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1525 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001526 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1527
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001528 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001529 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001530 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001531 // check to see if class implements forwardInvocation method and objects
1532 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001533 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001534 // Under such conditions, which means that every method possible is
1535 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001536 // found" warnings.
1537 // FIXME: Use a general GetUnarySelector method for this.
1538 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1539 Selector fISelector = Context.Selectors.getSelector(1, &II);
1540 if (InsMap.count(fISelector))
1541 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1542 // need be implemented in the implementation.
1543 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1544 }
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001546 // If a method lookup fails locally we still need to look and see if
1547 // the method was implemented by a base class or an inherited
1548 // protocol. This lookup is slow, but occurs rarely in correct code
1549 // and otherwise would terminate in a warning.
1550
Chris Lattner4d391482007-12-12 07:09:47 +00001551 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001552 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001553 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001554 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001555 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001556 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001557 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001558 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001559 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001560 // If a method is not implemented in the category implementation but
1561 // has been declared in its primary class, superclass,
1562 // or in one of their protocols, no need to issue the warning.
1563 // This is because method will be implemented in the primary class
1564 // or one of its super class implementation.
1565
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001566 // Ugly, but necessary. Method declared in protcol might have
1567 // have been synthesized due to a property declared in the class which
1568 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001569 if (ObjCMethodDecl *MethodInClass =
1570 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001571 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001572 if (C || MethodInClass->isSynthesized())
1573 continue;
1574 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1575 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1576 != DiagnosticsEngine::Ignored) {
1577 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001578 Diag(method->getLocation(), diag::note_method_declared_at)
1579 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001580 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1581 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001582 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001583 }
1584 }
Chris Lattner4d391482007-12-12 07:09:47 +00001585 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001586 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001587 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001588 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001589 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001590 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1591 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001592 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001593 // See above comment for instance method lookups.
1594 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001595 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001596 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001597 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001598 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1599 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001600 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001601 Diag(method->getLocation(), diag::note_method_declared_at)
1602 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001603 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1604 PDecl->getDeclName();
1605 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001606 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001607 }
Chris Lattner780f3292008-07-21 21:32:27 +00001608 // Check on this protocols's referenced protocols, recursively.
1609 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1610 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001611 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001612}
1613
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001614/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001615/// or protocol against those declared in their implementations.
1616///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001617void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1618 const SelectorSet &ClsMap,
1619 SelectorSet &InsMapSeen,
1620 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001621 ObjCImplDecl* IMPDecl,
1622 ObjCContainerDecl* CDecl,
1623 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001624 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001625 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001626 // Check and see if instance methods in class interface have been
1627 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001628 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1629 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001630 if (InsMapSeen.count((*I)->getSelector()))
1631 continue;
1632 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001633 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001634 !InsMap.count((*I)->getSelector())) {
1635 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001636 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1637 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001638 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001639 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001640 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001641 IMPDecl->getInstanceMethod((*I)->getSelector());
1642 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1643 "Expected to find the method through lookup as well");
1644 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001645 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001646 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001647 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001648 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1649 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001650 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001651 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001652 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001653 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001654 }
1655 }
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001657 // Check and see if class methods in class interface have been
1658 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001659 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001660 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001661 if (ClsMapSeen.count((*I)->getSelector()))
1662 continue;
1663 ClsMapSeen.insert((*I)->getSelector());
1664 if (!ClsMap.count((*I)->getSelector())) {
1665 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001666 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1667 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001668 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001669 ObjCMethodDecl *ImpMethodDecl =
1670 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001671 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1672 "Expected to find the method through lookup as well");
1673 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001674 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001675 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1676 isa<ObjCProtocolDecl>(CDecl));
1677 else
1678 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001679 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001680 }
1681 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001682
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001683 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001684 // Also methods in class extensions need be looked at next.
1685 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1686 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1687 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1688 IMPDecl,
1689 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001690 IncompleteImpl, false,
1691 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001692
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001693 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001694 for (ObjCInterfaceDecl::all_protocol_iterator
1695 PI = I->all_referenced_protocol_begin(),
1696 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001697 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1698 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001699 (*PI), IncompleteImpl, false,
1700 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001701
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001702 // FIXME. For now, we are not checking for extact match of methods
1703 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001704 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001705 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001706 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001707 I->getSuperClass(), IncompleteImpl, false);
1708 }
1709}
1710
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001711/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1712/// category matches with those implemented in its primary class and
1713/// warns each time an exact match is found.
1714void Sema::CheckCategoryVsClassMethodMatches(
1715 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001716 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001717
1718 for (ObjCImplementationDecl::instmeth_iterator
1719 I = CatIMPDecl->instmeth_begin(),
1720 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1721 InsMap.insert((*I)->getSelector());
1722
1723 for (ObjCImplementationDecl::classmeth_iterator
1724 I = CatIMPDecl->classmeth_begin(),
1725 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1726 ClsMap.insert((*I)->getSelector());
1727 if (InsMap.empty() && ClsMap.empty())
1728 return;
1729
1730 // Get category's primary class.
1731 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1732 if (!CatDecl)
1733 return;
1734 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1735 if (!IDecl)
1736 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001737 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001738 bool IncompleteImpl = false;
1739 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1740 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001741 IncompleteImpl, false,
1742 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001743}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001744
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001745void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001746 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001747 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001748 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001749 // Check and see if instance methods in class interface have been
1750 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001751 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001752 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001753 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001755 // Check and see if properties declared in the interface have either 1)
1756 // an implementation or 2) there is a @synthesize/@dynamic implementation
1757 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001758 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001759 if (!(LangOpts.ObjCDefaultSynthProperties &&
1760 LangOpts.ObjCRuntime.isNonFragile()) ||
1761 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001762 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001763
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001764 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001765 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001766 I = IMPDecl->classmeth_begin(),
1767 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001768 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001770 // Check for type conflict of methods declared in a class/protocol and
1771 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001772 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001773 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1774 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001775 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001776
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001777 // check all methods implemented in category against those declared
1778 // in its primary class.
1779 if (ObjCCategoryImplDecl *CatDecl =
1780 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1781 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Chris Lattner4d391482007-12-12 07:09:47 +00001783 // Check the protocol list for unimplemented methods in the @implementation
1784 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001785 // Check and see if class methods in class interface have been
1786 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Chris Lattnercddc8882009-03-01 00:56:52 +00001788 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001789 for (ObjCInterfaceDecl::all_protocol_iterator
1790 PI = I->all_referenced_protocol_begin(),
1791 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001792 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001793 InsMap, ClsMap, I);
1794 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001795 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1796 Categories; Categories = Categories->getNextClassExtension())
1797 ImplMethodsVsClassMethods(S, IMPDecl,
1798 const_cast<ObjCCategoryDecl*>(Categories),
1799 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001800 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001801 // For extended class, unimplemented methods in its protocols will
1802 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001803 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001804 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1805 E = C->protocol_end(); PI != E; ++PI)
1806 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001807 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001808 // Report unimplemented properties in the category as well.
1809 // When reporting on missing setter/getters, do not report when
1810 // setter/getter is implemented in category's primary class
1811 // implementation.
1812 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1813 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1814 for (ObjCImplementationDecl::instmeth_iterator
1815 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1816 InsMap.insert((*I)->getSelector());
1817 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001818 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001819 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001820 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001821 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001822}
1823
Mike Stump1eb44332009-09-09 15:08:12 +00001824/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001825Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001826Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001827 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001828 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001829 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001830 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001831 for (unsigned i = 0; i != NumElts; ++i) {
1832 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001833 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001834 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001835 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001836 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001837 // Maybe we will complain about the shadowed template parameter.
1838 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1839 // Just pretend that we didn't see the previous declaration.
1840 PrevDecl = 0;
1841 }
1842
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001843 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001844 // GCC apparently allows the following idiom:
1845 //
1846 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1847 // @class XCElementToggler;
1848 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001849 // Here we have chosen to ignore the forward class declaration
1850 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001851 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001852 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001853 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001854 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001855 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001856 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001857 // to the underlying class. Just ignore the forward class with a warning
1858 // as this will force the intended behavior which is to lookup the typedef
1859 // name.
1860 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1861 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1862 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1863 continue;
1864 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001865 }
Chris Lattner4d391482007-12-12 07:09:47 +00001866 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001867
1868 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001869 ObjCInterfaceDecl *PrevIDecl
1870 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001871 ObjCInterfaceDecl *IDecl
1872 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001873 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001874 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001875
Douglas Gregor7723fec2011-12-15 20:29:51 +00001876 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001877 CheckObjCDeclScope(IDecl);
1878 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001879 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001880
1881 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001882}
1883
John McCall0f4c4c42011-06-16 01:15:19 +00001884static bool tryMatchRecordTypes(ASTContext &Context,
1885 Sema::MethodMatchStrategy strategy,
1886 const Type *left, const Type *right);
1887
John McCallf85e1932011-06-15 23:02:42 +00001888static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1889 QualType leftQT, QualType rightQT) {
1890 const Type *left =
1891 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1892 const Type *right =
1893 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1894
1895 if (left == right) return true;
1896
1897 // If we're doing a strict match, the types have to match exactly.
1898 if (strategy == Sema::MMS_strict) return false;
1899
1900 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1901
1902 // Otherwise, use this absurdly complicated algorithm to try to
1903 // validate the basic, low-level compatibility of the two types.
1904
1905 // As a minimum, require the sizes and alignments to match.
1906 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1907 return false;
1908
1909 // Consider all the kinds of non-dependent canonical types:
1910 // - functions and arrays aren't possible as return and parameter types
1911
1912 // - vector types of equal size can be arbitrarily mixed
1913 if (isa<VectorType>(left)) return isa<VectorType>(right);
1914 if (isa<VectorType>(right)) return false;
1915
1916 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001917 // - structs, unions, and Objective-C objects must match more-or-less
1918 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001919 // - everything else should be a scalar
1920 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001921 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001922
John McCall1d9b3b22011-09-09 05:25:32 +00001923 // Make scalars agree in kind, except count bools as chars, and group
1924 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001925 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1926 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1927 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1928 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001929 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1930 leftSK = Type::STK_ObjCObjectPointer;
1931 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1932 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001933
1934 // Note that data member pointers and function member pointers don't
1935 // intermix because of the size differences.
1936
1937 return (leftSK == rightSK);
1938}
Chris Lattner4d391482007-12-12 07:09:47 +00001939
John McCall0f4c4c42011-06-16 01:15:19 +00001940static bool tryMatchRecordTypes(ASTContext &Context,
1941 Sema::MethodMatchStrategy strategy,
1942 const Type *lt, const Type *rt) {
1943 assert(lt && rt && lt != rt);
1944
1945 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1946 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1947 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1948
1949 // Require union-hood to match.
1950 if (left->isUnion() != right->isUnion()) return false;
1951
1952 // Require an exact match if either is non-POD.
1953 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1954 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1955 return false;
1956
1957 // Require size and alignment to match.
1958 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1959
1960 // Require fields to match.
1961 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1962 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1963 for (; li != le && ri != re; ++li, ++ri) {
1964 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1965 return false;
1966 }
1967 return (li == le && ri == re);
1968}
1969
Chris Lattner4d391482007-12-12 07:09:47 +00001970/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1971/// returns true, or false, accordingly.
1972/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001973bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1974 const ObjCMethodDecl *right,
1975 MethodMatchStrategy strategy) {
1976 if (!matchTypes(Context, strategy,
1977 left->getResultType(), right->getResultType()))
1978 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001979
David Blaikie4e4d0842012-03-11 07:00:24 +00001980 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001981 (left->hasAttr<NSReturnsRetainedAttr>()
1982 != right->hasAttr<NSReturnsRetainedAttr>() ||
1983 left->hasAttr<NSConsumesSelfAttr>()
1984 != right->hasAttr<NSConsumesSelfAttr>()))
1985 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001987 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001988 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
1989 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001991 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00001992 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001993 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001994
1995 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1996 return false;
1997
David Blaikie4e4d0842012-03-11 07:00:24 +00001998 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001999 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2000 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002001 }
2002 return true;
2003}
2004
Douglas Gregorff310c72012-05-01 23:37:00 +00002005void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002006 // If the list is empty, make it a singleton list.
2007 if (List->Method == 0) {
2008 List->Method = Method;
2009 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002010 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002011 }
2012
2013 // We've seen a method with this name, see if we have already seen this type
2014 // signature.
2015 ObjCMethodList *Previous = List;
2016 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002017 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002018 continue;
2019
2020 ObjCMethodDecl *PrevObjCMethod = List->Method;
2021
2022 // Propagate the 'defined' bit.
2023 if (Method->isDefined())
2024 PrevObjCMethod->setDefined(true);
2025
2026 // If a method is deprecated, push it in the global pool.
2027 // This is used for better diagnostics.
2028 if (Method->isDeprecated()) {
2029 if (!PrevObjCMethod->isDeprecated())
2030 List->Method = Method;
2031 }
2032 // If new method is unavailable, push it into global pool
2033 // unless previous one is deprecated.
2034 if (Method->isUnavailable()) {
2035 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2036 List->Method = Method;
2037 }
2038
Douglas Gregorff310c72012-05-01 23:37:00 +00002039 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002040 }
2041
2042 // We have a new signature for an existing method - add it.
2043 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002044 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002045 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2046}
2047
Sebastian Redldb9d2142010-08-02 23:18:59 +00002048/// \brief Read the contents of the method pool for a given selector from
2049/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002050void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002051 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002052 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002053}
2054
Douglas Gregorff310c72012-05-01 23:37:00 +00002055void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002056 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002057 // Ignore methods of invalid containers.
2058 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002059 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002060
Douglas Gregor0d266d62012-01-25 00:59:09 +00002061 if (ExternalSource)
2062 ReadMethodPool(Method->getSelector());
2063
Sebastian Redldb9d2142010-08-02 23:18:59 +00002064 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002065 if (Pos == MethodPool.end())
2066 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2067 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002068
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002069 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002070
Sebastian Redldb9d2142010-08-02 23:18:59 +00002071 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002072 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002073}
2074
John McCallf85e1932011-06-15 23:02:42 +00002075/// Determines if this is an "acceptable" loose mismatch in the global
2076/// method pool. This exists mostly as a hack to get around certain
2077/// global mismatches which we can't afford to make warnings / errors.
2078/// Really, what we want is a way to take a method out of the global
2079/// method pool.
2080static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2081 ObjCMethodDecl *other) {
2082 if (!chosen->isInstanceMethod())
2083 return false;
2084
2085 Selector sel = chosen->getSelector();
2086 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2087 return false;
2088
2089 // Don't complain about mismatches for -length if the method we
2090 // chose has an integral result type.
2091 return (chosen->getResultType()->isIntegerType());
2092}
2093
Sebastian Redldb9d2142010-08-02 23:18:59 +00002094ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002095 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002096 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002097 if (ExternalSource)
2098 ReadMethodPool(Sel);
2099
Sebastian Redldb9d2142010-08-02 23:18:59 +00002100 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002101 if (Pos == MethodPool.end())
2102 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002103
Sebastian Redldb9d2142010-08-02 23:18:59 +00002104 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Sebastian Redldb9d2142010-08-02 23:18:59 +00002106 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002107 bool issueDiagnostic = false, issueError = false;
2108
2109 // We support a warning which complains about *any* difference in
2110 // method signature.
2111 bool strictSelectorMatch =
2112 (receiverIdOrClass && warn &&
2113 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2114 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002115 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002116 if (strictSelectorMatch)
2117 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002118 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2119 MMS_strict)) {
2120 issueDiagnostic = true;
2121 break;
2122 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002123 }
2124
John McCallf85e1932011-06-15 23:02:42 +00002125 // If we didn't see any strict differences, we won't see any loose
2126 // differences. In ARC, however, we also need to check for loose
2127 // mismatches, because most of them are errors.
2128 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002129 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002130 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002131 // This checks if the methods differ in type mismatch.
2132 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2133 MMS_loose) &&
2134 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2135 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002136 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002137 issueError = true;
2138 break;
2139 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002140 }
2141
John McCallf85e1932011-06-15 23:02:42 +00002142 if (issueDiagnostic) {
2143 if (issueError)
2144 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2145 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002146 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2147 else
2148 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002149
2150 Diag(MethList.Method->getLocStart(),
2151 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002152 << MethList.Method->getSourceRange();
2153 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2154 Diag(Next->Method->getLocStart(), diag::note_also_found)
2155 << Next->Method->getSourceRange();
2156 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002157 }
2158 return MethList.Method;
2159}
2160
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002161ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002162 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2163 if (Pos == MethodPool.end())
2164 return 0;
2165
2166 GlobalMethods &Methods = Pos->second;
2167
2168 if (Methods.first.Method && Methods.first.Method->isDefined())
2169 return Methods.first.Method;
2170 if (Methods.second.Method && Methods.second.Method->isDefined())
2171 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002172 return 0;
2173}
2174
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002175/// DiagnoseDuplicateIvars -
2176/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002177/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002178/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002179/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002180void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2181 ObjCInterfaceDecl *SID) {
2182 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2183 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002184 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002185 if (Ivar->isInvalidDecl())
2186 continue;
2187 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2188 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2189 if (prevIvar) {
2190 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2191 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2192 Ivar->setInvalidDecl();
2193 }
2194 }
2195 }
2196}
2197
Erik Verbruggend64251f2011-12-06 09:25:23 +00002198Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2199 switch (CurContext->getDeclKind()) {
2200 case Decl::ObjCInterface:
2201 return Sema::OCK_Interface;
2202 case Decl::ObjCProtocol:
2203 return Sema::OCK_Protocol;
2204 case Decl::ObjCCategory:
2205 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2206 return Sema::OCK_ClassExtension;
2207 else
2208 return Sema::OCK_Category;
2209 case Decl::ObjCImplementation:
2210 return Sema::OCK_Implementation;
2211 case Decl::ObjCCategoryImpl:
2212 return Sema::OCK_CategoryImplementation;
2213
2214 default:
2215 return Sema::OCK_None;
2216 }
2217}
2218
Steve Naroffa56f6162007-12-18 01:30:32 +00002219// Note: For class/category implemenations, allMethods/allProperties is
2220// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002221Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2222 Decl **allMethods, unsigned allNum,
2223 Decl **allProperties, unsigned pNum,
2224 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002225
Erik Verbruggend64251f2011-12-06 09:25:23 +00002226 if (getObjCContainerKind() == Sema::OCK_None)
2227 return 0;
2228
2229 assert(AtEnd.isValid() && "Invalid location for '@end'");
2230
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002231 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2232 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002233
Mike Stump1eb44332009-09-09 15:08:12 +00002234 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002235 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2236 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002237 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002238
Steve Naroff0701bbb2009-01-08 17:28:14 +00002239 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2240 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2241 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2242
Chris Lattner4d391482007-12-12 07:09:47 +00002243 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002244 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002245 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002246
2247 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002248 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002249 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002250 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002251 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002252 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002253 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002254 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002255 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002256 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002257 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002258 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002259 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002260 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002261 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002262 if (!Context.getSourceManager().isInSystemHeader(
2263 Method->getLocation()))
2264 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2265 << Method->getDeclName();
2266 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2267 }
Chris Lattner4d391482007-12-12 07:09:47 +00002268 InsMap[Method->getSelector()] = Method;
2269 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002270 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002271 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002272 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002273 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002274 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002275 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002276 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002277 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002278 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002279 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002280 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002281 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002282 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002283 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002284 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002285 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002286 if (!Context.getSourceManager().isInSystemHeader(
2287 Method->getLocation()))
2288 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2289 << Method->getDeclName();
2290 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2291 }
Chris Lattner4d391482007-12-12 07:09:47 +00002292 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002293 AddFactoryMethodToGlobalPool(Method);
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)
David Blaikie581deb32012-06-06 20:45:41 +00002322 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) {
David Blaikie581deb32012-06-06 20:45:41 +00002338 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002339 // 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
Patrick Beardb2f68202012-04-06 18:12:22 +00002363 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2364 if (IDecl->getSuperClass() == NULL) {
2365 // This class has no superclass, so check that it has been marked with
2366 // __attribute((objc_root_class)).
2367 if (!HasRootClassAttr) {
2368 SourceLocation DeclLoc(IDecl->getLocation());
2369 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2370 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2371 << IDecl->getIdentifier();
2372 // See if NSObject is in the current scope, and if it is, suggest
2373 // adding " : NSObject " to the class declaration.
2374 NamedDecl *IF = LookupSingleName(TUScope,
2375 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2376 DeclLoc, LookupOrdinaryName);
2377 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2378 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2379 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2380 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2381 } else {
2382 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2383 }
2384 }
2385 } else if (HasRootClassAttr) {
2386 // Complain that only root classes may have this attribute.
2387 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2388 }
2389
John McCall260611a2012-06-20 06:18:46 +00002390 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002391 while (IDecl->getSuperClass()) {
2392 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2393 IDecl = IDecl->getSuperClass();
2394 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002395 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002396 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002397 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002398 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002399 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002400 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002401
Chris Lattner4d391482007-12-12 07:09:47 +00002402 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002403 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002404 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002405 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002406 Categories; Categories = Categories->getNextClassCategory()) {
2407 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002408 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002409 break;
2410 }
2411 }
2412 }
2413 }
Chris Lattner682bf922009-03-29 16:50:03 +00002414 if (isInterfaceDeclKind) {
2415 // Reject invalid vardecls.
2416 for (unsigned i = 0; i != tuvNum; i++) {
2417 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2418 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2419 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002420 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002421 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002422 }
Chris Lattner682bf922009-03-29 16:50:03 +00002423 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002424 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002425 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002426
2427 for (unsigned i = 0; i != tuvNum; i++) {
2428 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002429 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2430 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002431 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2432 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002433
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002434 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002435 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002436}
2437
2438
2439/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2440/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002441static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002442CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002443 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002444}
2445
Ted Kremenek422bae72010-04-18 04:59:38 +00002446static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002447unsigned countAlignAttr(const AttrVec &A) {
2448 unsigned count=0;
2449 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2450 if ((*i)->getKind() == attr::Aligned)
2451 ++count;
2452 return count;
2453}
2454
2455static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002456bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2457 const AttrVec &A) {
2458 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002459 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002460 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002461 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002462
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002463 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002464 // But implementation has attributes. This is invalid.
2465 // Except when implementation has 'Align' attribute which is
2466 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002467 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002468 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002469
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002470 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002471
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002472 unsigned countAlignOnImpl = countAlignAttr(A);
2473 if (!countAlignOnImpl && (A.size() != D.size()))
2474 return true;
2475 else if (countAlignOnImpl) {
2476 unsigned countAlignOnDecl = countAlignAttr(D);
2477 if (countAlignOnDecl && (A.size() != D.size()))
2478 return true;
2479 else if (!countAlignOnDecl &&
2480 ((A.size()-countAlignOnImpl) != D.size()))
2481 return true;
2482 }
2483
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002484 // attributes on method declaration and definition must match exactly.
2485 // Note that we have at most a couple of attributes on methods, so this
2486 // n*n search is good enough.
2487 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002488 if ((*i)->getKind() == attr::Aligned)
2489 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002490 bool match = false;
2491 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2492 if ((*i)->getKind() == (*i1)->getKind()) {
2493 match = true;
2494 break;
2495 }
2496 }
2497 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002498 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002499 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002500
Sean Huntcf807c42010-08-18 23:23:40 +00002501 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002502}
2503
Douglas Gregor926df6c2011-06-11 01:09:30 +00002504/// \brief Check whether the declared result type of the given Objective-C
2505/// method declaration is compatible with the method's class.
2506///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002507static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002508CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2509 ObjCInterfaceDecl *CurrentClass) {
2510 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002511
2512 // If an Objective-C method inherits its related result type, then its
2513 // declared result type must be compatible with its own class type. The
2514 // declared result type is compatible if:
2515 if (const ObjCObjectPointerType *ResultObjectType
2516 = ResultType->getAs<ObjCObjectPointerType>()) {
2517 // - it is id or qualified id, or
2518 if (ResultObjectType->isObjCIdType() ||
2519 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002520 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002521
2522 if (CurrentClass) {
2523 if (ObjCInterfaceDecl *ResultClass
2524 = ResultObjectType->getInterfaceDecl()) {
2525 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002526 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002527 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002528
2529 // - it is a superclass of the method's class type
2530 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002531 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002532 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002533 } else {
2534 // Any Objective-C pointer type might be acceptable for a protocol
2535 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002536 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002537 }
2538 }
2539
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002540 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002541}
2542
John McCall6c2c2502011-07-22 02:45:48 +00002543namespace {
2544/// A helper class for searching for methods which a particular method
2545/// overrides.
2546class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002547public:
John McCall6c2c2502011-07-22 02:45:48 +00002548 Sema &S;
2549 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002550 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002551 bool Recursive;
2552
2553public:
2554 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2555 Selector selector = method->getSelector();
2556
2557 // Bypass this search if we've never seen an instance/class method
2558 // with this selector before.
2559 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2560 if (it == S.MethodPool.end()) {
2561 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002562 S.ReadMethodPool(selector);
2563
2564 it = S.MethodPool.find(selector);
2565 if (it == S.MethodPool.end())
2566 return;
John McCall6c2c2502011-07-22 02:45:48 +00002567 }
2568 ObjCMethodList &list =
2569 method->isInstanceMethod() ? it->second.first : it->second.second;
2570 if (!list.Method) return;
2571
2572 ObjCContainerDecl *container
2573 = cast<ObjCContainerDecl>(method->getDeclContext());
2574
2575 // Prevent the search from reaching this container again. This is
2576 // important with categories, which override methods from the
2577 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002578 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2579 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002580 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2581 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002582 } else {
2583 searchFromContainer(container);
2584 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002585 }
John McCall6c2c2502011-07-22 02:45:48 +00002586
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002587 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002588 iterator begin() const { return Overridden.begin(); }
2589 iterator end() const { return Overridden.end(); }
2590
2591private:
2592 void searchFromContainer(ObjCContainerDecl *container) {
2593 if (container->isInvalidDecl()) return;
2594
2595 switch (container->getDeclKind()) {
2596#define OBJCCONTAINER(type, base) \
2597 case Decl::type: \
2598 searchFrom(cast<type##Decl>(container)); \
2599 break;
2600#define ABSTRACT_DECL(expansion)
2601#define DECL(type, base) \
2602 case Decl::type:
2603#include "clang/AST/DeclNodes.inc"
2604 llvm_unreachable("not an ObjC container!");
2605 }
2606 }
2607
2608 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002609 if (!protocol->hasDefinition())
2610 return;
2611
John McCall6c2c2502011-07-22 02:45:48 +00002612 // A method in a protocol declaration overrides declarations from
2613 // referenced ("parent") protocols.
2614 search(protocol->getReferencedProtocols());
2615 }
2616
2617 void searchFrom(ObjCCategoryDecl *category) {
2618 // A method in a category declaration overrides declarations from
2619 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002620 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002621 search(category->getReferencedProtocols());
2622 }
2623
2624 void searchFrom(ObjCCategoryImplDecl *impl) {
2625 // A method in a category definition that has a category
2626 // declaration overrides declarations from the category
2627 // declaration.
2628 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2629 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002630 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2631 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002632
2633 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002634 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2635 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002636 }
2637 }
2638
2639 void searchFrom(ObjCInterfaceDecl *iface) {
2640 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002641 if (!iface->hasDefinition())
2642 return;
2643
John McCall6c2c2502011-07-22 02:45:48 +00002644 // - categories,
2645 for (ObjCCategoryDecl *category = iface->getCategoryList();
2646 category; category = category->getNextClassCategory())
2647 search(category);
2648
2649 // - the super class, and
2650 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2651 search(super);
2652
2653 // - any referenced protocols.
2654 search(iface->getReferencedProtocols());
2655 }
2656
2657 void searchFrom(ObjCImplementationDecl *impl) {
2658 // A method in a class implementation overrides declarations from
2659 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002660 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2661 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002662 }
2663
2664
2665 void search(const ObjCProtocolList &protocols) {
2666 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2667 i != e; ++i)
2668 search(*i);
2669 }
2670
2671 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002672 // Check for a method in this container which matches this selector.
2673 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2674 Method->isInstanceMethod());
2675
2676 // If we find one, record it and bail out.
2677 if (meth) {
2678 Overridden.insert(meth);
2679 return;
2680 }
2681
2682 // Otherwise, search for methods that a hypothetical method here
2683 // would have overridden.
2684
2685 // Note that we're now in a recursive case.
2686 Recursive = true;
2687
2688 searchFromContainer(container);
2689 }
2690};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002691}
2692
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002693void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2694 ObjCInterfaceDecl *CurrentClass,
2695 ResultTypeCompatibilityKind RTC) {
2696 // Search for overridden methods and merge information down from them.
2697 OverrideSearch overrides(*this, ObjCMethod);
2698 // Keep track if the method overrides any method in the class's base classes,
2699 // its protocols, or its categories' protocols; we will keep that info
2700 // in the ObjCMethodDecl.
2701 // For this info, a method in an implementation is not considered as
2702 // overriding the same method in the interface or its categories.
2703 bool hasOverriddenMethodsInBaseOrProtocol = false;
2704 for (OverrideSearch::iterator
2705 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2706 ObjCMethodDecl *overridden = *i;
2707
2708 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2709 CurrentClass != overridden->getClassInterface() ||
2710 overridden->isOverriding())
2711 hasOverriddenMethodsInBaseOrProtocol = true;
2712
2713 // Propagate down the 'related result type' bit from overridden methods.
2714 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2715 ObjCMethod->SetRelatedResultType();
2716
2717 // Then merge the declarations.
2718 mergeObjCMethodDecls(ObjCMethod, overridden);
2719
2720 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2721 continue; // Conflicting properties are detected elsewhere.
2722
2723 // Check for overriding methods
2724 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2725 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2726 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2727 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2728
2729 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002730 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2731 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002732 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2733 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002734 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2735 PrevE = overridden->param_end();
2736 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002737 assert(PrevI != overridden->param_end() && "Param mismatch");
2738 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2739 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2740 // If type of argument of method in this class does not match its
2741 // respective argument type in the super class method, issue warning;
2742 if (!Context.typesAreCompatible(T1, T2)) {
2743 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2744 << T1 << T2;
2745 Diag(overridden->getLocation(), diag::note_previous_declaration);
2746 break;
2747 }
2748 }
2749 }
2750 }
2751
2752 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2753}
2754
John McCalld226f652010-08-21 09:40:31 +00002755Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002756 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002757 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002758 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002759 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002760 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002761 Selector Sel,
2762 // optional arguments. The number of types/arguments is obtained
2763 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002764 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002765 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002766 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002767 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002768 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002769 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002770 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002771 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002772 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002773 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2774 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002775 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002776
Douglas Gregore97179c2011-09-08 01:46:34 +00002777 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002778 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002779 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002780 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002781
Steve Naroffccef3712009-02-20 22:59:16 +00002782 // Methods cannot return interface types. All ObjC objects are
2783 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002784 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002785 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2786 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002787 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002788 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002789
2790 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002791 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002792 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002793 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002794 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002795 }
Mike Stump1eb44332009-09-09 15:08:12 +00002796
2797 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002798 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002799 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002800 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002801 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002802 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002803 /*isSynthesized=*/false,
2804 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002805 MethodDeclKind == tok::objc_optional
2806 ? ObjCMethodDecl::Optional
2807 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002808 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002809
Chris Lattner5f9e2722011-07-23 10:55:15 +00002810 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Chris Lattner7db638d2009-04-11 19:42:43 +00002812 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002813 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002814 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002815
Chris Lattnere294d3f2009-04-11 18:57:04 +00002816 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002817 ArgType = Context.getObjCIdType();
2818 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002819 } else {
John McCall58e46772009-10-23 21:48:59 +00002820 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002821 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002822 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002823 }
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002825 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2826 LookupOrdinaryName, ForRedeclaration);
2827 LookupName(R, S);
2828 if (R.isSingleResult()) {
2829 NamedDecl *PrevDecl = R.getFoundDecl();
2830 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002831 Diag(ArgInfo[i].NameLoc,
2832 (MethodDefinition ? diag::warn_method_param_redefinition
2833 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002834 << ArgInfo[i].Name;
2835 Diag(PrevDecl->getLocation(),
2836 diag::note_previous_declaration);
2837 }
2838 }
2839
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002840 SourceLocation StartLoc = DI
2841 ? DI->getTypeLoc().getBeginLoc()
2842 : ArgInfo[i].NameLoc;
2843
John McCall81ef3e62011-04-23 02:46:06 +00002844 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2845 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2846 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002847
John McCall70798862011-05-02 00:30:12 +00002848 Param->setObjCMethodScopeInfo(i);
2849
Chris Lattner0ed844b2008-04-04 06:12:32 +00002850 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002851 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002853 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002854 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002855
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002856 if (Param->hasAttr<BlocksAttr>()) {
2857 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2858 Param->setInvalidDecl();
2859 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002860 S->AddDecl(Param);
2861 IdResolver.AddDecl(Param);
2862
Chris Lattner0ed844b2008-04-04 06:12:32 +00002863 Params.push_back(Param);
2864 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002865
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002866 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002867 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002868 QualType ArgType = Param->getType();
2869 if (ArgType.isNull())
2870 ArgType = Context.getObjCIdType();
2871 else
2872 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002873 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002874 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002875 Diag(Param->getLocation(),
2876 diag::err_object_cannot_be_passed_returned_by_value)
2877 << 1 << ArgType;
2878 Param->setInvalidDecl();
2879 }
2880 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002881
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002882 Params.push_back(Param);
2883 }
2884
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002885 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002886 ObjCMethod->setObjCDeclQualifier(
2887 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002888
2889 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002890 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002892 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002893 const ObjCMethodDecl *PrevMethod = 0;
2894 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002895 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002896 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2897 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002898 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002899 PrevMethod = ImpDecl->getClassMethod(Sel);
2900 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002901 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002902
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002903 ObjCMethodDecl *IMD = 0;
2904 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2905 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2906 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002907 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002908 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002909 SourceLocation MethodLoc = IMD->getLocation();
2910 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2911 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002912 Diag(MethodLoc, diag::note_method_declared_at)
2913 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002914 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002915 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002916 } else {
2917 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002918 }
John McCall6c2c2502011-07-22 02:45:48 +00002919
Chris Lattner4d391482007-12-12 07:09:47 +00002920 if (PrevMethod) {
2921 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002922 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002923 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002924 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002925 }
John McCall54abf7d2009-11-04 02:18:39 +00002926
Douglas Gregor926df6c2011-06-11 01:09:30 +00002927 // If this Objective-C method does not have a related result type, but we
2928 // are allowed to infer related result types, try to do so based on the
2929 // method family.
2930 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2931 if (!CurrentClass) {
2932 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2933 CurrentClass = Cat->getClassInterface();
2934 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2935 CurrentClass = Impl->getClassInterface();
2936 else if (ObjCCategoryImplDecl *CatImpl
2937 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2938 CurrentClass = CatImpl->getClassInterface();
2939 }
John McCall6c2c2502011-07-22 02:45:48 +00002940
Douglas Gregore97179c2011-09-08 01:46:34 +00002941 ResultTypeCompatibilityKind RTC
2942 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002943
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002944 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002945
John McCallf85e1932011-06-15 23:02:42 +00002946 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002947 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002948 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2949
Douglas Gregore97179c2011-09-08 01:46:34 +00002950 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002951 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002952 !ObjCMethod->hasRelatedResultType() &&
2953 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002954 bool InferRelatedResultType = false;
2955 switch (ObjCMethod->getMethodFamily()) {
2956 case OMF_None:
2957 case OMF_copy:
2958 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002959 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002960 case OMF_mutableCopy:
2961 case OMF_release:
2962 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002963 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002964 break;
2965
2966 case OMF_alloc:
2967 case OMF_new:
2968 InferRelatedResultType = ObjCMethod->isClassMethod();
2969 break;
2970
2971 case OMF_init:
2972 case OMF_autorelease:
2973 case OMF_retain:
2974 case OMF_self:
2975 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2976 break;
2977 }
2978
John McCall6c2c2502011-07-22 02:45:48 +00002979 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002980 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002981 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00002982
2983 ActOnDocumentableDecl(ObjCMethod);
2984
John McCalld226f652010-08-21 09:40:31 +00002985 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002986}
2987
Chris Lattnercc98eac2008-12-17 07:13:27 +00002988bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002989 // Following is also an error. But it is caused by a missing @end
2990 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002991 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002992 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002993
2994 // If we switched context to translation unit while we are still lexically in
2995 // an objc container, it means the parser missed emitting an error.
2996 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
2997 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002998
Anders Carlsson15281452008-11-04 16:57:32 +00002999 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3000 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Anders Carlsson15281452008-11-04 16:57:32 +00003002 return true;
3003}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003004
James Dennett1dfbd922012-06-14 21:40:34 +00003005/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003006/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003007void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003008 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003009 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003010 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003011 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003012 if (!Class) {
3013 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3014 return;
3015 }
John McCall260611a2012-06-20 06:18:46 +00003016 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003017 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3018 return;
3019 }
Mike Stump1eb44332009-09-09 15:08:12 +00003020
Chris Lattnercc98eac2008-12-17 07:13:27 +00003021 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003022 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003023 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003024 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003025 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003026 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003027 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003028 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3029 /*FIXME: StartL=*/ID->getLocation(),
3030 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003031 ID->getIdentifier(), ID->getType(),
3032 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003033 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003034 }
Mike Stump1eb44332009-09-09 15:08:12 +00003035
Chris Lattnercc98eac2008-12-17 07:13:27 +00003036 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003037 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003038 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003039 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003040 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003041 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003042 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003043 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003044 }
3045}
3046
Douglas Gregor160b5632010-04-26 17:32:49 +00003047/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003048VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3049 SourceLocation StartLoc,
3050 SourceLocation IdLoc,
3051 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003052 bool Invalid) {
3053 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3054 // duration shall not be qualified by an address-space qualifier."
3055 // Since all parameters have automatic store duration, they can not have
3056 // an address space.
3057 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003058 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003059 Invalid = true;
3060 }
3061
3062 // An @catch parameter must be an unqualified object pointer type;
3063 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3064 if (Invalid) {
3065 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003066 } else if (T->isDependentType()) {
3067 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003068 } else if (!T->isObjCObjectPointerType()) {
3069 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003070 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003071 } else if (T->isObjCQualifiedIdType()) {
3072 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003073 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003074 }
3075
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003076 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3077 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003078 New->setExceptionVariable(true);
3079
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003080 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003081 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003082 Invalid = true;
3083
Douglas Gregor160b5632010-04-26 17:32:49 +00003084 if (Invalid)
3085 New->setInvalidDecl();
3086 return New;
3087}
3088
John McCalld226f652010-08-21 09:40:31 +00003089Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003090 const DeclSpec &DS = D.getDeclSpec();
3091
3092 // We allow the "register" storage class on exception variables because
3093 // GCC did, but we drop it completely. Any other storage class is an error.
3094 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3095 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3096 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3097 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3098 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3099 << DS.getStorageClassSpec();
3100 }
3101 if (D.getDeclSpec().isThreadSpecified())
3102 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3103 D.getMutableDeclSpec().ClearStorageClassSpecs();
3104
3105 DiagnoseFunctionSpecifiers(D);
3106
3107 // Check that there are no default arguments inside the type of this
3108 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003109 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003110 CheckExtraCXXDefaultArguments(D);
3111
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003112 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003113 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003114
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003115 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3116 D.getSourceRange().getBegin(),
3117 D.getIdentifierLoc(),
3118 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003119 D.isInvalidType());
3120
3121 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3122 if (D.getCXXScopeSpec().isSet()) {
3123 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3124 << D.getCXXScopeSpec().getRange();
3125 New->setInvalidDecl();
3126 }
3127
3128 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003129 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003130 if (D.getIdentifier())
3131 IdResolver.AddDecl(New);
3132
3133 ProcessDeclAttributes(S, New, D);
3134
3135 if (New->hasAttr<BlocksAttr>())
3136 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003137 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003138}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003139
3140/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003141/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003142void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003143 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003144 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3145 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003146 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003147 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003148 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003149 }
3150}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003151
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003152void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003153 // Load referenced selectors from the external source.
3154 if (ExternalSource) {
3155 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3156 ExternalSource->ReadReferencedSelectors(Sels);
3157 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3158 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3159 }
3160
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003161 // Warning will be issued only when selector table is
3162 // generated (which means there is at lease one implementation
3163 // in the TU). This is to match gcc's behavior.
3164 if (ReferencedSelectors.empty() ||
3165 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003166 return;
3167 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3168 ReferencedSelectors.begin(),
3169 E = ReferencedSelectors.end(); S != E; ++S) {
3170 Selector Sel = (*S).first;
3171 if (!LookupImplementedMethodInGlobalPool(Sel))
3172 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3173 }
3174 return;
3175}