blob: eccf2e1d2ae0786db2dbe64d0db240bccaaf43ee [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"
John McCallf85e1932011-06-15 23:02:42 +000015#include "clang/AST/ASTConsumer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTMutationListener.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070018#include "clang/AST/DataRecursiveASTVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/AST/DeclObjC.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"
John McCallf85e1932011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/ExternalSemaSource.h"
25#include "clang/Sema/Lookup.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.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
Stephen Hines651f13c2014-04-23 16:59:28 -070051 const ObjCObjectType *result =
52 method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType();
John McCallf85e1932011-06-15 23:02:42 +000053
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.
Stephen Hines6bcf27b2014-05-29 04:14:42 -070073 const ObjCInterfaceDecl *receiverClass = nullptr;
John McCallf85e1932011-06-15 23:02:42 +000074 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)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700100 method->addAttr(UnavailableAttr::CreateImplicit(Context,
101 "init method returns a type unrelated to its receiver type",
102 loc));
John McCallf85e1932011-06-15 23:02:42 +0000103 return true;
104 }
105
106 // Otherwise, it's an error.
107 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
108 method->setInvalidDecl();
109 return true;
110}
111
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000112void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregorf4d918f2013-01-15 22:43:08 +0000113 const ObjCMethodDecl *Overridden) {
Douglas Gregor926df6c2011-06-11 01:09:30 +0000114 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.
Stephen Hines651f13c2014-04-23 16:59:28 -0700120 QualType ResultType = NewMethod->getReturnType();
Stephen Hines176edba2014-12-01 14:53:08 -0800121 SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000122
123 // Figure out which class this method is part of, if any.
124 ObjCInterfaceDecl *CurrentClass
125 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
126 if (!CurrentClass) {
127 DeclContext *DC = NewMethod->getDeclContext();
128 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
129 CurrentClass = Cat->getClassInterface();
130 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
131 CurrentClass = Impl->getClassInterface();
132 else if (ObjCCategoryImplDecl *CatImpl
133 = dyn_cast<ObjCCategoryImplDecl>(DC))
134 CurrentClass = CatImpl->getClassInterface();
135 }
136
137 if (CurrentClass) {
138 Diag(NewMethod->getLocation(),
139 diag::warn_related_result_type_compatibility_class)
140 << Context.getObjCInterfaceType(CurrentClass)
141 << ResultType
142 << ResultTypeRange;
143 } else {
144 Diag(NewMethod->getLocation(),
145 diag::warn_related_result_type_compatibility_protocol)
146 << ResultType
147 << ResultTypeRange;
148 }
149
Douglas Gregore97179c2011-09-08 01:46:34 +0000150 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
151 Diag(Overridden->getLocation(),
John McCall7cca8212013-03-19 07:04:25 +0000152 diag::note_related_result_type_family)
153 << /*overridden method*/ 0
Douglas Gregore97179c2011-09-08 01:46:34 +0000154 << Family;
155 else
156 Diag(Overridden->getLocation(),
157 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000158 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000159 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000160 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
161 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
162 Diag(NewMethod->getLocation(),
163 diag::err_nsreturns_retained_attribute_mismatch) << 1;
164 Diag(Overridden->getLocation(), diag::note_previous_decl)
165 << "method";
166 }
167 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
168 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
169 Diag(NewMethod->getLocation(),
170 diag::err_nsreturns_retained_attribute_mismatch) << 0;
171 Diag(Overridden->getLocation(), diag::note_previous_decl)
172 << "method";
173 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000174 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
175 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000176 for (ObjCMethodDecl::param_iterator
177 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000178 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000179 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000180 ParmVarDecl *newDecl = (*ni);
181 if (newDecl->hasAttr<NSConsumedAttr>() !=
182 oldDecl->hasAttr<NSConsumedAttr>()) {
183 Diag(newDecl->getLocation(),
184 diag::err_nsconsumed_attribute_mismatch);
185 Diag(oldDecl->getLocation(), diag::note_previous_decl)
186 << "parameter";
187 }
188 }
189 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000190}
191
John McCallf85e1932011-06-15 23:02:42 +0000192/// \brief Check a method declaration for compatibility with the Objective-C
193/// ARC conventions.
John McCallb8463812013-04-04 01:38:37 +0000194bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
John McCallf85e1932011-06-15 23:02:42 +0000195 ObjCMethodFamily family = method->getMethodFamily();
196 switch (family) {
197 case OMF_None:
Nico Weber80cb6e62011-08-28 22:35:17 +0000198 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000199 case OMF_retain:
200 case OMF_release:
201 case OMF_autorelease:
202 case OMF_retainCount:
203 case OMF_self:
Stephen Hines176edba2014-12-01 14:53:08 -0800204 case OMF_initialize:
John McCall6c2c2502011-07-22 02:45:48 +0000205 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000206 return false;
207
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000208 case OMF_dealloc:
Stephen Hines651f13c2014-04-23 16:59:28 -0700209 if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800210 SourceRange ResultTypeRange = method->getReturnTypeSourceRange();
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000211 if (ResultTypeRange.isInvalid())
Stephen Hines651f13c2014-04-23 16:59:28 -0700212 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
213 << method->getReturnType()
214 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000215 else
Stephen Hines651f13c2014-04-23 16:59:28 -0700216 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
217 << method->getReturnType()
218 << FixItHint::CreateReplacement(ResultTypeRange, "void");
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000219 return true;
220 }
221 return false;
222
John McCallf85e1932011-06-15 23:02:42 +0000223 case OMF_init:
224 // If the method doesn't obey the init rules, don't bother annotating it.
John McCallb8463812013-04-04 01:38:37 +0000225 if (checkInitMethod(method, QualType()))
John McCallf85e1932011-06-15 23:02:42 +0000226 return true;
227
Stephen Hines651f13c2014-04-23 16:59:28 -0700228 method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context));
John McCallf85e1932011-06-15 23:02:42 +0000229
230 // Don't add a second copy of this attribute, but otherwise don't
231 // let it be suppressed.
232 if (method->hasAttr<NSReturnsRetainedAttr>())
233 return false;
234 break;
235
236 case OMF_alloc:
237 case OMF_copy:
238 case OMF_mutableCopy:
239 case OMF_new:
240 if (method->hasAttr<NSReturnsRetainedAttr>() ||
241 method->hasAttr<NSReturnsNotRetainedAttr>() ||
242 method->hasAttr<NSReturnsAutoreleasedAttr>())
243 return false;
244 break;
245 }
246
Stephen Hines651f13c2014-04-23 16:59:28 -0700247 method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context));
John McCallf85e1932011-06-15 23:02:42 +0000248 return false;
249}
250
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000251static void DiagnoseObjCImplementedDeprecations(Sema &S,
252 NamedDecl *ND,
253 SourceLocation ImplLoc,
254 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000255 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000256 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000257 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000258 S.Diag(ND->getLocation(), diag::note_method_declared_at)
259 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000260 else
261 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
262 }
263}
264
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000265/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
266/// pool.
267void Sema::AddAnyMethodToGlobalPool(Decl *D) {
268 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
269
270 // If we don't have a valid method decl, simply return.
271 if (!MDecl)
272 return;
273 if (MDecl->isInstanceMethod())
274 AddInstanceMethodToGlobalPool(MDecl, true);
275 else
276 AddFactoryMethodToGlobalPool(MDecl, true);
277}
278
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000279/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
280/// has explicit ownership attribute; false otherwise.
281static bool
282HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
283 QualType T = Param->getType();
284
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000285 if (const PointerType *PT = T->getAs<PointerType>()) {
286 T = PT->getPointeeType();
287 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
288 T = RT->getPointeeType();
289 } else {
290 return true;
291 }
292
293 // If we have a lifetime qualifier, but it's local, we must have
294 // inferred it. So, it is implicit.
295 return !T.getLocalQualifiers().hasObjCLifetime();
296}
297
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000298/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
299/// and user declared, in the method definition's AST.
300void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700301 assert((getCurMethodDecl() == nullptr) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000302 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000303
Steve Naroff394f3f42008-07-25 17:57:26 +0000304 // If we don't have a valid method decl, simply return.
305 if (!MDecl)
306 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000307
Chris Lattner4d391482007-12-12 07:09:47 +0000308 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000309 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000310 PushFunctionScope();
311
Chris Lattner4d391482007-12-12 07:09:47 +0000312 // Create Decl objects for each parameter, entrring them in the scope for
313 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000314
315 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000316 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Daniel Dunbar451318c2008-08-26 06:07:48 +0000318 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
319 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000320
Reid Kleckner8c0501c2013-06-24 14:38:26 +0000321 // The ObjC parser requires parameter names so there's no need to check.
322 CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(),
323 /*CheckParameterNames=*/false);
324
Chris Lattner8123a952008-04-10 02:22:51 +0000325 // Introduce all of the other parameters into this scope.
Stephen Hines651f13c2014-04-23 16:59:28 -0700326 for (auto *Param : MDecl->params()) {
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000327 if (!Param->isInvalidDecl() &&
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000328 getLangOpts().ObjCAutoRefCount &&
329 !HasExplicitOwnershipAttr(*this, Param))
330 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
331 Param->getType();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000332
Stephen Hines651f13c2014-04-23 16:59:28 -0700333 if (Param->getIdentifier())
334 PushOnScopeChains(Param, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000335 }
John McCallf85e1932011-06-15 23:02:42 +0000336
337 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000338 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000339 switch (MDecl->getMethodFamily()) {
340 case OMF_retain:
341 case OMF_retainCount:
342 case OMF_release:
343 case OMF_autorelease:
344 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +0000345 << 0 << MDecl->getSelector();
John McCallf85e1932011-06-15 23:02:42 +0000346 break;
347
348 case OMF_None:
349 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000350 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000351 case OMF_alloc:
352 case OMF_init:
353 case OMF_mutableCopy:
354 case OMF_copy:
355 case OMF_new:
356 case OMF_self:
Stephen Hines176edba2014-12-01 14:53:08 -0800357 case OMF_initialize:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000358 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000359 break;
360 }
361 }
362
Nico Weber9a1ecf02011-08-22 17:25:57 +0000363 // Warn on deprecated methods under -Wdeprecated-implementations,
364 // and prepare for warning on missing super calls.
365 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000366 ObjCMethodDecl *IMD =
367 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
368
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000369 if (IMD) {
370 ObjCImplDecl *ImplDeclOfMethodDef =
371 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
372 ObjCContainerDecl *ContDeclOfMethodDecl =
373 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700374 ObjCImplDecl *ImplDeclOfMethodDecl = nullptr;
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000375 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
376 ImplDeclOfMethodDecl = OID->getImplementation();
Stephen Hines651f13c2014-04-23 16:59:28 -0700377 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) {
378 if (CD->IsClassExtension()) {
379 if (ObjCInterfaceDecl *OID = CD->getClassInterface())
380 ImplDeclOfMethodDecl = OID->getImplementation();
381 } else
382 ImplDeclOfMethodDecl = CD->getImplementation();
383 }
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000384 // No need to issue deprecated warning if deprecated mehod in class/category
385 // is being implemented in its own implementation (no overriding is involved).
386 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
387 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000388 dyn_cast<NamedDecl>(IMD),
389 MDecl->getLocation(), 0);
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000390 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000391
Stephen Hines651f13c2014-04-23 16:59:28 -0700392 if (MDecl->getMethodFamily() == OMF_init) {
393 if (MDecl->isDesignatedInitializerForTheInterface()) {
394 getCurFunction()->ObjCIsDesignatedInit = true;
395 getCurFunction()->ObjCWarnForNoDesignatedInitChain =
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700396 IC->getSuperClass() != nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700397 } else if (IC->hasDesignatedInitializers()) {
398 getCurFunction()->ObjCIsSecondaryInit = true;
399 getCurFunction()->ObjCWarnForNoInitDelegation = true;
400 }
401 }
402
Nico Weber80cb6e62011-08-28 22:35:17 +0000403 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000404 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
405 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
406 // Only do this if the current class actually has a superclass.
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000407 if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
Jordan Rose535a5d02012-10-19 16:05:26 +0000408 ObjCMethodFamily Family = MDecl->getMethodFamily();
409 if (Family == OMF_dealloc) {
410 if (!(getLangOpts().ObjCAutoRefCount ||
411 getLangOpts().getGC() == LangOptions::GCOnly))
412 getCurFunction()->ObjCShouldCallSuper = true;
413
414 } else if (Family == OMF_finalize) {
415 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
416 getCurFunction()->ObjCShouldCallSuper = true;
417
Fariborz Jahanian2b610392013-11-05 00:28:21 +0000418 } else {
Jordan Rose535a5d02012-10-19 16:05:26 +0000419 const ObjCMethodDecl *SuperMethod =
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000420 SuperClass->lookupMethod(MDecl->getSelector(),
421 MDecl->isInstanceMethod());
Jordan Rose535a5d02012-10-19 16:05:26 +0000422 getCurFunction()->ObjCShouldCallSuper =
423 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000424 }
Nico Weber80cb6e62011-08-28 22:35:17 +0000425 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000426 }
Chris Lattner4d391482007-12-12 07:09:47 +0000427}
428
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000429namespace {
430
431// Callback to only accept typo corrections that are Objective-C classes.
432// If an ObjCInterfaceDecl* is given to the constructor, then the validation
433// function will reject corrections to that class.
434class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
435 public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700436 ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {}
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000437 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
438 : CurrentIDecl(IDecl) {}
439
Stephen Hines651f13c2014-04-23 16:59:28 -0700440 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000441 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
442 return ID && !declaresSameEntity(ID, CurrentIDecl);
443 }
444
445 private:
446 ObjCInterfaceDecl *CurrentIDecl;
447};
448
449}
450
John McCalld226f652010-08-21 09:40:31 +0000451Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000452ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
453 IdentifierInfo *ClassName, SourceLocation ClassLoc,
454 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000455 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000456 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000457 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000458 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattner4d391482007-12-12 07:09:47 +0000460 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000461 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000462 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000463
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000464 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000465 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000466 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Douglas Gregor7723fec2011-12-15 20:29:51 +0000469 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000470 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +0000471
472 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
473 // A previous decl with a different name is because of
474 // @compatibility_alias, for example:
475 // \code
476 // @class NewImage;
477 // @compatibility_alias OldImage NewImage;
478 // \endcode
479 // A lookup for 'OldImage' will return the 'NewImage' decl.
480 //
481 // In such a case use the real declaration name, instead of the alias one,
482 // otherwise we will break IdentifierResolver and redecls-chain invariants.
483 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
484 // has been aliased.
485 ClassName = PrevIDecl->getIdentifier();
486 }
487
Douglas Gregor7723fec2011-12-15 20:29:51 +0000488 ObjCInterfaceDecl *IDecl
489 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000490 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000491
Douglas Gregor7723fec2011-12-15 20:29:51 +0000492 if (PrevIDecl) {
493 // Class already seen. Was it a definition?
494 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
495 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
496 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000497 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000498 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000499 }
Chris Lattner4d391482007-12-12 07:09:47 +0000500 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000501
502 if (AttrList)
503 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
504 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Douglas Gregor7723fec2011-12-15 20:29:51 +0000506 // Start the definition of this class. If we're in a redefinition case, there
507 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000508 if (!IDecl->hasDefinition())
509 IDecl->startDefinition();
510
Chris Lattner4d391482007-12-12 07:09:47 +0000511 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000512 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000513 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
514 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000515
516 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000517 // Try to correct for a typo in the superclass name without correcting
518 // to the class we're defining.
Stephen Hines176edba2014-12-01 14:53:08 -0800519 if (TypoCorrection Corrected =
520 CorrectTypo(DeclarationNameInfo(SuperName, SuperLoc),
521 LookupOrdinaryName, TUScope, nullptr,
522 llvm::make_unique<ObjCInterfaceValidatorCCC>(IDecl),
523 CTK_ErrorRecovery)) {
Richard Smith2d670972013-08-17 00:46:16 +0000524 diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
525 << SuperName << ClassName);
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000526 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000527 }
528 }
529
Douglas Gregor60ef3082011-12-15 00:29:59 +0000530 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000531 Diag(SuperLoc, diag::err_recursive_superclass)
532 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000533 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000534 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000535 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000536 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000537
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000538 // Diagnose classes that inherit from deprecated classes.
539 if (SuperClassDecl)
540 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700542 if (PrevDecl && !SuperClassDecl) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000543 // The previous declaration was not a class decl. Check if we have a
544 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000545 if (const TypedefNameDecl *TDecl =
546 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000547 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000548 if (T->isObjCObjectType()) {
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000549 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000550 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000551 // This handles the following case:
552 // @interface NewI @end
553 // typedef NewI DeprI __attribute__((deprecated("blah")))
554 // @interface SI : DeprI /* warn here */ @end
555 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
556 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000557 }
558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000560 // This handles the following case:
561 //
562 // typedef int SuperClass;
563 // @interface MyClass : SuperClass {} @end
564 //
565 if (!SuperClassDecl) {
566 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
567 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000568 }
569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Richard Smith162e1c12011-04-15 14:24:37 +0000571 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000572 if (!SuperClassDecl)
573 Diag(SuperLoc, diag::err_undef_superclass)
574 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000575 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000576 Context.getObjCInterfaceType(SuperClassDecl),
577 diag::err_forward_superclass,
578 SuperClassDecl->getDeclName(),
579 ClassName,
580 SourceRange(AtInterfaceLoc, ClassLoc))) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700581 SuperClassDecl = nullptr;
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000582 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000583 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000584 IDecl->setSuperClass(SuperClassDecl);
585 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000586 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000587 }
Chris Lattner4d391482007-12-12 07:09:47 +0000588 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000589 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000590 }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Sebastian Redl0b17c612010-08-13 00:28:03 +0000592 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000593 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000594 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000595 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000596 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anders Carlsson15281452008-11-04 16:57:32 +0000599 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000600 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000601}
602
Fariborz Jahaniana924f842013-09-25 19:36:32 +0000603/// ActOnTypedefedProtocols - this action finds protocol list as part of the
604/// typedef'ed use for a qualified super class and adds them to the list
605/// of the protocols.
606void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs,
607 IdentifierInfo *SuperName,
608 SourceLocation SuperLoc) {
609 if (!SuperName)
610 return;
611 NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
612 LookupOrdinaryName);
613 if (!IDecl)
614 return;
615
616 if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) {
617 QualType T = TDecl->getUnderlyingType();
618 if (T->isObjCObjectType())
619 if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>())
Stephen Hines651f13c2014-04-23 16:59:28 -0700620 for (auto *I : OPT->quals())
621 ProtocolRefs.push_back(I);
Fariborz Jahaniana924f842013-09-25 19:36:32 +0000622 }
623}
624
Richard Smithde01b7a2012-08-08 23:32:13 +0000625/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000626/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000627Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
628 IdentifierInfo *AliasName,
629 SourceLocation AliasLocation,
630 IdentifierInfo *ClassName,
631 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000632 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000633 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000634 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000635 if (ADecl) {
Eli Friedman104f96b2013-06-21 01:49:53 +0000636 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000637 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700638 return nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +0000639 }
640 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000641 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000642 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000643 if (const TypedefNameDecl *TDecl =
644 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000645 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000646 if (T->isObjCObjectType()) {
647 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000648 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000649 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000650 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000651 }
652 }
653 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000654 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700655 if (!CDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000656 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000657 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000658 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700659 return nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +0000660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000662 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000663 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000664 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Anders Carlsson15281452008-11-04 16:57:32 +0000666 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000667 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000668
John McCalld226f652010-08-21 09:40:31 +0000669 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000670}
671
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000672bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000673 IdentifierInfo *PName,
674 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000675 const ObjCList<ObjCProtocolDecl> &PList) {
676
677 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000678 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
679 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000680 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
681 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000682 if (PDecl->getIdentifier() == PName) {
683 Diag(Ploc, diag::err_protocol_has_circular_dependency);
684 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000685 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000686 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000687
688 if (!PDecl->hasDefinition())
689 continue;
690
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000691 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
692 PDecl->getLocation(), PDecl->getReferencedProtocols()))
693 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000694 }
695 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000696 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000697}
698
John McCalld226f652010-08-21 09:40:31 +0000699Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000700Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
701 IdentifierInfo *ProtocolName,
702 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000703 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000704 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000705 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000706 SourceLocation EndProtoLoc,
707 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000708 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000709 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000710 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000711 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
712 ForRedeclaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700713 ObjCProtocolDecl *PDecl = nullptr;
714 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) {
Douglas Gregor27c6da22012-01-01 20:30:41 +0000715 // If we already have a definition, complain.
716 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
717 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregor27c6da22012-01-01 20:30:41 +0000719 // Create a new protocol that is completely distinct from previous
720 // declarations, and do not make this protocol available for name lookup.
721 // That way, we'll end up completely ignoring the duplicate.
722 // FIXME: Can we turn this into an error?
723 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
724 ProtocolLoc, AtProtoInterfaceLoc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700725 /*PrevDecl=*/nullptr);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000726 PDecl->startDefinition();
727 } else {
728 if (PrevDecl) {
729 // Check for circular dependencies among protocol declarations. This can
730 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000731 ObjCList<ObjCProtocolDecl> PList;
732 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
733 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000734 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000735 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000736
737 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000738 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000739 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000740 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000741
Douglas Gregor6e378de2009-04-23 23:18:26 +0000742 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000743 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000744 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000745
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000746 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000747 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000748
749 // Merge attributes from previous declarations.
750 if (PrevDecl)
751 mergeDeclAttributes(PDecl, PrevDecl);
752
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000753 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000754 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000755 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000756 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
759 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000760 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000761}
762
Stephen Hines651f13c2014-04-23 16:59:28 -0700763static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl,
764 ObjCProtocolDecl *&UndefinedProtocol) {
765 if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) {
766 UndefinedProtocol = PDecl;
767 return true;
768 }
769
770 for (auto *PI : PDecl->protocols())
771 if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) {
772 UndefinedProtocol = PI;
773 return true;
774 }
775 return false;
776}
777
Chris Lattner4d391482007-12-12 07:09:47 +0000778/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000779/// issues an error if they are not declared. It returns list of
780/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000781void
Chris Lattnere13b9592008-07-26 04:03:38 +0000782Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000783 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000784 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000785 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000786 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000787 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
788 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000789 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000790 TypoCorrection Corrected = CorrectTypo(
791 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Stephen Hines176edba2014-12-01 14:53:08 -0800792 LookupObjCProtocolName, TUScope, nullptr,
793 llvm::make_unique<DeclFilterCCC<ObjCProtocolDecl>>(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700794 CTK_ErrorRecovery);
Richard Smith2d670972013-08-17 00:46:16 +0000795 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
796 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
797 << ProtocolId[i].first);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000798 }
799
800 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000801 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000802 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000803 continue;
804 }
Fariborz Jahanian3c9a0242013-04-09 17:52:29 +0000805 // If this is a forward protocol declaration, get its definition.
806 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
807 PDecl = PDecl->getDefinition();
808
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000809 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000810
811 // If this is a forward declaration and we are supposed to warn in this
812 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000813 // FIXME: Recover nicely in the hidden case.
Stephen Hines651f13c2014-04-23 16:59:28 -0700814 ObjCProtocolDecl *UndefinedProtocol;
815
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000816 if (WarnOnDeclarations &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700817 NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000818 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000819 << ProtocolId[i].first;
Stephen Hines651f13c2014-04-23 16:59:28 -0700820 Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined)
821 << UndefinedProtocol;
822 }
John McCalld226f652010-08-21 09:40:31 +0000823 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000824 }
825}
826
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000827/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000828/// a class method in its extension.
829///
Mike Stump1eb44332009-09-09 15:08:12 +0000830void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000831 ObjCInterfaceDecl *ID) {
832 if (!ID)
833 return; // Possibly due to previous error
834
835 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Stephen Hines651f13c2014-04-23 16:59:28 -0700836 for (auto *MD : ID->methods())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000837 MethodMap[MD->getSelector()] = MD;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000838
839 if (MethodMap.empty())
840 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700841 for (const auto *Method : CAT->methods()) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000842 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
Stephen Hines651f13c2014-04-23 16:59:28 -0700843 if (PrevMethod &&
844 (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) &&
845 !MatchTwoMethodDeclarations(Method, PrevMethod)) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000846 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
847 << Method->getDeclName();
848 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
849 }
850 }
851}
852
James Dennett1dfbd922012-06-14 21:40:34 +0000853/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000854Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000855Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000856 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000857 unsigned NumElts,
858 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000859 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000860 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000861 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000862 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
863 ForRedeclaration);
864 ObjCProtocolDecl *PDecl
865 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
866 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000867 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000868
869 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000870 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000871
Douglas Gregor3937f872012-01-01 20:33:24 +0000872 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000873 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000874
875 if (PrevDecl)
876 mergeDeclAttributes(PDecl, PrevDecl);
877
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000878 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Rafael Espindola4549d7f2013-07-09 12:05:01 +0000881 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +0000882}
883
John McCalld226f652010-08-21 09:40:31 +0000884Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000885ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
886 IdentifierInfo *ClassName, SourceLocation ClassLoc,
887 IdentifierInfo *CategoryName,
888 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000889 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000890 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000891 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000892 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000893 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000894 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000895
896 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000897
898 if (!IDecl
899 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000900 diag::err_category_forward_interface,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700901 CategoryName == nullptr)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000902 // Create an invalid ObjCCategoryDecl to serve as context for
903 // the enclosing method declarations. We mark the decl invalid
904 // to make it clear that this isn't a valid AST.
905 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000906 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000907 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000908 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000909
910 if (!IDecl)
911 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000912 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000913 }
914
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000915 if (!CategoryName && IDecl->getImplementation()) {
916 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
917 Diag(IDecl->getImplementation()->getLocation(),
918 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000919 }
920
Fariborz Jahanian25760612010-02-15 21:55:26 +0000921 if (CategoryName) {
922 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000923 if (ObjCCategoryDecl *Previous
924 = IDecl->FindCategoryDeclaration(CategoryName)) {
925 // Class extensions can be declared multiple times, categories cannot.
926 Diag(CategoryLoc, diag::warn_dup_category_def)
927 << ClassName << CategoryName;
928 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000929 }
930 }
Chris Lattner70f19542009-02-16 21:26:43 +0000931
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000932 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
933 ClassLoc, CategoryLoc, CategoryName, IDecl);
934 // FIXME: PushOnScopeChains?
935 CurContext->addDecl(CDecl);
936
Chris Lattner4d391482007-12-12 07:09:47 +0000937 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000938 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000939 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000940 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000941 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000942 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000943 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000944 }
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Anders Carlsson15281452008-11-04 16:57:32 +0000946 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000947 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000948}
949
950/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000951/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000952/// object.
John McCalld226f652010-08-21 09:40:31 +0000953Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000954 SourceLocation AtCatImplLoc,
955 IdentifierInfo *ClassName, SourceLocation ClassLoc,
956 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000957 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700958 ObjCCategoryDecl *CatIDecl = nullptr;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000959 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000960 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
961 if (!CatIDecl) {
962 // Category @implementation with no corresponding @interface.
963 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000964 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
965 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000966 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000967 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000968 }
969 }
970
Mike Stump1eb44332009-09-09 15:08:12 +0000971 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000972 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000973 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000974 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000975 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000976 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000977 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000978 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
979 diag::err_undef_interface)) {
980 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000981 }
Chris Lattner4d391482007-12-12 07:09:47 +0000982
Douglas Gregord0434102009-01-09 00:49:46 +0000983 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000984 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000985
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000986 // If the interface is deprecated/unavailable, warn/error about it.
987 if (IDecl)
988 DiagnoseUseOfDecl(IDecl, ClassLoc);
989
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000990 /// Check that CatName, category name, is not used in another implementation.
991 if (CatIDecl) {
992 if (CatIDecl->getImplementation()) {
993 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
994 << CatName;
995 Diag(CatIDecl->getImplementation()->getLocation(),
996 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000997 CDecl->setInvalidDecl();
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000998 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000999 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001000 // Warn on implementating category of deprecated class under
1001 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001002 DiagnoseObjCImplementedDeprecations(*this,
1003 dyn_cast<NamedDecl>(IDecl),
1004 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001005 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001006 }
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Anders Carlsson15281452008-11-04 16:57:32 +00001008 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001009 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001010}
1011
John McCalld226f652010-08-21 09:40:31 +00001012Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +00001013 SourceLocation AtClassImplLoc,
1014 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001015 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +00001016 SourceLocation SuperClassLoc) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001017 ObjCInterfaceDecl *IDecl = nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +00001018 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001019 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +00001020 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
1021 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001022 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001023 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001024 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001025 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +00001026 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1027 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +00001028 } else {
Richard Smith2d670972013-08-17 00:46:16 +00001029 // We did not find anything with the name ClassName; try to correct for
Douglas Gregor95ff7422010-01-04 17:27:12 +00001030 // typos in the class name.
Stephen Hines176edba2014-12-01 14:53:08 -08001031 TypoCorrection Corrected = CorrectTypo(
1032 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
1033 nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(), CTK_NonError);
Richard Smith2d670972013-08-17 00:46:16 +00001034 if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1035 // Suggest the (potentially) correct interface name. Don't provide a
1036 // code-modification hint or use the typo name for recovery, because
1037 // this is just a warning. The program may actually be correct.
1038 diagnoseTypo(Corrected,
1039 PDiag(diag::warn_undef_interface_suggest) << ClassName,
1040 /*ErrorRecovery*/false);
Douglas Gregor95ff7422010-01-04 17:27:12 +00001041 } else {
1042 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1043 }
Chris Lattner4d391482007-12-12 07:09:47 +00001044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Chris Lattner4d391482007-12-12 07:09:47 +00001046 // Check that super class name is valid class name
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001047 ObjCInterfaceDecl *SDecl = nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +00001048 if (SuperClassname) {
1049 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +00001050 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1051 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001052 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001053 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1054 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001055 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +00001056 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001057 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +00001058 if (SDecl && !SDecl->hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001059 SDecl = nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +00001060 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +00001061 Diag(SuperClassLoc, diag::err_undef_superclass)
1062 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001063 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001064 // This implementation and its interface do not have the same
1065 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001066 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001067 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001068 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001069 }
1070 }
1071 }
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Chris Lattner4d391482007-12-12 07:09:47 +00001073 if (!IDecl) {
1074 // Legacy case of @implementation with no corresponding @interface.
1075 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001076
Mike Stump390b4cc2009-05-16 07:39:55 +00001077 // FIXME: Do we support attributes on the @implementation? If so we should
1078 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001079 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001080 ClassName, /*PrevDecl=*/nullptr, ClassLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001081 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001082 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001083 if (SDecl) {
1084 IDecl->setSuperClass(SDecl);
1085 IDecl->setSuperClassLoc(SuperClassLoc);
1086 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1087 } else {
1088 IDecl->setEndOfDefinitionLoc(ClassLoc);
1089 }
1090
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001091 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001092 } else {
1093 // Mark the interface as being completed, even if it was just as
1094 // @class ....;
1095 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001096 if (!IDecl->hasDefinition())
1097 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
1100 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001101 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001102 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Anders Carlsson15281452008-11-04 16:57:32 +00001104 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001105 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Chris Lattner4d391482007-12-12 07:09:47 +00001107 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001108 if (IDecl->getImplementation()) {
1109 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001110 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001111 Diag(IDecl->getImplementation()->getLocation(),
1112 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +00001113 IMPDecl->setInvalidDecl();
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001114 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001115 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001116 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001117 // Warn on implementating deprecated class under
1118 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001119 DiagnoseObjCImplementedDeprecations(*this,
1120 dyn_cast<NamedDecl>(IDecl),
1121 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001122 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001123 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001124}
1125
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001126Sema::DeclGroupPtrTy
1127Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1128 SmallVector<Decl *, 64> DeclsInGroup;
1129 DeclsInGroup.reserve(Decls.size() + 1);
1130
1131 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1132 Decl *Dcl = Decls[i];
1133 if (!Dcl)
1134 continue;
1135 if (Dcl->getDeclContext()->isFileContext())
1136 Dcl->setTopLevelDeclInObjCContainer();
1137 DeclsInGroup.push_back(Dcl);
1138 }
1139
1140 DeclsInGroup.push_back(ObjCImpDecl);
1141
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001142 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001143}
1144
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001145void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1146 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001147 SourceLocation RBrace) {
1148 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001149 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001150 if (!IDecl)
1151 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001152 /// Check case of non-existing \@interface decl.
1153 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001154 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001155 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001156 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001157 // Add ivar's to class's DeclContext.
1158 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001159 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001160 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001161 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001162 }
1163
Chris Lattner4d391482007-12-12 07:09:47 +00001164 return;
1165 }
1166 // If implementation has empty ivar list, just return.
1167 if (numIvars == 0)
1168 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Chris Lattner4d391482007-12-12 07:09:47 +00001170 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001171 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001172 if (ImpDecl->getSuperClass())
1173 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1174 for (unsigned i = 0; i < numIvars; i++) {
1175 ObjCIvarDecl* ImplIvar = ivars[i];
1176 if (const ObjCIvarDecl *ClsIvar =
1177 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1178 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1179 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1180 continue;
1181 }
Fariborz Jahanian3b20f582013-06-26 22:10:27 +00001182 // Check class extensions (unnamed categories) for duplicate ivars.
Stephen Hines651f13c2014-04-23 16:59:28 -07001183 for (const auto *CDecl : IDecl->visible_extensions()) {
Fariborz Jahanian3b20f582013-06-26 22:10:27 +00001184 if (const ObjCIvarDecl *ClsExtIvar =
1185 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1186 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1187 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1188 continue;
1189 }
1190 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001191 // Instance ivar to Implementation's DeclContext.
1192 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001193 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001194 ImpDecl->addDecl(ImplIvar);
1195 }
1196 return;
1197 }
Chris Lattner4d391482007-12-12 07:09:47 +00001198 // Check interface's Ivar list against those in the implementation.
1199 // names and types must match.
1200 //
Chris Lattner4d391482007-12-12 07:09:47 +00001201 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001202 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001203 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1204 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001205 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001206 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001207 assert (ImplIvar && "missing implementation ivar");
1208 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Steve Naroffca331292009-03-03 14:49:36 +00001210 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001211 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001212 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001213 << ImplIvar->getIdentifier()
1214 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001215 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001216 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1217 ImplIvar->getBitWidthValue(Context) !=
1218 ClsIvar->getBitWidthValue(Context)) {
1219 Diag(ImplIvar->getBitWidth()->getLocStart(),
1220 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1221 Diag(ClsIvar->getBitWidth()->getLocStart(),
1222 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001223 }
Steve Naroffca331292009-03-03 14:49:36 +00001224 // Make sure the names are identical.
1225 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001226 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001227 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001228 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001229 }
1230 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001231 }
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Chris Lattner609e4c72007-12-12 18:11:49 +00001233 if (numIvars > 0)
Stephen Hines651f13c2014-04-23 16:59:28 -07001234 Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001235 else if (IVI != IVE)
Stephen Hines651f13c2014-04-23 16:59:28 -07001236 Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001237}
1238
Stephen Hines651f13c2014-04-23 16:59:28 -07001239static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc,
1240 ObjCMethodDecl *method,
1241 bool &IncompleteImpl,
1242 unsigned DiagID,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001243 NamedDecl *NeededFor = nullptr) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001244 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001245 switch (method->getAvailability()) {
1246 case AR_Available:
1247 case AR_Deprecated:
1248 break;
1249
1250 // Don't warn about unavailable or not-yet-introduced methods.
1251 case AR_NotYetIntroduced:
1252 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001253 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001254 }
1255
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001256 // FIXME: For now ignore 'IncompleteImpl'.
1257 // Previously we grouped all unimplemented methods under a single
1258 // warning, but some users strongly voiced that they would prefer
1259 // separate warnings. We will give that approach a try, as that
1260 // matches what we do with protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001261 {
1262 const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID);
1263 B << method;
1264 if (NeededFor)
1265 B << NeededFor;
1266 }
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001267
1268 // Issue a note to the original declaration.
1269 SourceLocation MethodLoc = method->getLocStart();
1270 if (MethodLoc.isValid())
Stephen Hines651f13c2014-04-23 16:59:28 -07001271 S.Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001272}
1273
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001274/// Determines if type B can be substituted for type A. Returns true if we can
1275/// guarantee that anything that the user will do to an object of type A can
1276/// also be done to an object of type B. This is trivially true if the two
1277/// types are the same, or if B is a subclass of A. It becomes more complex
1278/// in cases where protocols are involved.
1279///
1280/// Object types in Objective-C describe the minimum requirements for an
1281/// object, rather than providing a complete description of a type. For
1282/// example, if A is a subclass of B, then B* may refer to an instance of A.
1283/// The principle of substitutability means that we may use an instance of A
1284/// anywhere that we may use an instance of B - it will implement all of the
1285/// ivars of B and all of the methods of B.
1286///
1287/// This substitutability is important when type checking methods, because
1288/// the implementation may have stricter type definitions than the interface.
1289/// The interface specifies minimum requirements, but the implementation may
1290/// have more accurate ones. For example, a method may privately accept
1291/// instances of B, but only publish that it accepts instances of A. Any
1292/// object passed to it will be type checked against B, and so will implicitly
1293/// by a valid A*. Similarly, a method may return a subclass of the class that
1294/// it is declared as returning.
1295///
1296/// This is most important when considering subclassing. A method in a
1297/// subclass must accept any object as an argument that its superclass's
1298/// implementation accepts. It may, however, accept a more general type
1299/// without breaking substitutability (i.e. you can still use the subclass
1300/// anywhere that you can use the superclass, but not vice versa). The
1301/// converse requirement applies to return types: the return type for a
1302/// subclass method must be a valid object of the kind that the superclass
1303/// advertises, but it may be specified more accurately. This avoids the need
1304/// for explicit down-casting by callers.
1305///
1306/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001307static bool isObjCTypeSubstitutable(ASTContext &Context,
1308 const ObjCObjectPointerType *A,
1309 const ObjCObjectPointerType *B,
1310 bool rejectId) {
1311 // Reject a protocol-unqualified id.
1312 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001313
1314 // If B is a qualified id, then A must also be a qualified id and it must
1315 // implement all of the protocols in B. It may not be a qualified class.
1316 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1317 // stricter definition so it is not substitutable for id<A>.
1318 if (B->isObjCQualifiedIdType()) {
1319 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001320 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1321 QualType(B,0),
1322 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001323 }
1324
1325 /*
1326 // id is a special type that bypasses type checking completely. We want a
1327 // warning when it is used in one place but not another.
1328 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1329
1330
1331 // If B is a qualified id, then A must also be a qualified id (which it isn't
1332 // if we've got this far)
1333 if (B->isObjCQualifiedIdType()) return false;
1334 */
1335
1336 // Now we know that A and B are (potentially-qualified) class types. The
1337 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001338 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001339}
1340
John McCall10302c02010-10-28 02:34:38 +00001341static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1342 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1343}
1344
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001345static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001346 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001347 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001348 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001349 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001350 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001351 if (IsProtocolMethodDecl &&
1352 (MethodDecl->getObjCDeclQualifier() !=
1353 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001354 if (Warn) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001355 S.Diag(MethodImpl->getLocation(),
1356 (IsOverridingMode
1357 ? diag::warn_conflicting_overriding_ret_type_modifiers
1358 : diag::warn_conflicting_ret_type_modifiers))
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001359 << MethodImpl->getDeclName()
Stephen Hines176edba2014-12-01 14:53:08 -08001360 << MethodImpl->getReturnTypeSourceRange();
Stephen Hines651f13c2014-04-23 16:59:28 -07001361 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
Stephen Hines176edba2014-12-01 14:53:08 -08001362 << MethodDecl->getReturnTypeSourceRange();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001363 }
1364 else
1365 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001366 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001367
1368 if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(),
1369 MethodDecl->getReturnType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001370 return true;
1371 if (!Warn)
1372 return false;
John McCall10302c02010-10-28 02:34:38 +00001373
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001374 unsigned DiagID =
1375 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1376 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001377
1378 // Mismatches between ObjC pointers go into a different warning
1379 // category, and sometimes they're even completely whitelisted.
1380 if (const ObjCObjectPointerType *ImplPtrTy =
Stephen Hines651f13c2014-04-23 16:59:28 -07001381 MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001382 if (const ObjCObjectPointerType *IfacePtrTy =
Stephen Hines651f13c2014-04-23 16:59:28 -07001383 MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001384 // Allow non-matching return types as long as they don't violate
1385 // the principle of substitutability. Specifically, we permit
1386 // return types that are subclasses of the declared return type,
1387 // or that are more-qualified versions of the declared type.
1388 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001389 return false;
John McCall10302c02010-10-28 02:34:38 +00001390
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001391 DiagID =
1392 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1393 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001394 }
1395 }
1396
1397 S.Diag(MethodImpl->getLocation(), DiagID)
Stephen Hines651f13c2014-04-23 16:59:28 -07001398 << MethodImpl->getDeclName() << MethodDecl->getReturnType()
1399 << MethodImpl->getReturnType()
Stephen Hines176edba2014-12-01 14:53:08 -08001400 << MethodImpl->getReturnTypeSourceRange();
Stephen Hines651f13c2014-04-23 16:59:28 -07001401 S.Diag(MethodDecl->getLocation(), IsOverridingMode
1402 ? diag::note_previous_declaration
1403 : diag::note_previous_definition)
Stephen Hines176edba2014-12-01 14:53:08 -08001404 << MethodDecl->getReturnTypeSourceRange();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001405 return false;
John McCall10302c02010-10-28 02:34:38 +00001406}
1407
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001408static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001409 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001410 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001411 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001412 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001413 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001414 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001415 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001416 if (IsProtocolMethodDecl &&
1417 (ImplVar->getObjCDeclQualifier() !=
1418 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001419 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001420 if (IsOverridingMode)
1421 S.Diag(ImplVar->getLocation(),
1422 diag::warn_conflicting_overriding_param_modifiers)
1423 << getTypeRange(ImplVar->getTypeSourceInfo())
1424 << MethodImpl->getDeclName();
1425 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001426 diag::warn_conflicting_param_modifiers)
1427 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001428 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001429 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1430 << getTypeRange(IfaceVar->getTypeSourceInfo());
1431 }
1432 else
1433 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001434 }
1435
John McCall10302c02010-10-28 02:34:38 +00001436 QualType ImplTy = ImplVar->getType();
1437 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001438
John McCall10302c02010-10-28 02:34:38 +00001439 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001440 return true;
1441
1442 if (!Warn)
1443 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001444 unsigned DiagID =
1445 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1446 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001447
1448 // Mismatches between ObjC pointers go into a different warning
1449 // category, and sometimes they're even completely whitelisted.
1450 if (const ObjCObjectPointerType *ImplPtrTy =
1451 ImplTy->getAs<ObjCObjectPointerType>()) {
1452 if (const ObjCObjectPointerType *IfacePtrTy =
1453 IfaceTy->getAs<ObjCObjectPointerType>()) {
1454 // Allow non-matching argument types as long as they don't
1455 // violate the principle of substitutability. Specifically, the
1456 // implementation must accept any objects that the superclass
1457 // accepts, however it may also accept others.
1458 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001459 return false;
John McCall10302c02010-10-28 02:34:38 +00001460
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001461 DiagID =
1462 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1463 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001464 }
1465 }
1466
1467 S.Diag(ImplVar->getLocation(), DiagID)
1468 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001469 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1470 S.Diag(IfaceVar->getLocation(),
1471 (IsOverridingMode ? diag::note_previous_declaration
1472 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001473 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001474 return false;
John McCall10302c02010-10-28 02:34:38 +00001475}
John McCallf85e1932011-06-15 23:02:42 +00001476
1477/// In ARC, check whether the conventional meanings of the two methods
1478/// match. If they don't, it's a hard error.
1479static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1480 ObjCMethodDecl *decl) {
1481 ObjCMethodFamily implFamily = impl->getMethodFamily();
1482 ObjCMethodFamily declFamily = decl->getMethodFamily();
1483 if (implFamily == declFamily) return false;
1484
1485 // Since conventions are sorted by selector, the only possibility is
1486 // that the types differ enough to cause one selector or the other
1487 // to fall out of the family.
1488 assert(implFamily == OMF_None || declFamily == OMF_None);
1489
1490 // No further diagnostics required on invalid declarations.
1491 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1492
1493 const ObjCMethodDecl *unmatched = impl;
1494 ObjCMethodFamily family = declFamily;
1495 unsigned errorID = diag::err_arc_lost_method_convention;
1496 unsigned noteID = diag::note_arc_lost_method_convention;
1497 if (declFamily == OMF_None) {
1498 unmatched = decl;
1499 family = implFamily;
1500 errorID = diag::err_arc_gained_method_convention;
1501 noteID = diag::note_arc_gained_method_convention;
1502 }
1503
1504 // Indexes into a %select clause in the diagnostic.
1505 enum FamilySelector {
1506 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1507 };
1508 FamilySelector familySelector = FamilySelector();
1509
1510 switch (family) {
1511 case OMF_None: llvm_unreachable("logic error, no method convention");
1512 case OMF_retain:
1513 case OMF_release:
1514 case OMF_autorelease:
1515 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001516 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001517 case OMF_retainCount:
1518 case OMF_self:
Stephen Hines176edba2014-12-01 14:53:08 -08001519 case OMF_initialize:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001520 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001521 // Mismatches for these methods don't change ownership
1522 // conventions, so we don't care.
1523 return false;
1524
1525 case OMF_init: familySelector = F_init; break;
1526 case OMF_alloc: familySelector = F_alloc; break;
1527 case OMF_copy: familySelector = F_copy; break;
1528 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1529 case OMF_new: familySelector = F_new; break;
1530 }
1531
1532 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1533 ReasonSelector reasonSelector;
1534
1535 // The only reason these methods don't fall within their families is
1536 // due to unusual result types.
Stephen Hines651f13c2014-04-23 16:59:28 -07001537 if (unmatched->getReturnType()->isObjCObjectPointerType()) {
John McCallf85e1932011-06-15 23:02:42 +00001538 reasonSelector = R_UnrelatedReturn;
1539 } else {
1540 reasonSelector = R_NonObjectReturn;
1541 }
1542
Joerg Sonnenberger73484542013-06-26 21:31:47 +00001543 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1544 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCallf85e1932011-06-15 23:02:42 +00001545
1546 return true;
1547}
John McCall10302c02010-10-28 02:34:38 +00001548
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001549void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001550 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001551 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001552 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001553 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1554 return;
1555
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001556 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001557 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001558 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Chris Lattner3aff9192009-04-11 19:58:42 +00001560 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001561 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1562 EF = MethodDecl->param_end();
1563 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001564 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001565 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001566 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001567
Fariborz Jahanian21121902011-08-08 18:03:17 +00001568 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001569 Diag(ImpMethodDecl->getLocation(),
1570 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001571 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001572 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001573}
1574
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001575void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1576 ObjCMethodDecl *Overridden,
1577 bool IsProtocolMethodDecl) {
1578
1579 CheckMethodOverrideReturn(*this, Method, Overridden,
1580 IsProtocolMethodDecl, true,
1581 true);
1582
1583 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001584 IF = Overridden->param_begin(), EM = Method->param_end(),
1585 EF = Overridden->param_end();
1586 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001587 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1588 IsProtocolMethodDecl, true, true);
1589 }
1590
1591 if (Method->isVariadic() != Overridden->isVariadic()) {
1592 Diag(Method->getLocation(),
1593 diag::warn_conflicting_overriding_variadic);
1594 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1595 }
1596}
1597
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001598/// WarnExactTypedMethods - This routine issues a warning if method
1599/// implementation declaration matches exactly that of its declaration.
1600void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1601 ObjCMethodDecl *MethodDecl,
1602 bool IsProtocolMethodDecl) {
1603 // don't issue warning when protocol method is optional because primary
1604 // class is not required to implement it and it is safe for protocol
1605 // to implement it.
1606 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1607 return;
1608 // don't issue warning when primary class's method is
1609 // depecated/unavailable.
1610 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1611 MethodDecl->hasAttr<DeprecatedAttr>())
1612 return;
1613
1614 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1615 IsProtocolMethodDecl, false, false);
1616 if (match)
1617 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001618 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1619 EF = MethodDecl->param_end();
1620 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001621 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1622 *IM, *IF,
1623 IsProtocolMethodDecl, false, false);
1624 if (!match)
1625 break;
1626 }
1627 if (match)
1628 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001629 if (match)
1630 match = !(MethodDecl->isClassMethod() &&
1631 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001632
1633 if (match) {
1634 Diag(ImpMethodDecl->getLocation(),
1635 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001636 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1637 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001638 }
1639}
1640
Mike Stump390b4cc2009-05-16 07:39:55 +00001641/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1642/// improve the efficiency of selector lookups and type checking by associating
1643/// with each protocol / interface / category the flattened instance tables. If
1644/// we used an immutable set to keep the table then it wouldn't add significant
1645/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001646
Stephen Hines651f13c2014-04-23 16:59:28 -07001647typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet;
1648typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet;
1649
1650static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl,
1651 ProtocolNameSet &PNS) {
1652 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1653 PNS.insert(PDecl->getIdentifier());
1654 for (const auto *PI : PDecl->protocols())
1655 findProtocolsWithExplicitImpls(PI, PNS);
1656}
1657
1658/// Recursively populates a set with all conformed protocols in a class
1659/// hierarchy that have the 'objc_protocol_requires_explicit_implementation'
1660/// attribute.
1661static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super,
1662 ProtocolNameSet &PNS) {
1663 if (!Super)
1664 return;
1665
1666 for (const auto *I : Super->all_referenced_protocols())
1667 findProtocolsWithExplicitImpls(I, PNS);
1668
1669 findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS);
1670}
1671
Steve Naroffefe7f362008-02-08 22:06:17 +00001672/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001673/// Declared in protocol, and those referenced by it.
Stephen Hines651f13c2014-04-23 16:59:28 -07001674static void CheckProtocolMethodDefs(Sema &S,
1675 SourceLocation ImpLoc,
1676 ObjCProtocolDecl *PDecl,
1677 bool& IncompleteImpl,
1678 const Sema::SelectorSet &InsMap,
1679 const Sema::SelectorSet &ClsMap,
1680 ObjCContainerDecl *CDecl,
1681 LazyProtocolNameSet &ProtocolsExplictImpl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001682 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1683 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1684 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001685 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1686
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001687 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001688 ObjCInterfaceDecl *NSIDecl = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001689
1690 // If this protocol is marked 'objc_protocol_requires_explicit_implementation'
1691 // then we should check if any class in the super class hierarchy also
1692 // conforms to this protocol, either directly or via protocol inheritance.
1693 // If so, we can skip checking this protocol completely because we
1694 // know that a parent class already satisfies this protocol.
1695 //
1696 // Note: we could generalize this logic for all protocols, and merely
1697 // add the limit on looking at the super class chain for just
1698 // specially marked protocols. This may be a good optimization. This
1699 // change is restricted to 'objc_protocol_requires_explicit_implementation'
1700 // protocols for now for controlled evaluation.
1701 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) {
1702 if (!ProtocolsExplictImpl) {
1703 ProtocolsExplictImpl.reset(new ProtocolNameSet);
1704 findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl);
1705 }
1706 if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) !=
1707 ProtocolsExplictImpl->end())
1708 return;
1709
1710 // If no super class conforms to the protocol, we should not search
1711 // for methods in the super class to implicitly satisfy the protocol.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001712 Super = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001713 }
1714
1715 if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001716 // check to see if class implements forwardInvocation method and objects
1717 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001718 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001719 // Under such conditions, which means that every method possible is
1720 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001721 // found" warnings.
1722 // FIXME: Use a general GetUnarySelector method for this.
Stephen Hines651f13c2014-04-23 16:59:28 -07001723 IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
1724 Selector fISelector = S.Context.Selectors.getSelector(1, &II);
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001725 if (InsMap.count(fISelector))
1726 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1727 // need be implemented in the implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001728 NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001729 }
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001731 // If this is a forward protocol declaration, get its definition.
1732 if (!PDecl->isThisDeclarationADefinition() &&
1733 PDecl->getDefinition())
1734 PDecl = PDecl->getDefinition();
1735
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001736 // If a method lookup fails locally we still need to look and see if
1737 // the method was implemented by a base class or an inherited
1738 // protocol. This lookup is slow, but occurs rarely in correct code
1739 // and otherwise would terminate in a warning.
1740
Chris Lattner4d391482007-12-12 07:09:47 +00001741 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001742 if (!NSIDecl)
Stephen Hines651f13c2014-04-23 16:59:28 -07001743 for (auto *method : PDecl->instance_methods()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001744 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001745 !method->isPropertyAccessor() &&
1746 !InsMap.count(method->getSelector()) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001747 (!Super || !Super->lookupMethod(method->getSelector(),
1748 true /* instance */,
1749 false /* shallowCategory */,
1750 true /* followsSuper */,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001751 nullptr /* category */))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001752 // If a method is not implemented in the category implementation but
1753 // has been declared in its primary class, superclass,
1754 // or in one of their protocols, no need to issue the warning.
1755 // This is because method will be implemented in the primary class
1756 // or one of its super class implementation.
1757
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001758 // Ugly, but necessary. Method declared in protcol might have
1759 // have been synthesized due to a property declared in the class which
1760 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001761 if (ObjCMethodDecl *MethodInClass =
Stephen Hines651f13c2014-04-23 16:59:28 -07001762 IDecl->lookupMethod(method->getSelector(),
1763 true /* instance */,
1764 true /* shallowCategoryLookup */,
1765 false /* followSuper */))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001766 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001767 continue;
1768 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001769 if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001770 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
1771 PDecl);
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001772 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001773 }
1774 }
Chris Lattner4d391482007-12-12 07:09:47 +00001775 // check unimplemented class methods
Stephen Hines651f13c2014-04-23 16:59:28 -07001776 for (auto *method : PDecl->class_methods()) {
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001777 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1778 !ClsMap.count(method->getSelector()) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001779 (!Super || !Super->lookupMethod(method->getSelector(),
1780 false /* class method */,
1781 false /* shallowCategoryLookup */,
1782 true /* followSuper */,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001783 nullptr /* category */))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001784 // See above comment for instance method lookups.
Stephen Hines651f13c2014-04-23 16:59:28 -07001785 if (C && IDecl->lookupMethod(method->getSelector(),
1786 false /* class */,
1787 true /* shallowCategoryLookup */,
1788 false /* followSuper */))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001789 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001790
Fariborz Jahanian52146832010-03-31 18:23:33 +00001791 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001792 if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001793 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001794 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001795 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001796 }
Chris Lattner780f3292008-07-21 21:32:27 +00001797 // Check on this protocols's referenced protocols, recursively.
Stephen Hines651f13c2014-04-23 16:59:28 -07001798 for (auto *PI : PDecl->protocols())
1799 CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap,
1800 CDecl, ProtocolsExplictImpl);
Chris Lattner4d391482007-12-12 07:09:47 +00001801}
1802
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001803/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001804/// or protocol against those declared in their implementations.
1805///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001806void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1807 const SelectorSet &ClsMap,
1808 SelectorSet &InsMapSeen,
1809 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001810 ObjCImplDecl* IMPDecl,
1811 ObjCContainerDecl* CDecl,
1812 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001813 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001814 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001815 // Check and see if instance methods in class interface have been
1816 // implemented in the implementation class. If so, their types match.
Stephen Hines651f13c2014-04-23 16:59:28 -07001817 for (auto *I : CDecl->instance_methods()) {
Stephen Hines176edba2014-12-01 14:53:08 -08001818 if (!InsMapSeen.insert(I->getSelector()).second)
Benjamin Kramer7dcff5b2013-10-14 15:16:10 +00001819 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001820 if (!I->isPropertyAccessor() &&
1821 !InsMap.count(I->getSelector())) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001822 if (ImmediateClass)
Stephen Hines651f13c2014-04-23 16:59:28 -07001823 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001824 diag::warn_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001825 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001826 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001827 ObjCMethodDecl *ImpMethodDecl =
Stephen Hines651f13c2014-04-23 16:59:28 -07001828 IMPDecl->getInstanceMethod(I->getSelector());
1829 assert(CDecl->getInstanceMethod(I->getSelector()) &&
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001830 "Expected to find the method through lookup as well");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001831 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001832 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001833 if (!WarnCategoryMethodImpl)
Stephen Hines651f13c2014-04-23 16:59:28 -07001834 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001835 isa<ObjCProtocolDecl>(CDecl));
Stephen Hines651f13c2014-04-23 16:59:28 -07001836 else if (!I->isPropertyAccessor())
1837 WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001838 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001839 }
1840 }
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001842 // Check and see if class methods in class interface have been
1843 // implemented in the implementation class. If so, their types match.
Stephen Hines651f13c2014-04-23 16:59:28 -07001844 for (auto *I : CDecl->class_methods()) {
Stephen Hines176edba2014-12-01 14:53:08 -08001845 if (!ClsMapSeen.insert(I->getSelector()).second)
Benjamin Kramer7dcff5b2013-10-14 15:16:10 +00001846 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001847 if (!ClsMap.count(I->getSelector())) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001848 if (ImmediateClass)
Stephen Hines651f13c2014-04-23 16:59:28 -07001849 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001850 diag::warn_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001851 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001852 ObjCMethodDecl *ImpMethodDecl =
Stephen Hines651f13c2014-04-23 16:59:28 -07001853 IMPDecl->getClassMethod(I->getSelector());
1854 assert(CDecl->getClassMethod(I->getSelector()) &&
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001855 "Expected to find the method through lookup as well");
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001856 if (!WarnCategoryMethodImpl)
Stephen Hines651f13c2014-04-23 16:59:28 -07001857 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001858 isa<ObjCProtocolDecl>(CDecl));
1859 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001860 WarnExactTypedMethods(ImpMethodDecl, I,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001861 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001862 }
1863 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001864
Fariborz Jahanian41594c52013-08-14 23:58:55 +00001865 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1866 // Also, check for methods declared in protocols inherited by
1867 // this protocol.
Stephen Hines651f13c2014-04-23 16:59:28 -07001868 for (auto *PI : PD->protocols())
Fariborz Jahanian41594c52013-08-14 23:58:55 +00001869 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Stephen Hines651f13c2014-04-23 16:59:28 -07001870 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanian41594c52013-08-14 23:58:55 +00001871 WarnCategoryMethodImpl);
1872 }
1873
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001874 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001875 // when checking that methods in implementation match their declaration,
1876 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1877 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001878 if (!WarnCategoryMethodImpl) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001879 for (auto *Cat : I->visible_categories())
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001880 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Stephen Hines651f13c2014-04-23 16:59:28 -07001881 IMPDecl, Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001882 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001883 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001884 // Also methods in class extensions need be looked at next.
Stephen Hines651f13c2014-04-23 16:59:28 -07001885 for (auto *Ext : I->visible_extensions())
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001886 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Stephen Hines651f13c2014-04-23 16:59:28 -07001887 IMPDecl, Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001888 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001889 }
1890
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001891 // Check for any implementation of a methods declared in protocol.
Stephen Hines651f13c2014-04-23 16:59:28 -07001892 for (auto *PI : I->all_referenced_protocols())
Mike Stump1eb44332009-09-09 15:08:12 +00001893 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Stephen Hines651f13c2014-04-23 16:59:28 -07001894 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001895 WarnCategoryMethodImpl);
Stephen Hines651f13c2014-04-23 16:59:28 -07001896
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001897 // FIXME. For now, we are not checking for extact match of methods
1898 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001899 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001900 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001901 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001902 I->getSuperClass(), IncompleteImpl, false);
1903 }
1904}
1905
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001906/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1907/// category matches with those implemented in its primary class and
1908/// warns each time an exact match is found.
1909void Sema::CheckCategoryVsClassMethodMatches(
1910 ObjCCategoryImplDecl *CatIMPDecl) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001911 // Get category's primary class.
1912 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1913 if (!CatDecl)
1914 return;
1915 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1916 if (!IDecl)
1917 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07001918 ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
1919 SelectorSet InsMap, ClsMap;
1920
1921 for (const auto *I : CatIMPDecl->instance_methods()) {
1922 Selector Sel = I->getSelector();
1923 // When checking for methods implemented in the category, skip over
1924 // those declared in category class's super class. This is because
1925 // the super class must implement the method.
1926 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
1927 continue;
1928 InsMap.insert(Sel);
1929 }
1930
1931 for (const auto *I : CatIMPDecl->class_methods()) {
1932 Selector Sel = I->getSelector();
1933 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
1934 continue;
1935 ClsMap.insert(Sel);
1936 }
1937 if (InsMap.empty() && ClsMap.empty())
1938 return;
1939
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001940 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001941 bool IncompleteImpl = false;
1942 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1943 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001944 IncompleteImpl, false,
1945 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001946}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001947
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001948void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001949 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001950 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001951 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001952 // Check and see if instance methods in class interface have been
1953 // implemented in the implementation class.
Stephen Hines651f13c2014-04-23 16:59:28 -07001954 for (const auto *I : IMPDecl->instance_methods())
1955 InsMap.insert(I->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001957 // Check and see if properties declared in the interface have either 1)
1958 // an implementation or 2) there is a @synthesize/@dynamic implementation
1959 // of the property in the @implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001960 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1961 bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties &&
1962 LangOpts.ObjCRuntime.isNonFragile() &&
1963 !IDecl->isObjCRequiresPropertyDefs();
1964 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties);
1965 }
1966
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001967 SelectorSet ClsMap;
Stephen Hines651f13c2014-04-23 16:59:28 -07001968 for (const auto *I : IMPDecl->class_methods())
1969 ClsMap.insert(I->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001971 // Check for type conflict of methods declared in a class/protocol and
1972 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001973 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001974 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1975 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001976 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001977
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001978 // check all methods implemented in category against those declared
1979 // in its primary class.
1980 if (ObjCCategoryImplDecl *CatDecl =
1981 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1982 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Chris Lattner4d391482007-12-12 07:09:47 +00001984 // Check the protocol list for unimplemented methods in the @implementation
1985 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001986 // Check and see if class methods in class interface have been
1987 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Stephen Hines651f13c2014-04-23 16:59:28 -07001989 LazyProtocolNameSet ExplicitImplProtocols;
1990
Chris Lattnercddc8882009-03-01 00:56:52 +00001991 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001992 for (auto *PI : I->all_referenced_protocols())
1993 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl,
1994 InsMap, ClsMap, I, ExplicitImplProtocols);
Chris Lattnercddc8882009-03-01 00:56:52 +00001995 // Check class extensions (unnamed categories)
Stephen Hines651f13c2014-04-23 16:59:28 -07001996 for (auto *Ext : I->visible_extensions())
1997 ImplMethodsVsClassMethods(S, IMPDecl, Ext, IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001998 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001999 // For extended class, unimplemented methods in its protocols will
2000 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00002001 if (!C->IsClassExtension()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002002 for (auto *P : C->protocols())
2003 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P,
2004 IncompleteImpl, InsMap, ClsMap, CDecl,
2005 ExplicitImplProtocols);
2006 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl,
2007 /* SynthesizeProperties */ false);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00002008 }
Chris Lattnercddc8882009-03-01 00:56:52 +00002009 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002010 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00002011}
2012
Mike Stump1eb44332009-09-09 15:08:12 +00002013/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00002014Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00002015Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00002016 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00002017 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00002018 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00002019 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00002020 for (unsigned i = 0; i != NumElts; ++i) {
2021 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00002022 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00002023 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00002024 LookupOrdinaryName, ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002025 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00002026 // GCC apparently allows the following idiom:
2027 //
2028 // typedef NSObject < XCElementTogglerP > XCElementToggler;
2029 // @class XCElementToggler;
2030 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00002031 // Here we have chosen to ignore the forward class declaration
2032 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00002033 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00002034 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002035 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00002036 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00002037 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002038 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00002039 // to the underlying class. Just ignore the forward class with a warning
2040 // as this will force the intended behavior which is to lookup the typedef
2041 // name.
2042 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
2043 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
2044 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2045 continue;
2046 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00002047 }
Chris Lattner4d391482007-12-12 07:09:47 +00002048 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00002049
2050 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00002051 ObjCInterfaceDecl *PrevIDecl
2052 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00002053
2054 IdentifierInfo *ClassName = IdentList[i];
2055 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2056 // A previous decl with a different name is because of
2057 // @compatibility_alias, for example:
2058 // \code
2059 // @class NewImage;
2060 // @compatibility_alias OldImage NewImage;
2061 // \endcode
2062 // A lookup for 'OldImage' will return the 'NewImage' decl.
2063 //
2064 // In such a case use the real declaration name, instead of the alias one,
2065 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2066 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2067 // has been aliased.
2068 ClassName = PrevIDecl->getIdentifier();
2069 }
2070
Douglas Gregor7723fec2011-12-15 20:29:51 +00002071 ObjCInterfaceDecl *IDecl
2072 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00002073 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00002074 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00002075
Douglas Gregor7723fec2011-12-15 20:29:51 +00002076 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00002077 CheckObjCDeclScope(IDecl);
2078 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002079 }
Rafael Espindola4549d7f2013-07-09 12:05:01 +00002080
2081 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002082}
2083
John McCall0f4c4c42011-06-16 01:15:19 +00002084static bool tryMatchRecordTypes(ASTContext &Context,
2085 Sema::MethodMatchStrategy strategy,
2086 const Type *left, const Type *right);
2087
John McCallf85e1932011-06-15 23:02:42 +00002088static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2089 QualType leftQT, QualType rightQT) {
2090 const Type *left =
2091 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2092 const Type *right =
2093 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2094
2095 if (left == right) return true;
2096
2097 // If we're doing a strict match, the types have to match exactly.
2098 if (strategy == Sema::MMS_strict) return false;
2099
2100 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2101
2102 // Otherwise, use this absurdly complicated algorithm to try to
2103 // validate the basic, low-level compatibility of the two types.
2104
2105 // As a minimum, require the sizes and alignments to match.
Stephen Hines176edba2014-12-01 14:53:08 -08002106 TypeInfo LeftTI = Context.getTypeInfo(left);
2107 TypeInfo RightTI = Context.getTypeInfo(right);
2108 if (LeftTI.Width != RightTI.Width)
2109 return false;
2110
2111 if (LeftTI.Align != RightTI.Align)
John McCallf85e1932011-06-15 23:02:42 +00002112 return false;
2113
2114 // Consider all the kinds of non-dependent canonical types:
2115 // - functions and arrays aren't possible as return and parameter types
2116
2117 // - vector types of equal size can be arbitrarily mixed
2118 if (isa<VectorType>(left)) return isa<VectorType>(right);
2119 if (isa<VectorType>(right)) return false;
2120
2121 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00002122 // - structs, unions, and Objective-C objects must match more-or-less
2123 // exactly
John McCallf85e1932011-06-15 23:02:42 +00002124 // - everything else should be a scalar
2125 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00002126 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00002127
John McCall1d9b3b22011-09-09 05:25:32 +00002128 // Make scalars agree in kind, except count bools as chars, and group
2129 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00002130 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2131 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2132 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2133 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00002134 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2135 leftSK = Type::STK_ObjCObjectPointer;
2136 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2137 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002138
2139 // Note that data member pointers and function member pointers don't
2140 // intermix because of the size differences.
2141
2142 return (leftSK == rightSK);
2143}
Chris Lattner4d391482007-12-12 07:09:47 +00002144
John McCall0f4c4c42011-06-16 01:15:19 +00002145static bool tryMatchRecordTypes(ASTContext &Context,
2146 Sema::MethodMatchStrategy strategy,
2147 const Type *lt, const Type *rt) {
2148 assert(lt && rt && lt != rt);
2149
2150 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2151 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2152 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2153
2154 // Require union-hood to match.
2155 if (left->isUnion() != right->isUnion()) return false;
2156
2157 // Require an exact match if either is non-POD.
2158 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2159 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2160 return false;
2161
2162 // Require size and alignment to match.
Stephen Hines176edba2014-12-01 14:53:08 -08002163 TypeInfo LeftTI = Context.getTypeInfo(lt);
2164 TypeInfo RightTI = Context.getTypeInfo(rt);
2165 if (LeftTI.Width != RightTI.Width)
2166 return false;
2167
2168 if (LeftTI.Align != RightTI.Align)
2169 return false;
John McCall0f4c4c42011-06-16 01:15:19 +00002170
2171 // Require fields to match.
2172 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2173 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2174 for (; li != le && ri != re; ++li, ++ri) {
2175 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2176 return false;
2177 }
2178 return (li == le && ri == re);
2179}
2180
Chris Lattner4d391482007-12-12 07:09:47 +00002181/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2182/// returns true, or false, accordingly.
2183/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002184bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2185 const ObjCMethodDecl *right,
2186 MethodMatchStrategy strategy) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002187 if (!matchTypes(Context, strategy, left->getReturnType(),
2188 right->getReturnType()))
John McCallf85e1932011-06-15 23:02:42 +00002189 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Douglas Gregor7666b032013-02-07 19:13:24 +00002191 // If either is hidden, it is not considered to match.
2192 if (left->isHidden() || right->isHidden())
2193 return false;
2194
David Blaikie4e4d0842012-03-11 07:00:24 +00002195 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002196 (left->hasAttr<NSReturnsRetainedAttr>()
2197 != right->hasAttr<NSReturnsRetainedAttr>() ||
2198 left->hasAttr<NSConsumesSelfAttr>()
2199 != right->hasAttr<NSConsumesSelfAttr>()))
2200 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002202 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002203 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2204 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002205
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002206 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002207 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002208 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002209
2210 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2211 return false;
2212
David Blaikie4e4d0842012-03-11 07:00:24 +00002213 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002214 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2215 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002216 }
2217 return true;
2218}
2219
Douglas Gregorff310c72012-05-01 23:37:00 +00002220void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002221 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2222 // inside categories.
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00002223 if (ObjCCategoryDecl *
2224 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2225 if (!CD->IsClassExtension() && List->getBits() < 2)
2226 List->setBits(List->getBits()+1);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002227
Douglas Gregor44fae522012-01-25 00:19:56 +00002228 // If the list is empty, make it a singleton list.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002229 if (List->Method == nullptr) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002230 List->Method = Method;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002231 List->setNext(nullptr);
Stephen Hines176edba2014-12-01 14:53:08 -08002232 List->Count = Method->isDefined() ? 0 : 1;
Douglas Gregorff310c72012-05-01 23:37:00 +00002233 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002234 }
2235
2236 // We've seen a method with this name, see if we have already seen this type
2237 // signature.
2238 ObjCMethodList *Previous = List;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002239 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregorfc46be92013-06-21 00:20:25 +00002240 // If we are building a module, keep all of the methods.
2241 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2242 continue;
2243
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002244 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002245 continue;
Stephen Hines176edba2014-12-01 14:53:08 -08002246
Douglas Gregor44fae522012-01-25 00:19:56 +00002247 ObjCMethodDecl *PrevObjCMethod = List->Method;
2248
2249 // Propagate the 'defined' bit.
2250 if (Method->isDefined())
2251 PrevObjCMethod->setDefined(true);
Stephen Hines176edba2014-12-01 14:53:08 -08002252 else
2253 ++List->Count;
Douglas Gregor44fae522012-01-25 00:19:56 +00002254
2255 // If a method is deprecated, push it in the global pool.
2256 // This is used for better diagnostics.
2257 if (Method->isDeprecated()) {
2258 if (!PrevObjCMethod->isDeprecated())
2259 List->Method = Method;
2260 }
2261 // If new method is unavailable, push it into global pool
2262 // unless previous one is deprecated.
2263 if (Method->isUnavailable()) {
2264 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2265 List->Method = Method;
2266 }
2267
Douglas Gregorff310c72012-05-01 23:37:00 +00002268 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002269 }
2270
2271 // We have a new signature for an existing method - add it.
2272 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002273 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Stephen Hines176edba2014-12-01 14:53:08 -08002274 Previous->setNext(new (Mem) ObjCMethodList(Method, 0, nullptr));
Douglas Gregor44fae522012-01-25 00:19:56 +00002275}
2276
Sebastian Redldb9d2142010-08-02 23:18:59 +00002277/// \brief Read the contents of the method pool for a given selector from
2278/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002279void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002280 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002281 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002282}
2283
Douglas Gregorff310c72012-05-01 23:37:00 +00002284void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002285 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002286 // Ignore methods of invalid containers.
2287 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002288 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002289
Douglas Gregor0d266d62012-01-25 00:59:09 +00002290 if (ExternalSource)
2291 ReadMethodPool(Method->getSelector());
2292
Sebastian Redldb9d2142010-08-02 23:18:59 +00002293 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002294 if (Pos == MethodPool.end())
2295 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2296 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002297
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002298 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002299
Sebastian Redldb9d2142010-08-02 23:18:59 +00002300 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002301 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002302}
2303
John McCallf85e1932011-06-15 23:02:42 +00002304/// Determines if this is an "acceptable" loose mismatch in the global
2305/// method pool. This exists mostly as a hack to get around certain
2306/// global mismatches which we can't afford to make warnings / errors.
2307/// Really, what we want is a way to take a method out of the global
2308/// method pool.
2309static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2310 ObjCMethodDecl *other) {
2311 if (!chosen->isInstanceMethod())
2312 return false;
2313
2314 Selector sel = chosen->getSelector();
2315 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2316 return false;
2317
2318 // Don't complain about mismatches for -length if the method we
2319 // chose has an integral result type.
Stephen Hines651f13c2014-04-23 16:59:28 -07002320 return (chosen->getReturnType()->isIntegerType());
John McCallf85e1932011-06-15 23:02:42 +00002321}
2322
Stephen Hines176edba2014-12-01 14:53:08 -08002323bool Sema::CollectMultipleMethodsInGlobalPool(Selector Sel,
2324 SmallVectorImpl<ObjCMethodDecl*>& Methods,
2325 bool instance) {
2326 if (ExternalSource)
2327 ReadMethodPool(Sel);
2328
2329 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2330 if (Pos == MethodPool.end())
2331 return false;
2332 // Gather the non-hidden methods.
2333 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
2334 for (ObjCMethodList *M = &MethList; M; M = M->getNext())
2335 if (M->Method && !M->Method->isHidden())
2336 Methods.push_back(M->Method);
2337 return (Methods.size() > 1);
2338}
2339
2340bool Sema::AreMultipleMethodsInGlobalPool(Selector Sel,
2341 bool instance) {
2342 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2343 // Test for no method in the pool which should not trigger any warning by caller.
2344 if (Pos == MethodPool.end())
2345 return true;
2346 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
2347 return MethList.Count > 1;
2348}
2349
Sebastian Redldb9d2142010-08-02 23:18:59 +00002350ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002351 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002352 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002353 if (ExternalSource)
2354 ReadMethodPool(Sel);
2355
Sebastian Redldb9d2142010-08-02 23:18:59 +00002356 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002357 if (Pos == MethodPool.end())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002358 return nullptr;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002359
Douglas Gregorf0e00042013-01-16 18:47:38 +00002360 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002361 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelme7205c02013-08-10 12:33:24 +00002362 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002363 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregorf0e00042013-01-16 18:47:38 +00002364 if (M->Method && !M->Method->isHidden()) {
2365 // If we're not supposed to warn about mismatches, we're done.
2366 if (!warn)
2367 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Douglas Gregorf0e00042013-01-16 18:47:38 +00002369 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002370 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002371 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002372
2373 // If there aren't any visible methods, we're done.
2374 // FIXME: Recover if there are any known-but-hidden methods?
2375 if (Methods.empty())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002376 return nullptr;
Douglas Gregorf0e00042013-01-16 18:47:38 +00002377
2378 if (Methods.size() == 1)
2379 return Methods[0];
2380
2381 // We found multiple methods, so we may have to complain.
2382 bool issueDiagnostic = false, issueError = false;
2383
2384 // We support a warning which complains about *any* difference in
2385 // method signature.
2386 bool strictSelectorMatch =
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002387 receiverIdOrClass && warn &&
2388 !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin());
Douglas Gregorf0e00042013-01-16 18:47:38 +00002389 if (strictSelectorMatch) {
2390 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2391 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2392 issueDiagnostic = true;
2393 break;
2394 }
2395 }
2396 }
2397
2398 // If we didn't see any strict differences, we won't see any loose
2399 // differences. In ARC, however, we also need to check for loose
2400 // mismatches, because most of them are errors.
2401 if (!strictSelectorMatch ||
2402 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2403 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2404 // This checks if the methods differ in type mismatch.
2405 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2406 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2407 issueDiagnostic = true;
2408 if (getLangOpts().ObjCAutoRefCount)
2409 issueError = true;
2410 break;
2411 }
2412 }
2413
2414 if (issueDiagnostic) {
2415 if (issueError)
2416 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2417 else if (strictSelectorMatch)
2418 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2419 else
2420 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2421
2422 Diag(Methods[0]->getLocStart(),
2423 issueError ? diag::note_possibility : diag::note_using)
2424 << Methods[0]->getSourceRange();
2425 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2426 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2427 << Methods[I]->getSourceRange();
2428 }
2429 }
2430 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002431}
2432
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002433ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002434 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2435 if (Pos == MethodPool.end())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002436 return nullptr;
Sebastian Redldb9d2142010-08-02 23:18:59 +00002437
2438 GlobalMethods &Methods = Pos->second;
Stephen Hines651f13c2014-04-23 16:59:28 -07002439 for (const ObjCMethodList *Method = &Methods.first; Method;
2440 Method = Method->getNext())
2441 if (Method->Method && Method->Method->isDefined())
2442 return Method->Method;
2443
2444 for (const ObjCMethodList *Method = &Methods.second; Method;
2445 Method = Method->getNext())
2446 if (Method->Method && Method->Method->isDefined())
2447 return Method->Method;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002448 return nullptr;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002449}
2450
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002451static void
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002452HelperSelectorsForTypoCorrection(
2453 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2454 StringRef Typo, const ObjCMethodDecl * Method) {
2455 const unsigned MaxEditDistance = 1;
2456 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieu4fe96442013-06-06 02:22:29 +00002457 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002458
2459 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2460 if (MinPossibleEditDistance > 0 &&
2461 Typo.size() / MinPossibleEditDistance < 1)
2462 return;
2463 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2464 if (EditDistance > MaxEditDistance)
2465 return;
2466 if (EditDistance == BestEditDistance)
2467 BestMethod.push_back(Method);
2468 else if (EditDistance < BestEditDistance) {
2469 BestMethod.clear();
2470 BestMethod.push_back(Method);
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002471 }
2472}
2473
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002474static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2475 QualType ObjectType) {
2476 if (ObjectType.isNull())
2477 return true;
2478 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2479 return true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002480 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) !=
2481 nullptr;
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002482}
2483
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002484const ObjCMethodDecl *
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002485Sema::SelectorsForTypoCorrection(Selector Sel,
2486 QualType ObjectType) {
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002487 unsigned NumArgs = Sel.getNumArgs();
2488 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002489 bool ObjectIsId = true, ObjectIsClass = true;
2490 if (ObjectType.isNull())
2491 ObjectIsId = ObjectIsClass = false;
2492 else if (!ObjectType->isObjCObjectPointerType())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002493 return nullptr;
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002494 else if (const ObjCObjectPointerType *ObjCPtr =
2495 ObjectType->getAsObjCInterfacePointerType()) {
2496 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2497 ObjectIsId = ObjectIsClass = false;
2498 }
2499 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2500 ObjectIsClass = false;
2501 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2502 ObjectIsId = false;
2503 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002504 return nullptr;
2505
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002506 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2507 e = MethodPool.end(); b != e; b++) {
2508 // instance methods
2509 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2510 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002511 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2512 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002513 if (ObjectIsId)
2514 Methods.push_back(M->Method);
2515 else if (!ObjectIsClass &&
2516 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2517 Methods.push_back(M->Method);
2518 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002519 // class methods
2520 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2521 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002522 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2523 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002524 if (ObjectIsClass)
2525 Methods.push_back(M->Method);
2526 else if (!ObjectIsId &&
2527 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2528 Methods.push_back(M->Method);
2529 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002530 }
2531
2532 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2533 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2534 HelperSelectorsForTypoCorrection(SelectedMethods,
2535 Sel.getAsString(), Methods[i]);
2536 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002537 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr;
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002538}
2539
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002540/// DiagnoseDuplicateIvars -
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002541/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002542/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002543/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002544/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002545void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2546 ObjCInterfaceDecl *SID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002547 for (auto *Ivar : ID->ivars()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002548 if (Ivar->isInvalidDecl())
2549 continue;
2550 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2551 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2552 if (prevIvar) {
2553 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2554 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2555 Ivar->setInvalidDecl();
2556 }
2557 }
2558 }
2559}
2560
Erik Verbruggend64251f2011-12-06 09:25:23 +00002561Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2562 switch (CurContext->getDeclKind()) {
2563 case Decl::ObjCInterface:
2564 return Sema::OCK_Interface;
2565 case Decl::ObjCProtocol:
2566 return Sema::OCK_Protocol;
2567 case Decl::ObjCCategory:
2568 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2569 return Sema::OCK_ClassExtension;
2570 else
2571 return Sema::OCK_Category;
2572 case Decl::ObjCImplementation:
2573 return Sema::OCK_Implementation;
2574 case Decl::ObjCCategoryImpl:
2575 return Sema::OCK_CategoryImplementation;
2576
2577 default:
2578 return Sema::OCK_None;
2579 }
2580}
2581
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002582// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm0111e4d2013-07-17 21:14:35 +00002583Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahanian80f8aca2013-07-17 00:05:08 +00002584 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggend64251f2011-12-06 09:25:23 +00002585 if (getObjCContainerKind() == Sema::OCK_None)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002586 return nullptr;
Erik Verbruggend64251f2011-12-06 09:25:23 +00002587
2588 assert(AtEnd.isValid() && "Invalid location for '@end'");
2589
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002590 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2591 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002592
Mike Stump1eb44332009-09-09 15:08:12 +00002593 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002594 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2595 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002596 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002597
Steve Naroff0701bbb2009-01-08 17:28:14 +00002598 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2599 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2600 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2601
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002602 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002603 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002604 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002605
2606 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002607 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002608 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002609 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002610 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002611 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002612 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002613 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002614 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002615 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002616 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002617 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002618 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002619 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002620 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002621 if (!Context.getSourceManager().isInSystemHeader(
2622 Method->getLocation()))
2623 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2624 << Method->getDeclName();
2625 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2626 }
Chris Lattner4d391482007-12-12 07:09:47 +00002627 InsMap[Method->getSelector()] = Method;
2628 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002629 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002630 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002631 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002632 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002633 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002634 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002635 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002636 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002637 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002638 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002639 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002640 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002641 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002642 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002643 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002644 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002645 if (!Context.getSourceManager().isInSystemHeader(
2646 Method->getLocation()))
2647 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2648 << Method->getDeclName();
2649 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2650 }
Chris Lattner4d391482007-12-12 07:09:47 +00002651 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002652 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002653 }
2654 }
2655 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002656 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2657 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002658 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002659 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002660 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002661 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002662
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002663 if (C->IsClassExtension()) {
2664 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2665 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002666 }
Chris Lattner4d391482007-12-12 07:09:47 +00002667 }
Steve Naroff09c47192009-01-09 15:36:25 +00002668 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002669 if (CDecl->getIdentifier())
2670 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2671 // user-defined setter/getter. It also synthesizes setter/getter methods
2672 // and adds them to the DeclContext and global method pools.
Stephen Hines651f13c2014-04-23 16:59:28 -07002673 for (auto *I : CDecl->properties())
2674 ProcessPropertyDecl(I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002675 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002676 }
2677 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002678 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002679 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002680 // Any property declared in a class extension might have user
2681 // declared setter or getter in current class extension or one
2682 // of the other class extensions. Mark them as synthesized as
2683 // property will be synthesized when property with same name is
2684 // seen in the @implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07002685 for (const auto *Ext : IDecl->visible_extensions()) {
2686 for (const auto *Property : Ext->properties()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002687 // Skip over properties declared @dynamic
2688 if (const ObjCPropertyImplDecl *PIDecl
2689 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2690 if (PIDecl->getPropertyImplementation()
2691 == ObjCPropertyImplDecl::Dynamic)
2692 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002693
Stephen Hines651f13c2014-04-23 16:59:28 -07002694 for (const auto *Ext : IDecl->visible_extensions()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002695 if (ObjCMethodDecl *GetterMethod
2696 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002697 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002698 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002699 if (ObjCMethodDecl *SetterMethod
2700 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002701 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002702 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002703 }
2704 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002705 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002706 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002707 DiagnoseOwningPropertyGetterSynthesis(IC);
Stephen Hines651f13c2014-04-23 16:59:28 -07002708 DiagnoseUnusedBackingIvarInAccessor(S, IC);
2709 if (IDecl->hasDesignatedInitializers())
2710 DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
2711
Patrick Beardb2f68202012-04-06 18:12:22 +00002712 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002713 if (IDecl->getSuperClass() == nullptr) {
Patrick Beardb2f68202012-04-06 18:12:22 +00002714 // This class has no superclass, so check that it has been marked with
2715 // __attribute((objc_root_class)).
2716 if (!HasRootClassAttr) {
2717 SourceLocation DeclLoc(IDecl->getLocation());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002718 SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc));
Patrick Beardb2f68202012-04-06 18:12:22 +00002719 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2720 << IDecl->getIdentifier();
2721 // See if NSObject is in the current scope, and if it is, suggest
2722 // adding " : NSObject " to the class declaration.
2723 NamedDecl *IF = LookupSingleName(TUScope,
2724 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2725 DeclLoc, LookupOrdinaryName);
2726 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2727 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2728 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2729 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2730 } else {
2731 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2732 }
2733 }
2734 } else if (HasRootClassAttr) {
2735 // Complain that only root classes may have this attribute.
2736 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2737 }
2738
John McCall260611a2012-06-20 06:18:46 +00002739 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002740 while (IDecl->getSuperClass()) {
2741 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2742 IDecl = IDecl->getSuperClass();
2743 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002744 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002745 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002746 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002747 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002748 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002749 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002750
Chris Lattner4d391482007-12-12 07:09:47 +00002751 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002752 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002753 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002754 if (ObjCCategoryDecl *Cat
2755 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2756 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002757 }
2758 }
2759 }
Chris Lattner682bf922009-03-29 16:50:03 +00002760 if (isInterfaceDeclKind) {
2761 // Reject invalid vardecls.
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002762 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov18062392013-08-27 13:15:56 +00002763 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner682bf922009-03-29 16:50:03 +00002764 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2765 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002766 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002767 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002768 }
Chris Lattner682bf922009-03-29 16:50:03 +00002769 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002770 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002771 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002772
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002773 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov18062392013-08-27 13:15:56 +00002774 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002775 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2776 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002777 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2778 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002779
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002780 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002781 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002782}
2783
2784
2785/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2786/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002787static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002788CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002789 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002790}
2791
Douglas Gregor926df6c2011-06-11 01:09:30 +00002792/// \brief Check whether the declared result type of the given Objective-C
2793/// method declaration is compatible with the method's class.
2794///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002795static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002796CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2797 ObjCInterfaceDecl *CurrentClass) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002798 QualType ResultType = Method->getReturnType();
2799
Douglas Gregor926df6c2011-06-11 01:09:30 +00002800 // If an Objective-C method inherits its related result type, then its
2801 // declared result type must be compatible with its own class type. The
2802 // declared result type is compatible if:
2803 if (const ObjCObjectPointerType *ResultObjectType
2804 = ResultType->getAs<ObjCObjectPointerType>()) {
2805 // - it is id or qualified id, or
2806 if (ResultObjectType->isObjCIdType() ||
2807 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002808 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002809
2810 if (CurrentClass) {
2811 if (ObjCInterfaceDecl *ResultClass
2812 = ResultObjectType->getInterfaceDecl()) {
2813 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002814 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002815 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002816
2817 // - it is a superclass of the method's class type
2818 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002819 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002820 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002821 } else {
2822 // Any Objective-C pointer type might be acceptable for a protocol
2823 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002824 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002825 }
2826 }
2827
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002828 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002829}
2830
John McCall6c2c2502011-07-22 02:45:48 +00002831namespace {
2832/// A helper class for searching for methods which a particular method
2833/// overrides.
2834class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002835public:
John McCall6c2c2502011-07-22 02:45:48 +00002836 Sema &S;
2837 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002838 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002839 bool Recursive;
2840
2841public:
2842 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2843 Selector selector = method->getSelector();
2844
2845 // Bypass this search if we've never seen an instance/class method
2846 // with this selector before.
2847 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2848 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002849 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002850 S.ReadMethodPool(selector);
2851
2852 it = S.MethodPool.find(selector);
2853 if (it == S.MethodPool.end())
2854 return;
John McCall6c2c2502011-07-22 02:45:48 +00002855 }
2856 ObjCMethodList &list =
2857 method->isInstanceMethod() ? it->second.first : it->second.second;
2858 if (!list.Method) return;
2859
2860 ObjCContainerDecl *container
2861 = cast<ObjCContainerDecl>(method->getDeclContext());
2862
2863 // Prevent the search from reaching this container again. This is
2864 // important with categories, which override methods from the
2865 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002866 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2867 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002868 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2869 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002870 } else {
2871 searchFromContainer(container);
2872 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002873 }
John McCall6c2c2502011-07-22 02:45:48 +00002874
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002875 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002876 iterator begin() const { return Overridden.begin(); }
2877 iterator end() const { return Overridden.end(); }
2878
2879private:
2880 void searchFromContainer(ObjCContainerDecl *container) {
2881 if (container->isInvalidDecl()) return;
2882
2883 switch (container->getDeclKind()) {
2884#define OBJCCONTAINER(type, base) \
2885 case Decl::type: \
2886 searchFrom(cast<type##Decl>(container)); \
2887 break;
2888#define ABSTRACT_DECL(expansion)
2889#define DECL(type, base) \
2890 case Decl::type:
2891#include "clang/AST/DeclNodes.inc"
2892 llvm_unreachable("not an ObjC container!");
2893 }
2894 }
2895
2896 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002897 if (!protocol->hasDefinition())
2898 return;
2899
John McCall6c2c2502011-07-22 02:45:48 +00002900 // A method in a protocol declaration overrides declarations from
2901 // referenced ("parent") protocols.
2902 search(protocol->getReferencedProtocols());
2903 }
2904
2905 void searchFrom(ObjCCategoryDecl *category) {
2906 // A method in a category declaration overrides declarations from
2907 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002908 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002909 search(category->getReferencedProtocols());
2910 }
2911
2912 void searchFrom(ObjCCategoryImplDecl *impl) {
2913 // A method in a category definition that has a category
2914 // declaration overrides declarations from the category
2915 // declaration.
2916 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2917 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002918 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2919 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002920
2921 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002922 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2923 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002924 }
2925 }
2926
2927 void searchFrom(ObjCInterfaceDecl *iface) {
2928 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002929 if (!iface->hasDefinition())
2930 return;
2931
John McCall6c2c2502011-07-22 02:45:48 +00002932 // - categories,
Stephen Hines651f13c2014-04-23 16:59:28 -07002933 for (auto *Cat : iface->known_categories())
2934 search(Cat);
John McCall6c2c2502011-07-22 02:45:48 +00002935
2936 // - the super class, and
2937 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2938 search(super);
2939
2940 // - any referenced protocols.
2941 search(iface->getReferencedProtocols());
2942 }
2943
2944 void searchFrom(ObjCImplementationDecl *impl) {
2945 // A method in a class implementation overrides declarations from
2946 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002947 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2948 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002949 }
2950
2951
2952 void search(const ObjCProtocolList &protocols) {
2953 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2954 i != e; ++i)
2955 search(*i);
2956 }
2957
2958 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002959 // Check for a method in this container which matches this selector.
2960 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002961 Method->isInstanceMethod(),
2962 /*AllowHidden=*/true);
John McCall6c2c2502011-07-22 02:45:48 +00002963
2964 // If we find one, record it and bail out.
2965 if (meth) {
2966 Overridden.insert(meth);
2967 return;
2968 }
2969
2970 // Otherwise, search for methods that a hypothetical method here
2971 // would have overridden.
2972
2973 // Note that we're now in a recursive case.
2974 Recursive = true;
2975
2976 searchFromContainer(container);
2977 }
2978};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002979}
2980
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002981void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2982 ObjCInterfaceDecl *CurrentClass,
2983 ResultTypeCompatibilityKind RTC) {
2984 // Search for overridden methods and merge information down from them.
2985 OverrideSearch overrides(*this, ObjCMethod);
2986 // Keep track if the method overrides any method in the class's base classes,
2987 // its protocols, or its categories' protocols; we will keep that info
2988 // in the ObjCMethodDecl.
2989 // For this info, a method in an implementation is not considered as
2990 // overriding the same method in the interface or its categories.
2991 bool hasOverriddenMethodsInBaseOrProtocol = false;
2992 for (OverrideSearch::iterator
2993 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2994 ObjCMethodDecl *overridden = *i;
2995
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00002996 if (!hasOverriddenMethodsInBaseOrProtocol) {
2997 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2998 CurrentClass != overridden->getClassInterface() ||
2999 overridden->isOverriding()) {
3000 hasOverriddenMethodsInBaseOrProtocol = true;
3001
3002 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
3003 // OverrideSearch will return as "overridden" the same method in the
3004 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
3005 // check whether a category of a base class introduced a method with the
3006 // same selector, after the interface method declaration.
3007 // To avoid unnecessary lookups in the majority of cases, we use the
3008 // extra info bits in GlobalMethodPool to check whether there were any
3009 // category methods with this selector.
3010 GlobalMethodPool::iterator It =
3011 MethodPool.find(ObjCMethod->getSelector());
3012 if (It != MethodPool.end()) {
3013 ObjCMethodList &List =
3014 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
3015 unsigned CategCount = List.getBits();
3016 if (CategCount > 0) {
3017 // If the method is in a category we'll do lookup if there were at
3018 // least 2 category methods recorded, otherwise only one will do.
3019 if (CategCount > 1 ||
3020 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3021 OverrideSearch overrides(*this, overridden);
3022 for (OverrideSearch::iterator
3023 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3024 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00003025 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3026 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00003027 hasOverriddenMethodsInBaseOrProtocol = true;
3028 overridden->setOverriding(true);
3029 break;
3030 }
3031 }
3032 }
3033 }
3034 }
3035 }
3036 }
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003037
3038 // Propagate down the 'related result type' bit from overridden methods.
3039 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3040 ObjCMethod->SetRelatedResultType();
3041
3042 // Then merge the declarations.
3043 mergeObjCMethodDecls(ObjCMethod, overridden);
3044
3045 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3046 continue; // Conflicting properties are detected elsewhere.
3047
3048 // Check for overriding methods
3049 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3050 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3051 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3052 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3053
3054 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00003055 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3056 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003057 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3058 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00003059 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3060 PrevE = overridden->param_end();
3061 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003062 assert(PrevI != overridden->param_end() && "Param mismatch");
3063 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3064 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3065 // If type of argument of method in this class does not match its
3066 // respective argument type in the super class method, issue warning;
3067 if (!Context.typesAreCompatible(T1, T2)) {
3068 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3069 << T1 << T2;
3070 Diag(overridden->getLocation(), diag::note_previous_declaration);
3071 break;
3072 }
3073 }
3074 }
3075 }
3076
3077 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3078}
3079
John McCalld226f652010-08-21 09:40:31 +00003080Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003081 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00003082 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003083 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00003084 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003085 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00003086 Selector Sel,
3087 // optional arguments. The number of types/arguments is obtained
3088 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00003089 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003090 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00003091 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003092 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003093 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003094 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003095 Diag(MethodLoc, diag::error_missing_method_context);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003096 return nullptr;
Steve Naroffda323ad2008-02-29 21:48:07 +00003097 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003098 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3099 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00003100 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00003101
Douglas Gregore97179c2011-09-08 01:46:34 +00003102 bool HasRelatedResultType = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003103 TypeSourceInfo *ReturnTInfo = nullptr;
Steve Naroffccef3712009-02-20 22:59:16 +00003104 if (ReturnType) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003105 resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Eli Friedmanddb5a392013-06-14 21:14:10 +00003107 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003108 return nullptr;
Eli Friedmanddb5a392013-06-14 21:14:10 +00003109
Douglas Gregore97179c2011-09-08 01:46:34 +00003110 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003111 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003112 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00003113 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003114 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003115 }
Mike Stump1eb44332009-09-09 15:08:12 +00003116
Stephen Hines651f13c2014-04-23 16:59:28 -07003117 ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create(
3118 Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext,
3119 MethodType == tok::minus, isVariadic,
3120 /*isPropertyAccessor=*/false,
3121 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
3122 MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional
3123 : ObjCMethodDecl::Required,
3124 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00003125
Chris Lattner5f9e2722011-07-23 10:55:15 +00003126 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00003127
Chris Lattner7db638d2009-04-11 19:42:43 +00003128 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00003129 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00003130 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00003131
David Blaikie7247c882013-05-15 07:37:26 +00003132 if (!ArgInfo[i].Type) {
John McCall58e46772009-10-23 21:48:59 +00003133 ArgType = Context.getObjCIdType();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003134 DI = nullptr;
Chris Lattnere294d3f2009-04-11 18:57:04 +00003135 } else {
John McCall58e46772009-10-23 21:48:59 +00003136 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00003137 }
Mike Stump1eb44332009-09-09 15:08:12 +00003138
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003139 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3140 LookupOrdinaryName, ForRedeclaration);
3141 LookupName(R, S);
3142 if (R.isSingleResult()) {
3143 NamedDecl *PrevDecl = R.getFoundDecl();
3144 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003145 Diag(ArgInfo[i].NameLoc,
3146 (MethodDefinition ? diag::warn_method_param_redefinition
3147 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003148 << ArgInfo[i].Name;
3149 Diag(PrevDecl->getLocation(),
3150 diag::note_previous_declaration);
3151 }
3152 }
3153
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003154 SourceLocation StartLoc = DI
3155 ? DI->getTypeLoc().getBeginLoc()
3156 : ArgInfo[i].NameLoc;
3157
John McCall81ef3e62011-04-23 02:46:06 +00003158 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3159 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003160 ArgType, DI, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00003161
John McCall70798862011-05-02 00:30:12 +00003162 Param->setObjCMethodScopeInfo(i);
3163
Chris Lattner0ed844b2008-04-04 06:12:32 +00003164 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00003165 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00003166
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00003167 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003168 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003169
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00003170 if (Param->hasAttr<BlocksAttr>()) {
3171 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3172 Param->setInvalidDecl();
3173 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003174 S->AddDecl(Param);
3175 IdResolver.AddDecl(Param);
3176
Chris Lattner0ed844b2008-04-04 06:12:32 +00003177 Params.push_back(Param);
3178 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003179
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003180 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00003181 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003182 QualType ArgType = Param->getType();
3183 if (ArgType.isNull())
3184 ArgType = Context.getObjCIdType();
3185 else
3186 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00003187 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedmanddb5a392013-06-14 21:14:10 +00003188
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003189 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003190 Params.push_back(Param);
3191 }
3192
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003193 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003194 ObjCMethod->setObjCDeclQualifier(
3195 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00003196
3197 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003198 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00003199
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003200 // Add the method now.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003201 const ObjCMethodDecl *PrevMethod = nullptr;
John McCall6c2c2502011-07-22 02:45:48 +00003202 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00003203 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003204 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3205 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003206 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003207 PrevMethod = ImpDecl->getClassMethod(Sel);
3208 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003209 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00003210
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003211 ObjCMethodDecl *IMD = nullptr;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00003212 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3213 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3214 ObjCMethod->isInstanceMethod());
Fariborz Jahanian38b3bd82013-07-09 22:02:20 +00003215 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3216 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3217 // merge the attribute into implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07003218 ObjCMethod->addAttr(ObjCRequiresSuperAttr::CreateImplicit(Context,
3219 ObjCMethod->getLocation()));
Fariborz Jahanian38b3bd82013-07-09 22:02:20 +00003220 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003221 if (isa<ObjCCategoryImplDecl>(ImpDecl)) {
3222 ObjCMethodFamily family =
3223 ObjCMethod->getSelector().getMethodFamily();
3224 if (family == OMF_dealloc && IMD && IMD->isOverriding())
3225 Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
Ted Kremenek3306ec12012-02-27 22:55:11 +00003226 << ObjCMethod->getDeclName();
Fariborz Jahanianec236782011-12-06 00:02:41 +00003227 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003228 } else {
3229 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003230 }
John McCall6c2c2502011-07-22 02:45:48 +00003231
Chris Lattner4d391482007-12-12 07:09:47 +00003232 if (PrevMethod) {
3233 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003234 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003235 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003236 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanianfbff0c42013-05-13 17:27:00 +00003237 ObjCMethod->setInvalidDecl();
3238 return ObjCMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00003239 }
John McCall54abf7d2009-11-04 02:18:39 +00003240
Douglas Gregor926df6c2011-06-11 01:09:30 +00003241 // If this Objective-C method does not have a related result type, but we
3242 // are allowed to infer related result types, try to do so based on the
3243 // method family.
3244 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3245 if (!CurrentClass) {
3246 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3247 CurrentClass = Cat->getClassInterface();
3248 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3249 CurrentClass = Impl->getClassInterface();
3250 else if (ObjCCategoryImplDecl *CatImpl
3251 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3252 CurrentClass = CatImpl->getClassInterface();
3253 }
John McCall6c2c2502011-07-22 02:45:48 +00003254
Douglas Gregore97179c2011-09-08 01:46:34 +00003255 ResultTypeCompatibilityKind RTC
3256 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003257
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003258 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003259
John McCallf85e1932011-06-15 23:02:42 +00003260 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003261 if (getLangOpts().ObjCAutoRefCount)
John McCallb8463812013-04-04 01:38:37 +00003262 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCallf85e1932011-06-15 23:02:42 +00003263
Douglas Gregore97179c2011-09-08 01:46:34 +00003264 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003265 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003266 !ObjCMethod->hasRelatedResultType() &&
3267 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003268 bool InferRelatedResultType = false;
3269 switch (ObjCMethod->getMethodFamily()) {
3270 case OMF_None:
3271 case OMF_copy:
3272 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003273 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003274 case OMF_mutableCopy:
3275 case OMF_release:
3276 case OMF_retainCount:
Stephen Hines176edba2014-12-01 14:53:08 -08003277 case OMF_initialize:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003278 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003279 break;
3280
3281 case OMF_alloc:
3282 case OMF_new:
3283 InferRelatedResultType = ObjCMethod->isClassMethod();
3284 break;
3285
3286 case OMF_init:
3287 case OMF_autorelease:
3288 case OMF_retain:
3289 case OMF_self:
3290 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3291 break;
3292 }
3293
John McCall6c2c2502011-07-22 02:45:48 +00003294 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003295 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003296 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003297
3298 ActOnDocumentableDecl(ObjCMethod);
3299
John McCalld226f652010-08-21 09:40:31 +00003300 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003301}
3302
Chris Lattnercc98eac2008-12-17 07:13:27 +00003303bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003304 // Following is also an error. But it is caused by a missing @end
3305 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003306 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003307 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003308
3309 // If we switched context to translation unit while we are still lexically in
3310 // an objc container, it means the parser missed emitting an error.
3311 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3312 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003313
Anders Carlsson15281452008-11-04 16:57:32 +00003314 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3315 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003316
Anders Carlsson15281452008-11-04 16:57:32 +00003317 return true;
3318}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003319
James Dennett1dfbd922012-06-14 21:40:34 +00003320/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003321/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003322void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003323 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003324 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003325 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003326 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003327 if (!Class) {
3328 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3329 return;
3330 }
John McCall260611a2012-06-20 06:18:46 +00003331 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003332 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3333 return;
3334 }
Mike Stump1eb44332009-09-09 15:08:12 +00003335
Chris Lattnercc98eac2008-12-17 07:13:27 +00003336 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003337 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003338 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003339 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003340 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003341 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003342 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003343 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3344 /*FIXME: StartL=*/ID->getLocation(),
3345 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003346 ID->getIdentifier(), ID->getType(),
3347 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003348 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003349 }
Mike Stump1eb44332009-09-09 15:08:12 +00003350
Chris Lattnercc98eac2008-12-17 07:13:27 +00003351 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003352 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003353 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003354 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003355 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003356 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003357 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003358 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003359 }
3360}
3361
Douglas Gregor160b5632010-04-26 17:32:49 +00003362/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003363VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3364 SourceLocation StartLoc,
3365 SourceLocation IdLoc,
3366 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003367 bool Invalid) {
3368 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3369 // duration shall not be qualified by an address-space qualifier."
3370 // Since all parameters have automatic store duration, they can not have
3371 // an address space.
3372 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003373 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003374 Invalid = true;
3375 }
3376
3377 // An @catch parameter must be an unqualified object pointer type;
3378 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3379 if (Invalid) {
3380 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003381 } else if (T->isDependentType()) {
3382 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003383 } else if (!T->isObjCObjectPointerType()) {
3384 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003385 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003386 } else if (T->isObjCQualifiedIdType()) {
3387 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003388 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003389 }
3390
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003391 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003392 T, TInfo, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003393 New->setExceptionVariable(true);
3394
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003395 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003396 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003397 Invalid = true;
3398
Douglas Gregor160b5632010-04-26 17:32:49 +00003399 if (Invalid)
3400 New->setInvalidDecl();
3401 return New;
3402}
3403
John McCalld226f652010-08-21 09:40:31 +00003404Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003405 const DeclSpec &DS = D.getDeclSpec();
3406
3407 // We allow the "register" storage class on exception variables because
3408 // GCC did, but we drop it completely. Any other storage class is an error.
3409 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3410 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3411 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithec642442013-04-12 22:46:28 +00003412 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003413 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithec642442013-04-12 22:46:28 +00003414 << DeclSpec::getSpecifierName(SCS);
3415 }
3416 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3417 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3418 diag::err_invalid_thread)
3419 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregor160b5632010-04-26 17:32:49 +00003420 D.getMutableDeclSpec().ClearStorageClassSpecs();
3421
Richard Smithc7f81162013-03-18 22:52:47 +00003422 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregor160b5632010-04-26 17:32:49 +00003423
3424 // Check that there are no default arguments inside the type of this
3425 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003426 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003427 CheckExtraCXXDefaultArguments(D);
3428
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003429 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003430 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003431
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003432 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3433 D.getSourceRange().getBegin(),
3434 D.getIdentifierLoc(),
3435 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003436 D.isInvalidType());
3437
3438 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3439 if (D.getCXXScopeSpec().isSet()) {
3440 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3441 << D.getCXXScopeSpec().getRange();
3442 New->setInvalidDecl();
3443 }
3444
3445 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003446 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003447 if (D.getIdentifier())
3448 IdResolver.AddDecl(New);
3449
3450 ProcessDeclAttributes(S, New, D);
3451
3452 if (New->hasAttr<BlocksAttr>())
3453 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003454 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003455}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003456
3457/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003458/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003459void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003460 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003461 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3462 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003463 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003464 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003465 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003466 }
3467}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003468
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003469void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003470 // Load referenced selectors from the external source.
3471 if (ExternalSource) {
3472 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3473 ExternalSource->ReadReferencedSelectors(Sels);
3474 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3475 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3476 }
3477
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003478 // Warning will be issued only when selector table is
3479 // generated (which means there is at lease one implementation
3480 // in the TU). This is to match gcc's behavior.
3481 if (ReferencedSelectors.empty() ||
3482 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003483 return;
3484 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3485 ReferencedSelectors.begin(),
3486 E = ReferencedSelectors.end(); S != E; ++S) {
3487 Selector Sel = (*S).first;
3488 if (!LookupImplementedMethodInGlobalPool(Sel))
3489 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3490 }
3491 return;
3492}
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003493
3494ObjCIvarDecl *
3495Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3496 const ObjCPropertyDecl *&PDecl) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07003497 if (Method->isClassMethod())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003498 return nullptr;
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003499 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3500 if (!IDecl)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003501 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07003502 Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true,
3503 /*shallowCategoryLookup=*/false,
3504 /*followSuper=*/false);
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003505 if (!Method || !Method->isPropertyAccessor())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003506 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07003507 if ((PDecl = Method->findPropertyDecl()))
3508 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) {
3509 // property backing ivar must belong to property's class
3510 // or be a private ivar in class's implementation.
3511 // FIXME. fix the const-ness issue.
3512 IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable(
3513 IV->getIdentifier());
3514 return IV;
3515 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003516 return nullptr;
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003517}
3518
Stephen Hines651f13c2014-04-23 16:59:28 -07003519namespace {
3520 /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property
3521 /// accessor references the backing ivar.
3522 class UnusedBackingIvarChecker :
3523 public DataRecursiveASTVisitor<UnusedBackingIvarChecker> {
3524 public:
3525 Sema &S;
3526 const ObjCMethodDecl *Method;
3527 const ObjCIvarDecl *IvarD;
3528 bool AccessedIvar;
3529 bool InvokedSelfMethod;
3530
3531 UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method,
3532 const ObjCIvarDecl *IvarD)
3533 : S(S), Method(Method), IvarD(IvarD),
3534 AccessedIvar(false), InvokedSelfMethod(false) {
3535 assert(IvarD);
3536 }
3537
3538 bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
3539 if (E->getDecl() == IvarD) {
3540 AccessedIvar = true;
3541 return false;
3542 }
3543 return true;
3544 }
3545
3546 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
3547 if (E->getReceiverKind() == ObjCMessageExpr::Instance &&
3548 S.isSelfExpr(E->getInstanceReceiver(), Method)) {
3549 InvokedSelfMethod = true;
3550 }
3551 return true;
3552 }
3553 };
3554}
3555
3556void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S,
3557 const ObjCImplementationDecl *ImplD) {
3558 if (S->hasUnrecoverableErrorOccurred())
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003559 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07003560
3561 for (const auto *CurMethod : ImplD->instance_methods()) {
3562 unsigned DIAG = diag::warn_unused_property_backing_ivar;
3563 SourceLocation Loc = CurMethod->getLocation();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003564 if (Diags.isIgnored(DIAG, Loc))
Stephen Hines651f13c2014-04-23 16:59:28 -07003565 continue;
3566
3567 const ObjCPropertyDecl *PDecl;
3568 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3569 if (!IV)
3570 continue;
3571
3572 UnusedBackingIvarChecker Checker(*this, CurMethod, IV);
3573 Checker.TraverseStmt(CurMethod->getBody());
3574 if (Checker.AccessedIvar)
3575 continue;
3576
3577 // Do not issue this warning if backing ivar is used somewhere and accessor
3578 // implementation makes a self call. This is to prevent false positive in
3579 // cases where the ivar is accessed by another method that the accessor
3580 // delegates to.
3581 if (!IV->isReferenced() || !Checker.InvokedSelfMethod) {
3582 Diag(Loc, DIAG) << IV;
3583 Diag(PDecl->getLocation(), diag::note_property_declare);
3584 }
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003585 }
3586}