blob: 87acd1e63d0c007a61b95a5382cc7c29a980fbb9 [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:
200 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000201 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000202 case OMF_retain:
203 case OMF_release:
204 case OMF_autorelease:
205 case OMF_retainCount:
206 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000207 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000208 return false;
209
210 case OMF_init:
211 // If the method doesn't obey the init rules, don't bother annotating it.
212 if (S.checkInitMethod(method, QualType()))
213 return true;
214
215 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
216 S.Context));
217
218 // Don't add a second copy of this attribute, but otherwise don't
219 // let it be suppressed.
220 if (method->hasAttr<NSReturnsRetainedAttr>())
221 return false;
222 break;
223
224 case OMF_alloc:
225 case OMF_copy:
226 case OMF_mutableCopy:
227 case OMF_new:
228 if (method->hasAttr<NSReturnsRetainedAttr>() ||
229 method->hasAttr<NSReturnsNotRetainedAttr>() ||
230 method->hasAttr<NSReturnsAutoreleasedAttr>())
231 return false;
232 break;
233 }
234
235 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
236 S.Context));
237 return false;
238}
239
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000240static void DiagnoseObjCImplementedDeprecations(Sema &S,
241 NamedDecl *ND,
242 SourceLocation ImplLoc,
243 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000244 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000245 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000246 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000247 S.Diag(ND->getLocation(), diag::note_method_declared_at)
248 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000249 else
250 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
251 }
252}
253
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000254/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
255/// pool.
256void Sema::AddAnyMethodToGlobalPool(Decl *D) {
257 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
258
259 // If we don't have a valid method decl, simply return.
260 if (!MDecl)
261 return;
262 if (MDecl->isInstanceMethod())
263 AddInstanceMethodToGlobalPool(MDecl, true);
264 else
265 AddFactoryMethodToGlobalPool(MDecl, true);
266}
267
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000268/// ActOnStartOfObjCMethodOrCFunctionDef - This routine sets up parameters; invisible
269/// and user declared, in the method definition's AST. This routine is also called
270/// for C-functions defined in an Objective-c class implementation.
271void Sema::ActOnStartOfObjCMethodOrCFunctionDef(Scope *FnBodyScope, Decl *D,
272 bool parseMethod) {
273 assert((getCurMethodDecl() == 0 && getCurFunctionDecl() == 0) &&
274 "Method/c-function parsing confused");
275 if (!parseMethod) {
Chandler Carruth2e2c7a42012-07-03 00:15:11 +0000276 FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000277 // If we don't have a valid c-function decl, simply return.
278 if (!FDecl)
279 return;
280 PushDeclContext(FnBodyScope, FDecl);
281 PushFunctionScope();
282
283 for (FunctionDecl::param_const_iterator PI = FDecl->param_begin(),
284 E = FDecl->param_end(); PI != E; ++PI) {
285 ParmVarDecl *Param = (*PI);
286 if (!Param->isInvalidDecl() &&
287 RequireCompleteType(Param->getLocation(), Param->getType(),
288 diag::err_typecheck_decl_incomplete_type))
289 Param->setInvalidDecl();
290 if ((*PI)->getIdentifier())
291 PushOnScopeChains(*PI, FnBodyScope);
292 }
293 return;
294 }
295
John McCalld226f652010-08-21 09:40:31 +0000296 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000297
Steve Naroff394f3f42008-07-25 17:57:26 +0000298 // If we don't have a valid method decl, simply return.
299 if (!MDecl)
300 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000301
Chris Lattner4d391482007-12-12 07:09:47 +0000302 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000303 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000304 PushFunctionScope();
305
Chris Lattner4d391482007-12-12 07:09:47 +0000306 // Create Decl objects for each parameter, entrring them in the scope for
307 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000308
309 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000310 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Daniel Dunbar451318c2008-08-26 06:07:48 +0000312 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
313 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000314
Chris Lattner8123a952008-04-10 02:22:51 +0000315 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000316 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000317 E = MDecl->param_end(); PI != E; ++PI) {
318 ParmVarDecl *Param = (*PI);
319 if (!Param->isInvalidDecl() &&
320 RequireCompleteType(Param->getLocation(), Param->getType(),
321 diag::err_typecheck_decl_incomplete_type))
322 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000323 if ((*PI)->getIdentifier())
324 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000325 }
John McCallf85e1932011-06-15 23:02:42 +0000326
327 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000328 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000329 switch (MDecl->getMethodFamily()) {
330 case OMF_retain:
331 case OMF_retainCount:
332 case OMF_release:
333 case OMF_autorelease:
334 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
335 << MDecl->getSelector();
336 break;
337
338 case OMF_None:
339 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000340 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000341 case OMF_alloc:
342 case OMF_init:
343 case OMF_mutableCopy:
344 case OMF_copy:
345 case OMF_new:
346 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000347 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000348 break;
349 }
350 }
351
Nico Weber9a1ecf02011-08-22 17:25:57 +0000352 // Warn on deprecated methods under -Wdeprecated-implementations,
353 // and prepare for warning on missing super calls.
354 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000355 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000356 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000357 DiagnoseObjCImplementedDeprecations(*this,
358 dyn_cast<NamedDecl>(IMD),
359 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000360
Nico Weber80cb6e62011-08-28 22:35:17 +0000361 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000362 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
363 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
364 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000365 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000366 ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000367 !(Context.getLangOpts().ObjCAutoRefCount ||
368 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000369 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000370 ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000371 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000372 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000373 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000374 }
Chris Lattner4d391482007-12-12 07:09:47 +0000375}
376
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000377namespace {
378
379// Callback to only accept typo corrections that are Objective-C classes.
380// If an ObjCInterfaceDecl* is given to the constructor, then the validation
381// function will reject corrections to that class.
382class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
383 public:
384 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
385 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
386 : CurrentIDecl(IDecl) {}
387
388 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
389 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
390 return ID && !declaresSameEntity(ID, CurrentIDecl);
391 }
392
393 private:
394 ObjCInterfaceDecl *CurrentIDecl;
395};
396
397}
398
John McCalld226f652010-08-21 09:40:31 +0000399Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000400ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
401 IdentifierInfo *ClassName, SourceLocation ClassLoc,
402 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000403 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000404 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000405 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000406 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattner4d391482007-12-12 07:09:47 +0000408 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000409 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000410 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000411
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000412 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000413 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000414 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Douglas Gregor7723fec2011-12-15 20:29:51 +0000417 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000418 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000419 ObjCInterfaceDecl *IDecl
420 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000421 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000422
Douglas Gregor7723fec2011-12-15 20:29:51 +0000423 if (PrevIDecl) {
424 // Class already seen. Was it a definition?
425 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
426 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
427 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000428 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000429 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000430 }
Chris Lattner4d391482007-12-12 07:09:47 +0000431 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000432
433 if (AttrList)
434 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
435 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregor7723fec2011-12-15 20:29:51 +0000437 // Start the definition of this class. If we're in a redefinition case, there
438 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000439 if (!IDecl->hasDefinition())
440 IDecl->startDefinition();
441
Chris Lattner4d391482007-12-12 07:09:47 +0000442 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000443 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000444 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
445 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000446
447 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000448 // Try to correct for a typo in the superclass name without correcting
449 // to the class we're defining.
450 ObjCInterfaceValidatorCCC Validator(IDecl);
451 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000452 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000453 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000454 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
455 Diag(SuperLoc, diag::err_undef_superclass_suggest)
456 << SuperName << ClassName << PrevDecl->getDeclName();
457 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
458 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000459 }
460 }
461
Douglas Gregor60ef3082011-12-15 00:29:59 +0000462 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000463 Diag(SuperLoc, diag::err_recursive_superclass)
464 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000465 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000466 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000467 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000468 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000469
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000470 // Diagnose classes that inherit from deprecated classes.
471 if (SuperClassDecl)
472 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000474 if (PrevDecl && SuperClassDecl == 0) {
475 // The previous declaration was not a class decl. Check if we have a
476 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000477 if (const TypedefNameDecl *TDecl =
478 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000479 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000480 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000481 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
482 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000483 }
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000486 // This handles the following case:
487 //
488 // typedef int SuperClass;
489 // @interface MyClass : SuperClass {} @end
490 //
491 if (!SuperClassDecl) {
492 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
493 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000494 }
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Richard Smith162e1c12011-04-15 14:24:37 +0000497 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000498 if (!SuperClassDecl)
499 Diag(SuperLoc, diag::err_undef_superclass)
500 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000501 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000502 Context.getObjCInterfaceType(SuperClassDecl),
503 diag::err_forward_superclass,
504 SuperClassDecl->getDeclName(),
505 ClassName,
506 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000507 SuperClassDecl = 0;
508 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000509 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000510 IDecl->setSuperClass(SuperClassDecl);
511 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000512 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000513 }
Chris Lattner4d391482007-12-12 07:09:47 +0000514 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000515 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Sebastian Redl0b17c612010-08-13 00:28:03 +0000518 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000519 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000520 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000521 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000522 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000523 }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Anders Carlsson15281452008-11-04 16:57:32 +0000525 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000526 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000527}
528
529/// ActOnCompatiblityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000530/// a \@compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000531Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
532 IdentifierInfo *AliasName,
533 SourceLocation AliasLocation,
534 IdentifierInfo *ClassName,
535 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000536 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000537 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000538 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000539 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000540 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000541 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000542 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000543 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000544 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000545 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000546 }
547 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000548 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000549 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000550 if (const TypedefNameDecl *TDecl =
551 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000552 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000553 if (T->isObjCObjectType()) {
554 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000555 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000556 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000557 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000558 }
559 }
560 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000561 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
562 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000563 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000564 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000565 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000566 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000569 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000570 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000571 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Anders Carlsson15281452008-11-04 16:57:32 +0000573 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000574 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000575
John McCalld226f652010-08-21 09:40:31 +0000576 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000577}
578
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000579bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000580 IdentifierInfo *PName,
581 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000582 const ObjCList<ObjCProtocolDecl> &PList) {
583
584 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000585 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
586 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000587 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
588 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000589 if (PDecl->getIdentifier() == PName) {
590 Diag(Ploc, diag::err_protocol_has_circular_dependency);
591 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000592 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000593 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000594
595 if (!PDecl->hasDefinition())
596 continue;
597
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000598 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
599 PDecl->getLocation(), PDecl->getReferencedProtocols()))
600 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000601 }
602 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000603 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000604}
605
John McCalld226f652010-08-21 09:40:31 +0000606Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000607Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
608 IdentifierInfo *ProtocolName,
609 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000610 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000611 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000612 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000613 SourceLocation EndProtoLoc,
614 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000615 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000616 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000617 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000618 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
619 ForRedeclaration);
620 ObjCProtocolDecl *PDecl = 0;
621 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
622 // If we already have a definition, complain.
623 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
624 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Douglas Gregor27c6da22012-01-01 20:30:41 +0000626 // Create a new protocol that is completely distinct from previous
627 // declarations, and do not make this protocol available for name lookup.
628 // That way, we'll end up completely ignoring the duplicate.
629 // FIXME: Can we turn this into an error?
630 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
631 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000632 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000633 PDecl->startDefinition();
634 } else {
635 if (PrevDecl) {
636 // Check for circular dependencies among protocol declarations. This can
637 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000638 ObjCList<ObjCProtocolDecl> PList;
639 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
640 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000641 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000642 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000643
644 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000645 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000646 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000647 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000648
Douglas Gregor6e378de2009-04-23 23:18:26 +0000649 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000650 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000651 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000652
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000653 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000654 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000655
656 // Merge attributes from previous declarations.
657 if (PrevDecl)
658 mergeDeclAttributes(PDecl, PrevDecl);
659
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000660 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000661 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000662 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
663 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000664 }
Mike Stump1eb44332009-09-09 15:08:12 +0000665
666 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000667 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000668}
669
670/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000671/// issues an error if they are not declared. It returns list of
672/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000673void
Chris Lattnere13b9592008-07-26 04:03:38 +0000674Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000675 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000676 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000677 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000678 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000679 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
680 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000681 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000682 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000683 TypoCorrection Corrected = CorrectTypo(
684 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000685 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000686 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000687 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000688 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000689 Diag(PDecl->getLocation(), diag::note_previous_decl)
690 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000691 }
692 }
693
694 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000695 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000696 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000697 continue;
698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000700 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000701
702 // If this is a forward declaration and we are supposed to warn in this
703 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000704 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000705 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000706 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000707 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000708 }
709}
710
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000711/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000712/// a class method in its extension.
713///
Mike Stump1eb44332009-09-09 15:08:12 +0000714void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000715 ObjCInterfaceDecl *ID) {
716 if (!ID)
717 return; // Possibly due to previous error
718
719 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000720 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
721 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000722 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000723 MethodMap[MD->getSelector()] = MD;
724 }
725
726 if (MethodMap.empty())
727 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000728 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
729 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000730 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000731 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
732 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
733 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
734 << Method->getDeclName();
735 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
736 }
737 }
738}
739
James Dennett1dfbd922012-06-14 21:40:34 +0000740/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000741Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000742Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000743 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000744 unsigned NumElts,
745 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000746 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000747 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000748 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000749 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
750 ForRedeclaration);
751 ObjCProtocolDecl *PDecl
752 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
753 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000754 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000755
756 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000757 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000758
Douglas Gregor3937f872012-01-01 20:33:24 +0000759 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000760 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000761
762 if (PrevDecl)
763 mergeDeclAttributes(PDecl, PrevDecl);
764
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000765 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000766 }
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000768 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000769}
770
John McCalld226f652010-08-21 09:40:31 +0000771Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000772ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
773 IdentifierInfo *ClassName, SourceLocation ClassLoc,
774 IdentifierInfo *CategoryName,
775 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000776 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000777 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000778 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000779 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000780 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000781 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000782
783 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000784
785 if (!IDecl
786 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000787 diag::err_category_forward_interface,
788 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000789 // Create an invalid ObjCCategoryDecl to serve as context for
790 // the enclosing method declarations. We mark the decl invalid
791 // to make it clear that this isn't a valid AST.
792 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000793 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000794 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000795 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000796
797 if (!IDecl)
798 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000799 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000800 }
801
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000802 if (!CategoryName && IDecl->getImplementation()) {
803 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
804 Diag(IDecl->getImplementation()->getLocation(),
805 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000806 }
807
Fariborz Jahanian25760612010-02-15 21:55:26 +0000808 if (CategoryName) {
809 /// Check for duplicate interface declaration for this category
810 ObjCCategoryDecl *CDeclChain;
811 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
812 CDeclChain = CDeclChain->getNextClassCategory()) {
813 if (CDeclChain->getIdentifier() == CategoryName) {
814 // Class extensions can be declared multiple times.
815 Diag(CategoryLoc, diag::warn_dup_category_def)
816 << ClassName << CategoryName;
817 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
818 break;
819 }
Chris Lattner70f19542009-02-16 21:26:43 +0000820 }
821 }
Chris Lattner70f19542009-02-16 21:26:43 +0000822
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000823 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
824 ClassLoc, CategoryLoc, CategoryName, IDecl);
825 // FIXME: PushOnScopeChains?
826 CurContext->addDecl(CDecl);
827
Chris Lattner4d391482007-12-12 07:09:47 +0000828 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000829 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000830 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000831 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000832 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000833 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000834 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000835 }
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Anders Carlsson15281452008-11-04 16:57:32 +0000837 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000838 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000839}
840
841/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000842/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000843/// object.
John McCalld226f652010-08-21 09:40:31 +0000844Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000845 SourceLocation AtCatImplLoc,
846 IdentifierInfo *ClassName, SourceLocation ClassLoc,
847 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000848 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000849 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000850 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000851 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
852 if (!CatIDecl) {
853 // Category @implementation with no corresponding @interface.
854 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000855 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
856 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000857 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000858 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000859 }
860 }
861
Mike Stump1eb44332009-09-09 15:08:12 +0000862 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000863 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000864 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000865 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000866 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000867 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000868 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000869 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
870 diag::err_undef_interface)) {
871 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000872 }
Chris Lattner4d391482007-12-12 07:09:47 +0000873
Douglas Gregord0434102009-01-09 00:49:46 +0000874 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000875 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000876
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000877 // If the interface is deprecated/unavailable, warn/error about it.
878 if (IDecl)
879 DiagnoseUseOfDecl(IDecl, ClassLoc);
880
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000881 /// Check that CatName, category name, is not used in another implementation.
882 if (CatIDecl) {
883 if (CatIDecl->getImplementation()) {
884 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
885 << CatName;
886 Diag(CatIDecl->getImplementation()->getLocation(),
887 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000888 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000889 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000890 // Warn on implementating category of deprecated class under
891 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000892 DiagnoseObjCImplementedDeprecations(*this,
893 dyn_cast<NamedDecl>(IDecl),
894 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000895 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Anders Carlsson15281452008-11-04 16:57:32 +0000898 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000899 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000900}
901
John McCalld226f652010-08-21 09:40:31 +0000902Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000903 SourceLocation AtClassImplLoc,
904 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000905 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000906 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000907 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000908 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000909 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000910 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
911 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000912 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000913 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000914 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000915 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000916 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
917 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000918 } else {
919 // We did not find anything with the name ClassName; try to correct for
920 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000921 ObjCInterfaceValidatorCCC Validator;
922 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000923 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000924 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000925 // Suggest the (potentially) correct interface name. However, put the
926 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000927 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000928 // provide a code-modification hint or use the typo name for recovery,
929 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000930 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000931 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000932 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000933 << ClassName << CorrectedName;
934 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
935 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000936 IDecl = 0;
937 } else {
938 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
939 }
Chris Lattner4d391482007-12-12 07:09:47 +0000940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Chris Lattner4d391482007-12-12 07:09:47 +0000942 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000943 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000944 if (SuperClassname) {
945 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000946 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
947 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000948 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000949 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
950 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000951 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000952 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000953 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000954 if (SDecl && !SDecl->hasDefinition())
955 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000956 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000957 Diag(SuperClassLoc, diag::err_undef_superclass)
958 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000959 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000960 // This implementation and its interface do not have the same
961 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000962 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000963 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000964 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000965 }
966 }
967 }
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Chris Lattner4d391482007-12-12 07:09:47 +0000969 if (!IDecl) {
970 // Legacy case of @implementation with no corresponding @interface.
971 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000972
Mike Stump390b4cc2009-05-16 07:39:55 +0000973 // FIXME: Do we support attributes on the @implementation? If so we should
974 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000975 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000976 ClassName, /*PrevDecl=*/0, ClassLoc,
977 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000978 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000979 if (SDecl) {
980 IDecl->setSuperClass(SDecl);
981 IDecl->setSuperClassLoc(SuperClassLoc);
982 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
983 } else {
984 IDecl->setEndOfDefinitionLoc(ClassLoc);
985 }
986
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000987 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000988 } else {
989 // Mark the interface as being completed, even if it was just as
990 // @class ....;
991 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000992 if (!IDecl->hasDefinition())
993 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
996 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000997 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
998 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Anders Carlsson15281452008-11-04 16:57:32 +00001000 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001001 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Chris Lattner4d391482007-12-12 07:09:47 +00001003 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001004 if (IDecl->getImplementation()) {
1005 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001006 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001007 Diag(IDecl->getImplementation()->getLocation(),
1008 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001009 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001010 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001011 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001012 // Warn on implementating deprecated class under
1013 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001014 DiagnoseObjCImplementedDeprecations(*this,
1015 dyn_cast<NamedDecl>(IDecl),
1016 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001017 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001018 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001019}
1020
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001021Sema::DeclGroupPtrTy
1022Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1023 SmallVector<Decl *, 64> DeclsInGroup;
1024 DeclsInGroup.reserve(Decls.size() + 1);
1025
1026 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1027 Decl *Dcl = Decls[i];
1028 if (!Dcl)
1029 continue;
1030 if (Dcl->getDeclContext()->isFileContext())
1031 Dcl->setTopLevelDeclInObjCContainer();
1032 DeclsInGroup.push_back(Dcl);
1033 }
1034
1035 DeclsInGroup.push_back(ObjCImpDecl);
1036
1037 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1038}
1039
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001040void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1041 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001042 SourceLocation RBrace) {
1043 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001044 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001045 if (!IDecl)
1046 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001047 /// Check case of non-existing \@interface decl.
1048 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001049 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001050 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001051 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001052 // Add ivar's to class's DeclContext.
1053 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001054 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001055 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001056 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001057 }
1058
Chris Lattner4d391482007-12-12 07:09:47 +00001059 return;
1060 }
1061 // If implementation has empty ivar list, just return.
1062 if (numIvars == 0)
1063 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattner4d391482007-12-12 07:09:47 +00001065 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001066 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001067 if (ImpDecl->getSuperClass())
1068 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1069 for (unsigned i = 0; i < numIvars; i++) {
1070 ObjCIvarDecl* ImplIvar = ivars[i];
1071 if (const ObjCIvarDecl *ClsIvar =
1072 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1073 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1074 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1075 continue;
1076 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001077 // Instance ivar to Implementation's DeclContext.
1078 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001079 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001080 ImpDecl->addDecl(ImplIvar);
1081 }
1082 return;
1083 }
Chris Lattner4d391482007-12-12 07:09:47 +00001084 // Check interface's Ivar list against those in the implementation.
1085 // names and types must match.
1086 //
Chris Lattner4d391482007-12-12 07:09:47 +00001087 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001088 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001089 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1090 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001091 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001092 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001093 assert (ImplIvar && "missing implementation ivar");
1094 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Steve Naroffca331292009-03-03 14:49:36 +00001096 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001097 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001098 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001099 << ImplIvar->getIdentifier()
1100 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001101 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001102 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1103 ImplIvar->getBitWidthValue(Context) !=
1104 ClsIvar->getBitWidthValue(Context)) {
1105 Diag(ImplIvar->getBitWidth()->getLocStart(),
1106 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1107 Diag(ClsIvar->getBitWidth()->getLocStart(),
1108 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001109 }
Steve Naroffca331292009-03-03 14:49:36 +00001110 // Make sure the names are identical.
1111 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001112 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001113 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001114 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001115 }
1116 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Chris Lattner609e4c72007-12-12 18:11:49 +00001119 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001120 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001121 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001122 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001123}
1124
Steve Naroff3c2eb662008-02-10 21:38:56 +00001125void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001126 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001127 // No point warning no definition of method which is 'unavailable'.
1128 if (method->hasAttr<UnavailableAttr>())
1129 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001130 if (!IncompleteImpl) {
1131 Diag(ImpLoc, diag::warn_incomplete_impl);
1132 IncompleteImpl = true;
1133 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001134 if (DiagID == diag::warn_unimplemented_protocol_method)
1135 Diag(ImpLoc, DiagID) << method->getDeclName();
1136 else
1137 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001138}
1139
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001140/// Determines if type B can be substituted for type A. Returns true if we can
1141/// guarantee that anything that the user will do to an object of type A can
1142/// also be done to an object of type B. This is trivially true if the two
1143/// types are the same, or if B is a subclass of A. It becomes more complex
1144/// in cases where protocols are involved.
1145///
1146/// Object types in Objective-C describe the minimum requirements for an
1147/// object, rather than providing a complete description of a type. For
1148/// example, if A is a subclass of B, then B* may refer to an instance of A.
1149/// The principle of substitutability means that we may use an instance of A
1150/// anywhere that we may use an instance of B - it will implement all of the
1151/// ivars of B and all of the methods of B.
1152///
1153/// This substitutability is important when type checking methods, because
1154/// the implementation may have stricter type definitions than the interface.
1155/// The interface specifies minimum requirements, but the implementation may
1156/// have more accurate ones. For example, a method may privately accept
1157/// instances of B, but only publish that it accepts instances of A. Any
1158/// object passed to it will be type checked against B, and so will implicitly
1159/// by a valid A*. Similarly, a method may return a subclass of the class that
1160/// it is declared as returning.
1161///
1162/// This is most important when considering subclassing. A method in a
1163/// subclass must accept any object as an argument that its superclass's
1164/// implementation accepts. It may, however, accept a more general type
1165/// without breaking substitutability (i.e. you can still use the subclass
1166/// anywhere that you can use the superclass, but not vice versa). The
1167/// converse requirement applies to return types: the return type for a
1168/// subclass method must be a valid object of the kind that the superclass
1169/// advertises, but it may be specified more accurately. This avoids the need
1170/// for explicit down-casting by callers.
1171///
1172/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001173static bool isObjCTypeSubstitutable(ASTContext &Context,
1174 const ObjCObjectPointerType *A,
1175 const ObjCObjectPointerType *B,
1176 bool rejectId) {
1177 // Reject a protocol-unqualified id.
1178 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001179
1180 // If B is a qualified id, then A must also be a qualified id and it must
1181 // implement all of the protocols in B. It may not be a qualified class.
1182 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1183 // stricter definition so it is not substitutable for id<A>.
1184 if (B->isObjCQualifiedIdType()) {
1185 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001186 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1187 QualType(B,0),
1188 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001189 }
1190
1191 /*
1192 // id is a special type that bypasses type checking completely. We want a
1193 // warning when it is used in one place but not another.
1194 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1195
1196
1197 // If B is a qualified id, then A must also be a qualified id (which it isn't
1198 // if we've got this far)
1199 if (B->isObjCQualifiedIdType()) return false;
1200 */
1201
1202 // Now we know that A and B are (potentially-qualified) class types. The
1203 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001204 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001205}
1206
John McCall10302c02010-10-28 02:34:38 +00001207static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1208 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1209}
1210
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001211static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001212 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001213 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001214 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001215 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001216 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001217 if (IsProtocolMethodDecl &&
1218 (MethodDecl->getObjCDeclQualifier() !=
1219 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001220 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001221 S.Diag(MethodImpl->getLocation(),
1222 (IsOverridingMode ?
1223 diag::warn_conflicting_overriding_ret_type_modifiers
1224 : diag::warn_conflicting_ret_type_modifiers))
1225 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001226 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1227 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1228 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1229 }
1230 else
1231 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001232 }
1233
John McCall10302c02010-10-28 02:34:38 +00001234 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001235 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001236 return true;
1237 if (!Warn)
1238 return false;
John McCall10302c02010-10-28 02:34:38 +00001239
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001240 unsigned DiagID =
1241 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1242 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001243
1244 // Mismatches between ObjC pointers go into a different warning
1245 // category, and sometimes they're even completely whitelisted.
1246 if (const ObjCObjectPointerType *ImplPtrTy =
1247 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1248 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001249 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001250 // Allow non-matching return types as long as they don't violate
1251 // the principle of substitutability. Specifically, we permit
1252 // return types that are subclasses of the declared return type,
1253 // or that are more-qualified versions of the declared type.
1254 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001255 return false;
John McCall10302c02010-10-28 02:34:38 +00001256
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001257 DiagID =
1258 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1259 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001260 }
1261 }
1262
1263 S.Diag(MethodImpl->getLocation(), DiagID)
1264 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001265 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001266 << MethodImpl->getResultType()
1267 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001268 S.Diag(MethodDecl->getLocation(),
1269 IsOverridingMode ? diag::note_previous_declaration
1270 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001271 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001272 return false;
John McCall10302c02010-10-28 02:34:38 +00001273}
1274
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001275static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001276 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001277 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001278 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001279 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001280 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001281 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001282 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001283 if (IsProtocolMethodDecl &&
1284 (ImplVar->getObjCDeclQualifier() !=
1285 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001286 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001287 if (IsOverridingMode)
1288 S.Diag(ImplVar->getLocation(),
1289 diag::warn_conflicting_overriding_param_modifiers)
1290 << getTypeRange(ImplVar->getTypeSourceInfo())
1291 << MethodImpl->getDeclName();
1292 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001293 diag::warn_conflicting_param_modifiers)
1294 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001295 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001296 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1297 << getTypeRange(IfaceVar->getTypeSourceInfo());
1298 }
1299 else
1300 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001301 }
1302
John McCall10302c02010-10-28 02:34:38 +00001303 QualType ImplTy = ImplVar->getType();
1304 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001305
John McCall10302c02010-10-28 02:34:38 +00001306 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001307 return true;
1308
1309 if (!Warn)
1310 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001311 unsigned DiagID =
1312 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1313 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001314
1315 // Mismatches between ObjC pointers go into a different warning
1316 // category, and sometimes they're even completely whitelisted.
1317 if (const ObjCObjectPointerType *ImplPtrTy =
1318 ImplTy->getAs<ObjCObjectPointerType>()) {
1319 if (const ObjCObjectPointerType *IfacePtrTy =
1320 IfaceTy->getAs<ObjCObjectPointerType>()) {
1321 // Allow non-matching argument types as long as they don't
1322 // violate the principle of substitutability. Specifically, the
1323 // implementation must accept any objects that the superclass
1324 // accepts, however it may also accept others.
1325 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001326 return false;
John McCall10302c02010-10-28 02:34:38 +00001327
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001328 DiagID =
1329 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1330 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001331 }
1332 }
1333
1334 S.Diag(ImplVar->getLocation(), DiagID)
1335 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001336 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1337 S.Diag(IfaceVar->getLocation(),
1338 (IsOverridingMode ? diag::note_previous_declaration
1339 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001340 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001341 return false;
John McCall10302c02010-10-28 02:34:38 +00001342}
John McCallf85e1932011-06-15 23:02:42 +00001343
1344/// In ARC, check whether the conventional meanings of the two methods
1345/// match. If they don't, it's a hard error.
1346static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1347 ObjCMethodDecl *decl) {
1348 ObjCMethodFamily implFamily = impl->getMethodFamily();
1349 ObjCMethodFamily declFamily = decl->getMethodFamily();
1350 if (implFamily == declFamily) return false;
1351
1352 // Since conventions are sorted by selector, the only possibility is
1353 // that the types differ enough to cause one selector or the other
1354 // to fall out of the family.
1355 assert(implFamily == OMF_None || declFamily == OMF_None);
1356
1357 // No further diagnostics required on invalid declarations.
1358 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1359
1360 const ObjCMethodDecl *unmatched = impl;
1361 ObjCMethodFamily family = declFamily;
1362 unsigned errorID = diag::err_arc_lost_method_convention;
1363 unsigned noteID = diag::note_arc_lost_method_convention;
1364 if (declFamily == OMF_None) {
1365 unmatched = decl;
1366 family = implFamily;
1367 errorID = diag::err_arc_gained_method_convention;
1368 noteID = diag::note_arc_gained_method_convention;
1369 }
1370
1371 // Indexes into a %select clause in the diagnostic.
1372 enum FamilySelector {
1373 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1374 };
1375 FamilySelector familySelector = FamilySelector();
1376
1377 switch (family) {
1378 case OMF_None: llvm_unreachable("logic error, no method convention");
1379 case OMF_retain:
1380 case OMF_release:
1381 case OMF_autorelease:
1382 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001383 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001384 case OMF_retainCount:
1385 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001386 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001387 // Mismatches for these methods don't change ownership
1388 // conventions, so we don't care.
1389 return false;
1390
1391 case OMF_init: familySelector = F_init; break;
1392 case OMF_alloc: familySelector = F_alloc; break;
1393 case OMF_copy: familySelector = F_copy; break;
1394 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1395 case OMF_new: familySelector = F_new; break;
1396 }
1397
1398 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1399 ReasonSelector reasonSelector;
1400
1401 // The only reason these methods don't fall within their families is
1402 // due to unusual result types.
1403 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1404 reasonSelector = R_UnrelatedReturn;
1405 } else {
1406 reasonSelector = R_NonObjectReturn;
1407 }
1408
1409 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1410 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1411
1412 return true;
1413}
John McCall10302c02010-10-28 02:34:38 +00001414
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001415void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001416 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001417 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001418 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001419 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1420 return;
1421
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001422 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001423 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001424 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Chris Lattner3aff9192009-04-11 19:58:42 +00001426 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001427 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1428 EF = MethodDecl->param_end();
1429 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001430 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001431 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001432 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001433
Fariborz Jahanian21121902011-08-08 18:03:17 +00001434 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001435 Diag(ImpMethodDecl->getLocation(),
1436 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001437 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001438 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001439}
1440
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001441void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1442 ObjCMethodDecl *Overridden,
1443 bool IsProtocolMethodDecl) {
1444
1445 CheckMethodOverrideReturn(*this, Method, Overridden,
1446 IsProtocolMethodDecl, true,
1447 true);
1448
1449 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001450 IF = Overridden->param_begin(), EM = Method->param_end(),
1451 EF = Overridden->param_end();
1452 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001453 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1454 IsProtocolMethodDecl, true, true);
1455 }
1456
1457 if (Method->isVariadic() != Overridden->isVariadic()) {
1458 Diag(Method->getLocation(),
1459 diag::warn_conflicting_overriding_variadic);
1460 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1461 }
1462}
1463
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001464/// WarnExactTypedMethods - This routine issues a warning if method
1465/// implementation declaration matches exactly that of its declaration.
1466void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1467 ObjCMethodDecl *MethodDecl,
1468 bool IsProtocolMethodDecl) {
1469 // don't issue warning when protocol method is optional because primary
1470 // class is not required to implement it and it is safe for protocol
1471 // to implement it.
1472 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1473 return;
1474 // don't issue warning when primary class's method is
1475 // depecated/unavailable.
1476 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1477 MethodDecl->hasAttr<DeprecatedAttr>())
1478 return;
1479
1480 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1481 IsProtocolMethodDecl, false, false);
1482 if (match)
1483 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001484 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1485 EF = MethodDecl->param_end();
1486 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001487 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1488 *IM, *IF,
1489 IsProtocolMethodDecl, false, false);
1490 if (!match)
1491 break;
1492 }
1493 if (match)
1494 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001495 if (match)
1496 match = !(MethodDecl->isClassMethod() &&
1497 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001498
1499 if (match) {
1500 Diag(ImpMethodDecl->getLocation(),
1501 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001502 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1503 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001504 }
1505}
1506
Mike Stump390b4cc2009-05-16 07:39:55 +00001507/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1508/// improve the efficiency of selector lookups and type checking by associating
1509/// with each protocol / interface / category the flattened instance tables. If
1510/// we used an immutable set to keep the table then it wouldn't add significant
1511/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001512
Steve Naroffefe7f362008-02-08 22:06:17 +00001513/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001514/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001515void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1516 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001517 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001518 const SelectorSet &InsMap,
1519 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001520 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001521 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1522 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1523 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001524 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1525
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001526 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001527 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001528 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001529 // check to see if class implements forwardInvocation method and objects
1530 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001531 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001532 // Under such conditions, which means that every method possible is
1533 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001534 // found" warnings.
1535 // FIXME: Use a general GetUnarySelector method for this.
1536 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1537 Selector fISelector = Context.Selectors.getSelector(1, &II);
1538 if (InsMap.count(fISelector))
1539 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1540 // need be implemented in the implementation.
1541 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001544 // If a method lookup fails locally we still need to look and see if
1545 // the method was implemented by a base class or an inherited
1546 // protocol. This lookup is slow, but occurs rarely in correct code
1547 // and otherwise would terminate in a warning.
1548
Chris Lattner4d391482007-12-12 07:09:47 +00001549 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001550 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001551 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001552 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001553 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001554 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001555 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001556 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001557 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001558 // If a method is not implemented in the category implementation but
1559 // has been declared in its primary class, superclass,
1560 // or in one of their protocols, no need to issue the warning.
1561 // This is because method will be implemented in the primary class
1562 // or one of its super class implementation.
1563
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001564 // Ugly, but necessary. Method declared in protcol might have
1565 // have been synthesized due to a property declared in the class which
1566 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001567 if (ObjCMethodDecl *MethodInClass =
1568 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001569 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001570 if (C || MethodInClass->isSynthesized())
1571 continue;
1572 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1573 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1574 != DiagnosticsEngine::Ignored) {
1575 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001576 Diag(method->getLocation(), diag::note_method_declared_at)
1577 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001578 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1579 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001580 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001581 }
1582 }
Chris Lattner4d391482007-12-12 07:09:47 +00001583 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001584 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001585 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001586 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001588 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1589 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001590 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001591 // See above comment for instance method lookups.
1592 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001593 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001594 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001595 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001596 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1597 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001598 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001599 Diag(method->getLocation(), diag::note_method_declared_at)
1600 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001601 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1602 PDecl->getDeclName();
1603 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001604 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001605 }
Chris Lattner780f3292008-07-21 21:32:27 +00001606 // Check on this protocols's referenced protocols, recursively.
1607 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1608 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001609 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001610}
1611
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001612/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001613/// or protocol against those declared in their implementations.
1614///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001615void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1616 const SelectorSet &ClsMap,
1617 SelectorSet &InsMapSeen,
1618 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001619 ObjCImplDecl* IMPDecl,
1620 ObjCContainerDecl* CDecl,
1621 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001622 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001623 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001624 // Check and see if instance methods in class interface have been
1625 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001626 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1627 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001628 if (InsMapSeen.count((*I)->getSelector()))
1629 continue;
1630 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001631 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001632 !InsMap.count((*I)->getSelector())) {
1633 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001634 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1635 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001636 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001637 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001638 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001639 IMPDecl->getInstanceMethod((*I)->getSelector());
1640 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1641 "Expected to find the method through lookup as well");
1642 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001643 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001644 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001645 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001646 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1647 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001648 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001649 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001650 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001651 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001652 }
1653 }
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001655 // Check and see if class methods in class interface have been
1656 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001657 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001658 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001659 if (ClsMapSeen.count((*I)->getSelector()))
1660 continue;
1661 ClsMapSeen.insert((*I)->getSelector());
1662 if (!ClsMap.count((*I)->getSelector())) {
1663 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001664 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1665 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001666 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001667 ObjCMethodDecl *ImpMethodDecl =
1668 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001669 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1670 "Expected to find the method through lookup as well");
1671 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001672 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001673 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1674 isa<ObjCProtocolDecl>(CDecl));
1675 else
1676 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001677 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001678 }
1679 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001680
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001681 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001682 // Also methods in class extensions need be looked at next.
1683 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1684 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1685 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1686 IMPDecl,
1687 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001688 IncompleteImpl, false,
1689 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001690
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001691 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001692 for (ObjCInterfaceDecl::all_protocol_iterator
1693 PI = I->all_referenced_protocol_begin(),
1694 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001695 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1696 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001697 (*PI), IncompleteImpl, false,
1698 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001699
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001700 // FIXME. For now, we are not checking for extact match of methods
1701 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001702 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001703 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001704 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001705 I->getSuperClass(), IncompleteImpl, false);
1706 }
1707}
1708
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001709/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1710/// category matches with those implemented in its primary class and
1711/// warns each time an exact match is found.
1712void Sema::CheckCategoryVsClassMethodMatches(
1713 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001714 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001715
1716 for (ObjCImplementationDecl::instmeth_iterator
1717 I = CatIMPDecl->instmeth_begin(),
1718 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1719 InsMap.insert((*I)->getSelector());
1720
1721 for (ObjCImplementationDecl::classmeth_iterator
1722 I = CatIMPDecl->classmeth_begin(),
1723 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1724 ClsMap.insert((*I)->getSelector());
1725 if (InsMap.empty() && ClsMap.empty())
1726 return;
1727
1728 // Get category's primary class.
1729 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1730 if (!CatDecl)
1731 return;
1732 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1733 if (!IDecl)
1734 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001735 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001736 bool IncompleteImpl = false;
1737 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1738 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001739 IncompleteImpl, false,
1740 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001741}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001742
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001743void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001744 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001745 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001746 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001747 // Check and see if instance methods in class interface have been
1748 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001749 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001750 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001751 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001753 // Check and see if properties declared in the interface have either 1)
1754 // an implementation or 2) there is a @synthesize/@dynamic implementation
1755 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001756 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001757 if (!(LangOpts.ObjCDefaultSynthProperties &&
1758 LangOpts.ObjCRuntime.isNonFragile()) ||
1759 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001760 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001761
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001762 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001763 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001764 I = IMPDecl->classmeth_begin(),
1765 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001766 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001768 // Check for type conflict of methods declared in a class/protocol and
1769 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001770 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001771 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1772 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001773 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001774
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001775 // check all methods implemented in category against those declared
1776 // in its primary class.
1777 if (ObjCCategoryImplDecl *CatDecl =
1778 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1779 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Chris Lattner4d391482007-12-12 07:09:47 +00001781 // Check the protocol list for unimplemented methods in the @implementation
1782 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001783 // Check and see if class methods in class interface have been
1784 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Chris Lattnercddc8882009-03-01 00:56:52 +00001786 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001787 for (ObjCInterfaceDecl::all_protocol_iterator
1788 PI = I->all_referenced_protocol_begin(),
1789 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001790 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001791 InsMap, ClsMap, I);
1792 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001793 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1794 Categories; Categories = Categories->getNextClassExtension())
1795 ImplMethodsVsClassMethods(S, IMPDecl,
1796 const_cast<ObjCCategoryDecl*>(Categories),
1797 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001798 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001799 // For extended class, unimplemented methods in its protocols will
1800 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001801 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001802 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1803 E = C->protocol_end(); PI != E; ++PI)
1804 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001805 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001806 // Report unimplemented properties in the category as well.
1807 // When reporting on missing setter/getters, do not report when
1808 // setter/getter is implemented in category's primary class
1809 // implementation.
1810 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1811 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1812 for (ObjCImplementationDecl::instmeth_iterator
1813 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1814 InsMap.insert((*I)->getSelector());
1815 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001816 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001817 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001818 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001819 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001820}
1821
Mike Stump1eb44332009-09-09 15:08:12 +00001822/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001823Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001824Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001825 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001826 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001827 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001828 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001829 for (unsigned i = 0; i != NumElts; ++i) {
1830 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001831 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001832 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001833 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001834 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001835 // Maybe we will complain about the shadowed template parameter.
1836 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1837 // Just pretend that we didn't see the previous declaration.
1838 PrevDecl = 0;
1839 }
1840
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001841 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001842 // GCC apparently allows the following idiom:
1843 //
1844 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1845 // @class XCElementToggler;
1846 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001847 // Here we have chosen to ignore the forward class declaration
1848 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001849 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001850 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001851 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001852 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001853 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001854 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001855 // to the underlying class. Just ignore the forward class with a warning
1856 // as this will force the intended behavior which is to lookup the typedef
1857 // name.
1858 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1859 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1860 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1861 continue;
1862 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001863 }
Chris Lattner4d391482007-12-12 07:09:47 +00001864 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001865
1866 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001867 ObjCInterfaceDecl *PrevIDecl
1868 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001869 ObjCInterfaceDecl *IDecl
1870 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001871 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001872 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001873
Douglas Gregor7723fec2011-12-15 20:29:51 +00001874 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001875 CheckObjCDeclScope(IDecl);
1876 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001877 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001878
1879 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001880}
1881
John McCall0f4c4c42011-06-16 01:15:19 +00001882static bool tryMatchRecordTypes(ASTContext &Context,
1883 Sema::MethodMatchStrategy strategy,
1884 const Type *left, const Type *right);
1885
John McCallf85e1932011-06-15 23:02:42 +00001886static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1887 QualType leftQT, QualType rightQT) {
1888 const Type *left =
1889 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1890 const Type *right =
1891 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1892
1893 if (left == right) return true;
1894
1895 // If we're doing a strict match, the types have to match exactly.
1896 if (strategy == Sema::MMS_strict) return false;
1897
1898 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1899
1900 // Otherwise, use this absurdly complicated algorithm to try to
1901 // validate the basic, low-level compatibility of the two types.
1902
1903 // As a minimum, require the sizes and alignments to match.
1904 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1905 return false;
1906
1907 // Consider all the kinds of non-dependent canonical types:
1908 // - functions and arrays aren't possible as return and parameter types
1909
1910 // - vector types of equal size can be arbitrarily mixed
1911 if (isa<VectorType>(left)) return isa<VectorType>(right);
1912 if (isa<VectorType>(right)) return false;
1913
1914 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001915 // - structs, unions, and Objective-C objects must match more-or-less
1916 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001917 // - everything else should be a scalar
1918 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001919 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001920
John McCall1d9b3b22011-09-09 05:25:32 +00001921 // Make scalars agree in kind, except count bools as chars, and group
1922 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001923 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1924 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1925 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1926 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001927 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1928 leftSK = Type::STK_ObjCObjectPointer;
1929 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1930 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001931
1932 // Note that data member pointers and function member pointers don't
1933 // intermix because of the size differences.
1934
1935 return (leftSK == rightSK);
1936}
Chris Lattner4d391482007-12-12 07:09:47 +00001937
John McCall0f4c4c42011-06-16 01:15:19 +00001938static bool tryMatchRecordTypes(ASTContext &Context,
1939 Sema::MethodMatchStrategy strategy,
1940 const Type *lt, const Type *rt) {
1941 assert(lt && rt && lt != rt);
1942
1943 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1944 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1945 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1946
1947 // Require union-hood to match.
1948 if (left->isUnion() != right->isUnion()) return false;
1949
1950 // Require an exact match if either is non-POD.
1951 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1952 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1953 return false;
1954
1955 // Require size and alignment to match.
1956 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1957
1958 // Require fields to match.
1959 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1960 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1961 for (; li != le && ri != re; ++li, ++ri) {
1962 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1963 return false;
1964 }
1965 return (li == le && ri == re);
1966}
1967
Chris Lattner4d391482007-12-12 07:09:47 +00001968/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1969/// returns true, or false, accordingly.
1970/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001971bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1972 const ObjCMethodDecl *right,
1973 MethodMatchStrategy strategy) {
1974 if (!matchTypes(Context, strategy,
1975 left->getResultType(), right->getResultType()))
1976 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001977
David Blaikie4e4d0842012-03-11 07:00:24 +00001978 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001979 (left->hasAttr<NSReturnsRetainedAttr>()
1980 != right->hasAttr<NSReturnsRetainedAttr>() ||
1981 left->hasAttr<NSConsumesSelfAttr>()
1982 != right->hasAttr<NSConsumesSelfAttr>()))
1983 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001985 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001986 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
1987 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001989 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00001990 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001991 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001992
1993 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1994 return false;
1995
David Blaikie4e4d0842012-03-11 07:00:24 +00001996 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001997 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1998 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001999 }
2000 return true;
2001}
2002
Douglas Gregorff310c72012-05-01 23:37:00 +00002003void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002004 // If the list is empty, make it a singleton list.
2005 if (List->Method == 0) {
2006 List->Method = Method;
2007 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002008 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002009 }
2010
2011 // We've seen a method with this name, see if we have already seen this type
2012 // signature.
2013 ObjCMethodList *Previous = List;
2014 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002015 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002016 continue;
2017
2018 ObjCMethodDecl *PrevObjCMethod = List->Method;
2019
2020 // Propagate the 'defined' bit.
2021 if (Method->isDefined())
2022 PrevObjCMethod->setDefined(true);
2023
2024 // If a method is deprecated, push it in the global pool.
2025 // This is used for better diagnostics.
2026 if (Method->isDeprecated()) {
2027 if (!PrevObjCMethod->isDeprecated())
2028 List->Method = Method;
2029 }
2030 // If new method is unavailable, push it into global pool
2031 // unless previous one is deprecated.
2032 if (Method->isUnavailable()) {
2033 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2034 List->Method = Method;
2035 }
2036
Douglas Gregorff310c72012-05-01 23:37:00 +00002037 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002038 }
2039
2040 // We have a new signature for an existing method - add it.
2041 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002042 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002043 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2044}
2045
Sebastian Redldb9d2142010-08-02 23:18:59 +00002046/// \brief Read the contents of the method pool for a given selector from
2047/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002048void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002049 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002050 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002051}
2052
Douglas Gregorff310c72012-05-01 23:37:00 +00002053void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002054 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002055 // Ignore methods of invalid containers.
2056 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002057 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002058
Douglas Gregor0d266d62012-01-25 00:59:09 +00002059 if (ExternalSource)
2060 ReadMethodPool(Method->getSelector());
2061
Sebastian Redldb9d2142010-08-02 23:18:59 +00002062 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002063 if (Pos == MethodPool.end())
2064 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2065 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002066
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002067 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002068
Sebastian Redldb9d2142010-08-02 23:18:59 +00002069 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002070 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002071}
2072
John McCallf85e1932011-06-15 23:02:42 +00002073/// Determines if this is an "acceptable" loose mismatch in the global
2074/// method pool. This exists mostly as a hack to get around certain
2075/// global mismatches which we can't afford to make warnings / errors.
2076/// Really, what we want is a way to take a method out of the global
2077/// method pool.
2078static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2079 ObjCMethodDecl *other) {
2080 if (!chosen->isInstanceMethod())
2081 return false;
2082
2083 Selector sel = chosen->getSelector();
2084 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2085 return false;
2086
2087 // Don't complain about mismatches for -length if the method we
2088 // chose has an integral result type.
2089 return (chosen->getResultType()->isIntegerType());
2090}
2091
Sebastian Redldb9d2142010-08-02 23:18:59 +00002092ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002093 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002094 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002095 if (ExternalSource)
2096 ReadMethodPool(Sel);
2097
Sebastian Redldb9d2142010-08-02 23:18:59 +00002098 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002099 if (Pos == MethodPool.end())
2100 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002101
Sebastian Redldb9d2142010-08-02 23:18:59 +00002102 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Sebastian Redldb9d2142010-08-02 23:18:59 +00002104 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002105 bool issueDiagnostic = false, issueError = false;
2106
2107 // We support a warning which complains about *any* difference in
2108 // method signature.
2109 bool strictSelectorMatch =
2110 (receiverIdOrClass && warn &&
2111 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2112 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002113 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002114 if (strictSelectorMatch)
2115 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002116 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2117 MMS_strict)) {
2118 issueDiagnostic = true;
2119 break;
2120 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002121 }
2122
John McCallf85e1932011-06-15 23:02:42 +00002123 // If we didn't see any strict differences, we won't see any loose
2124 // differences. In ARC, however, we also need to check for loose
2125 // mismatches, because most of them are errors.
2126 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002127 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002128 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002129 // This checks if the methods differ in type mismatch.
2130 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2131 MMS_loose) &&
2132 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2133 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002134 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002135 issueError = true;
2136 break;
2137 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002138 }
2139
John McCallf85e1932011-06-15 23:02:42 +00002140 if (issueDiagnostic) {
2141 if (issueError)
2142 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2143 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002144 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2145 else
2146 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002147
2148 Diag(MethList.Method->getLocStart(),
2149 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002150 << MethList.Method->getSourceRange();
2151 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2152 Diag(Next->Method->getLocStart(), diag::note_also_found)
2153 << Next->Method->getSourceRange();
2154 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002155 }
2156 return MethList.Method;
2157}
2158
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002159ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002160 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2161 if (Pos == MethodPool.end())
2162 return 0;
2163
2164 GlobalMethods &Methods = Pos->second;
2165
2166 if (Methods.first.Method && Methods.first.Method->isDefined())
2167 return Methods.first.Method;
2168 if (Methods.second.Method && Methods.second.Method->isDefined())
2169 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002170 return 0;
2171}
2172
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002173/// DiagnoseDuplicateIvars -
2174/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002175/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002176/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002177/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002178void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2179 ObjCInterfaceDecl *SID) {
2180 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2181 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002182 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002183 if (Ivar->isInvalidDecl())
2184 continue;
2185 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2186 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2187 if (prevIvar) {
2188 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2189 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2190 Ivar->setInvalidDecl();
2191 }
2192 }
2193 }
2194}
2195
Erik Verbruggend64251f2011-12-06 09:25:23 +00002196Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2197 switch (CurContext->getDeclKind()) {
2198 case Decl::ObjCInterface:
2199 return Sema::OCK_Interface;
2200 case Decl::ObjCProtocol:
2201 return Sema::OCK_Protocol;
2202 case Decl::ObjCCategory:
2203 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2204 return Sema::OCK_ClassExtension;
2205 else
2206 return Sema::OCK_Category;
2207 case Decl::ObjCImplementation:
2208 return Sema::OCK_Implementation;
2209 case Decl::ObjCCategoryImpl:
2210 return Sema::OCK_CategoryImplementation;
2211
2212 default:
2213 return Sema::OCK_None;
2214 }
2215}
2216
Steve Naroffa56f6162007-12-18 01:30:32 +00002217// Note: For class/category implemenations, allMethods/allProperties is
2218// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002219Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2220 Decl **allMethods, unsigned allNum,
2221 Decl **allProperties, unsigned pNum,
2222 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002223
Erik Verbruggend64251f2011-12-06 09:25:23 +00002224 if (getObjCContainerKind() == Sema::OCK_None)
2225 return 0;
2226
2227 assert(AtEnd.isValid() && "Invalid location for '@end'");
2228
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002229 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2230 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002231
Mike Stump1eb44332009-09-09 15:08:12 +00002232 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002233 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2234 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002235 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002236
Steve Naroff0701bbb2009-01-08 17:28:14 +00002237 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2238 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2239 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2240
Chris Lattner4d391482007-12-12 07:09:47 +00002241 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002242 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002243 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002244
2245 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002246 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002247 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002248 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002249 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002250 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002251 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002252 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002253 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002254 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002255 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002256 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002257 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002258 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002259 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002260 if (!Context.getSourceManager().isInSystemHeader(
2261 Method->getLocation()))
2262 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2263 << Method->getDeclName();
2264 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2265 }
Chris Lattner4d391482007-12-12 07:09:47 +00002266 InsMap[Method->getSelector()] = Method;
2267 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002268 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002269 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002270 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002271 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002272 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002273 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002274 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002275 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002276 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002277 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002278 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002279 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002280 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002281 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002282 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002283 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002284 if (!Context.getSourceManager().isInSystemHeader(
2285 Method->getLocation()))
2286 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2287 << Method->getDeclName();
2288 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2289 }
Chris Lattner4d391482007-12-12 07:09:47 +00002290 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002291 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002292 }
2293 }
2294 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002295 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002296 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002297 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002298 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002299 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002300 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002301 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002302 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002303 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002304
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002305 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002306 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002307 if (C->IsClassExtension()) {
2308 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2309 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002310 }
Chris Lattner4d391482007-12-12 07:09:47 +00002311 }
Steve Naroff09c47192009-01-09 15:36:25 +00002312 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002313 if (CDecl->getIdentifier())
2314 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2315 // user-defined setter/getter. It also synthesizes setter/getter methods
2316 // and adds them to the DeclContext and global method pools.
2317 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2318 E = CDecl->prop_end();
2319 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002320 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002321 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002322 }
2323 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002324 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002325 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002326 // Any property declared in a class extension might have user
2327 // declared setter or getter in current class extension or one
2328 // of the other class extensions. Mark them as synthesized as
2329 // property will be synthesized when property with same name is
2330 // seen in the @implementation.
2331 for (const ObjCCategoryDecl *ClsExtDecl =
2332 IDecl->getFirstClassExtension();
2333 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2334 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2335 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002336 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002337 // Skip over properties declared @dynamic
2338 if (const ObjCPropertyImplDecl *PIDecl
2339 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2340 if (PIDecl->getPropertyImplementation()
2341 == ObjCPropertyImplDecl::Dynamic)
2342 continue;
2343
2344 for (const ObjCCategoryDecl *CExtDecl =
2345 IDecl->getFirstClassExtension();
2346 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2347 if (ObjCMethodDecl *GetterMethod =
2348 CExtDecl->getInstanceMethod(Property->getGetterName()))
2349 GetterMethod->setSynthesized(true);
2350 if (!Property->isReadOnly())
2351 if (ObjCMethodDecl *SetterMethod =
2352 CExtDecl->getInstanceMethod(Property->getSetterName()))
2353 SetterMethod->setSynthesized(true);
2354 }
2355 }
2356 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002357 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002358 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002359 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002360
Patrick Beardb2f68202012-04-06 18:12:22 +00002361 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2362 if (IDecl->getSuperClass() == NULL) {
2363 // This class has no superclass, so check that it has been marked with
2364 // __attribute((objc_root_class)).
2365 if (!HasRootClassAttr) {
2366 SourceLocation DeclLoc(IDecl->getLocation());
2367 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2368 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2369 << IDecl->getIdentifier();
2370 // See if NSObject is in the current scope, and if it is, suggest
2371 // adding " : NSObject " to the class declaration.
2372 NamedDecl *IF = LookupSingleName(TUScope,
2373 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2374 DeclLoc, LookupOrdinaryName);
2375 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2376 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2377 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2378 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2379 } else {
2380 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2381 }
2382 }
2383 } else if (HasRootClassAttr) {
2384 // Complain that only root classes may have this attribute.
2385 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2386 }
2387
John McCall260611a2012-06-20 06:18:46 +00002388 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002389 while (IDecl->getSuperClass()) {
2390 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2391 IDecl = IDecl->getSuperClass();
2392 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002393 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002394 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002395 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002396 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002397 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002398 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Chris Lattner4d391482007-12-12 07:09:47 +00002400 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002401 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002402 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002403 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002404 Categories; Categories = Categories->getNextClassCategory()) {
2405 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002406 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002407 break;
2408 }
2409 }
2410 }
2411 }
Chris Lattner682bf922009-03-29 16:50:03 +00002412 if (isInterfaceDeclKind) {
2413 // Reject invalid vardecls.
2414 for (unsigned i = 0; i != tuvNum; i++) {
2415 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2416 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2417 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002418 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002419 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002420 }
Chris Lattner682bf922009-03-29 16:50:03 +00002421 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002422 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002423 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002424
2425 for (unsigned i = 0; i != tuvNum; i++) {
2426 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002427 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2428 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002429 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2430 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002431
2432 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002433}
2434
2435
2436/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2437/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002438static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002439CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002440 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002441}
2442
Ted Kremenek422bae72010-04-18 04:59:38 +00002443static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002444bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2445 const AttrVec &A) {
2446 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002447 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002448 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002449 return false;
2450
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002451 // method declared in interface has no attribute.
2452 // But implementation has attributes. This is invalid
2453 if (!IMD->hasAttrs())
2454 return true;
2455
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002456 const AttrVec &D = IMD->getAttrs();
2457 if (D.size() != A.size())
2458 return true;
2459
2460 // attributes on method declaration and definition must match exactly.
2461 // Note that we have at most a couple of attributes on methods, so this
2462 // n*n search is good enough.
2463 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2464 bool match = false;
2465 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2466 if ((*i)->getKind() == (*i1)->getKind()) {
2467 match = true;
2468 break;
2469 }
2470 }
2471 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002472 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002473 }
Sean Huntcf807c42010-08-18 23:23:40 +00002474 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002475}
2476
Douglas Gregor926df6c2011-06-11 01:09:30 +00002477/// \brief Check whether the declared result type of the given Objective-C
2478/// method declaration is compatible with the method's class.
2479///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002480static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002481CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2482 ObjCInterfaceDecl *CurrentClass) {
2483 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002484
2485 // If an Objective-C method inherits its related result type, then its
2486 // declared result type must be compatible with its own class type. The
2487 // declared result type is compatible if:
2488 if (const ObjCObjectPointerType *ResultObjectType
2489 = ResultType->getAs<ObjCObjectPointerType>()) {
2490 // - it is id or qualified id, or
2491 if (ResultObjectType->isObjCIdType() ||
2492 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002493 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002494
2495 if (CurrentClass) {
2496 if (ObjCInterfaceDecl *ResultClass
2497 = ResultObjectType->getInterfaceDecl()) {
2498 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002499 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002500 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002501
2502 // - it is a superclass of the method's class type
2503 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002504 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002505 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002506 } else {
2507 // Any Objective-C pointer type might be acceptable for a protocol
2508 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002509 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002510 }
2511 }
2512
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002513 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002514}
2515
John McCall6c2c2502011-07-22 02:45:48 +00002516namespace {
2517/// A helper class for searching for methods which a particular method
2518/// overrides.
2519class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002520public:
John McCall6c2c2502011-07-22 02:45:48 +00002521 Sema &S;
2522 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002523 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002524 bool Recursive;
2525
2526public:
2527 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2528 Selector selector = method->getSelector();
2529
2530 // Bypass this search if we've never seen an instance/class method
2531 // with this selector before.
2532 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2533 if (it == S.MethodPool.end()) {
2534 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002535 S.ReadMethodPool(selector);
2536
2537 it = S.MethodPool.find(selector);
2538 if (it == S.MethodPool.end())
2539 return;
John McCall6c2c2502011-07-22 02:45:48 +00002540 }
2541 ObjCMethodList &list =
2542 method->isInstanceMethod() ? it->second.first : it->second.second;
2543 if (!list.Method) return;
2544
2545 ObjCContainerDecl *container
2546 = cast<ObjCContainerDecl>(method->getDeclContext());
2547
2548 // Prevent the search from reaching this container again. This is
2549 // important with categories, which override methods from the
2550 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002551 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2552 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002553 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2554 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002555 } else {
2556 searchFromContainer(container);
2557 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002558 }
John McCall6c2c2502011-07-22 02:45:48 +00002559
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002560 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002561 iterator begin() const { return Overridden.begin(); }
2562 iterator end() const { return Overridden.end(); }
2563
2564private:
2565 void searchFromContainer(ObjCContainerDecl *container) {
2566 if (container->isInvalidDecl()) return;
2567
2568 switch (container->getDeclKind()) {
2569#define OBJCCONTAINER(type, base) \
2570 case Decl::type: \
2571 searchFrom(cast<type##Decl>(container)); \
2572 break;
2573#define ABSTRACT_DECL(expansion)
2574#define DECL(type, base) \
2575 case Decl::type:
2576#include "clang/AST/DeclNodes.inc"
2577 llvm_unreachable("not an ObjC container!");
2578 }
2579 }
2580
2581 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002582 if (!protocol->hasDefinition())
2583 return;
2584
John McCall6c2c2502011-07-22 02:45:48 +00002585 // A method in a protocol declaration overrides declarations from
2586 // referenced ("parent") protocols.
2587 search(protocol->getReferencedProtocols());
2588 }
2589
2590 void searchFrom(ObjCCategoryDecl *category) {
2591 // A method in a category declaration overrides declarations from
2592 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002593 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002594 search(category->getReferencedProtocols());
2595 }
2596
2597 void searchFrom(ObjCCategoryImplDecl *impl) {
2598 // A method in a category definition that has a category
2599 // declaration overrides declarations from the category
2600 // declaration.
2601 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2602 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002603 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2604 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002605
2606 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002607 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2608 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002609 }
2610 }
2611
2612 void searchFrom(ObjCInterfaceDecl *iface) {
2613 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002614 if (!iface->hasDefinition())
2615 return;
2616
John McCall6c2c2502011-07-22 02:45:48 +00002617 // - categories,
2618 for (ObjCCategoryDecl *category = iface->getCategoryList();
2619 category; category = category->getNextClassCategory())
2620 search(category);
2621
2622 // - the super class, and
2623 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2624 search(super);
2625
2626 // - any referenced protocols.
2627 search(iface->getReferencedProtocols());
2628 }
2629
2630 void searchFrom(ObjCImplementationDecl *impl) {
2631 // A method in a class implementation overrides declarations from
2632 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002633 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2634 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002635 }
2636
2637
2638 void search(const ObjCProtocolList &protocols) {
2639 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2640 i != e; ++i)
2641 search(*i);
2642 }
2643
2644 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002645 // Check for a method in this container which matches this selector.
2646 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2647 Method->isInstanceMethod());
2648
2649 // If we find one, record it and bail out.
2650 if (meth) {
2651 Overridden.insert(meth);
2652 return;
2653 }
2654
2655 // Otherwise, search for methods that a hypothetical method here
2656 // would have overridden.
2657
2658 // Note that we're now in a recursive case.
2659 Recursive = true;
2660
2661 searchFromContainer(container);
2662 }
2663};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002664}
2665
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002666void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2667 ObjCInterfaceDecl *CurrentClass,
2668 ResultTypeCompatibilityKind RTC) {
2669 // Search for overridden methods and merge information down from them.
2670 OverrideSearch overrides(*this, ObjCMethod);
2671 // Keep track if the method overrides any method in the class's base classes,
2672 // its protocols, or its categories' protocols; we will keep that info
2673 // in the ObjCMethodDecl.
2674 // For this info, a method in an implementation is not considered as
2675 // overriding the same method in the interface or its categories.
2676 bool hasOverriddenMethodsInBaseOrProtocol = false;
2677 for (OverrideSearch::iterator
2678 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2679 ObjCMethodDecl *overridden = *i;
2680
2681 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2682 CurrentClass != overridden->getClassInterface() ||
2683 overridden->isOverriding())
2684 hasOverriddenMethodsInBaseOrProtocol = true;
2685
2686 // Propagate down the 'related result type' bit from overridden methods.
2687 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2688 ObjCMethod->SetRelatedResultType();
2689
2690 // Then merge the declarations.
2691 mergeObjCMethodDecls(ObjCMethod, overridden);
2692
2693 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2694 continue; // Conflicting properties are detected elsewhere.
2695
2696 // Check for overriding methods
2697 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2698 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2699 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2700 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2701
2702 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
2703 isa<ObjCInterfaceDecl>(overridden->getDeclContext())) {
2704 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2705 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002706 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2707 PrevE = overridden->param_end();
2708 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002709 assert(PrevI != overridden->param_end() && "Param mismatch");
2710 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2711 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2712 // If type of argument of method in this class does not match its
2713 // respective argument type in the super class method, issue warning;
2714 if (!Context.typesAreCompatible(T1, T2)) {
2715 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2716 << T1 << T2;
2717 Diag(overridden->getLocation(), diag::note_previous_declaration);
2718 break;
2719 }
2720 }
2721 }
2722 }
2723
2724 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2725}
2726
John McCalld226f652010-08-21 09:40:31 +00002727Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002728 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002729 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002730 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002731 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002732 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002733 Selector Sel,
2734 // optional arguments. The number of types/arguments is obtained
2735 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002736 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002737 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002738 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002739 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002740 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002741 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002742 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002743 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002744 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002745 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2746 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002747 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002748
Douglas Gregore97179c2011-09-08 01:46:34 +00002749 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002750 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002751 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002752 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002753
Steve Naroffccef3712009-02-20 22:59:16 +00002754 // Methods cannot return interface types. All ObjC objects are
2755 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002756 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002757 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2758 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002759 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002760 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002761
2762 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002763 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002764 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002765 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002766 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002767 }
Mike Stump1eb44332009-09-09 15:08:12 +00002768
2769 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002770 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002771 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002772 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002773 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002774 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002775 /*isSynthesized=*/false,
2776 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002777 MethodDeclKind == tok::objc_optional
2778 ? ObjCMethodDecl::Optional
2779 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002780 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002781
Chris Lattner5f9e2722011-07-23 10:55:15 +00002782 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002783
Chris Lattner7db638d2009-04-11 19:42:43 +00002784 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002785 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002786 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002787
Chris Lattnere294d3f2009-04-11 18:57:04 +00002788 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002789 ArgType = Context.getObjCIdType();
2790 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002791 } else {
John McCall58e46772009-10-23 21:48:59 +00002792 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002793 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002794 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002795 }
Mike Stump1eb44332009-09-09 15:08:12 +00002796
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002797 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2798 LookupOrdinaryName, ForRedeclaration);
2799 LookupName(R, S);
2800 if (R.isSingleResult()) {
2801 NamedDecl *PrevDecl = R.getFoundDecl();
2802 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002803 Diag(ArgInfo[i].NameLoc,
2804 (MethodDefinition ? diag::warn_method_param_redefinition
2805 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002806 << ArgInfo[i].Name;
2807 Diag(PrevDecl->getLocation(),
2808 diag::note_previous_declaration);
2809 }
2810 }
2811
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002812 SourceLocation StartLoc = DI
2813 ? DI->getTypeLoc().getBeginLoc()
2814 : ArgInfo[i].NameLoc;
2815
John McCall81ef3e62011-04-23 02:46:06 +00002816 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2817 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2818 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002819
John McCall70798862011-05-02 00:30:12 +00002820 Param->setObjCMethodScopeInfo(i);
2821
Chris Lattner0ed844b2008-04-04 06:12:32 +00002822 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002823 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002825 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002826 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002828 if (Param->hasAttr<BlocksAttr>()) {
2829 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2830 Param->setInvalidDecl();
2831 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002832 S->AddDecl(Param);
2833 IdResolver.AddDecl(Param);
2834
Chris Lattner0ed844b2008-04-04 06:12:32 +00002835 Params.push_back(Param);
2836 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002837
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002838 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002839 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002840 QualType ArgType = Param->getType();
2841 if (ArgType.isNull())
2842 ArgType = Context.getObjCIdType();
2843 else
2844 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002845 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002846 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002847 Diag(Param->getLocation(),
2848 diag::err_object_cannot_be_passed_returned_by_value)
2849 << 1 << ArgType;
2850 Param->setInvalidDecl();
2851 }
2852 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002853
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002854 Params.push_back(Param);
2855 }
2856
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002857 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002858 ObjCMethod->setObjCDeclQualifier(
2859 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002860
2861 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002862 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002864 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002865 const ObjCMethodDecl *PrevMethod = 0;
2866 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002867 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002868 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2869 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002870 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002871 PrevMethod = ImpDecl->getClassMethod(Sel);
2872 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002873 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002874
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002875 ObjCMethodDecl *IMD = 0;
2876 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2877 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2878 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002879 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002880 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002881 SourceLocation MethodLoc = IMD->getLocation();
2882 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2883 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002884 Diag(MethodLoc, diag::note_method_declared_at)
2885 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002886 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002887 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002888 } else {
2889 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002890 }
John McCall6c2c2502011-07-22 02:45:48 +00002891
Chris Lattner4d391482007-12-12 07:09:47 +00002892 if (PrevMethod) {
2893 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002894 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002895 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002896 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002897 }
John McCall54abf7d2009-11-04 02:18:39 +00002898
Douglas Gregor926df6c2011-06-11 01:09:30 +00002899 // If this Objective-C method does not have a related result type, but we
2900 // are allowed to infer related result types, try to do so based on the
2901 // method family.
2902 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2903 if (!CurrentClass) {
2904 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2905 CurrentClass = Cat->getClassInterface();
2906 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2907 CurrentClass = Impl->getClassInterface();
2908 else if (ObjCCategoryImplDecl *CatImpl
2909 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2910 CurrentClass = CatImpl->getClassInterface();
2911 }
John McCall6c2c2502011-07-22 02:45:48 +00002912
Douglas Gregore97179c2011-09-08 01:46:34 +00002913 ResultTypeCompatibilityKind RTC
2914 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002915
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002916 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002917
John McCallf85e1932011-06-15 23:02:42 +00002918 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002919 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002920 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2921
Douglas Gregore97179c2011-09-08 01:46:34 +00002922 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002923 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002924 !ObjCMethod->hasRelatedResultType() &&
2925 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002926 bool InferRelatedResultType = false;
2927 switch (ObjCMethod->getMethodFamily()) {
2928 case OMF_None:
2929 case OMF_copy:
2930 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002931 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002932 case OMF_mutableCopy:
2933 case OMF_release:
2934 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002935 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002936 break;
2937
2938 case OMF_alloc:
2939 case OMF_new:
2940 InferRelatedResultType = ObjCMethod->isClassMethod();
2941 break;
2942
2943 case OMF_init:
2944 case OMF_autorelease:
2945 case OMF_retain:
2946 case OMF_self:
2947 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2948 break;
2949 }
2950
John McCall6c2c2502011-07-22 02:45:48 +00002951 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002952 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002953 }
2954
John McCalld226f652010-08-21 09:40:31 +00002955 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002956}
2957
Chris Lattnercc98eac2008-12-17 07:13:27 +00002958bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002959 // Following is also an error. But it is caused by a missing @end
2960 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002961 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002962 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002963
2964 // If we switched context to translation unit while we are still lexically in
2965 // an objc container, it means the parser missed emitting an error.
2966 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
2967 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002968
Anders Carlsson15281452008-11-04 16:57:32 +00002969 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2970 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002971
Anders Carlsson15281452008-11-04 16:57:32 +00002972 return true;
2973}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002974
James Dennett1dfbd922012-06-14 21:40:34 +00002975/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00002976/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002977void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002978 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002979 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002980 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002981 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002982 if (!Class) {
2983 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2984 return;
2985 }
John McCall260611a2012-06-20 06:18:46 +00002986 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002987 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2988 return;
2989 }
Mike Stump1eb44332009-09-09 15:08:12 +00002990
Chris Lattnercc98eac2008-12-17 07:13:27 +00002991 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002992 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002993 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002994 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002995 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002996 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002997 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002998 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2999 /*FIXME: StartL=*/ID->getLocation(),
3000 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003001 ID->getIdentifier(), ID->getType(),
3002 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003003 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003004 }
Mike Stump1eb44332009-09-09 15:08:12 +00003005
Chris Lattnercc98eac2008-12-17 07:13:27 +00003006 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003007 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003008 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003009 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003010 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003011 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003012 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003013 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003014 }
3015}
3016
Douglas Gregor160b5632010-04-26 17:32:49 +00003017/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003018VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3019 SourceLocation StartLoc,
3020 SourceLocation IdLoc,
3021 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003022 bool Invalid) {
3023 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3024 // duration shall not be qualified by an address-space qualifier."
3025 // Since all parameters have automatic store duration, they can not have
3026 // an address space.
3027 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003028 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003029 Invalid = true;
3030 }
3031
3032 // An @catch parameter must be an unqualified object pointer type;
3033 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3034 if (Invalid) {
3035 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003036 } else if (T->isDependentType()) {
3037 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003038 } else if (!T->isObjCObjectPointerType()) {
3039 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003040 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003041 } else if (T->isObjCQualifiedIdType()) {
3042 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003043 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003044 }
3045
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003046 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3047 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003048 New->setExceptionVariable(true);
3049
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003050 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003051 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003052 Invalid = true;
3053
Douglas Gregor160b5632010-04-26 17:32:49 +00003054 if (Invalid)
3055 New->setInvalidDecl();
3056 return New;
3057}
3058
John McCalld226f652010-08-21 09:40:31 +00003059Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003060 const DeclSpec &DS = D.getDeclSpec();
3061
3062 // We allow the "register" storage class on exception variables because
3063 // GCC did, but we drop it completely. Any other storage class is an error.
3064 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3065 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3066 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3067 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3068 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3069 << DS.getStorageClassSpec();
3070 }
3071 if (D.getDeclSpec().isThreadSpecified())
3072 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3073 D.getMutableDeclSpec().ClearStorageClassSpecs();
3074
3075 DiagnoseFunctionSpecifiers(D);
3076
3077 // Check that there are no default arguments inside the type of this
3078 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003079 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003080 CheckExtraCXXDefaultArguments(D);
3081
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003082 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003083 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003084
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003085 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3086 D.getSourceRange().getBegin(),
3087 D.getIdentifierLoc(),
3088 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003089 D.isInvalidType());
3090
3091 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3092 if (D.getCXXScopeSpec().isSet()) {
3093 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3094 << D.getCXXScopeSpec().getRange();
3095 New->setInvalidDecl();
3096 }
3097
3098 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003099 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003100 if (D.getIdentifier())
3101 IdResolver.AddDecl(New);
3102
3103 ProcessDeclAttributes(S, New, D);
3104
3105 if (New->hasAttr<BlocksAttr>())
3106 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003107 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003108}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003109
3110/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003111/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003112void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003113 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003114 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3115 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003116 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003117 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003118 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003119 }
3120}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003121
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003122void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003123 // Load referenced selectors from the external source.
3124 if (ExternalSource) {
3125 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3126 ExternalSource->ReadReferencedSelectors(Sels);
3127 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3128 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3129 }
3130
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003131 // Warning will be issued only when selector table is
3132 // generated (which means there is at lease one implementation
3133 // in the TU). This is to match gcc's behavior.
3134 if (ReferencedSelectors.empty() ||
3135 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003136 return;
3137 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3138 ReferencedSelectors.begin(),
3139 E = ReferencedSelectors.end(); S != E; ++S) {
3140 Selector Sel = (*S).first;
3141 if (!LookupImplementedMethodInGlobalPool(Sel))
3142 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3143 }
3144 return;
3145}