blob: 457cb1b9757df340d5421bfd4485cf27c89af32d [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();
Chris Lattner89951a82009-02-20 18:43:26 +0000316 if ((*PI)->getIdentifier())
317 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000318 }
John McCallf85e1932011-06-15 23:02:42 +0000319
320 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000321 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000322 switch (MDecl->getMethodFamily()) {
323 case OMF_retain:
324 case OMF_retainCount:
325 case OMF_release:
326 case OMF_autorelease:
327 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
328 << MDecl->getSelector();
329 break;
330
331 case OMF_None:
332 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000333 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000334 case OMF_alloc:
335 case OMF_init:
336 case OMF_mutableCopy:
337 case OMF_copy:
338 case OMF_new:
339 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000340 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000341 break;
342 }
343 }
344
Nico Weber9a1ecf02011-08-22 17:25:57 +0000345 // Warn on deprecated methods under -Wdeprecated-implementations,
346 // and prepare for warning on missing super calls.
347 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000348 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000349 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000350 DiagnoseObjCImplementedDeprecations(*this,
351 dyn_cast<NamedDecl>(IMD),
352 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000353
Nico Weber80cb6e62011-08-28 22:35:17 +0000354 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000355 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
356 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
357 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000358 if (IC->getSuperClass()) {
Eli Friedman95aac152012-08-01 21:02:59 +0000359 getCurFunction()->ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000360 !(Context.getLangOpts().ObjCAutoRefCount ||
361 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000362 MDecl->getMethodFamily() == OMF_dealloc;
Eli Friedman95aac152012-08-01 21:02:59 +0000363 getCurFunction()->ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000364 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000365 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000366 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000367 }
Chris Lattner4d391482007-12-12 07:09:47 +0000368}
369
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000370namespace {
371
372// Callback to only accept typo corrections that are Objective-C classes.
373// If an ObjCInterfaceDecl* is given to the constructor, then the validation
374// function will reject corrections to that class.
375class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
376 public:
377 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
378 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
379 : CurrentIDecl(IDecl) {}
380
381 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
382 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
383 return ID && !declaresSameEntity(ID, CurrentIDecl);
384 }
385
386 private:
387 ObjCInterfaceDecl *CurrentIDecl;
388};
389
390}
391
John McCalld226f652010-08-21 09:40:31 +0000392Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000393ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
394 IdentifierInfo *ClassName, SourceLocation ClassLoc,
395 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000396 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000397 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000398 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000399 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattner4d391482007-12-12 07:09:47 +0000401 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000402 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000403 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000404
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000405 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000406 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000407 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregor7723fec2011-12-15 20:29:51 +0000410 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000411 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000412 ObjCInterfaceDecl *IDecl
413 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000414 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000415
Douglas Gregor7723fec2011-12-15 20:29:51 +0000416 if (PrevIDecl) {
417 // Class already seen. Was it a definition?
418 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
419 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
420 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000421 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000422 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000423 }
Chris Lattner4d391482007-12-12 07:09:47 +0000424 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000425
426 if (AttrList)
427 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
428 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregor7723fec2011-12-15 20:29:51 +0000430 // Start the definition of this class. If we're in a redefinition case, there
431 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000432 if (!IDecl->hasDefinition())
433 IDecl->startDefinition();
434
Chris Lattner4d391482007-12-12 07:09:47 +0000435 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000436 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000437 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
438 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000439
440 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000441 // Try to correct for a typo in the superclass name without correcting
442 // to the class we're defining.
443 ObjCInterfaceValidatorCCC Validator(IDecl);
444 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000445 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000446 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000447 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
448 Diag(SuperLoc, diag::err_undef_superclass_suggest)
449 << SuperName << ClassName << PrevDecl->getDeclName();
450 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
451 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000452 }
453 }
454
Douglas Gregor60ef3082011-12-15 00:29:59 +0000455 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000456 Diag(SuperLoc, diag::err_recursive_superclass)
457 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000458 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000459 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000460 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000461 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000462
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000463 // Diagnose classes that inherit from deprecated classes.
464 if (SuperClassDecl)
465 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000467 if (PrevDecl && SuperClassDecl == 0) {
468 // The previous declaration was not a class decl. Check if we have a
469 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000470 if (const TypedefNameDecl *TDecl =
471 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000472 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000473 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000474 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
475 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000476 }
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000479 // This handles the following case:
480 //
481 // typedef int SuperClass;
482 // @interface MyClass : SuperClass {} @end
483 //
484 if (!SuperClassDecl) {
485 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
486 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000487 }
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Richard Smith162e1c12011-04-15 14:24:37 +0000490 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000491 if (!SuperClassDecl)
492 Diag(SuperLoc, diag::err_undef_superclass)
493 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000494 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000495 Context.getObjCInterfaceType(SuperClassDecl),
496 diag::err_forward_superclass,
497 SuperClassDecl->getDeclName(),
498 ClassName,
499 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000500 SuperClassDecl = 0;
501 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000502 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000503 IDecl->setSuperClass(SuperClassDecl);
504 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000505 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000506 }
Chris Lattner4d391482007-12-12 07:09:47 +0000507 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000508 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Sebastian Redl0b17c612010-08-13 00:28:03 +0000511 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000512 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000513 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000514 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000515 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Anders Carlsson15281452008-11-04 16:57:32 +0000518 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000519 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000520}
521
Richard Smithde01b7a2012-08-08 23:32:13 +0000522/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000523/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000524Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
525 IdentifierInfo *AliasName,
526 SourceLocation AliasLocation,
527 IdentifierInfo *ClassName,
528 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000529 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000530 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000531 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000532 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000533 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000534 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000535 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000536 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000537 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000538 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000539 }
540 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000541 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000542 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000543 if (const TypedefNameDecl *TDecl =
544 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000545 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000546 if (T->isObjCObjectType()) {
547 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000548 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000549 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000550 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000551 }
552 }
553 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000554 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
555 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000557 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000558 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000559 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000560 }
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000562 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000563 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000564 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Anders Carlsson15281452008-11-04 16:57:32 +0000566 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000567 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000568
John McCalld226f652010-08-21 09:40:31 +0000569 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000570}
571
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000572bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000573 IdentifierInfo *PName,
574 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000575 const ObjCList<ObjCProtocolDecl> &PList) {
576
577 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000578 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
579 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000580 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
581 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000582 if (PDecl->getIdentifier() == PName) {
583 Diag(Ploc, diag::err_protocol_has_circular_dependency);
584 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000585 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000586 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000587
588 if (!PDecl->hasDefinition())
589 continue;
590
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000591 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
592 PDecl->getLocation(), PDecl->getReferencedProtocols()))
593 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000594 }
595 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000596 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000597}
598
John McCalld226f652010-08-21 09:40:31 +0000599Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000600Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
601 IdentifierInfo *ProtocolName,
602 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000603 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000604 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000605 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000606 SourceLocation EndProtoLoc,
607 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000608 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000609 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000610 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000611 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
612 ForRedeclaration);
613 ObjCProtocolDecl *PDecl = 0;
614 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
615 // If we already have a definition, complain.
616 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
617 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregor27c6da22012-01-01 20:30:41 +0000619 // Create a new protocol that is completely distinct from previous
620 // declarations, and do not make this protocol available for name lookup.
621 // That way, we'll end up completely ignoring the duplicate.
622 // FIXME: Can we turn this into an error?
623 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
624 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000625 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000626 PDecl->startDefinition();
627 } else {
628 if (PrevDecl) {
629 // Check for circular dependencies among protocol declarations. This can
630 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000631 ObjCList<ObjCProtocolDecl> PList;
632 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
633 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000634 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000635 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000636
637 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000638 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000639 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000640 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000641
Douglas Gregor6e378de2009-04-23 23:18:26 +0000642 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000643 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000644 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000645
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000646 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000647 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000648
649 // Merge attributes from previous declarations.
650 if (PrevDecl)
651 mergeDeclAttributes(PDecl, PrevDecl);
652
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000653 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000654 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000655 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
656 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
659 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000660 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000661}
662
663/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000664/// issues an error if they are not declared. It returns list of
665/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000666void
Chris Lattnere13b9592008-07-26 04:03:38 +0000667Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000668 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000669 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000670 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000671 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000672 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
673 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000674 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000675 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000676 TypoCorrection Corrected = CorrectTypo(
677 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000678 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000679 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000680 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000681 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000682 Diag(PDecl->getLocation(), diag::note_previous_decl)
683 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000684 }
685 }
686
687 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000688 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000689 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000690 continue;
691 }
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000693 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000694
695 // If this is a forward declaration and we are supposed to warn in this
696 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000697 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000698 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000699 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000700 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000701 }
702}
703
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000704/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000705/// a class method in its extension.
706///
Mike Stump1eb44332009-09-09 15:08:12 +0000707void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000708 ObjCInterfaceDecl *ID) {
709 if (!ID)
710 return; // Possibly due to previous error
711
712 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000713 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
714 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000715 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000716 MethodMap[MD->getSelector()] = MD;
717 }
718
719 if (MethodMap.empty())
720 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000721 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
722 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000723 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000724 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
725 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
726 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
727 << Method->getDeclName();
728 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
729 }
730 }
731}
732
James Dennett1dfbd922012-06-14 21:40:34 +0000733/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000734Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000735Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000736 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000737 unsigned NumElts,
738 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000739 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000740 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000741 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000742 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
743 ForRedeclaration);
744 ObjCProtocolDecl *PDecl
745 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
746 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000747 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000748
749 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000750 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000751
Douglas Gregor3937f872012-01-01 20:33:24 +0000752 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000753 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000754
755 if (PrevDecl)
756 mergeDeclAttributes(PDecl, PrevDecl);
757
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000758 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000761 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000762}
763
John McCalld226f652010-08-21 09:40:31 +0000764Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000765ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
766 IdentifierInfo *ClassName, SourceLocation ClassLoc,
767 IdentifierInfo *CategoryName,
768 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000769 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000770 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000771 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000772 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000773 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000774 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000775
776 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000777
778 if (!IDecl
779 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000780 diag::err_category_forward_interface,
781 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000782 // Create an invalid ObjCCategoryDecl to serve as context for
783 // the enclosing method declarations. We mark the decl invalid
784 // to make it clear that this isn't a valid AST.
785 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000786 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000787 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000788 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000789
790 if (!IDecl)
791 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000792 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000793 }
794
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000795 if (!CategoryName && IDecl->getImplementation()) {
796 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
797 Diag(IDecl->getImplementation()->getLocation(),
798 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000799 }
800
Fariborz Jahanian25760612010-02-15 21:55:26 +0000801 if (CategoryName) {
802 /// Check for duplicate interface declaration for this category
803 ObjCCategoryDecl *CDeclChain;
804 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
805 CDeclChain = CDeclChain->getNextClassCategory()) {
806 if (CDeclChain->getIdentifier() == CategoryName) {
807 // Class extensions can be declared multiple times.
808 Diag(CategoryLoc, diag::warn_dup_category_def)
809 << ClassName << CategoryName;
810 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
811 break;
812 }
Chris Lattner70f19542009-02-16 21:26:43 +0000813 }
814 }
Chris Lattner70f19542009-02-16 21:26:43 +0000815
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000816 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
817 ClassLoc, CategoryLoc, CategoryName, IDecl);
818 // FIXME: PushOnScopeChains?
819 CurContext->addDecl(CDecl);
820
Chris Lattner4d391482007-12-12 07:09:47 +0000821 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000822 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000823 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000824 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000825 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000826 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000827 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Anders Carlsson15281452008-11-04 16:57:32 +0000830 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000831 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000832}
833
834/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000836/// object.
John McCalld226f652010-08-21 09:40:31 +0000837Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000838 SourceLocation AtCatImplLoc,
839 IdentifierInfo *ClassName, SourceLocation ClassLoc,
840 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000841 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000842 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000843 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000844 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
845 if (!CatIDecl) {
846 // Category @implementation with no corresponding @interface.
847 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000848 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
849 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000850 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000851 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000852 }
853 }
854
Mike Stump1eb44332009-09-09 15:08:12 +0000855 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000856 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000857 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000858 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000859 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000860 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000861 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000862 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
863 diag::err_undef_interface)) {
864 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000865 }
Chris Lattner4d391482007-12-12 07:09:47 +0000866
Douglas Gregord0434102009-01-09 00:49:46 +0000867 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000868 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000869
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000870 // If the interface is deprecated/unavailable, warn/error about it.
871 if (IDecl)
872 DiagnoseUseOfDecl(IDecl, ClassLoc);
873
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000874 /// Check that CatName, category name, is not used in another implementation.
875 if (CatIDecl) {
876 if (CatIDecl->getImplementation()) {
877 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
878 << CatName;
879 Diag(CatIDecl->getImplementation()->getLocation(),
880 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000881 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000882 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000883 // Warn on implementating category of deprecated class under
884 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000885 DiagnoseObjCImplementedDeprecations(*this,
886 dyn_cast<NamedDecl>(IDecl),
887 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000888 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Anders Carlsson15281452008-11-04 16:57:32 +0000891 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000892 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000893}
894
John McCalld226f652010-08-21 09:40:31 +0000895Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000896 SourceLocation AtClassImplLoc,
897 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000898 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000899 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000900 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000901 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000902 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000903 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
904 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000905 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000906 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000907 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000908 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000909 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
910 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000911 } else {
912 // We did not find anything with the name ClassName; try to correct for
913 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000914 ObjCInterfaceValidatorCCC Validator;
915 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000916 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000917 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000918 // Suggest the (potentially) correct interface name. However, put the
919 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000920 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000921 // provide a code-modification hint or use the typo name for recovery,
922 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000923 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000924 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000925 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000926 << ClassName << CorrectedName;
927 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
928 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000929 IDecl = 0;
930 } else {
931 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
932 }
Chris Lattner4d391482007-12-12 07:09:47 +0000933 }
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Chris Lattner4d391482007-12-12 07:09:47 +0000935 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000936 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000937 if (SuperClassname) {
938 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000939 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
940 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000941 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000942 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
943 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000944 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000945 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000946 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000947 if (SDecl && !SDecl->hasDefinition())
948 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000949 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000950 Diag(SuperClassLoc, diag::err_undef_superclass)
951 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000952 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000953 // This implementation and its interface do not have the same
954 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000955 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000956 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000957 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000958 }
959 }
960 }
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattner4d391482007-12-12 07:09:47 +0000962 if (!IDecl) {
963 // Legacy case of @implementation with no corresponding @interface.
964 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000965
Mike Stump390b4cc2009-05-16 07:39:55 +0000966 // FIXME: Do we support attributes on the @implementation? If so we should
967 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000968 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000969 ClassName, /*PrevDecl=*/0, ClassLoc,
970 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000971 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000972 if (SDecl) {
973 IDecl->setSuperClass(SDecl);
974 IDecl->setSuperClassLoc(SuperClassLoc);
975 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
976 } else {
977 IDecl->setEndOfDefinitionLoc(ClassLoc);
978 }
979
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000980 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000981 } else {
982 // Mark the interface as being completed, even if it was just as
983 // @class ....;
984 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000985 if (!IDecl->hasDefinition())
986 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
989 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000990 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
991 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Anders Carlsson15281452008-11-04 16:57:32 +0000993 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000994 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattner4d391482007-12-12 07:09:47 +0000996 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000997 if (IDecl->getImplementation()) {
998 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000999 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001000 Diag(IDecl->getImplementation()->getLocation(),
1001 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001002 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001003 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001004 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001005 // Warn on implementating deprecated class under
1006 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001007 DiagnoseObjCImplementedDeprecations(*this,
1008 dyn_cast<NamedDecl>(IDecl),
1009 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001010 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001011 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001012}
1013
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001014Sema::DeclGroupPtrTy
1015Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1016 SmallVector<Decl *, 64> DeclsInGroup;
1017 DeclsInGroup.reserve(Decls.size() + 1);
1018
1019 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1020 Decl *Dcl = Decls[i];
1021 if (!Dcl)
1022 continue;
1023 if (Dcl->getDeclContext()->isFileContext())
1024 Dcl->setTopLevelDeclInObjCContainer();
1025 DeclsInGroup.push_back(Dcl);
1026 }
1027
1028 DeclsInGroup.push_back(ObjCImpDecl);
1029
1030 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1031}
1032
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001033void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1034 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001035 SourceLocation RBrace) {
1036 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001037 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001038 if (!IDecl)
1039 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001040 /// Check case of non-existing \@interface decl.
1041 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001042 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001043 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001044 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001045 // Add ivar's to class's DeclContext.
1046 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001047 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001048 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001049 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001050 }
1051
Chris Lattner4d391482007-12-12 07:09:47 +00001052 return;
1053 }
1054 // If implementation has empty ivar list, just return.
1055 if (numIvars == 0)
1056 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattner4d391482007-12-12 07:09:47 +00001058 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001059 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001060 if (ImpDecl->getSuperClass())
1061 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1062 for (unsigned i = 0; i < numIvars; i++) {
1063 ObjCIvarDecl* ImplIvar = ivars[i];
1064 if (const ObjCIvarDecl *ClsIvar =
1065 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1066 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1067 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1068 continue;
1069 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001070 // Instance ivar to Implementation's DeclContext.
1071 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001072 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001073 ImpDecl->addDecl(ImplIvar);
1074 }
1075 return;
1076 }
Chris Lattner4d391482007-12-12 07:09:47 +00001077 // Check interface's Ivar list against those in the implementation.
1078 // names and types must match.
1079 //
Chris Lattner4d391482007-12-12 07:09:47 +00001080 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001081 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001082 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1083 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001084 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001085 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001086 assert (ImplIvar && "missing implementation ivar");
1087 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Steve Naroffca331292009-03-03 14:49:36 +00001089 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001090 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001091 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001092 << ImplIvar->getIdentifier()
1093 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001094 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001095 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1096 ImplIvar->getBitWidthValue(Context) !=
1097 ClsIvar->getBitWidthValue(Context)) {
1098 Diag(ImplIvar->getBitWidth()->getLocStart(),
1099 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1100 Diag(ClsIvar->getBitWidth()->getLocStart(),
1101 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001102 }
Steve Naroffca331292009-03-03 14:49:36 +00001103 // Make sure the names are identical.
1104 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001105 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001106 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001107 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001108 }
1109 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner609e4c72007-12-12 18:11:49 +00001112 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001113 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001114 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001115 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001116}
1117
Steve Naroff3c2eb662008-02-10 21:38:56 +00001118void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001119 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001120 // No point warning no definition of method which is 'unavailable'.
1121 if (method->hasAttr<UnavailableAttr>())
1122 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001123 if (!IncompleteImpl) {
1124 Diag(ImpLoc, diag::warn_incomplete_impl);
1125 IncompleteImpl = true;
1126 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001127 if (DiagID == diag::warn_unimplemented_protocol_method)
1128 Diag(ImpLoc, DiagID) << method->getDeclName();
1129 else
1130 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001131}
1132
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001133/// Determines if type B can be substituted for type A. Returns true if we can
1134/// guarantee that anything that the user will do to an object of type A can
1135/// also be done to an object of type B. This is trivially true if the two
1136/// types are the same, or if B is a subclass of A. It becomes more complex
1137/// in cases where protocols are involved.
1138///
1139/// Object types in Objective-C describe the minimum requirements for an
1140/// object, rather than providing a complete description of a type. For
1141/// example, if A is a subclass of B, then B* may refer to an instance of A.
1142/// The principle of substitutability means that we may use an instance of A
1143/// anywhere that we may use an instance of B - it will implement all of the
1144/// ivars of B and all of the methods of B.
1145///
1146/// This substitutability is important when type checking methods, because
1147/// the implementation may have stricter type definitions than the interface.
1148/// The interface specifies minimum requirements, but the implementation may
1149/// have more accurate ones. For example, a method may privately accept
1150/// instances of B, but only publish that it accepts instances of A. Any
1151/// object passed to it will be type checked against B, and so will implicitly
1152/// by a valid A*. Similarly, a method may return a subclass of the class that
1153/// it is declared as returning.
1154///
1155/// This is most important when considering subclassing. A method in a
1156/// subclass must accept any object as an argument that its superclass's
1157/// implementation accepts. It may, however, accept a more general type
1158/// without breaking substitutability (i.e. you can still use the subclass
1159/// anywhere that you can use the superclass, but not vice versa). The
1160/// converse requirement applies to return types: the return type for a
1161/// subclass method must be a valid object of the kind that the superclass
1162/// advertises, but it may be specified more accurately. This avoids the need
1163/// for explicit down-casting by callers.
1164///
1165/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001166static bool isObjCTypeSubstitutable(ASTContext &Context,
1167 const ObjCObjectPointerType *A,
1168 const ObjCObjectPointerType *B,
1169 bool rejectId) {
1170 // Reject a protocol-unqualified id.
1171 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001172
1173 // If B is a qualified id, then A must also be a qualified id and it must
1174 // implement all of the protocols in B. It may not be a qualified class.
1175 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1176 // stricter definition so it is not substitutable for id<A>.
1177 if (B->isObjCQualifiedIdType()) {
1178 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001179 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1180 QualType(B,0),
1181 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001182 }
1183
1184 /*
1185 // id is a special type that bypasses type checking completely. We want a
1186 // warning when it is used in one place but not another.
1187 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1188
1189
1190 // If B is a qualified id, then A must also be a qualified id (which it isn't
1191 // if we've got this far)
1192 if (B->isObjCQualifiedIdType()) return false;
1193 */
1194
1195 // Now we know that A and B are (potentially-qualified) class types. The
1196 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001197 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001198}
1199
John McCall10302c02010-10-28 02:34:38 +00001200static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1201 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1202}
1203
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001204static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001205 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001206 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001207 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001208 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001209 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001210 if (IsProtocolMethodDecl &&
1211 (MethodDecl->getObjCDeclQualifier() !=
1212 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001213 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001214 S.Diag(MethodImpl->getLocation(),
1215 (IsOverridingMode ?
1216 diag::warn_conflicting_overriding_ret_type_modifiers
1217 : diag::warn_conflicting_ret_type_modifiers))
1218 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001219 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1220 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1221 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1222 }
1223 else
1224 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001225 }
1226
John McCall10302c02010-10-28 02:34:38 +00001227 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001228 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001229 return true;
1230 if (!Warn)
1231 return false;
John McCall10302c02010-10-28 02:34:38 +00001232
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001233 unsigned DiagID =
1234 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1235 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001236
1237 // Mismatches between ObjC pointers go into a different warning
1238 // category, and sometimes they're even completely whitelisted.
1239 if (const ObjCObjectPointerType *ImplPtrTy =
1240 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1241 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001242 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001243 // Allow non-matching return types as long as they don't violate
1244 // the principle of substitutability. Specifically, we permit
1245 // return types that are subclasses of the declared return type,
1246 // or that are more-qualified versions of the declared type.
1247 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001248 return false;
John McCall10302c02010-10-28 02:34:38 +00001249
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001250 DiagID =
1251 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1252 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001253 }
1254 }
1255
1256 S.Diag(MethodImpl->getLocation(), DiagID)
1257 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001258 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001259 << MethodImpl->getResultType()
1260 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001261 S.Diag(MethodDecl->getLocation(),
1262 IsOverridingMode ? diag::note_previous_declaration
1263 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001264 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001265 return false;
John McCall10302c02010-10-28 02:34:38 +00001266}
1267
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001268static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001269 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001270 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001271 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001272 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001273 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001274 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001275 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001276 if (IsProtocolMethodDecl &&
1277 (ImplVar->getObjCDeclQualifier() !=
1278 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001279 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001280 if (IsOverridingMode)
1281 S.Diag(ImplVar->getLocation(),
1282 diag::warn_conflicting_overriding_param_modifiers)
1283 << getTypeRange(ImplVar->getTypeSourceInfo())
1284 << MethodImpl->getDeclName();
1285 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001286 diag::warn_conflicting_param_modifiers)
1287 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001288 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001289 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1290 << getTypeRange(IfaceVar->getTypeSourceInfo());
1291 }
1292 else
1293 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001294 }
1295
John McCall10302c02010-10-28 02:34:38 +00001296 QualType ImplTy = ImplVar->getType();
1297 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001298
John McCall10302c02010-10-28 02:34:38 +00001299 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001300 return true;
1301
1302 if (!Warn)
1303 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001304 unsigned DiagID =
1305 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1306 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001307
1308 // Mismatches between ObjC pointers go into a different warning
1309 // category, and sometimes they're even completely whitelisted.
1310 if (const ObjCObjectPointerType *ImplPtrTy =
1311 ImplTy->getAs<ObjCObjectPointerType>()) {
1312 if (const ObjCObjectPointerType *IfacePtrTy =
1313 IfaceTy->getAs<ObjCObjectPointerType>()) {
1314 // Allow non-matching argument types as long as they don't
1315 // violate the principle of substitutability. Specifically, the
1316 // implementation must accept any objects that the superclass
1317 // accepts, however it may also accept others.
1318 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001319 return false;
John McCall10302c02010-10-28 02:34:38 +00001320
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001321 DiagID =
1322 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1323 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001324 }
1325 }
1326
1327 S.Diag(ImplVar->getLocation(), DiagID)
1328 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001329 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1330 S.Diag(IfaceVar->getLocation(),
1331 (IsOverridingMode ? diag::note_previous_declaration
1332 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001333 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001334 return false;
John McCall10302c02010-10-28 02:34:38 +00001335}
John McCallf85e1932011-06-15 23:02:42 +00001336
1337/// In ARC, check whether the conventional meanings of the two methods
1338/// match. If they don't, it's a hard error.
1339static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1340 ObjCMethodDecl *decl) {
1341 ObjCMethodFamily implFamily = impl->getMethodFamily();
1342 ObjCMethodFamily declFamily = decl->getMethodFamily();
1343 if (implFamily == declFamily) return false;
1344
1345 // Since conventions are sorted by selector, the only possibility is
1346 // that the types differ enough to cause one selector or the other
1347 // to fall out of the family.
1348 assert(implFamily == OMF_None || declFamily == OMF_None);
1349
1350 // No further diagnostics required on invalid declarations.
1351 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1352
1353 const ObjCMethodDecl *unmatched = impl;
1354 ObjCMethodFamily family = declFamily;
1355 unsigned errorID = diag::err_arc_lost_method_convention;
1356 unsigned noteID = diag::note_arc_lost_method_convention;
1357 if (declFamily == OMF_None) {
1358 unmatched = decl;
1359 family = implFamily;
1360 errorID = diag::err_arc_gained_method_convention;
1361 noteID = diag::note_arc_gained_method_convention;
1362 }
1363
1364 // Indexes into a %select clause in the diagnostic.
1365 enum FamilySelector {
1366 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1367 };
1368 FamilySelector familySelector = FamilySelector();
1369
1370 switch (family) {
1371 case OMF_None: llvm_unreachable("logic error, no method convention");
1372 case OMF_retain:
1373 case OMF_release:
1374 case OMF_autorelease:
1375 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001376 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001377 case OMF_retainCount:
1378 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001379 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001380 // Mismatches for these methods don't change ownership
1381 // conventions, so we don't care.
1382 return false;
1383
1384 case OMF_init: familySelector = F_init; break;
1385 case OMF_alloc: familySelector = F_alloc; break;
1386 case OMF_copy: familySelector = F_copy; break;
1387 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1388 case OMF_new: familySelector = F_new; break;
1389 }
1390
1391 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1392 ReasonSelector reasonSelector;
1393
1394 // The only reason these methods don't fall within their families is
1395 // due to unusual result types.
1396 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1397 reasonSelector = R_UnrelatedReturn;
1398 } else {
1399 reasonSelector = R_NonObjectReturn;
1400 }
1401
1402 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1403 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1404
1405 return true;
1406}
John McCall10302c02010-10-28 02:34:38 +00001407
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001408void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001409 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001410 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001411 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001412 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1413 return;
1414
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001415 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001416 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001417 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Chris Lattner3aff9192009-04-11 19:58:42 +00001419 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001420 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1421 EF = MethodDecl->param_end();
1422 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001423 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001424 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001425 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001426
Fariborz Jahanian21121902011-08-08 18:03:17 +00001427 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001428 Diag(ImpMethodDecl->getLocation(),
1429 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001430 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001431 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001432}
1433
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001434void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1435 ObjCMethodDecl *Overridden,
1436 bool IsProtocolMethodDecl) {
1437
1438 CheckMethodOverrideReturn(*this, Method, Overridden,
1439 IsProtocolMethodDecl, true,
1440 true);
1441
1442 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001443 IF = Overridden->param_begin(), EM = Method->param_end(),
1444 EF = Overridden->param_end();
1445 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001446 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1447 IsProtocolMethodDecl, true, true);
1448 }
1449
1450 if (Method->isVariadic() != Overridden->isVariadic()) {
1451 Diag(Method->getLocation(),
1452 diag::warn_conflicting_overriding_variadic);
1453 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1454 }
1455}
1456
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001457/// WarnExactTypedMethods - This routine issues a warning if method
1458/// implementation declaration matches exactly that of its declaration.
1459void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1460 ObjCMethodDecl *MethodDecl,
1461 bool IsProtocolMethodDecl) {
1462 // don't issue warning when protocol method is optional because primary
1463 // class is not required to implement it and it is safe for protocol
1464 // to implement it.
1465 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1466 return;
1467 // don't issue warning when primary class's method is
1468 // depecated/unavailable.
1469 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1470 MethodDecl->hasAttr<DeprecatedAttr>())
1471 return;
1472
1473 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1474 IsProtocolMethodDecl, false, false);
1475 if (match)
1476 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001477 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1478 EF = MethodDecl->param_end();
1479 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001480 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1481 *IM, *IF,
1482 IsProtocolMethodDecl, false, false);
1483 if (!match)
1484 break;
1485 }
1486 if (match)
1487 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001488 if (match)
1489 match = !(MethodDecl->isClassMethod() &&
1490 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001491
1492 if (match) {
1493 Diag(ImpMethodDecl->getLocation(),
1494 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001495 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1496 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001497 }
1498}
1499
Mike Stump390b4cc2009-05-16 07:39:55 +00001500/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1501/// improve the efficiency of selector lookups and type checking by associating
1502/// with each protocol / interface / category the flattened instance tables. If
1503/// we used an immutable set to keep the table then it wouldn't add significant
1504/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001505
Steve Naroffefe7f362008-02-08 22:06:17 +00001506/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001507/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001508void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1509 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001510 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001511 const SelectorSet &InsMap,
1512 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001513 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001514 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1515 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1516 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001517 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1518
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001519 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001520 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001521 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001522 // check to see if class implements forwardInvocation method and objects
1523 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001524 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001525 // Under such conditions, which means that every method possible is
1526 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001527 // found" warnings.
1528 // FIXME: Use a general GetUnarySelector method for this.
1529 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1530 Selector fISelector = Context.Selectors.getSelector(1, &II);
1531 if (InsMap.count(fISelector))
1532 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1533 // need be implemented in the implementation.
1534 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001537 // If a method lookup fails locally we still need to look and see if
1538 // the method was implemented by a base class or an inherited
1539 // protocol. This lookup is slow, but occurs rarely in correct code
1540 // and otherwise would terminate in a warning.
1541
Chris Lattner4d391482007-12-12 07:09:47 +00001542 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001543 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001544 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001545 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001546 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001547 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001548 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001549 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001550 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001551 // If a method is not implemented in the category implementation but
1552 // has been declared in its primary class, superclass,
1553 // or in one of their protocols, no need to issue the warning.
1554 // This is because method will be implemented in the primary class
1555 // or one of its super class implementation.
1556
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001557 // Ugly, but necessary. Method declared in protcol might have
1558 // have been synthesized due to a property declared in the class which
1559 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001560 if (ObjCMethodDecl *MethodInClass =
1561 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001562 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001563 if (C || MethodInClass->isSynthesized())
1564 continue;
1565 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1566 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1567 != DiagnosticsEngine::Ignored) {
1568 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001569 Diag(method->getLocation(), diag::note_method_declared_at)
1570 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001571 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1572 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001573 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001574 }
1575 }
Chris Lattner4d391482007-12-12 07:09:47 +00001576 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001577 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001578 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001579 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001580 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001581 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1582 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001583 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001584 // See above comment for instance method lookups.
1585 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001586 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001587 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001588 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001589 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1590 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001591 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001592 Diag(method->getLocation(), diag::note_method_declared_at)
1593 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001594 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1595 PDecl->getDeclName();
1596 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001597 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001598 }
Chris Lattner780f3292008-07-21 21:32:27 +00001599 // Check on this protocols's referenced protocols, recursively.
1600 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1601 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001602 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001603}
1604
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001605/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001606/// or protocol against those declared in their implementations.
1607///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001608void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1609 const SelectorSet &ClsMap,
1610 SelectorSet &InsMapSeen,
1611 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001612 ObjCImplDecl* IMPDecl,
1613 ObjCContainerDecl* CDecl,
1614 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001615 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001616 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001617 // Check and see if instance methods in class interface have been
1618 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001619 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1620 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001621 if (InsMapSeen.count((*I)->getSelector()))
1622 continue;
1623 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001624 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001625 !InsMap.count((*I)->getSelector())) {
1626 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001627 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1628 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001629 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001630 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001631 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001632 IMPDecl->getInstanceMethod((*I)->getSelector());
1633 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1634 "Expected to find the method through lookup as well");
1635 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001636 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001637 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001638 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001639 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1640 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001641 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001642 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001643 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001644 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001645 }
1646 }
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001648 // Check and see if class methods in class interface have been
1649 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001650 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001651 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001652 if (ClsMapSeen.count((*I)->getSelector()))
1653 continue;
1654 ClsMapSeen.insert((*I)->getSelector());
1655 if (!ClsMap.count((*I)->getSelector())) {
1656 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001657 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1658 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001659 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001660 ObjCMethodDecl *ImpMethodDecl =
1661 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001662 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1663 "Expected to find the method through lookup as well");
1664 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001665 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001666 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1667 isa<ObjCProtocolDecl>(CDecl));
1668 else
1669 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001670 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001671 }
1672 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001673
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001674 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001675 // Also methods in class extensions need be looked at next.
1676 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1677 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1678 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1679 IMPDecl,
1680 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001681 IncompleteImpl, false,
1682 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001683
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001684 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001685 for (ObjCInterfaceDecl::all_protocol_iterator
1686 PI = I->all_referenced_protocol_begin(),
1687 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001688 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1689 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001690 (*PI), IncompleteImpl, false,
1691 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001692
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001693 // FIXME. For now, we are not checking for extact match of methods
1694 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001695 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001696 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001697 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001698 I->getSuperClass(), IncompleteImpl, false);
1699 }
1700}
1701
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001702/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1703/// category matches with those implemented in its primary class and
1704/// warns each time an exact match is found.
1705void Sema::CheckCategoryVsClassMethodMatches(
1706 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001707 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001708
1709 for (ObjCImplementationDecl::instmeth_iterator
1710 I = CatIMPDecl->instmeth_begin(),
1711 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1712 InsMap.insert((*I)->getSelector());
1713
1714 for (ObjCImplementationDecl::classmeth_iterator
1715 I = CatIMPDecl->classmeth_begin(),
1716 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1717 ClsMap.insert((*I)->getSelector());
1718 if (InsMap.empty() && ClsMap.empty())
1719 return;
1720
1721 // Get category's primary class.
1722 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1723 if (!CatDecl)
1724 return;
1725 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1726 if (!IDecl)
1727 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001728 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001729 bool IncompleteImpl = false;
1730 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1731 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001732 IncompleteImpl, false,
1733 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001734}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001735
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001736void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001737 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001738 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001739 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001740 // Check and see if instance methods in class interface have been
1741 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001742 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001743 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001744 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001746 // Check and see if properties declared in the interface have either 1)
1747 // an implementation or 2) there is a @synthesize/@dynamic implementation
1748 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001749 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001750 if (!(LangOpts.ObjCDefaultSynthProperties &&
1751 LangOpts.ObjCRuntime.isNonFragile()) ||
1752 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001753 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001754
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001755 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001756 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001757 I = IMPDecl->classmeth_begin(),
1758 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001759 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001761 // Check for type conflict of methods declared in a class/protocol and
1762 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001763 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001764 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1765 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001766 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001767
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001768 // check all methods implemented in category against those declared
1769 // in its primary class.
1770 if (ObjCCategoryImplDecl *CatDecl =
1771 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1772 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Chris Lattner4d391482007-12-12 07:09:47 +00001774 // Check the protocol list for unimplemented methods in the @implementation
1775 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001776 // Check and see if class methods in class interface have been
1777 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Chris Lattnercddc8882009-03-01 00:56:52 +00001779 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001780 for (ObjCInterfaceDecl::all_protocol_iterator
1781 PI = I->all_referenced_protocol_begin(),
1782 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001783 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001784 InsMap, ClsMap, I);
1785 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001786 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1787 Categories; Categories = Categories->getNextClassExtension())
1788 ImplMethodsVsClassMethods(S, IMPDecl,
1789 const_cast<ObjCCategoryDecl*>(Categories),
1790 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001791 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001792 // For extended class, unimplemented methods in its protocols will
1793 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001794 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001795 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1796 E = C->protocol_end(); PI != E; ++PI)
1797 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001798 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001799 // Report unimplemented properties in the category as well.
1800 // When reporting on missing setter/getters, do not report when
1801 // setter/getter is implemented in category's primary class
1802 // implementation.
1803 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1804 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1805 for (ObjCImplementationDecl::instmeth_iterator
1806 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1807 InsMap.insert((*I)->getSelector());
1808 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001809 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001810 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001811 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001812 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001813}
1814
Mike Stump1eb44332009-09-09 15:08:12 +00001815/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001816Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001817Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001818 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001819 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001820 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001821 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001822 for (unsigned i = 0; i != NumElts; ++i) {
1823 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001824 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001825 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001826 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001827 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001828 // Maybe we will complain about the shadowed template parameter.
1829 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1830 // Just pretend that we didn't see the previous declaration.
1831 PrevDecl = 0;
1832 }
1833
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001834 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001835 // GCC apparently allows the following idiom:
1836 //
1837 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1838 // @class XCElementToggler;
1839 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001840 // Here we have chosen to ignore the forward class declaration
1841 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001842 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001843 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001844 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001845 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001846 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001847 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001848 // to the underlying class. Just ignore the forward class with a warning
1849 // as this will force the intended behavior which is to lookup the typedef
1850 // name.
1851 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1852 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1853 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1854 continue;
1855 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001856 }
Chris Lattner4d391482007-12-12 07:09:47 +00001857 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001858
1859 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001860 ObjCInterfaceDecl *PrevIDecl
1861 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001862 ObjCInterfaceDecl *IDecl
1863 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001864 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001865 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001866
Douglas Gregor7723fec2011-12-15 20:29:51 +00001867 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001868 CheckObjCDeclScope(IDecl);
1869 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001870 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001871
1872 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001873}
1874
John McCall0f4c4c42011-06-16 01:15:19 +00001875static bool tryMatchRecordTypes(ASTContext &Context,
1876 Sema::MethodMatchStrategy strategy,
1877 const Type *left, const Type *right);
1878
John McCallf85e1932011-06-15 23:02:42 +00001879static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1880 QualType leftQT, QualType rightQT) {
1881 const Type *left =
1882 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1883 const Type *right =
1884 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1885
1886 if (left == right) return true;
1887
1888 // If we're doing a strict match, the types have to match exactly.
1889 if (strategy == Sema::MMS_strict) return false;
1890
1891 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1892
1893 // Otherwise, use this absurdly complicated algorithm to try to
1894 // validate the basic, low-level compatibility of the two types.
1895
1896 // As a minimum, require the sizes and alignments to match.
1897 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1898 return false;
1899
1900 // Consider all the kinds of non-dependent canonical types:
1901 // - functions and arrays aren't possible as return and parameter types
1902
1903 // - vector types of equal size can be arbitrarily mixed
1904 if (isa<VectorType>(left)) return isa<VectorType>(right);
1905 if (isa<VectorType>(right)) return false;
1906
1907 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001908 // - structs, unions, and Objective-C objects must match more-or-less
1909 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001910 // - everything else should be a scalar
1911 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001912 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001913
John McCall1d9b3b22011-09-09 05:25:32 +00001914 // Make scalars agree in kind, except count bools as chars, and group
1915 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001916 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1917 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1918 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1919 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001920 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1921 leftSK = Type::STK_ObjCObjectPointer;
1922 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1923 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001924
1925 // Note that data member pointers and function member pointers don't
1926 // intermix because of the size differences.
1927
1928 return (leftSK == rightSK);
1929}
Chris Lattner4d391482007-12-12 07:09:47 +00001930
John McCall0f4c4c42011-06-16 01:15:19 +00001931static bool tryMatchRecordTypes(ASTContext &Context,
1932 Sema::MethodMatchStrategy strategy,
1933 const Type *lt, const Type *rt) {
1934 assert(lt && rt && lt != rt);
1935
1936 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1937 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1938 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1939
1940 // Require union-hood to match.
1941 if (left->isUnion() != right->isUnion()) return false;
1942
1943 // Require an exact match if either is non-POD.
1944 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1945 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1946 return false;
1947
1948 // Require size and alignment to match.
1949 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1950
1951 // Require fields to match.
1952 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1953 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1954 for (; li != le && ri != re; ++li, ++ri) {
1955 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1956 return false;
1957 }
1958 return (li == le && ri == re);
1959}
1960
Chris Lattner4d391482007-12-12 07:09:47 +00001961/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1962/// returns true, or false, accordingly.
1963/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001964bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1965 const ObjCMethodDecl *right,
1966 MethodMatchStrategy strategy) {
1967 if (!matchTypes(Context, strategy,
1968 left->getResultType(), right->getResultType()))
1969 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001970
David Blaikie4e4d0842012-03-11 07:00:24 +00001971 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001972 (left->hasAttr<NSReturnsRetainedAttr>()
1973 != right->hasAttr<NSReturnsRetainedAttr>() ||
1974 left->hasAttr<NSConsumesSelfAttr>()
1975 != right->hasAttr<NSConsumesSelfAttr>()))
1976 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001978 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001979 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
1980 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001982 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00001983 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001984 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001985
1986 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1987 return false;
1988
David Blaikie4e4d0842012-03-11 07:00:24 +00001989 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001990 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1991 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001992 }
1993 return true;
1994}
1995
Douglas Gregorff310c72012-05-01 23:37:00 +00001996void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00001997 // If the list is empty, make it a singleton list.
1998 if (List->Method == 0) {
1999 List->Method = Method;
2000 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002001 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002002 }
2003
2004 // We've seen a method with this name, see if we have already seen this type
2005 // signature.
2006 ObjCMethodList *Previous = List;
2007 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002008 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002009 continue;
2010
2011 ObjCMethodDecl *PrevObjCMethod = List->Method;
2012
2013 // Propagate the 'defined' bit.
2014 if (Method->isDefined())
2015 PrevObjCMethod->setDefined(true);
2016
2017 // If a method is deprecated, push it in the global pool.
2018 // This is used for better diagnostics.
2019 if (Method->isDeprecated()) {
2020 if (!PrevObjCMethod->isDeprecated())
2021 List->Method = Method;
2022 }
2023 // If new method is unavailable, push it into global pool
2024 // unless previous one is deprecated.
2025 if (Method->isUnavailable()) {
2026 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2027 List->Method = Method;
2028 }
2029
Douglas Gregorff310c72012-05-01 23:37:00 +00002030 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002031 }
2032
2033 // We have a new signature for an existing method - add it.
2034 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002035 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002036 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2037}
2038
Sebastian Redldb9d2142010-08-02 23:18:59 +00002039/// \brief Read the contents of the method pool for a given selector from
2040/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002041void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002042 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002043 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002044}
2045
Douglas Gregorff310c72012-05-01 23:37:00 +00002046void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002047 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002048 // Ignore methods of invalid containers.
2049 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002050 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002051
Douglas Gregor0d266d62012-01-25 00:59:09 +00002052 if (ExternalSource)
2053 ReadMethodPool(Method->getSelector());
2054
Sebastian Redldb9d2142010-08-02 23:18:59 +00002055 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002056 if (Pos == MethodPool.end())
2057 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2058 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002059
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002060 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002061
Sebastian Redldb9d2142010-08-02 23:18:59 +00002062 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002063 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002064}
2065
John McCallf85e1932011-06-15 23:02:42 +00002066/// Determines if this is an "acceptable" loose mismatch in the global
2067/// method pool. This exists mostly as a hack to get around certain
2068/// global mismatches which we can't afford to make warnings / errors.
2069/// Really, what we want is a way to take a method out of the global
2070/// method pool.
2071static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2072 ObjCMethodDecl *other) {
2073 if (!chosen->isInstanceMethod())
2074 return false;
2075
2076 Selector sel = chosen->getSelector();
2077 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2078 return false;
2079
2080 // Don't complain about mismatches for -length if the method we
2081 // chose has an integral result type.
2082 return (chosen->getResultType()->isIntegerType());
2083}
2084
Sebastian Redldb9d2142010-08-02 23:18:59 +00002085ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002086 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002087 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002088 if (ExternalSource)
2089 ReadMethodPool(Sel);
2090
Sebastian Redldb9d2142010-08-02 23:18:59 +00002091 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002092 if (Pos == MethodPool.end())
2093 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002094
Sebastian Redldb9d2142010-08-02 23:18:59 +00002095 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002096
Sebastian Redldb9d2142010-08-02 23:18:59 +00002097 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002098 bool issueDiagnostic = false, issueError = false;
2099
2100 // We support a warning which complains about *any* difference in
2101 // method signature.
2102 bool strictSelectorMatch =
2103 (receiverIdOrClass && warn &&
2104 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2105 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002106 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002107 if (strictSelectorMatch)
2108 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002109 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2110 MMS_strict)) {
2111 issueDiagnostic = true;
2112 break;
2113 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002114 }
2115
John McCallf85e1932011-06-15 23:02:42 +00002116 // If we didn't see any strict differences, we won't see any loose
2117 // differences. In ARC, however, we also need to check for loose
2118 // mismatches, because most of them are errors.
2119 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002120 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002121 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002122 // This checks if the methods differ in type mismatch.
2123 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2124 MMS_loose) &&
2125 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2126 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002127 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002128 issueError = true;
2129 break;
2130 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002131 }
2132
John McCallf85e1932011-06-15 23:02:42 +00002133 if (issueDiagnostic) {
2134 if (issueError)
2135 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2136 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002137 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2138 else
2139 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002140
2141 Diag(MethList.Method->getLocStart(),
2142 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002143 << MethList.Method->getSourceRange();
2144 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2145 Diag(Next->Method->getLocStart(), diag::note_also_found)
2146 << Next->Method->getSourceRange();
2147 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002148 }
2149 return MethList.Method;
2150}
2151
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002152ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002153 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2154 if (Pos == MethodPool.end())
2155 return 0;
2156
2157 GlobalMethods &Methods = Pos->second;
2158
2159 if (Methods.first.Method && Methods.first.Method->isDefined())
2160 return Methods.first.Method;
2161 if (Methods.second.Method && Methods.second.Method->isDefined())
2162 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002163 return 0;
2164}
2165
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002166/// DiagnoseDuplicateIvars -
2167/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002168/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002169/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002170/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002171void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2172 ObjCInterfaceDecl *SID) {
2173 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2174 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002175 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002176 if (Ivar->isInvalidDecl())
2177 continue;
2178 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2179 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2180 if (prevIvar) {
2181 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2182 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2183 Ivar->setInvalidDecl();
2184 }
2185 }
2186 }
2187}
2188
Erik Verbruggend64251f2011-12-06 09:25:23 +00002189Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2190 switch (CurContext->getDeclKind()) {
2191 case Decl::ObjCInterface:
2192 return Sema::OCK_Interface;
2193 case Decl::ObjCProtocol:
2194 return Sema::OCK_Protocol;
2195 case Decl::ObjCCategory:
2196 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2197 return Sema::OCK_ClassExtension;
2198 else
2199 return Sema::OCK_Category;
2200 case Decl::ObjCImplementation:
2201 return Sema::OCK_Implementation;
2202 case Decl::ObjCCategoryImpl:
2203 return Sema::OCK_CategoryImplementation;
2204
2205 default:
2206 return Sema::OCK_None;
2207 }
2208}
2209
Steve Naroffa56f6162007-12-18 01:30:32 +00002210// Note: For class/category implemenations, allMethods/allProperties is
2211// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002212Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2213 Decl **allMethods, unsigned allNum,
2214 Decl **allProperties, unsigned pNum,
2215 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002216
Erik Verbruggend64251f2011-12-06 09:25:23 +00002217 if (getObjCContainerKind() == Sema::OCK_None)
2218 return 0;
2219
2220 assert(AtEnd.isValid() && "Invalid location for '@end'");
2221
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002222 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2223 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002224
Mike Stump1eb44332009-09-09 15:08:12 +00002225 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002226 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2227 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002228 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002229
Steve Naroff0701bbb2009-01-08 17:28:14 +00002230 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2231 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2232 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2233
Chris Lattner4d391482007-12-12 07:09:47 +00002234 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002235 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002236 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002237
2238 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002239 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002240 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002241 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002242 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002243 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002244 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002245 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002246 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002247 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002248 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002249 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002250 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002251 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002252 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002253 if (!Context.getSourceManager().isInSystemHeader(
2254 Method->getLocation()))
2255 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2256 << Method->getDeclName();
2257 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2258 }
Chris Lattner4d391482007-12-12 07:09:47 +00002259 InsMap[Method->getSelector()] = Method;
2260 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002261 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002262 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002263 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002264 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002265 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002266 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002267 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002268 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002269 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002270 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002271 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002272 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002273 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002274 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002275 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002276 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002277 if (!Context.getSourceManager().isInSystemHeader(
2278 Method->getLocation()))
2279 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2280 << Method->getDeclName();
2281 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2282 }
Chris Lattner4d391482007-12-12 07:09:47 +00002283 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002284 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002285 }
2286 }
2287 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002288 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002289 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002290 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002291 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002292 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002293 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002294 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002295 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002296 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002297
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002298 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002299 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002300 if (C->IsClassExtension()) {
2301 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2302 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002303 }
Chris Lattner4d391482007-12-12 07:09:47 +00002304 }
Steve Naroff09c47192009-01-09 15:36:25 +00002305 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002306 if (CDecl->getIdentifier())
2307 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2308 // user-defined setter/getter. It also synthesizes setter/getter methods
2309 // and adds them to the DeclContext and global method pools.
2310 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2311 E = CDecl->prop_end();
2312 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002313 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002314 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002315 }
2316 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002317 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002318 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002319 // Any property declared in a class extension might have user
2320 // declared setter or getter in current class extension or one
2321 // of the other class extensions. Mark them as synthesized as
2322 // property will be synthesized when property with same name is
2323 // seen in the @implementation.
2324 for (const ObjCCategoryDecl *ClsExtDecl =
2325 IDecl->getFirstClassExtension();
2326 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2327 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2328 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002329 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002330 // Skip over properties declared @dynamic
2331 if (const ObjCPropertyImplDecl *PIDecl
2332 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2333 if (PIDecl->getPropertyImplementation()
2334 == ObjCPropertyImplDecl::Dynamic)
2335 continue;
2336
2337 for (const ObjCCategoryDecl *CExtDecl =
2338 IDecl->getFirstClassExtension();
2339 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2340 if (ObjCMethodDecl *GetterMethod =
2341 CExtDecl->getInstanceMethod(Property->getGetterName()))
2342 GetterMethod->setSynthesized(true);
2343 if (!Property->isReadOnly())
2344 if (ObjCMethodDecl *SetterMethod =
2345 CExtDecl->getInstanceMethod(Property->getSetterName()))
2346 SetterMethod->setSynthesized(true);
2347 }
2348 }
2349 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002350 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002351 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002352 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002353
Patrick Beardb2f68202012-04-06 18:12:22 +00002354 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2355 if (IDecl->getSuperClass() == NULL) {
2356 // This class has no superclass, so check that it has been marked with
2357 // __attribute((objc_root_class)).
2358 if (!HasRootClassAttr) {
2359 SourceLocation DeclLoc(IDecl->getLocation());
2360 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2361 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2362 << IDecl->getIdentifier();
2363 // See if NSObject is in the current scope, and if it is, suggest
2364 // adding " : NSObject " to the class declaration.
2365 NamedDecl *IF = LookupSingleName(TUScope,
2366 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2367 DeclLoc, LookupOrdinaryName);
2368 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2369 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2370 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2371 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2372 } else {
2373 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2374 }
2375 }
2376 } else if (HasRootClassAttr) {
2377 // Complain that only root classes may have this attribute.
2378 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2379 }
2380
John McCall260611a2012-06-20 06:18:46 +00002381 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002382 while (IDecl->getSuperClass()) {
2383 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2384 IDecl = IDecl->getSuperClass();
2385 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002386 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002387 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002388 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002389 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002390 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002391 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002392
Chris Lattner4d391482007-12-12 07:09:47 +00002393 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002394 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002395 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002396 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002397 Categories; Categories = Categories->getNextClassCategory()) {
2398 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002399 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002400 break;
2401 }
2402 }
2403 }
2404 }
Chris Lattner682bf922009-03-29 16:50:03 +00002405 if (isInterfaceDeclKind) {
2406 // Reject invalid vardecls.
2407 for (unsigned i = 0; i != tuvNum; i++) {
2408 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2409 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2410 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002411 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002412 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002413 }
Chris Lattner682bf922009-03-29 16:50:03 +00002414 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002415 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002416 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002417
2418 for (unsigned i = 0; i != tuvNum; i++) {
2419 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002420 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2421 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002422 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2423 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002424
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002425 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002426 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002427}
2428
2429
2430/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2431/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002432static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002433CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002434 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002435}
2436
Ted Kremenek422bae72010-04-18 04:59:38 +00002437static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002438unsigned countAlignAttr(const AttrVec &A) {
2439 unsigned count=0;
2440 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2441 if ((*i)->getKind() == attr::Aligned)
2442 ++count;
2443 return count;
2444}
2445
2446static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002447bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2448 const AttrVec &A) {
2449 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002450 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002451 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002452 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002453
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002454 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002455 // But implementation has attributes. This is invalid.
2456 // Except when implementation has 'Align' attribute which is
2457 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002458 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002459 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002460
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002461 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002462
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002463 unsigned countAlignOnImpl = countAlignAttr(A);
2464 if (!countAlignOnImpl && (A.size() != D.size()))
2465 return true;
2466 else if (countAlignOnImpl) {
2467 unsigned countAlignOnDecl = countAlignAttr(D);
2468 if (countAlignOnDecl && (A.size() != D.size()))
2469 return true;
2470 else if (!countAlignOnDecl &&
2471 ((A.size()-countAlignOnImpl) != D.size()))
2472 return true;
2473 }
2474
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002475 // attributes on method declaration and definition must match exactly.
2476 // Note that we have at most a couple of attributes on methods, so this
2477 // n*n search is good enough.
2478 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002479 if ((*i)->getKind() == attr::Aligned)
2480 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002481 bool match = false;
2482 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2483 if ((*i)->getKind() == (*i1)->getKind()) {
2484 match = true;
2485 break;
2486 }
2487 }
2488 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002489 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002490 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002491
Sean Huntcf807c42010-08-18 23:23:40 +00002492 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002493}
2494
Douglas Gregor926df6c2011-06-11 01:09:30 +00002495/// \brief Check whether the declared result type of the given Objective-C
2496/// method declaration is compatible with the method's class.
2497///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002498static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002499CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2500 ObjCInterfaceDecl *CurrentClass) {
2501 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002502
2503 // If an Objective-C method inherits its related result type, then its
2504 // declared result type must be compatible with its own class type. The
2505 // declared result type is compatible if:
2506 if (const ObjCObjectPointerType *ResultObjectType
2507 = ResultType->getAs<ObjCObjectPointerType>()) {
2508 // - it is id or qualified id, or
2509 if (ResultObjectType->isObjCIdType() ||
2510 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002511 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002512
2513 if (CurrentClass) {
2514 if (ObjCInterfaceDecl *ResultClass
2515 = ResultObjectType->getInterfaceDecl()) {
2516 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002517 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002518 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002519
2520 // - it is a superclass of the method's class type
2521 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002522 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002523 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002524 } else {
2525 // Any Objective-C pointer type might be acceptable for a protocol
2526 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002527 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002528 }
2529 }
2530
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002531 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002532}
2533
John McCall6c2c2502011-07-22 02:45:48 +00002534namespace {
2535/// A helper class for searching for methods which a particular method
2536/// overrides.
2537class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002538public:
John McCall6c2c2502011-07-22 02:45:48 +00002539 Sema &S;
2540 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002541 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002542 bool Recursive;
2543
2544public:
2545 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2546 Selector selector = method->getSelector();
2547
2548 // Bypass this search if we've never seen an instance/class method
2549 // with this selector before.
2550 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2551 if (it == S.MethodPool.end()) {
2552 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002553 S.ReadMethodPool(selector);
2554
2555 it = S.MethodPool.find(selector);
2556 if (it == S.MethodPool.end())
2557 return;
John McCall6c2c2502011-07-22 02:45:48 +00002558 }
2559 ObjCMethodList &list =
2560 method->isInstanceMethod() ? it->second.first : it->second.second;
2561 if (!list.Method) return;
2562
2563 ObjCContainerDecl *container
2564 = cast<ObjCContainerDecl>(method->getDeclContext());
2565
2566 // Prevent the search from reaching this container again. This is
2567 // important with categories, which override methods from the
2568 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002569 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2570 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002571 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2572 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002573 } else {
2574 searchFromContainer(container);
2575 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002576 }
John McCall6c2c2502011-07-22 02:45:48 +00002577
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002578 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002579 iterator begin() const { return Overridden.begin(); }
2580 iterator end() const { return Overridden.end(); }
2581
2582private:
2583 void searchFromContainer(ObjCContainerDecl *container) {
2584 if (container->isInvalidDecl()) return;
2585
2586 switch (container->getDeclKind()) {
2587#define OBJCCONTAINER(type, base) \
2588 case Decl::type: \
2589 searchFrom(cast<type##Decl>(container)); \
2590 break;
2591#define ABSTRACT_DECL(expansion)
2592#define DECL(type, base) \
2593 case Decl::type:
2594#include "clang/AST/DeclNodes.inc"
2595 llvm_unreachable("not an ObjC container!");
2596 }
2597 }
2598
2599 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002600 if (!protocol->hasDefinition())
2601 return;
2602
John McCall6c2c2502011-07-22 02:45:48 +00002603 // A method in a protocol declaration overrides declarations from
2604 // referenced ("parent") protocols.
2605 search(protocol->getReferencedProtocols());
2606 }
2607
2608 void searchFrom(ObjCCategoryDecl *category) {
2609 // A method in a category declaration overrides declarations from
2610 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002611 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002612 search(category->getReferencedProtocols());
2613 }
2614
2615 void searchFrom(ObjCCategoryImplDecl *impl) {
2616 // A method in a category definition that has a category
2617 // declaration overrides declarations from the category
2618 // declaration.
2619 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2620 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002621 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2622 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002623
2624 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002625 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2626 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002627 }
2628 }
2629
2630 void searchFrom(ObjCInterfaceDecl *iface) {
2631 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002632 if (!iface->hasDefinition())
2633 return;
2634
John McCall6c2c2502011-07-22 02:45:48 +00002635 // - categories,
2636 for (ObjCCategoryDecl *category = iface->getCategoryList();
2637 category; category = category->getNextClassCategory())
2638 search(category);
2639
2640 // - the super class, and
2641 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2642 search(super);
2643
2644 // - any referenced protocols.
2645 search(iface->getReferencedProtocols());
2646 }
2647
2648 void searchFrom(ObjCImplementationDecl *impl) {
2649 // A method in a class implementation overrides declarations from
2650 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002651 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2652 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002653 }
2654
2655
2656 void search(const ObjCProtocolList &protocols) {
2657 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2658 i != e; ++i)
2659 search(*i);
2660 }
2661
2662 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002663 // Check for a method in this container which matches this selector.
2664 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2665 Method->isInstanceMethod());
2666
2667 // If we find one, record it and bail out.
2668 if (meth) {
2669 Overridden.insert(meth);
2670 return;
2671 }
2672
2673 // Otherwise, search for methods that a hypothetical method here
2674 // would have overridden.
2675
2676 // Note that we're now in a recursive case.
2677 Recursive = true;
2678
2679 searchFromContainer(container);
2680 }
2681};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002682}
2683
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002684void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2685 ObjCInterfaceDecl *CurrentClass,
2686 ResultTypeCompatibilityKind RTC) {
2687 // Search for overridden methods and merge information down from them.
2688 OverrideSearch overrides(*this, ObjCMethod);
2689 // Keep track if the method overrides any method in the class's base classes,
2690 // its protocols, or its categories' protocols; we will keep that info
2691 // in the ObjCMethodDecl.
2692 // For this info, a method in an implementation is not considered as
2693 // overriding the same method in the interface or its categories.
2694 bool hasOverriddenMethodsInBaseOrProtocol = false;
2695 for (OverrideSearch::iterator
2696 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2697 ObjCMethodDecl *overridden = *i;
2698
2699 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2700 CurrentClass != overridden->getClassInterface() ||
2701 overridden->isOverriding())
2702 hasOverriddenMethodsInBaseOrProtocol = true;
2703
2704 // Propagate down the 'related result type' bit from overridden methods.
2705 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2706 ObjCMethod->SetRelatedResultType();
2707
2708 // Then merge the declarations.
2709 mergeObjCMethodDecls(ObjCMethod, overridden);
2710
2711 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2712 continue; // Conflicting properties are detected elsewhere.
2713
2714 // Check for overriding methods
2715 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2716 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2717 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2718 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2719
2720 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002721 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2722 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002723 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2724 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002725 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2726 PrevE = overridden->param_end();
2727 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002728 assert(PrevI != overridden->param_end() && "Param mismatch");
2729 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2730 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2731 // If type of argument of method in this class does not match its
2732 // respective argument type in the super class method, issue warning;
2733 if (!Context.typesAreCompatible(T1, T2)) {
2734 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2735 << T1 << T2;
2736 Diag(overridden->getLocation(), diag::note_previous_declaration);
2737 break;
2738 }
2739 }
2740 }
2741 }
2742
2743 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2744}
2745
John McCalld226f652010-08-21 09:40:31 +00002746Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002747 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002748 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002749 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002750 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002751 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002752 Selector Sel,
2753 // optional arguments. The number of types/arguments is obtained
2754 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002755 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002756 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002757 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002758 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002759 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002760 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002761 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002762 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002763 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002764 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2765 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002766 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Douglas Gregore97179c2011-09-08 01:46:34 +00002768 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002769 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002770 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002771 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002772
Steve Naroffccef3712009-02-20 22:59:16 +00002773 // Methods cannot return interface types. All ObjC objects are
2774 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002775 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002776 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2777 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002778 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002779 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002780
2781 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002782 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002783 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002784 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002785 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002786 }
Mike Stump1eb44332009-09-09 15:08:12 +00002787
2788 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002789 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002790 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002791 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002792 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002793 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002794 /*isSynthesized=*/false,
2795 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002796 MethodDeclKind == tok::objc_optional
2797 ? ObjCMethodDecl::Optional
2798 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002799 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002800
Chris Lattner5f9e2722011-07-23 10:55:15 +00002801 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Chris Lattner7db638d2009-04-11 19:42:43 +00002803 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002804 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002805 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Chris Lattnere294d3f2009-04-11 18:57:04 +00002807 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002808 ArgType = Context.getObjCIdType();
2809 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002810 } else {
John McCall58e46772009-10-23 21:48:59 +00002811 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002812 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002813 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002814 }
Mike Stump1eb44332009-09-09 15:08:12 +00002815
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002816 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2817 LookupOrdinaryName, ForRedeclaration);
2818 LookupName(R, S);
2819 if (R.isSingleResult()) {
2820 NamedDecl *PrevDecl = R.getFoundDecl();
2821 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002822 Diag(ArgInfo[i].NameLoc,
2823 (MethodDefinition ? diag::warn_method_param_redefinition
2824 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002825 << ArgInfo[i].Name;
2826 Diag(PrevDecl->getLocation(),
2827 diag::note_previous_declaration);
2828 }
2829 }
2830
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002831 SourceLocation StartLoc = DI
2832 ? DI->getTypeLoc().getBeginLoc()
2833 : ArgInfo[i].NameLoc;
2834
John McCall81ef3e62011-04-23 02:46:06 +00002835 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2836 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2837 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002838
John McCall70798862011-05-02 00:30:12 +00002839 Param->setObjCMethodScopeInfo(i);
2840
Chris Lattner0ed844b2008-04-04 06:12:32 +00002841 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002842 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002844 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002845 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002846
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002847 if (Param->hasAttr<BlocksAttr>()) {
2848 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2849 Param->setInvalidDecl();
2850 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002851 S->AddDecl(Param);
2852 IdResolver.AddDecl(Param);
2853
Chris Lattner0ed844b2008-04-04 06:12:32 +00002854 Params.push_back(Param);
2855 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002856
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002857 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002858 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002859 QualType ArgType = Param->getType();
2860 if (ArgType.isNull())
2861 ArgType = Context.getObjCIdType();
2862 else
2863 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002864 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002865 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002866 Diag(Param->getLocation(),
2867 diag::err_object_cannot_be_passed_returned_by_value)
2868 << 1 << ArgType;
2869 Param->setInvalidDecl();
2870 }
2871 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002872
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002873 Params.push_back(Param);
2874 }
2875
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002876 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002877 ObjCMethod->setObjCDeclQualifier(
2878 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002879
2880 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002881 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002883 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002884 const ObjCMethodDecl *PrevMethod = 0;
2885 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002886 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002887 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2888 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002889 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002890 PrevMethod = ImpDecl->getClassMethod(Sel);
2891 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002892 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002893
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002894 ObjCMethodDecl *IMD = 0;
2895 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2896 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2897 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002898 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002899 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002900 SourceLocation MethodLoc = IMD->getLocation();
2901 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2902 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002903 Diag(MethodLoc, diag::note_method_declared_at)
2904 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002905 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002906 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002907 } else {
2908 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002909 }
John McCall6c2c2502011-07-22 02:45:48 +00002910
Chris Lattner4d391482007-12-12 07:09:47 +00002911 if (PrevMethod) {
2912 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002913 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002914 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002915 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002916 }
John McCall54abf7d2009-11-04 02:18:39 +00002917
Douglas Gregor926df6c2011-06-11 01:09:30 +00002918 // If this Objective-C method does not have a related result type, but we
2919 // are allowed to infer related result types, try to do so based on the
2920 // method family.
2921 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2922 if (!CurrentClass) {
2923 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2924 CurrentClass = Cat->getClassInterface();
2925 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2926 CurrentClass = Impl->getClassInterface();
2927 else if (ObjCCategoryImplDecl *CatImpl
2928 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2929 CurrentClass = CatImpl->getClassInterface();
2930 }
John McCall6c2c2502011-07-22 02:45:48 +00002931
Douglas Gregore97179c2011-09-08 01:46:34 +00002932 ResultTypeCompatibilityKind RTC
2933 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002934
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002935 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002936
John McCallf85e1932011-06-15 23:02:42 +00002937 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002938 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002939 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2940
Douglas Gregore97179c2011-09-08 01:46:34 +00002941 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002942 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002943 !ObjCMethod->hasRelatedResultType() &&
2944 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002945 bool InferRelatedResultType = false;
2946 switch (ObjCMethod->getMethodFamily()) {
2947 case OMF_None:
2948 case OMF_copy:
2949 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002950 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002951 case OMF_mutableCopy:
2952 case OMF_release:
2953 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002954 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002955 break;
2956
2957 case OMF_alloc:
2958 case OMF_new:
2959 InferRelatedResultType = ObjCMethod->isClassMethod();
2960 break;
2961
2962 case OMF_init:
2963 case OMF_autorelease:
2964 case OMF_retain:
2965 case OMF_self:
2966 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2967 break;
2968 }
2969
John McCall6c2c2502011-07-22 02:45:48 +00002970 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002971 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002972 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00002973
2974 ActOnDocumentableDecl(ObjCMethod);
2975
John McCalld226f652010-08-21 09:40:31 +00002976 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002977}
2978
Chris Lattnercc98eac2008-12-17 07:13:27 +00002979bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002980 // Following is also an error. But it is caused by a missing @end
2981 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002982 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002983 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002984
2985 // If we switched context to translation unit while we are still lexically in
2986 // an objc container, it means the parser missed emitting an error.
2987 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
2988 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002989
Anders Carlsson15281452008-11-04 16:57:32 +00002990 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2991 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002992
Anders Carlsson15281452008-11-04 16:57:32 +00002993 return true;
2994}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002995
James Dennett1dfbd922012-06-14 21:40:34 +00002996/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00002997/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002998void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002999 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003000 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003001 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003002 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003003 if (!Class) {
3004 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3005 return;
3006 }
John McCall260611a2012-06-20 06:18:46 +00003007 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003008 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3009 return;
3010 }
Mike Stump1eb44332009-09-09 15:08:12 +00003011
Chris Lattnercc98eac2008-12-17 07:13:27 +00003012 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003013 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003014 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003015 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003016 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003017 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003018 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003019 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3020 /*FIXME: StartL=*/ID->getLocation(),
3021 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003022 ID->getIdentifier(), ID->getType(),
3023 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003024 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003025 }
Mike Stump1eb44332009-09-09 15:08:12 +00003026
Chris Lattnercc98eac2008-12-17 07:13:27 +00003027 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003028 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003029 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003030 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003031 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003032 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003033 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003034 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003035 }
3036}
3037
Douglas Gregor160b5632010-04-26 17:32:49 +00003038/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003039VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3040 SourceLocation StartLoc,
3041 SourceLocation IdLoc,
3042 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003043 bool Invalid) {
3044 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3045 // duration shall not be qualified by an address-space qualifier."
3046 // Since all parameters have automatic store duration, they can not have
3047 // an address space.
3048 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003049 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003050 Invalid = true;
3051 }
3052
3053 // An @catch parameter must be an unqualified object pointer type;
3054 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3055 if (Invalid) {
3056 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003057 } else if (T->isDependentType()) {
3058 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003059 } else if (!T->isObjCObjectPointerType()) {
3060 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003061 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003062 } else if (T->isObjCQualifiedIdType()) {
3063 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003064 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003065 }
3066
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003067 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3068 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003069 New->setExceptionVariable(true);
3070
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003071 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003072 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003073 Invalid = true;
3074
Douglas Gregor160b5632010-04-26 17:32:49 +00003075 if (Invalid)
3076 New->setInvalidDecl();
3077 return New;
3078}
3079
John McCalld226f652010-08-21 09:40:31 +00003080Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003081 const DeclSpec &DS = D.getDeclSpec();
3082
3083 // We allow the "register" storage class on exception variables because
3084 // GCC did, but we drop it completely. Any other storage class is an error.
3085 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3086 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3087 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3088 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3089 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3090 << DS.getStorageClassSpec();
3091 }
3092 if (D.getDeclSpec().isThreadSpecified())
3093 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3094 D.getMutableDeclSpec().ClearStorageClassSpecs();
3095
3096 DiagnoseFunctionSpecifiers(D);
3097
3098 // Check that there are no default arguments inside the type of this
3099 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003100 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003101 CheckExtraCXXDefaultArguments(D);
3102
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003103 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003104 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003105
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003106 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3107 D.getSourceRange().getBegin(),
3108 D.getIdentifierLoc(),
3109 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003110 D.isInvalidType());
3111
3112 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3113 if (D.getCXXScopeSpec().isSet()) {
3114 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3115 << D.getCXXScopeSpec().getRange();
3116 New->setInvalidDecl();
3117 }
3118
3119 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003120 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003121 if (D.getIdentifier())
3122 IdResolver.AddDecl(New);
3123
3124 ProcessDeclAttributes(S, New, D);
3125
3126 if (New->hasAttr<BlocksAttr>())
3127 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003128 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003129}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003130
3131/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003132/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003133void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003134 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003135 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3136 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003137 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003138 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003139 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003140 }
3141}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003142
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003143void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003144 // Load referenced selectors from the external source.
3145 if (ExternalSource) {
3146 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3147 ExternalSource->ReadReferencedSelectors(Sels);
3148 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3149 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3150 }
3151
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003152 // Warning will be issued only when selector table is
3153 // generated (which means there is at lease one implementation
3154 // in the TU). This is to match gcc's behavior.
3155 if (ReferencedSelectors.empty() ||
3156 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003157 return;
3158 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3159 ReferencedSelectors.begin(),
3160 E = ReferencedSelectors.end(); S != E; ++S) {
3161 Selector Sel = (*S).first;
3162 if (!LookupImplementedMethodInGlobalPool(Sel))
3163 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3164 }
3165 return;
3166}