blob: 4a34095c1f6c8c5447458db1f6448bbc63dae2f2 [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 Jahanian6c89eaf2012-07-02 23:37:09 +0000285/// ActOnStartOfObjCMethodOrCFunctionDef - This routine sets up parameters; invisible
286/// and user declared, in the method definition's AST. This routine is also called
287/// for C-functions defined in an Objective-c class implementation.
288void Sema::ActOnStartOfObjCMethodOrCFunctionDef(Scope *FnBodyScope, Decl *D,
289 bool parseMethod) {
290 assert((getCurMethodDecl() == 0 && getCurFunctionDecl() == 0) &&
291 "Method/c-function parsing confused");
292 if (!parseMethod) {
Chandler Carruth2e2c7a42012-07-03 00:15:11 +0000293 FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000294 // If we don't have a valid c-function decl, simply return.
295 if (!FDecl)
296 return;
297 PushDeclContext(FnBodyScope, FDecl);
298 PushFunctionScope();
299
300 for (FunctionDecl::param_const_iterator PI = FDecl->param_begin(),
301 E = FDecl->param_end(); PI != E; ++PI) {
302 ParmVarDecl *Param = (*PI);
303 if (!Param->isInvalidDecl() &&
304 RequireCompleteType(Param->getLocation(), Param->getType(),
305 diag::err_typecheck_decl_incomplete_type))
306 Param->setInvalidDecl();
307 if ((*PI)->getIdentifier())
308 PushOnScopeChains(*PI, FnBodyScope);
309 }
310 return;
311 }
312
John McCalld226f652010-08-21 09:40:31 +0000313 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000314
Steve Naroff394f3f42008-07-25 17:57:26 +0000315 // If we don't have a valid method decl, simply return.
316 if (!MDecl)
317 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000318
Chris Lattner4d391482007-12-12 07:09:47 +0000319 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000320 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000321 PushFunctionScope();
322
Chris Lattner4d391482007-12-12 07:09:47 +0000323 // Create Decl objects for each parameter, entrring them in the scope for
324 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000325
326 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000327 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Daniel Dunbar451318c2008-08-26 06:07:48 +0000329 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
330 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000331
Chris Lattner8123a952008-04-10 02:22:51 +0000332 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000333 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000334 E = MDecl->param_end(); PI != E; ++PI) {
335 ParmVarDecl *Param = (*PI);
336 if (!Param->isInvalidDecl() &&
337 RequireCompleteType(Param->getLocation(), Param->getType(),
338 diag::err_typecheck_decl_incomplete_type))
339 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000340 if ((*PI)->getIdentifier())
341 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000342 }
John McCallf85e1932011-06-15 23:02:42 +0000343
344 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000345 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000346 switch (MDecl->getMethodFamily()) {
347 case OMF_retain:
348 case OMF_retainCount:
349 case OMF_release:
350 case OMF_autorelease:
351 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
352 << MDecl->getSelector();
353 break;
354
355 case OMF_None:
356 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000357 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000358 case OMF_alloc:
359 case OMF_init:
360 case OMF_mutableCopy:
361 case OMF_copy:
362 case OMF_new:
363 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000364 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000365 break;
366 }
367 }
368
Nico Weber9a1ecf02011-08-22 17:25:57 +0000369 // Warn on deprecated methods under -Wdeprecated-implementations,
370 // and prepare for warning on missing super calls.
371 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000372 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000373 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000374 DiagnoseObjCImplementedDeprecations(*this,
375 dyn_cast<NamedDecl>(IMD),
376 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000377
Nico Weber80cb6e62011-08-28 22:35:17 +0000378 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000379 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
380 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
381 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000382 if (IC->getSuperClass()) {
Eli Friedman95aac152012-08-01 21:02:59 +0000383 getCurFunction()->ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000384 !(Context.getLangOpts().ObjCAutoRefCount ||
385 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000386 MDecl->getMethodFamily() == OMF_dealloc;
Eli Friedman95aac152012-08-01 21:02:59 +0000387 getCurFunction()->ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000388 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000389 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000390 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000391 }
Chris Lattner4d391482007-12-12 07:09:47 +0000392}
393
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000394namespace {
395
396// Callback to only accept typo corrections that are Objective-C classes.
397// If an ObjCInterfaceDecl* is given to the constructor, then the validation
398// function will reject corrections to that class.
399class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
400 public:
401 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
402 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
403 : CurrentIDecl(IDecl) {}
404
405 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
406 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
407 return ID && !declaresSameEntity(ID, CurrentIDecl);
408 }
409
410 private:
411 ObjCInterfaceDecl *CurrentIDecl;
412};
413
414}
415
John McCalld226f652010-08-21 09:40:31 +0000416Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000417ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
418 IdentifierInfo *ClassName, SourceLocation ClassLoc,
419 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000420 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000421 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000422 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000423 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner4d391482007-12-12 07:09:47 +0000425 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000426 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000427 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000428
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000429 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000430 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000431 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregor7723fec2011-12-15 20:29:51 +0000434 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000435 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000436 ObjCInterfaceDecl *IDecl
437 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000438 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000439
Douglas Gregor7723fec2011-12-15 20:29:51 +0000440 if (PrevIDecl) {
441 // Class already seen. Was it a definition?
442 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
443 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
444 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000445 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000446 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000447 }
Chris Lattner4d391482007-12-12 07:09:47 +0000448 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000449
450 if (AttrList)
451 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
452 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Douglas Gregor7723fec2011-12-15 20:29:51 +0000454 // Start the definition of this class. If we're in a redefinition case, there
455 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000456 if (!IDecl->hasDefinition())
457 IDecl->startDefinition();
458
Chris Lattner4d391482007-12-12 07:09:47 +0000459 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000460 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000461 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
462 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000463
464 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000465 // Try to correct for a typo in the superclass name without correcting
466 // to the class we're defining.
467 ObjCInterfaceValidatorCCC Validator(IDecl);
468 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000469 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000470 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000471 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
472 Diag(SuperLoc, diag::err_undef_superclass_suggest)
473 << SuperName << ClassName << PrevDecl->getDeclName();
474 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
475 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000476 }
477 }
478
Douglas Gregor60ef3082011-12-15 00:29:59 +0000479 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000480 Diag(SuperLoc, diag::err_recursive_superclass)
481 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000482 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000483 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000484 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000485 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000486
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000487 // Diagnose classes that inherit from deprecated classes.
488 if (SuperClassDecl)
489 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000491 if (PrevDecl && SuperClassDecl == 0) {
492 // The previous declaration was not a class decl. Check if we have a
493 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000494 if (const TypedefNameDecl *TDecl =
495 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000496 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000497 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000498 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
499 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000500 }
501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000503 // This handles the following case:
504 //
505 // typedef int SuperClass;
506 // @interface MyClass : SuperClass {} @end
507 //
508 if (!SuperClassDecl) {
509 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
510 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000511 }
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Richard Smith162e1c12011-04-15 14:24:37 +0000514 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000515 if (!SuperClassDecl)
516 Diag(SuperLoc, diag::err_undef_superclass)
517 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000518 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000519 Context.getObjCInterfaceType(SuperClassDecl),
520 diag::err_forward_superclass,
521 SuperClassDecl->getDeclName(),
522 ClassName,
523 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000524 SuperClassDecl = 0;
525 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000526 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000527 IDecl->setSuperClass(SuperClassDecl);
528 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000529 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000530 }
Chris Lattner4d391482007-12-12 07:09:47 +0000531 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000532 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Sebastian Redl0b17c612010-08-13 00:28:03 +0000535 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000536 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000537 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000538 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000539 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Anders Carlsson15281452008-11-04 16:57:32 +0000542 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000543 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000544}
545
Richard Smithde01b7a2012-08-08 23:32:13 +0000546/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000547/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000548Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
549 IdentifierInfo *AliasName,
550 SourceLocation AliasLocation,
551 IdentifierInfo *ClassName,
552 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000553 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000554 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000555 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000556 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000557 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000558 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000559 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000560 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000561 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000562 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000563 }
564 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000565 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000566 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000567 if (const TypedefNameDecl *TDecl =
568 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000569 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000570 if (T->isObjCObjectType()) {
571 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000572 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000573 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000574 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000575 }
576 }
577 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000578 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
579 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000580 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000581 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000582 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000583 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000584 }
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000586 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000587 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000588 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Anders Carlsson15281452008-11-04 16:57:32 +0000590 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000591 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000592
John McCalld226f652010-08-21 09:40:31 +0000593 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000594}
595
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000596bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000597 IdentifierInfo *PName,
598 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000599 const ObjCList<ObjCProtocolDecl> &PList) {
600
601 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000602 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
603 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000604 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
605 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000606 if (PDecl->getIdentifier() == PName) {
607 Diag(Ploc, diag::err_protocol_has_circular_dependency);
608 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000609 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000610 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000611
612 if (!PDecl->hasDefinition())
613 continue;
614
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000615 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
616 PDecl->getLocation(), PDecl->getReferencedProtocols()))
617 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000618 }
619 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000620 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000621}
622
John McCalld226f652010-08-21 09:40:31 +0000623Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000624Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
625 IdentifierInfo *ProtocolName,
626 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000627 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000628 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000629 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000630 SourceLocation EndProtoLoc,
631 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000632 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000633 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000634 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000635 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
636 ForRedeclaration);
637 ObjCProtocolDecl *PDecl = 0;
638 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
639 // If we already have a definition, complain.
640 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
641 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Douglas Gregor27c6da22012-01-01 20:30:41 +0000643 // Create a new protocol that is completely distinct from previous
644 // declarations, and do not make this protocol available for name lookup.
645 // That way, we'll end up completely ignoring the duplicate.
646 // FIXME: Can we turn this into an error?
647 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
648 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000649 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000650 PDecl->startDefinition();
651 } else {
652 if (PrevDecl) {
653 // Check for circular dependencies among protocol declarations. This can
654 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000655 ObjCList<ObjCProtocolDecl> PList;
656 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
657 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000658 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000659 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000660
661 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000662 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000663 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000664 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000665
Douglas Gregor6e378de2009-04-23 23:18:26 +0000666 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000667 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000668 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000669
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000670 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000671 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000672
673 // Merge attributes from previous declarations.
674 if (PrevDecl)
675 mergeDeclAttributes(PDecl, PrevDecl);
676
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000677 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000678 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000679 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
680 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
683 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000684 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000685}
686
687/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000688/// issues an error if they are not declared. It returns list of
689/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000690void
Chris Lattnere13b9592008-07-26 04:03:38 +0000691Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000692 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000693 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000694 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000695 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000696 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
697 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000698 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000699 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000700 TypoCorrection Corrected = CorrectTypo(
701 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000702 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000703 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000704 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000705 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000706 Diag(PDecl->getLocation(), diag::note_previous_decl)
707 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000708 }
709 }
710
711 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000712 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000713 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000714 continue;
715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000717 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000718
719 // If this is a forward declaration and we are supposed to warn in this
720 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000721 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000722 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000723 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000724 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000725 }
726}
727
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000728/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000729/// a class method in its extension.
730///
Mike Stump1eb44332009-09-09 15:08:12 +0000731void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000732 ObjCInterfaceDecl *ID) {
733 if (!ID)
734 return; // Possibly due to previous error
735
736 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000737 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
738 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000739 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000740 MethodMap[MD->getSelector()] = MD;
741 }
742
743 if (MethodMap.empty())
744 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000745 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
746 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000747 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000748 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
749 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
750 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
751 << Method->getDeclName();
752 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
753 }
754 }
755}
756
James Dennett1dfbd922012-06-14 21:40:34 +0000757/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000758Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000759Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000760 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000761 unsigned NumElts,
762 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000763 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000764 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000765 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000766 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
767 ForRedeclaration);
768 ObjCProtocolDecl *PDecl
769 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
770 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000771 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000772
773 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000774 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000775
Douglas Gregor3937f872012-01-01 20:33:24 +0000776 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000777 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000778
779 if (PrevDecl)
780 mergeDeclAttributes(PDecl, PrevDecl);
781
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000782 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000785 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000786}
787
John McCalld226f652010-08-21 09:40:31 +0000788Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000789ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
790 IdentifierInfo *ClassName, SourceLocation ClassLoc,
791 IdentifierInfo *CategoryName,
792 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000793 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000794 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000795 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000796 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000797 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000798 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000799
800 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000801
802 if (!IDecl
803 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000804 diag::err_category_forward_interface,
805 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000806 // Create an invalid ObjCCategoryDecl to serve as context for
807 // the enclosing method declarations. We mark the decl invalid
808 // to make it clear that this isn't a valid AST.
809 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000810 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000811 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000812 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000813
814 if (!IDecl)
815 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000816 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000817 }
818
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000819 if (!CategoryName && IDecl->getImplementation()) {
820 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
821 Diag(IDecl->getImplementation()->getLocation(),
822 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000823 }
824
Fariborz Jahanian25760612010-02-15 21:55:26 +0000825 if (CategoryName) {
826 /// Check for duplicate interface declaration for this category
827 ObjCCategoryDecl *CDeclChain;
828 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
829 CDeclChain = CDeclChain->getNextClassCategory()) {
830 if (CDeclChain->getIdentifier() == CategoryName) {
831 // Class extensions can be declared multiple times.
832 Diag(CategoryLoc, diag::warn_dup_category_def)
833 << ClassName << CategoryName;
834 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
835 break;
836 }
Chris Lattner70f19542009-02-16 21:26:43 +0000837 }
838 }
Chris Lattner70f19542009-02-16 21:26:43 +0000839
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000840 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
841 ClassLoc, CategoryLoc, CategoryName, IDecl);
842 // FIXME: PushOnScopeChains?
843 CurContext->addDecl(CDecl);
844
Chris Lattner4d391482007-12-12 07:09:47 +0000845 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000846 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000847 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000848 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000849 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000850 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000851 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Anders Carlsson15281452008-11-04 16:57:32 +0000854 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000855 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000856}
857
858/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000859/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000860/// object.
John McCalld226f652010-08-21 09:40:31 +0000861Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000862 SourceLocation AtCatImplLoc,
863 IdentifierInfo *ClassName, SourceLocation ClassLoc,
864 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000865 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000866 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000867 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000868 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
869 if (!CatIDecl) {
870 // Category @implementation with no corresponding @interface.
871 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000872 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
873 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000874 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000875 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000876 }
877 }
878
Mike Stump1eb44332009-09-09 15:08:12 +0000879 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000880 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000881 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000882 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000883 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000884 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000885 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000886 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
887 diag::err_undef_interface)) {
888 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000889 }
Chris Lattner4d391482007-12-12 07:09:47 +0000890
Douglas Gregord0434102009-01-09 00:49:46 +0000891 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000892 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000893
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000894 // If the interface is deprecated/unavailable, warn/error about it.
895 if (IDecl)
896 DiagnoseUseOfDecl(IDecl, ClassLoc);
897
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000898 /// Check that CatName, category name, is not used in another implementation.
899 if (CatIDecl) {
900 if (CatIDecl->getImplementation()) {
901 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
902 << CatName;
903 Diag(CatIDecl->getImplementation()->getLocation(),
904 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000905 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000906 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000907 // Warn on implementating category of deprecated class under
908 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000909 DiagnoseObjCImplementedDeprecations(*this,
910 dyn_cast<NamedDecl>(IDecl),
911 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000912 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Anders Carlsson15281452008-11-04 16:57:32 +0000915 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000916 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000917}
918
John McCalld226f652010-08-21 09:40:31 +0000919Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000920 SourceLocation AtClassImplLoc,
921 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000922 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000923 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000924 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000925 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000926 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000927 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
928 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000929 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000930 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000931 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000932 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000933 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
934 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000935 } else {
936 // We did not find anything with the name ClassName; try to correct for
937 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000938 ObjCInterfaceValidatorCCC Validator;
939 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000940 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000941 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000942 // Suggest the (potentially) correct interface name. However, put the
943 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000944 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000945 // provide a code-modification hint or use the typo name for recovery,
946 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000947 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000948 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000949 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000950 << ClassName << CorrectedName;
951 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
952 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000953 IDecl = 0;
954 } else {
955 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
956 }
Chris Lattner4d391482007-12-12 07:09:47 +0000957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Chris Lattner4d391482007-12-12 07:09:47 +0000959 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000960 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000961 if (SuperClassname) {
962 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000963 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
964 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000965 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000966 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
967 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000968 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000969 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000970 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000971 if (SDecl && !SDecl->hasDefinition())
972 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000973 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000974 Diag(SuperClassLoc, diag::err_undef_superclass)
975 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000976 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000977 // This implementation and its interface do not have the same
978 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000979 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000980 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000981 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000982 }
983 }
984 }
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chris Lattner4d391482007-12-12 07:09:47 +0000986 if (!IDecl) {
987 // Legacy case of @implementation with no corresponding @interface.
988 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000989
Mike Stump390b4cc2009-05-16 07:39:55 +0000990 // FIXME: Do we support attributes on the @implementation? If so we should
991 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000992 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000993 ClassName, /*PrevDecl=*/0, ClassLoc,
994 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000995 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000996 if (SDecl) {
997 IDecl->setSuperClass(SDecl);
998 IDecl->setSuperClassLoc(SuperClassLoc);
999 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1000 } else {
1001 IDecl->setEndOfDefinitionLoc(ClassLoc);
1002 }
1003
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001004 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001005 } else {
1006 // Mark the interface as being completed, even if it was just as
1007 // @class ....;
1008 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001009 if (!IDecl->hasDefinition())
1010 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
1013 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001014 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1015 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Anders Carlsson15281452008-11-04 16:57:32 +00001017 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001018 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattner4d391482007-12-12 07:09:47 +00001020 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001021 if (IDecl->getImplementation()) {
1022 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001023 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001024 Diag(IDecl->getImplementation()->getLocation(),
1025 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001026 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001027 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001028 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001029 // Warn on implementating deprecated class under
1030 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001031 DiagnoseObjCImplementedDeprecations(*this,
1032 dyn_cast<NamedDecl>(IDecl),
1033 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001034 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001035 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001036}
1037
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001038Sema::DeclGroupPtrTy
1039Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1040 SmallVector<Decl *, 64> DeclsInGroup;
1041 DeclsInGroup.reserve(Decls.size() + 1);
1042
1043 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1044 Decl *Dcl = Decls[i];
1045 if (!Dcl)
1046 continue;
1047 if (Dcl->getDeclContext()->isFileContext())
1048 Dcl->setTopLevelDeclInObjCContainer();
1049 DeclsInGroup.push_back(Dcl);
1050 }
1051
1052 DeclsInGroup.push_back(ObjCImpDecl);
1053
1054 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1055}
1056
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001057void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1058 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001059 SourceLocation RBrace) {
1060 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001061 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001062 if (!IDecl)
1063 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001064 /// Check case of non-existing \@interface decl.
1065 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001066 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001067 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001068 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001069 // Add ivar's to class's DeclContext.
1070 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001071 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001072 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001073 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001074 }
1075
Chris Lattner4d391482007-12-12 07:09:47 +00001076 return;
1077 }
1078 // If implementation has empty ivar list, just return.
1079 if (numIvars == 0)
1080 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Chris Lattner4d391482007-12-12 07:09:47 +00001082 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001083 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001084 if (ImpDecl->getSuperClass())
1085 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1086 for (unsigned i = 0; i < numIvars; i++) {
1087 ObjCIvarDecl* ImplIvar = ivars[i];
1088 if (const ObjCIvarDecl *ClsIvar =
1089 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1090 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1091 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1092 continue;
1093 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001094 // Instance ivar to Implementation's DeclContext.
1095 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001096 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001097 ImpDecl->addDecl(ImplIvar);
1098 }
1099 return;
1100 }
Chris Lattner4d391482007-12-12 07:09:47 +00001101 // Check interface's Ivar list against those in the implementation.
1102 // names and types must match.
1103 //
Chris Lattner4d391482007-12-12 07:09:47 +00001104 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001105 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001106 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1107 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001109 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001110 assert (ImplIvar && "missing implementation ivar");
1111 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Steve Naroffca331292009-03-03 14:49:36 +00001113 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001114 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001115 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001116 << ImplIvar->getIdentifier()
1117 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001118 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001119 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1120 ImplIvar->getBitWidthValue(Context) !=
1121 ClsIvar->getBitWidthValue(Context)) {
1122 Diag(ImplIvar->getBitWidth()->getLocStart(),
1123 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1124 Diag(ClsIvar->getBitWidth()->getLocStart(),
1125 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001126 }
Steve Naroffca331292009-03-03 14:49:36 +00001127 // Make sure the names are identical.
1128 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001129 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001130 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001131 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001132 }
1133 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner609e4c72007-12-12 18:11:49 +00001136 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001137 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001138 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001139 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001140}
1141
Steve Naroff3c2eb662008-02-10 21:38:56 +00001142void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001143 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001144 // No point warning no definition of method which is 'unavailable'.
1145 if (method->hasAttr<UnavailableAttr>())
1146 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001147 if (!IncompleteImpl) {
1148 Diag(ImpLoc, diag::warn_incomplete_impl);
1149 IncompleteImpl = true;
1150 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001151 if (DiagID == diag::warn_unimplemented_protocol_method)
1152 Diag(ImpLoc, DiagID) << method->getDeclName();
1153 else
1154 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001155}
1156
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001157/// Determines if type B can be substituted for type A. Returns true if we can
1158/// guarantee that anything that the user will do to an object of type A can
1159/// also be done to an object of type B. This is trivially true if the two
1160/// types are the same, or if B is a subclass of A. It becomes more complex
1161/// in cases where protocols are involved.
1162///
1163/// Object types in Objective-C describe the minimum requirements for an
1164/// object, rather than providing a complete description of a type. For
1165/// example, if A is a subclass of B, then B* may refer to an instance of A.
1166/// The principle of substitutability means that we may use an instance of A
1167/// anywhere that we may use an instance of B - it will implement all of the
1168/// ivars of B and all of the methods of B.
1169///
1170/// This substitutability is important when type checking methods, because
1171/// the implementation may have stricter type definitions than the interface.
1172/// The interface specifies minimum requirements, but the implementation may
1173/// have more accurate ones. For example, a method may privately accept
1174/// instances of B, but only publish that it accepts instances of A. Any
1175/// object passed to it will be type checked against B, and so will implicitly
1176/// by a valid A*. Similarly, a method may return a subclass of the class that
1177/// it is declared as returning.
1178///
1179/// This is most important when considering subclassing. A method in a
1180/// subclass must accept any object as an argument that its superclass's
1181/// implementation accepts. It may, however, accept a more general type
1182/// without breaking substitutability (i.e. you can still use the subclass
1183/// anywhere that you can use the superclass, but not vice versa). The
1184/// converse requirement applies to return types: the return type for a
1185/// subclass method must be a valid object of the kind that the superclass
1186/// advertises, but it may be specified more accurately. This avoids the need
1187/// for explicit down-casting by callers.
1188///
1189/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001190static bool isObjCTypeSubstitutable(ASTContext &Context,
1191 const ObjCObjectPointerType *A,
1192 const ObjCObjectPointerType *B,
1193 bool rejectId) {
1194 // Reject a protocol-unqualified id.
1195 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001196
1197 // If B is a qualified id, then A must also be a qualified id and it must
1198 // implement all of the protocols in B. It may not be a qualified class.
1199 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1200 // stricter definition so it is not substitutable for id<A>.
1201 if (B->isObjCQualifiedIdType()) {
1202 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001203 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1204 QualType(B,0),
1205 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001206 }
1207
1208 /*
1209 // id is a special type that bypasses type checking completely. We want a
1210 // warning when it is used in one place but not another.
1211 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1212
1213
1214 // If B is a qualified id, then A must also be a qualified id (which it isn't
1215 // if we've got this far)
1216 if (B->isObjCQualifiedIdType()) return false;
1217 */
1218
1219 // Now we know that A and B are (potentially-qualified) class types. The
1220 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001221 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001222}
1223
John McCall10302c02010-10-28 02:34:38 +00001224static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1225 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1226}
1227
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001228static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001229 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001230 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001231 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001232 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001233 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001234 if (IsProtocolMethodDecl &&
1235 (MethodDecl->getObjCDeclQualifier() !=
1236 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001237 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001238 S.Diag(MethodImpl->getLocation(),
1239 (IsOverridingMode ?
1240 diag::warn_conflicting_overriding_ret_type_modifiers
1241 : diag::warn_conflicting_ret_type_modifiers))
1242 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001243 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1244 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1245 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1246 }
1247 else
1248 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001249 }
1250
John McCall10302c02010-10-28 02:34:38 +00001251 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001252 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001253 return true;
1254 if (!Warn)
1255 return false;
John McCall10302c02010-10-28 02:34:38 +00001256
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001257 unsigned DiagID =
1258 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1259 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001260
1261 // Mismatches between ObjC pointers go into a different warning
1262 // category, and sometimes they're even completely whitelisted.
1263 if (const ObjCObjectPointerType *ImplPtrTy =
1264 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1265 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001266 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001267 // Allow non-matching return types as long as they don't violate
1268 // the principle of substitutability. Specifically, we permit
1269 // return types that are subclasses of the declared return type,
1270 // or that are more-qualified versions of the declared type.
1271 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001272 return false;
John McCall10302c02010-10-28 02:34:38 +00001273
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001274 DiagID =
1275 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1276 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001277 }
1278 }
1279
1280 S.Diag(MethodImpl->getLocation(), DiagID)
1281 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001282 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001283 << MethodImpl->getResultType()
1284 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001285 S.Diag(MethodDecl->getLocation(),
1286 IsOverridingMode ? diag::note_previous_declaration
1287 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001288 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001289 return false;
John McCall10302c02010-10-28 02:34:38 +00001290}
1291
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001292static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001293 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001294 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001295 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001296 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001297 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001298 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001299 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001300 if (IsProtocolMethodDecl &&
1301 (ImplVar->getObjCDeclQualifier() !=
1302 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001303 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001304 if (IsOverridingMode)
1305 S.Diag(ImplVar->getLocation(),
1306 diag::warn_conflicting_overriding_param_modifiers)
1307 << getTypeRange(ImplVar->getTypeSourceInfo())
1308 << MethodImpl->getDeclName();
1309 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001310 diag::warn_conflicting_param_modifiers)
1311 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001312 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001313 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1314 << getTypeRange(IfaceVar->getTypeSourceInfo());
1315 }
1316 else
1317 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001318 }
1319
John McCall10302c02010-10-28 02:34:38 +00001320 QualType ImplTy = ImplVar->getType();
1321 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001322
John McCall10302c02010-10-28 02:34:38 +00001323 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001324 return true;
1325
1326 if (!Warn)
1327 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001328 unsigned DiagID =
1329 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1330 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001331
1332 // Mismatches between ObjC pointers go into a different warning
1333 // category, and sometimes they're even completely whitelisted.
1334 if (const ObjCObjectPointerType *ImplPtrTy =
1335 ImplTy->getAs<ObjCObjectPointerType>()) {
1336 if (const ObjCObjectPointerType *IfacePtrTy =
1337 IfaceTy->getAs<ObjCObjectPointerType>()) {
1338 // Allow non-matching argument types as long as they don't
1339 // violate the principle of substitutability. Specifically, the
1340 // implementation must accept any objects that the superclass
1341 // accepts, however it may also accept others.
1342 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001343 return false;
John McCall10302c02010-10-28 02:34:38 +00001344
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001345 DiagID =
1346 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1347 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001348 }
1349 }
1350
1351 S.Diag(ImplVar->getLocation(), DiagID)
1352 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001353 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1354 S.Diag(IfaceVar->getLocation(),
1355 (IsOverridingMode ? diag::note_previous_declaration
1356 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001357 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001358 return false;
John McCall10302c02010-10-28 02:34:38 +00001359}
John McCallf85e1932011-06-15 23:02:42 +00001360
1361/// In ARC, check whether the conventional meanings of the two methods
1362/// match. If they don't, it's a hard error.
1363static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1364 ObjCMethodDecl *decl) {
1365 ObjCMethodFamily implFamily = impl->getMethodFamily();
1366 ObjCMethodFamily declFamily = decl->getMethodFamily();
1367 if (implFamily == declFamily) return false;
1368
1369 // Since conventions are sorted by selector, the only possibility is
1370 // that the types differ enough to cause one selector or the other
1371 // to fall out of the family.
1372 assert(implFamily == OMF_None || declFamily == OMF_None);
1373
1374 // No further diagnostics required on invalid declarations.
1375 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1376
1377 const ObjCMethodDecl *unmatched = impl;
1378 ObjCMethodFamily family = declFamily;
1379 unsigned errorID = diag::err_arc_lost_method_convention;
1380 unsigned noteID = diag::note_arc_lost_method_convention;
1381 if (declFamily == OMF_None) {
1382 unmatched = decl;
1383 family = implFamily;
1384 errorID = diag::err_arc_gained_method_convention;
1385 noteID = diag::note_arc_gained_method_convention;
1386 }
1387
1388 // Indexes into a %select clause in the diagnostic.
1389 enum FamilySelector {
1390 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1391 };
1392 FamilySelector familySelector = FamilySelector();
1393
1394 switch (family) {
1395 case OMF_None: llvm_unreachable("logic error, no method convention");
1396 case OMF_retain:
1397 case OMF_release:
1398 case OMF_autorelease:
1399 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001400 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001401 case OMF_retainCount:
1402 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001403 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001404 // Mismatches for these methods don't change ownership
1405 // conventions, so we don't care.
1406 return false;
1407
1408 case OMF_init: familySelector = F_init; break;
1409 case OMF_alloc: familySelector = F_alloc; break;
1410 case OMF_copy: familySelector = F_copy; break;
1411 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1412 case OMF_new: familySelector = F_new; break;
1413 }
1414
1415 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1416 ReasonSelector reasonSelector;
1417
1418 // The only reason these methods don't fall within their families is
1419 // due to unusual result types.
1420 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1421 reasonSelector = R_UnrelatedReturn;
1422 } else {
1423 reasonSelector = R_NonObjectReturn;
1424 }
1425
1426 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1427 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1428
1429 return true;
1430}
John McCall10302c02010-10-28 02:34:38 +00001431
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001432void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001433 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001434 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001435 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001436 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1437 return;
1438
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001439 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001440 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001441 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Chris Lattner3aff9192009-04-11 19:58:42 +00001443 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001444 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1445 EF = MethodDecl->param_end();
1446 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001447 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001448 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001449 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001450
Fariborz Jahanian21121902011-08-08 18:03:17 +00001451 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001452 Diag(ImpMethodDecl->getLocation(),
1453 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001454 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001455 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001456}
1457
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001458void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1459 ObjCMethodDecl *Overridden,
1460 bool IsProtocolMethodDecl) {
1461
1462 CheckMethodOverrideReturn(*this, Method, Overridden,
1463 IsProtocolMethodDecl, true,
1464 true);
1465
1466 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001467 IF = Overridden->param_begin(), EM = Method->param_end(),
1468 EF = Overridden->param_end();
1469 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001470 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1471 IsProtocolMethodDecl, true, true);
1472 }
1473
1474 if (Method->isVariadic() != Overridden->isVariadic()) {
1475 Diag(Method->getLocation(),
1476 diag::warn_conflicting_overriding_variadic);
1477 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1478 }
1479}
1480
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001481/// WarnExactTypedMethods - This routine issues a warning if method
1482/// implementation declaration matches exactly that of its declaration.
1483void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1484 ObjCMethodDecl *MethodDecl,
1485 bool IsProtocolMethodDecl) {
1486 // don't issue warning when protocol method is optional because primary
1487 // class is not required to implement it and it is safe for protocol
1488 // to implement it.
1489 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1490 return;
1491 // don't issue warning when primary class's method is
1492 // depecated/unavailable.
1493 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1494 MethodDecl->hasAttr<DeprecatedAttr>())
1495 return;
1496
1497 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1498 IsProtocolMethodDecl, false, false);
1499 if (match)
1500 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001501 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1502 EF = MethodDecl->param_end();
1503 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001504 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1505 *IM, *IF,
1506 IsProtocolMethodDecl, false, false);
1507 if (!match)
1508 break;
1509 }
1510 if (match)
1511 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001512 if (match)
1513 match = !(MethodDecl->isClassMethod() &&
1514 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001515
1516 if (match) {
1517 Diag(ImpMethodDecl->getLocation(),
1518 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001519 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1520 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001521 }
1522}
1523
Mike Stump390b4cc2009-05-16 07:39:55 +00001524/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1525/// improve the efficiency of selector lookups and type checking by associating
1526/// with each protocol / interface / category the flattened instance tables. If
1527/// we used an immutable set to keep the table then it wouldn't add significant
1528/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001529
Steve Naroffefe7f362008-02-08 22:06:17 +00001530/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001531/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001532void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1533 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001534 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001535 const SelectorSet &InsMap,
1536 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001537 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001538 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1539 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1540 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001541 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1542
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001543 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001544 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001545 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001546 // check to see if class implements forwardInvocation method and objects
1547 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001548 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001549 // Under such conditions, which means that every method possible is
1550 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001551 // found" warnings.
1552 // FIXME: Use a general GetUnarySelector method for this.
1553 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1554 Selector fISelector = Context.Selectors.getSelector(1, &II);
1555 if (InsMap.count(fISelector))
1556 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1557 // need be implemented in the implementation.
1558 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001561 // If a method lookup fails locally we still need to look and see if
1562 // the method was implemented by a base class or an inherited
1563 // protocol. This lookup is slow, but occurs rarely in correct code
1564 // and otherwise would terminate in a warning.
1565
Chris Lattner4d391482007-12-12 07:09:47 +00001566 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001567 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001568 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001569 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001570 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001571 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001572 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001573 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001574 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001575 // If a method is not implemented in the category implementation but
1576 // has been declared in its primary class, superclass,
1577 // or in one of their protocols, no need to issue the warning.
1578 // This is because method will be implemented in the primary class
1579 // or one of its super class implementation.
1580
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001581 // Ugly, but necessary. Method declared in protcol might have
1582 // have been synthesized due to a property declared in the class which
1583 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001584 if (ObjCMethodDecl *MethodInClass =
1585 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001586 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001587 if (C || MethodInClass->isSynthesized())
1588 continue;
1589 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1590 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1591 != DiagnosticsEngine::Ignored) {
1592 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001593 Diag(method->getLocation(), diag::note_method_declared_at)
1594 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001595 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1596 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001597 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001598 }
1599 }
Chris Lattner4d391482007-12-12 07:09:47 +00001600 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001601 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001602 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001603 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001604 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001605 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1606 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001607 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001608 // See above comment for instance method lookups.
1609 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001610 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001611 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001612 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001613 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1614 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001615 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001616 Diag(method->getLocation(), diag::note_method_declared_at)
1617 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001618 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1619 PDecl->getDeclName();
1620 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001621 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001622 }
Chris Lattner780f3292008-07-21 21:32:27 +00001623 // Check on this protocols's referenced protocols, recursively.
1624 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1625 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001626 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001627}
1628
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001629/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001630/// or protocol against those declared in their implementations.
1631///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001632void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1633 const SelectorSet &ClsMap,
1634 SelectorSet &InsMapSeen,
1635 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001636 ObjCImplDecl* IMPDecl,
1637 ObjCContainerDecl* CDecl,
1638 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001639 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001640 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001641 // Check and see if instance methods in class interface have been
1642 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001643 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1644 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001645 if (InsMapSeen.count((*I)->getSelector()))
1646 continue;
1647 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001648 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001649 !InsMap.count((*I)->getSelector())) {
1650 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001651 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1652 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001653 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001654 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001655 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001656 IMPDecl->getInstanceMethod((*I)->getSelector());
1657 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1658 "Expected to find the method through lookup as well");
1659 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001660 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001661 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001662 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001663 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1664 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001665 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001666 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001667 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001668 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001669 }
1670 }
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001672 // Check and see if class methods in class interface have been
1673 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001674 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001675 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001676 if (ClsMapSeen.count((*I)->getSelector()))
1677 continue;
1678 ClsMapSeen.insert((*I)->getSelector());
1679 if (!ClsMap.count((*I)->getSelector())) {
1680 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001681 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1682 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001683 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001684 ObjCMethodDecl *ImpMethodDecl =
1685 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001686 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1687 "Expected to find the method through lookup as well");
1688 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001689 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001690 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1691 isa<ObjCProtocolDecl>(CDecl));
1692 else
1693 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001694 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001695 }
1696 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001697
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001698 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001699 // Also methods in class extensions need be looked at next.
1700 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1701 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1702 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1703 IMPDecl,
1704 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001705 IncompleteImpl, false,
1706 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001707
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001708 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001709 for (ObjCInterfaceDecl::all_protocol_iterator
1710 PI = I->all_referenced_protocol_begin(),
1711 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001712 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1713 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001714 (*PI), IncompleteImpl, false,
1715 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001716
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001717 // FIXME. For now, we are not checking for extact match of methods
1718 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001719 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001720 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001721 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001722 I->getSuperClass(), IncompleteImpl, false);
1723 }
1724}
1725
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001726/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1727/// category matches with those implemented in its primary class and
1728/// warns each time an exact match is found.
1729void Sema::CheckCategoryVsClassMethodMatches(
1730 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001731 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001732
1733 for (ObjCImplementationDecl::instmeth_iterator
1734 I = CatIMPDecl->instmeth_begin(),
1735 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1736 InsMap.insert((*I)->getSelector());
1737
1738 for (ObjCImplementationDecl::classmeth_iterator
1739 I = CatIMPDecl->classmeth_begin(),
1740 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1741 ClsMap.insert((*I)->getSelector());
1742 if (InsMap.empty() && ClsMap.empty())
1743 return;
1744
1745 // Get category's primary class.
1746 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1747 if (!CatDecl)
1748 return;
1749 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1750 if (!IDecl)
1751 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001752 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001753 bool IncompleteImpl = false;
1754 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1755 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001756 IncompleteImpl, false,
1757 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001758}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001759
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001760void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001761 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001762 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001763 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001764 // Check and see if instance methods in class interface have been
1765 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001766 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001767 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001768 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001770 // Check and see if properties declared in the interface have either 1)
1771 // an implementation or 2) there is a @synthesize/@dynamic implementation
1772 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001773 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001774 if (!(LangOpts.ObjCDefaultSynthProperties &&
1775 LangOpts.ObjCRuntime.isNonFragile()) ||
1776 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001777 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001778
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001779 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001780 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001781 I = IMPDecl->classmeth_begin(),
1782 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001783 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001785 // Check for type conflict of methods declared in a class/protocol and
1786 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001787 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001788 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1789 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001790 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001791
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001792 // check all methods implemented in category against those declared
1793 // in its primary class.
1794 if (ObjCCategoryImplDecl *CatDecl =
1795 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1796 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Chris Lattner4d391482007-12-12 07:09:47 +00001798 // Check the protocol list for unimplemented methods in the @implementation
1799 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001800 // Check and see if class methods in class interface have been
1801 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Chris Lattnercddc8882009-03-01 00:56:52 +00001803 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001804 for (ObjCInterfaceDecl::all_protocol_iterator
1805 PI = I->all_referenced_protocol_begin(),
1806 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001807 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001808 InsMap, ClsMap, I);
1809 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001810 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1811 Categories; Categories = Categories->getNextClassExtension())
1812 ImplMethodsVsClassMethods(S, IMPDecl,
1813 const_cast<ObjCCategoryDecl*>(Categories),
1814 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001815 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001816 // For extended class, unimplemented methods in its protocols will
1817 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001818 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001819 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1820 E = C->protocol_end(); PI != E; ++PI)
1821 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001822 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001823 // Report unimplemented properties in the category as well.
1824 // When reporting on missing setter/getters, do not report when
1825 // setter/getter is implemented in category's primary class
1826 // implementation.
1827 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1828 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1829 for (ObjCImplementationDecl::instmeth_iterator
1830 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1831 InsMap.insert((*I)->getSelector());
1832 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001833 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001834 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001835 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001836 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001837}
1838
Mike Stump1eb44332009-09-09 15:08:12 +00001839/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001840Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001841Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001842 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001843 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001844 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001845 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001846 for (unsigned i = 0; i != NumElts; ++i) {
1847 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001848 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001849 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001850 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001851 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001852 // Maybe we will complain about the shadowed template parameter.
1853 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1854 // Just pretend that we didn't see the previous declaration.
1855 PrevDecl = 0;
1856 }
1857
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001858 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001859 // GCC apparently allows the following idiom:
1860 //
1861 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1862 // @class XCElementToggler;
1863 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001864 // Here we have chosen to ignore the forward class declaration
1865 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001866 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001867 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001868 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001869 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001870 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001871 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001872 // to the underlying class. Just ignore the forward class with a warning
1873 // as this will force the intended behavior which is to lookup the typedef
1874 // name.
1875 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1876 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1877 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1878 continue;
1879 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001880 }
Chris Lattner4d391482007-12-12 07:09:47 +00001881 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001882
1883 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001884 ObjCInterfaceDecl *PrevIDecl
1885 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001886 ObjCInterfaceDecl *IDecl
1887 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001888 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001889 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001890
Douglas Gregor7723fec2011-12-15 20:29:51 +00001891 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001892 CheckObjCDeclScope(IDecl);
1893 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001894 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001895
1896 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001897}
1898
John McCall0f4c4c42011-06-16 01:15:19 +00001899static bool tryMatchRecordTypes(ASTContext &Context,
1900 Sema::MethodMatchStrategy strategy,
1901 const Type *left, const Type *right);
1902
John McCallf85e1932011-06-15 23:02:42 +00001903static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1904 QualType leftQT, QualType rightQT) {
1905 const Type *left =
1906 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1907 const Type *right =
1908 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1909
1910 if (left == right) return true;
1911
1912 // If we're doing a strict match, the types have to match exactly.
1913 if (strategy == Sema::MMS_strict) return false;
1914
1915 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1916
1917 // Otherwise, use this absurdly complicated algorithm to try to
1918 // validate the basic, low-level compatibility of the two types.
1919
1920 // As a minimum, require the sizes and alignments to match.
1921 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1922 return false;
1923
1924 // Consider all the kinds of non-dependent canonical types:
1925 // - functions and arrays aren't possible as return and parameter types
1926
1927 // - vector types of equal size can be arbitrarily mixed
1928 if (isa<VectorType>(left)) return isa<VectorType>(right);
1929 if (isa<VectorType>(right)) return false;
1930
1931 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001932 // - structs, unions, and Objective-C objects must match more-or-less
1933 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001934 // - everything else should be a scalar
1935 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001936 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001937
John McCall1d9b3b22011-09-09 05:25:32 +00001938 // Make scalars agree in kind, except count bools as chars, and group
1939 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001940 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1941 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1942 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1943 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001944 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1945 leftSK = Type::STK_ObjCObjectPointer;
1946 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1947 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001948
1949 // Note that data member pointers and function member pointers don't
1950 // intermix because of the size differences.
1951
1952 return (leftSK == rightSK);
1953}
Chris Lattner4d391482007-12-12 07:09:47 +00001954
John McCall0f4c4c42011-06-16 01:15:19 +00001955static bool tryMatchRecordTypes(ASTContext &Context,
1956 Sema::MethodMatchStrategy strategy,
1957 const Type *lt, const Type *rt) {
1958 assert(lt && rt && lt != rt);
1959
1960 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1961 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1962 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1963
1964 // Require union-hood to match.
1965 if (left->isUnion() != right->isUnion()) return false;
1966
1967 // Require an exact match if either is non-POD.
1968 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1969 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1970 return false;
1971
1972 // Require size and alignment to match.
1973 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1974
1975 // Require fields to match.
1976 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1977 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1978 for (; li != le && ri != re; ++li, ++ri) {
1979 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1980 return false;
1981 }
1982 return (li == le && ri == re);
1983}
1984
Chris Lattner4d391482007-12-12 07:09:47 +00001985/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1986/// returns true, or false, accordingly.
1987/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001988bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1989 const ObjCMethodDecl *right,
1990 MethodMatchStrategy strategy) {
1991 if (!matchTypes(Context, strategy,
1992 left->getResultType(), right->getResultType()))
1993 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001994
David Blaikie4e4d0842012-03-11 07:00:24 +00001995 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001996 (left->hasAttr<NSReturnsRetainedAttr>()
1997 != right->hasAttr<NSReturnsRetainedAttr>() ||
1998 left->hasAttr<NSConsumesSelfAttr>()
1999 != right->hasAttr<NSConsumesSelfAttr>()))
2000 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002002 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002003 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2004 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002005
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002006 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002007 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002008 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002009
2010 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2011 return false;
2012
David Blaikie4e4d0842012-03-11 07:00:24 +00002013 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002014 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2015 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002016 }
2017 return true;
2018}
2019
Douglas Gregorff310c72012-05-01 23:37:00 +00002020void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002021 // If the list is empty, make it a singleton list.
2022 if (List->Method == 0) {
2023 List->Method = Method;
2024 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002025 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002026 }
2027
2028 // We've seen a method with this name, see if we have already seen this type
2029 // signature.
2030 ObjCMethodList *Previous = List;
2031 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002032 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002033 continue;
2034
2035 ObjCMethodDecl *PrevObjCMethod = List->Method;
2036
2037 // Propagate the 'defined' bit.
2038 if (Method->isDefined())
2039 PrevObjCMethod->setDefined(true);
2040
2041 // If a method is deprecated, push it in the global pool.
2042 // This is used for better diagnostics.
2043 if (Method->isDeprecated()) {
2044 if (!PrevObjCMethod->isDeprecated())
2045 List->Method = Method;
2046 }
2047 // If new method is unavailable, push it into global pool
2048 // unless previous one is deprecated.
2049 if (Method->isUnavailable()) {
2050 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2051 List->Method = Method;
2052 }
2053
Douglas Gregorff310c72012-05-01 23:37:00 +00002054 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002055 }
2056
2057 // We have a new signature for an existing method - add it.
2058 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002059 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002060 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2061}
2062
Sebastian Redldb9d2142010-08-02 23:18:59 +00002063/// \brief Read the contents of the method pool for a given selector from
2064/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002065void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002066 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002067 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002068}
2069
Douglas Gregorff310c72012-05-01 23:37:00 +00002070void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002071 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002072 // Ignore methods of invalid containers.
2073 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002074 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002075
Douglas Gregor0d266d62012-01-25 00:59:09 +00002076 if (ExternalSource)
2077 ReadMethodPool(Method->getSelector());
2078
Sebastian Redldb9d2142010-08-02 23:18:59 +00002079 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002080 if (Pos == MethodPool.end())
2081 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2082 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002083
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002084 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002085
Sebastian Redldb9d2142010-08-02 23:18:59 +00002086 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002087 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002088}
2089
John McCallf85e1932011-06-15 23:02:42 +00002090/// Determines if this is an "acceptable" loose mismatch in the global
2091/// method pool. This exists mostly as a hack to get around certain
2092/// global mismatches which we can't afford to make warnings / errors.
2093/// Really, what we want is a way to take a method out of the global
2094/// method pool.
2095static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2096 ObjCMethodDecl *other) {
2097 if (!chosen->isInstanceMethod())
2098 return false;
2099
2100 Selector sel = chosen->getSelector();
2101 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2102 return false;
2103
2104 // Don't complain about mismatches for -length if the method we
2105 // chose has an integral result type.
2106 return (chosen->getResultType()->isIntegerType());
2107}
2108
Sebastian Redldb9d2142010-08-02 23:18:59 +00002109ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002110 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002111 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002112 if (ExternalSource)
2113 ReadMethodPool(Sel);
2114
Sebastian Redldb9d2142010-08-02 23:18:59 +00002115 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002116 if (Pos == MethodPool.end())
2117 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002118
Sebastian Redldb9d2142010-08-02 23:18:59 +00002119 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Sebastian Redldb9d2142010-08-02 23:18:59 +00002121 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002122 bool issueDiagnostic = false, issueError = false;
2123
2124 // We support a warning which complains about *any* difference in
2125 // method signature.
2126 bool strictSelectorMatch =
2127 (receiverIdOrClass && warn &&
2128 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2129 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002130 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002131 if (strictSelectorMatch)
2132 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002133 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2134 MMS_strict)) {
2135 issueDiagnostic = true;
2136 break;
2137 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002138 }
2139
John McCallf85e1932011-06-15 23:02:42 +00002140 // If we didn't see any strict differences, we won't see any loose
2141 // differences. In ARC, however, we also need to check for loose
2142 // mismatches, because most of them are errors.
2143 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002144 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002145 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002146 // This checks if the methods differ in type mismatch.
2147 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2148 MMS_loose) &&
2149 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2150 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002151 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002152 issueError = true;
2153 break;
2154 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002155 }
2156
John McCallf85e1932011-06-15 23:02:42 +00002157 if (issueDiagnostic) {
2158 if (issueError)
2159 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2160 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002161 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2162 else
2163 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002164
2165 Diag(MethList.Method->getLocStart(),
2166 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002167 << MethList.Method->getSourceRange();
2168 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2169 Diag(Next->Method->getLocStart(), diag::note_also_found)
2170 << Next->Method->getSourceRange();
2171 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002172 }
2173 return MethList.Method;
2174}
2175
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002176ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002177 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2178 if (Pos == MethodPool.end())
2179 return 0;
2180
2181 GlobalMethods &Methods = Pos->second;
2182
2183 if (Methods.first.Method && Methods.first.Method->isDefined())
2184 return Methods.first.Method;
2185 if (Methods.second.Method && Methods.second.Method->isDefined())
2186 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002187 return 0;
2188}
2189
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002190/// DiagnoseDuplicateIvars -
2191/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002192/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002193/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002194/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002195void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2196 ObjCInterfaceDecl *SID) {
2197 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2198 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002199 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002200 if (Ivar->isInvalidDecl())
2201 continue;
2202 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2203 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2204 if (prevIvar) {
2205 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2206 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2207 Ivar->setInvalidDecl();
2208 }
2209 }
2210 }
2211}
2212
Erik Verbruggend64251f2011-12-06 09:25:23 +00002213Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2214 switch (CurContext->getDeclKind()) {
2215 case Decl::ObjCInterface:
2216 return Sema::OCK_Interface;
2217 case Decl::ObjCProtocol:
2218 return Sema::OCK_Protocol;
2219 case Decl::ObjCCategory:
2220 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2221 return Sema::OCK_ClassExtension;
2222 else
2223 return Sema::OCK_Category;
2224 case Decl::ObjCImplementation:
2225 return Sema::OCK_Implementation;
2226 case Decl::ObjCCategoryImpl:
2227 return Sema::OCK_CategoryImplementation;
2228
2229 default:
2230 return Sema::OCK_None;
2231 }
2232}
2233
Steve Naroffa56f6162007-12-18 01:30:32 +00002234// Note: For class/category implemenations, allMethods/allProperties is
2235// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002236Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2237 Decl **allMethods, unsigned allNum,
2238 Decl **allProperties, unsigned pNum,
2239 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002240
Erik Verbruggend64251f2011-12-06 09:25:23 +00002241 if (getObjCContainerKind() == Sema::OCK_None)
2242 return 0;
2243
2244 assert(AtEnd.isValid() && "Invalid location for '@end'");
2245
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002246 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2247 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002248
Mike Stump1eb44332009-09-09 15:08:12 +00002249 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002250 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2251 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002252 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002253
Steve Naroff0701bbb2009-01-08 17:28:14 +00002254 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2255 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2256 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2257
Chris Lattner4d391482007-12-12 07:09:47 +00002258 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002259 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002260 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002261
2262 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002263 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002264 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002265 const ObjCMethodDecl *&PrevMethod = InsMap[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 InsMap[Method->getSelector()] = Method;
2284 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002285 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002286 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002287 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002288 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002289 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002290 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002291 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002292 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002293 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002294 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002295 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002296 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002297 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002298 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002299 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002300 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002301 if (!Context.getSourceManager().isInSystemHeader(
2302 Method->getLocation()))
2303 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2304 << Method->getDeclName();
2305 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2306 }
Chris Lattner4d391482007-12-12 07:09:47 +00002307 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002308 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002309 }
2310 }
2311 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002312 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002313 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002314 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002315 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002316 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002317 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002318 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002319 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002320 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002321
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002322 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002323 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002324 if (C->IsClassExtension()) {
2325 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2326 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002327 }
Chris Lattner4d391482007-12-12 07:09:47 +00002328 }
Steve Naroff09c47192009-01-09 15:36:25 +00002329 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002330 if (CDecl->getIdentifier())
2331 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2332 // user-defined setter/getter. It also synthesizes setter/getter methods
2333 // and adds them to the DeclContext and global method pools.
2334 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2335 E = CDecl->prop_end();
2336 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002337 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002338 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002339 }
2340 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002341 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002342 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002343 // Any property declared in a class extension might have user
2344 // declared setter or getter in current class extension or one
2345 // of the other class extensions. Mark them as synthesized as
2346 // property will be synthesized when property with same name is
2347 // seen in the @implementation.
2348 for (const ObjCCategoryDecl *ClsExtDecl =
2349 IDecl->getFirstClassExtension();
2350 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2351 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2352 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002353 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002354 // Skip over properties declared @dynamic
2355 if (const ObjCPropertyImplDecl *PIDecl
2356 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2357 if (PIDecl->getPropertyImplementation()
2358 == ObjCPropertyImplDecl::Dynamic)
2359 continue;
2360
2361 for (const ObjCCategoryDecl *CExtDecl =
2362 IDecl->getFirstClassExtension();
2363 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2364 if (ObjCMethodDecl *GetterMethod =
2365 CExtDecl->getInstanceMethod(Property->getGetterName()))
2366 GetterMethod->setSynthesized(true);
2367 if (!Property->isReadOnly())
2368 if (ObjCMethodDecl *SetterMethod =
2369 CExtDecl->getInstanceMethod(Property->getSetterName()))
2370 SetterMethod->setSynthesized(true);
2371 }
2372 }
2373 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002374 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002375 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002376 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002377
Patrick Beardb2f68202012-04-06 18:12:22 +00002378 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2379 if (IDecl->getSuperClass() == NULL) {
2380 // This class has no superclass, so check that it has been marked with
2381 // __attribute((objc_root_class)).
2382 if (!HasRootClassAttr) {
2383 SourceLocation DeclLoc(IDecl->getLocation());
2384 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2385 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2386 << IDecl->getIdentifier();
2387 // See if NSObject is in the current scope, and if it is, suggest
2388 // adding " : NSObject " to the class declaration.
2389 NamedDecl *IF = LookupSingleName(TUScope,
2390 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2391 DeclLoc, LookupOrdinaryName);
2392 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2393 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2394 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2395 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2396 } else {
2397 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2398 }
2399 }
2400 } else if (HasRootClassAttr) {
2401 // Complain that only root classes may have this attribute.
2402 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2403 }
2404
John McCall260611a2012-06-20 06:18:46 +00002405 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002406 while (IDecl->getSuperClass()) {
2407 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2408 IDecl = IDecl->getSuperClass();
2409 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002410 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002411 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002412 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002413 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002414 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002415 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002416
Chris Lattner4d391482007-12-12 07:09:47 +00002417 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002418 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002419 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002420 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002421 Categories; Categories = Categories->getNextClassCategory()) {
2422 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002423 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002424 break;
2425 }
2426 }
2427 }
2428 }
Chris Lattner682bf922009-03-29 16:50:03 +00002429 if (isInterfaceDeclKind) {
2430 // Reject invalid vardecls.
2431 for (unsigned i = 0; i != tuvNum; i++) {
2432 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2433 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2434 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002435 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002436 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002437 }
Chris Lattner682bf922009-03-29 16:50:03 +00002438 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002439 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002440 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002441
2442 for (unsigned i = 0; i != tuvNum; i++) {
2443 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002444 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2445 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002446 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2447 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002448
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002449 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002450 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002451}
2452
2453
2454/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2455/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002456static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002457CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002458 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002459}
2460
Ted Kremenek422bae72010-04-18 04:59:38 +00002461static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002462bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2463 const AttrVec &A) {
2464 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002465 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002466 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002467 return false;
2468
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002469 // method declared in interface has no attribute.
2470 // But implementation has attributes. This is invalid
2471 if (!IMD->hasAttrs())
2472 return true;
2473
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002474 const AttrVec &D = IMD->getAttrs();
2475 if (D.size() != A.size())
2476 return true;
2477
2478 // attributes on method declaration and definition must match exactly.
2479 // Note that we have at most a couple of attributes on methods, so this
2480 // n*n search is good enough.
2481 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2482 bool match = false;
2483 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2484 if ((*i)->getKind() == (*i1)->getKind()) {
2485 match = true;
2486 break;
2487 }
2488 }
2489 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002490 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +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}