blob: dc47ce966f71a1426a906c6a5c91a89d85d07a4e [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 Hines0e2c34f2015-03-23 12:09:02 -0700620 ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end());
Fariborz Jahaniana924f842013-09-25 19:36:32 +0000621 }
622}
623
Richard Smithde01b7a2012-08-08 23:32:13 +0000624/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000625/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000626Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
627 IdentifierInfo *AliasName,
628 SourceLocation AliasLocation,
629 IdentifierInfo *ClassName,
630 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000631 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000632 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000633 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000634 if (ADecl) {
Eli Friedman104f96b2013-06-21 01:49:53 +0000635 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000636 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700637 return nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +0000638 }
639 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000640 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000641 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000642 if (const TypedefNameDecl *TDecl =
643 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000644 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000645 if (T->isObjCObjectType()) {
646 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000647 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000648 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000649 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000650 }
651 }
652 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000653 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700654 if (!CDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000655 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000656 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000657 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700658 return nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +0000659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000661 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000662 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000663 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Anders Carlsson15281452008-11-04 16:57:32 +0000665 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000666 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000667
John McCalld226f652010-08-21 09:40:31 +0000668 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000669}
670
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000671bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000672 IdentifierInfo *PName,
673 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000674 const ObjCList<ObjCProtocolDecl> &PList) {
675
676 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000677 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
678 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000679 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
680 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000681 if (PDecl->getIdentifier() == PName) {
682 Diag(Ploc, diag::err_protocol_has_circular_dependency);
683 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000684 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000685 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000686
687 if (!PDecl->hasDefinition())
688 continue;
689
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000690 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
691 PDecl->getLocation(), PDecl->getReferencedProtocols()))
692 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000693 }
694 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000695 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000696}
697
John McCalld226f652010-08-21 09:40:31 +0000698Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000699Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
700 IdentifierInfo *ProtocolName,
701 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000702 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000703 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000704 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000705 SourceLocation EndProtoLoc,
706 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000707 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000708 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000709 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000710 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
711 ForRedeclaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700712 ObjCProtocolDecl *PDecl = nullptr;
713 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) {
Douglas Gregor27c6da22012-01-01 20:30:41 +0000714 // If we already have a definition, complain.
715 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
716 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Douglas Gregor27c6da22012-01-01 20:30:41 +0000718 // Create a new protocol that is completely distinct from previous
719 // declarations, and do not make this protocol available for name lookup.
720 // That way, we'll end up completely ignoring the duplicate.
721 // FIXME: Can we turn this into an error?
722 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
723 ProtocolLoc, AtProtoInterfaceLoc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700724 /*PrevDecl=*/nullptr);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000725 PDecl->startDefinition();
726 } else {
727 if (PrevDecl) {
728 // Check for circular dependencies among protocol declarations. This can
729 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000730 ObjCList<ObjCProtocolDecl> PList;
731 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
732 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000733 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000734 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000735
736 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000737 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000738 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000739 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000740
Douglas Gregor6e378de2009-04-23 23:18:26 +0000741 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000742 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000743 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000744
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000745 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000746 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000747
748 // Merge attributes from previous declarations.
749 if (PrevDecl)
750 mergeDeclAttributes(PDecl, PrevDecl);
751
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000752 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000753 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000754 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000755 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
758 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000759 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000760}
761
Stephen Hines651f13c2014-04-23 16:59:28 -0700762static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl,
763 ObjCProtocolDecl *&UndefinedProtocol) {
764 if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) {
765 UndefinedProtocol = PDecl;
766 return true;
767 }
768
769 for (auto *PI : PDecl->protocols())
770 if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) {
771 UndefinedProtocol = PI;
772 return true;
773 }
774 return false;
775}
776
Chris Lattner4d391482007-12-12 07:09:47 +0000777/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000778/// issues an error if they are not declared. It returns list of
779/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000780void
Chris Lattnere13b9592008-07-26 04:03:38 +0000781Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000782 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000783 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000784 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000785 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000786 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
787 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000788 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000789 TypoCorrection Corrected = CorrectTypo(
790 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Stephen Hines176edba2014-12-01 14:53:08 -0800791 LookupObjCProtocolName, TUScope, nullptr,
792 llvm::make_unique<DeclFilterCCC<ObjCProtocolDecl>>(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700793 CTK_ErrorRecovery);
Richard Smith2d670972013-08-17 00:46:16 +0000794 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
795 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
796 << ProtocolId[i].first);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000797 }
798
799 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000800 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000801 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000802 continue;
803 }
Fariborz Jahanian3c9a0242013-04-09 17:52:29 +0000804 // If this is a forward protocol declaration, get its definition.
805 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
806 PDecl = PDecl->getDefinition();
807
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000808 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000809
810 // If this is a forward declaration and we are supposed to warn in this
811 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000812 // FIXME: Recover nicely in the hidden case.
Stephen Hines651f13c2014-04-23 16:59:28 -0700813 ObjCProtocolDecl *UndefinedProtocol;
814
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000815 if (WarnOnDeclarations &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700816 NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000817 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000818 << ProtocolId[i].first;
Stephen Hines651f13c2014-04-23 16:59:28 -0700819 Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined)
820 << UndefinedProtocol;
821 }
John McCalld226f652010-08-21 09:40:31 +0000822 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000823 }
824}
825
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000826/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000827/// a class method in its extension.
828///
Mike Stump1eb44332009-09-09 15:08:12 +0000829void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000830 ObjCInterfaceDecl *ID) {
831 if (!ID)
832 return; // Possibly due to previous error
833
834 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Stephen Hines651f13c2014-04-23 16:59:28 -0700835 for (auto *MD : ID->methods())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000836 MethodMap[MD->getSelector()] = MD;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000837
838 if (MethodMap.empty())
839 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700840 for (const auto *Method : CAT->methods()) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000841 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
Stephen Hines651f13c2014-04-23 16:59:28 -0700842 if (PrevMethod &&
843 (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) &&
844 !MatchTwoMethodDeclarations(Method, PrevMethod)) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000845 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
846 << Method->getDeclName();
847 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
848 }
849 }
850}
851
James Dennett1dfbd922012-06-14 21:40:34 +0000852/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000853Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000854Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000855 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000856 unsigned NumElts,
857 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000858 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000859 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000860 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000861 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
862 ForRedeclaration);
863 ObjCProtocolDecl *PDecl
864 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
865 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000866 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000867
868 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000869 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000870
Douglas Gregor3937f872012-01-01 20:33:24 +0000871 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000872 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000873
874 if (PrevDecl)
875 mergeDeclAttributes(PDecl, PrevDecl);
876
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000877 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Rafael Espindola4549d7f2013-07-09 12:05:01 +0000880 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +0000881}
882
John McCalld226f652010-08-21 09:40:31 +0000883Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000884ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
885 IdentifierInfo *ClassName, SourceLocation ClassLoc,
886 IdentifierInfo *CategoryName,
887 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000888 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000889 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000890 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000891 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000892 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000893 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000894
895 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000896
897 if (!IDecl
898 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000899 diag::err_category_forward_interface,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700900 CategoryName == nullptr)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000901 // Create an invalid ObjCCategoryDecl to serve as context for
902 // the enclosing method declarations. We mark the decl invalid
903 // to make it clear that this isn't a valid AST.
904 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000905 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000906 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000907 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000908
909 if (!IDecl)
910 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000911 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000912 }
913
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000914 if (!CategoryName && IDecl->getImplementation()) {
915 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
916 Diag(IDecl->getImplementation()->getLocation(),
917 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000918 }
919
Fariborz Jahanian25760612010-02-15 21:55:26 +0000920 if (CategoryName) {
921 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000922 if (ObjCCategoryDecl *Previous
923 = IDecl->FindCategoryDeclaration(CategoryName)) {
924 // Class extensions can be declared multiple times, categories cannot.
925 Diag(CategoryLoc, diag::warn_dup_category_def)
926 << ClassName << CategoryName;
927 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000928 }
929 }
Chris Lattner70f19542009-02-16 21:26:43 +0000930
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000931 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
932 ClassLoc, CategoryLoc, CategoryName, IDecl);
933 // FIXME: PushOnScopeChains?
934 CurContext->addDecl(CDecl);
935
Chris Lattner4d391482007-12-12 07:09:47 +0000936 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000937 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000938 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000939 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000940 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000941 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000942 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Anders Carlsson15281452008-11-04 16:57:32 +0000945 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000946 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000947}
948
949/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000950/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000951/// object.
John McCalld226f652010-08-21 09:40:31 +0000952Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000953 SourceLocation AtCatImplLoc,
954 IdentifierInfo *ClassName, SourceLocation ClassLoc,
955 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000956 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700957 ObjCCategoryDecl *CatIDecl = nullptr;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000958 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000959 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
960 if (!CatIDecl) {
961 // Category @implementation with no corresponding @interface.
962 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000963 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
964 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000965 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000966 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000967 }
968 }
969
Mike Stump1eb44332009-09-09 15:08:12 +0000970 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000971 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000972 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000973 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000974 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000975 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000976 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000977 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
978 diag::err_undef_interface)) {
979 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000980 }
Chris Lattner4d391482007-12-12 07:09:47 +0000981
Douglas Gregord0434102009-01-09 00:49:46 +0000982 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000983 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000984
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000985 // If the interface is deprecated/unavailable, warn/error about it.
986 if (IDecl)
987 DiagnoseUseOfDecl(IDecl, ClassLoc);
988
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000989 /// Check that CatName, category name, is not used in another implementation.
990 if (CatIDecl) {
991 if (CatIDecl->getImplementation()) {
992 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
993 << CatName;
994 Diag(CatIDecl->getImplementation()->getLocation(),
995 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000996 CDecl->setInvalidDecl();
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000997 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000998 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000999 // Warn on implementating category of deprecated class under
1000 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001001 DiagnoseObjCImplementedDeprecations(*this,
1002 dyn_cast<NamedDecl>(IDecl),
1003 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001004 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Anders Carlsson15281452008-11-04 16:57:32 +00001007 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001008 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001009}
1010
John McCalld226f652010-08-21 09:40:31 +00001011Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +00001012 SourceLocation AtClassImplLoc,
1013 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001014 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +00001015 SourceLocation SuperClassLoc) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001016 ObjCInterfaceDecl *IDecl = nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +00001017 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001018 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +00001019 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
1020 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001021 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001022 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001023 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001024 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +00001025 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1026 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +00001027 } else {
Richard Smith2d670972013-08-17 00:46:16 +00001028 // We did not find anything with the name ClassName; try to correct for
Douglas Gregor95ff7422010-01-04 17:27:12 +00001029 // typos in the class name.
Stephen Hines176edba2014-12-01 14:53:08 -08001030 TypoCorrection Corrected = CorrectTypo(
1031 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
1032 nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(), CTK_NonError);
Richard Smith2d670972013-08-17 00:46:16 +00001033 if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1034 // Suggest the (potentially) correct interface name. Don't provide a
1035 // code-modification hint or use the typo name for recovery, because
1036 // this is just a warning. The program may actually be correct.
1037 diagnoseTypo(Corrected,
1038 PDiag(diag::warn_undef_interface_suggest) << ClassName,
1039 /*ErrorRecovery*/false);
Douglas Gregor95ff7422010-01-04 17:27:12 +00001040 } else {
1041 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1042 }
Chris Lattner4d391482007-12-12 07:09:47 +00001043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattner4d391482007-12-12 07:09:47 +00001045 // Check that super class name is valid class name
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001046 ObjCInterfaceDecl *SDecl = nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +00001047 if (SuperClassname) {
1048 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +00001049 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1050 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001052 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1053 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001054 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +00001055 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001056 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +00001057 if (SDecl && !SDecl->hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001058 SDecl = nullptr;
Chris Lattner4d391482007-12-12 07:09:47 +00001059 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +00001060 Diag(SuperClassLoc, diag::err_undef_superclass)
1061 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001062 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001063 // This implementation and its interface do not have the same
1064 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001065 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001066 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001067 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001068 }
1069 }
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner4d391482007-12-12 07:09:47 +00001072 if (!IDecl) {
1073 // Legacy case of @implementation with no corresponding @interface.
1074 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001075
Mike Stump390b4cc2009-05-16 07:39:55 +00001076 // FIXME: Do we support attributes on the @implementation? If so we should
1077 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001078 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001079 ClassName, /*PrevDecl=*/nullptr, ClassLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001080 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001081 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001082 if (SDecl) {
1083 IDecl->setSuperClass(SDecl);
1084 IDecl->setSuperClassLoc(SuperClassLoc);
1085 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1086 } else {
1087 IDecl->setEndOfDefinitionLoc(ClassLoc);
1088 }
1089
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001090 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001091 } else {
1092 // Mark the interface as being completed, even if it was just as
1093 // @class ....;
1094 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001095 if (!IDecl->hasDefinition())
1096 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
1099 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001100 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001101 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Anders Carlsson15281452008-11-04 16:57:32 +00001103 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001104 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner4d391482007-12-12 07:09:47 +00001106 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001107 if (IDecl->getImplementation()) {
1108 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001109 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001110 Diag(IDecl->getImplementation()->getLocation(),
1111 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +00001112 IMPDecl->setInvalidDecl();
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001113 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001114 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001115 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001116 // Warn on implementating deprecated class under
1117 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001118 DiagnoseObjCImplementedDeprecations(*this,
1119 dyn_cast<NamedDecl>(IDecl),
1120 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001121 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001122 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001123}
1124
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001125Sema::DeclGroupPtrTy
1126Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1127 SmallVector<Decl *, 64> DeclsInGroup;
1128 DeclsInGroup.reserve(Decls.size() + 1);
1129
1130 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1131 Decl *Dcl = Decls[i];
1132 if (!Dcl)
1133 continue;
1134 if (Dcl->getDeclContext()->isFileContext())
1135 Dcl->setTopLevelDeclInObjCContainer();
1136 DeclsInGroup.push_back(Dcl);
1137 }
1138
1139 DeclsInGroup.push_back(ObjCImpDecl);
1140
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001141 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001142}
1143
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001144void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1145 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001146 SourceLocation RBrace) {
1147 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001148 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001149 if (!IDecl)
1150 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001151 /// Check case of non-existing \@interface decl.
1152 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001153 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001154 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001155 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001156 // Add ivar's to class's DeclContext.
1157 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001158 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001159 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001160 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001161 }
1162
Chris Lattner4d391482007-12-12 07:09:47 +00001163 return;
1164 }
1165 // If implementation has empty ivar list, just return.
1166 if (numIvars == 0)
1167 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Chris Lattner4d391482007-12-12 07:09:47 +00001169 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001170 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001171 if (ImpDecl->getSuperClass())
1172 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1173 for (unsigned i = 0; i < numIvars; i++) {
1174 ObjCIvarDecl* ImplIvar = ivars[i];
1175 if (const ObjCIvarDecl *ClsIvar =
1176 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1177 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1178 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1179 continue;
1180 }
Fariborz Jahanian3b20f582013-06-26 22:10:27 +00001181 // Check class extensions (unnamed categories) for duplicate ivars.
Stephen Hines651f13c2014-04-23 16:59:28 -07001182 for (const auto *CDecl : IDecl->visible_extensions()) {
Fariborz Jahanian3b20f582013-06-26 22:10:27 +00001183 if (const ObjCIvarDecl *ClsExtIvar =
1184 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1185 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1186 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1187 continue;
1188 }
1189 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001190 // Instance ivar to Implementation's DeclContext.
1191 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001192 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001193 ImpDecl->addDecl(ImplIvar);
1194 }
1195 return;
1196 }
Chris Lattner4d391482007-12-12 07:09:47 +00001197 // Check interface's Ivar list against those in the implementation.
1198 // names and types must match.
1199 //
Chris Lattner4d391482007-12-12 07:09:47 +00001200 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001201 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001202 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1203 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001204 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001205 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001206 assert (ImplIvar && "missing implementation ivar");
1207 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Steve Naroffca331292009-03-03 14:49:36 +00001209 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001210 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001211 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001212 << ImplIvar->getIdentifier()
1213 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001214 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001215 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1216 ImplIvar->getBitWidthValue(Context) !=
1217 ClsIvar->getBitWidthValue(Context)) {
1218 Diag(ImplIvar->getBitWidth()->getLocStart(),
1219 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1220 Diag(ClsIvar->getBitWidth()->getLocStart(),
1221 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001222 }
Steve Naroffca331292009-03-03 14:49:36 +00001223 // Make sure the names are identical.
1224 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001225 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001226 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001227 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001228 }
1229 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001230 }
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Chris Lattner609e4c72007-12-12 18:11:49 +00001232 if (numIvars > 0)
Stephen Hines651f13c2014-04-23 16:59:28 -07001233 Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001234 else if (IVI != IVE)
Stephen Hines651f13c2014-04-23 16:59:28 -07001235 Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001236}
1237
Stephen Hines651f13c2014-04-23 16:59:28 -07001238static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc,
1239 ObjCMethodDecl *method,
1240 bool &IncompleteImpl,
1241 unsigned DiagID,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001242 NamedDecl *NeededFor = nullptr) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001243 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001244 switch (method->getAvailability()) {
1245 case AR_Available:
1246 case AR_Deprecated:
1247 break;
1248
1249 // Don't warn about unavailable or not-yet-introduced methods.
1250 case AR_NotYetIntroduced:
1251 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001252 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001253 }
1254
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001255 // FIXME: For now ignore 'IncompleteImpl'.
1256 // Previously we grouped all unimplemented methods under a single
1257 // warning, but some users strongly voiced that they would prefer
1258 // separate warnings. We will give that approach a try, as that
1259 // matches what we do with protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001260 {
1261 const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID);
1262 B << method;
1263 if (NeededFor)
1264 B << NeededFor;
1265 }
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001266
1267 // Issue a note to the original declaration.
1268 SourceLocation MethodLoc = method->getLocStart();
1269 if (MethodLoc.isValid())
Stephen Hines651f13c2014-04-23 16:59:28 -07001270 S.Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001271}
1272
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001273/// Determines if type B can be substituted for type A. Returns true if we can
1274/// guarantee that anything that the user will do to an object of type A can
1275/// also be done to an object of type B. This is trivially true if the two
1276/// types are the same, or if B is a subclass of A. It becomes more complex
1277/// in cases where protocols are involved.
1278///
1279/// Object types in Objective-C describe the minimum requirements for an
1280/// object, rather than providing a complete description of a type. For
1281/// example, if A is a subclass of B, then B* may refer to an instance of A.
1282/// The principle of substitutability means that we may use an instance of A
1283/// anywhere that we may use an instance of B - it will implement all of the
1284/// ivars of B and all of the methods of B.
1285///
1286/// This substitutability is important when type checking methods, because
1287/// the implementation may have stricter type definitions than the interface.
1288/// The interface specifies minimum requirements, but the implementation may
1289/// have more accurate ones. For example, a method may privately accept
1290/// instances of B, but only publish that it accepts instances of A. Any
1291/// object passed to it will be type checked against B, and so will implicitly
1292/// by a valid A*. Similarly, a method may return a subclass of the class that
1293/// it is declared as returning.
1294///
1295/// This is most important when considering subclassing. A method in a
1296/// subclass must accept any object as an argument that its superclass's
1297/// implementation accepts. It may, however, accept a more general type
1298/// without breaking substitutability (i.e. you can still use the subclass
1299/// anywhere that you can use the superclass, but not vice versa). The
1300/// converse requirement applies to return types: the return type for a
1301/// subclass method must be a valid object of the kind that the superclass
1302/// advertises, but it may be specified more accurately. This avoids the need
1303/// for explicit down-casting by callers.
1304///
1305/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001306static bool isObjCTypeSubstitutable(ASTContext &Context,
1307 const ObjCObjectPointerType *A,
1308 const ObjCObjectPointerType *B,
1309 bool rejectId) {
1310 // Reject a protocol-unqualified id.
1311 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001312
1313 // If B is a qualified id, then A must also be a qualified id and it must
1314 // implement all of the protocols in B. It may not be a qualified class.
1315 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1316 // stricter definition so it is not substitutable for id<A>.
1317 if (B->isObjCQualifiedIdType()) {
1318 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001319 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1320 QualType(B,0),
1321 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001322 }
1323
1324 /*
1325 // id is a special type that bypasses type checking completely. We want a
1326 // warning when it is used in one place but not another.
1327 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1328
1329
1330 // If B is a qualified id, then A must also be a qualified id (which it isn't
1331 // if we've got this far)
1332 if (B->isObjCQualifiedIdType()) return false;
1333 */
1334
1335 // Now we know that A and B are (potentially-qualified) class types. The
1336 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001337 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001338}
1339
John McCall10302c02010-10-28 02:34:38 +00001340static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1341 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1342}
1343
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001344static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001345 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001346 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001347 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001348 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001349 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001350 if (IsProtocolMethodDecl &&
1351 (MethodDecl->getObjCDeclQualifier() !=
1352 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001353 if (Warn) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001354 S.Diag(MethodImpl->getLocation(),
1355 (IsOverridingMode
1356 ? diag::warn_conflicting_overriding_ret_type_modifiers
1357 : diag::warn_conflicting_ret_type_modifiers))
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001358 << MethodImpl->getDeclName()
Stephen Hines176edba2014-12-01 14:53:08 -08001359 << MethodImpl->getReturnTypeSourceRange();
Stephen Hines651f13c2014-04-23 16:59:28 -07001360 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
Stephen Hines176edba2014-12-01 14:53:08 -08001361 << MethodDecl->getReturnTypeSourceRange();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001362 }
1363 else
1364 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001365 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001366
1367 if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(),
1368 MethodDecl->getReturnType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001369 return true;
1370 if (!Warn)
1371 return false;
John McCall10302c02010-10-28 02:34:38 +00001372
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001373 unsigned DiagID =
1374 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1375 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001376
1377 // Mismatches between ObjC pointers go into a different warning
1378 // category, and sometimes they're even completely whitelisted.
1379 if (const ObjCObjectPointerType *ImplPtrTy =
Stephen Hines651f13c2014-04-23 16:59:28 -07001380 MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001381 if (const ObjCObjectPointerType *IfacePtrTy =
Stephen Hines651f13c2014-04-23 16:59:28 -07001382 MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001383 // Allow non-matching return types as long as they don't violate
1384 // the principle of substitutability. Specifically, we permit
1385 // return types that are subclasses of the declared return type,
1386 // or that are more-qualified versions of the declared type.
1387 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001388 return false;
John McCall10302c02010-10-28 02:34:38 +00001389
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001390 DiagID =
1391 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1392 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001393 }
1394 }
1395
1396 S.Diag(MethodImpl->getLocation(), DiagID)
Stephen Hines651f13c2014-04-23 16:59:28 -07001397 << MethodImpl->getDeclName() << MethodDecl->getReturnType()
1398 << MethodImpl->getReturnType()
Stephen Hines176edba2014-12-01 14:53:08 -08001399 << MethodImpl->getReturnTypeSourceRange();
Stephen Hines651f13c2014-04-23 16:59:28 -07001400 S.Diag(MethodDecl->getLocation(), IsOverridingMode
1401 ? diag::note_previous_declaration
1402 : diag::note_previous_definition)
Stephen Hines176edba2014-12-01 14:53:08 -08001403 << MethodDecl->getReturnTypeSourceRange();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001404 return false;
John McCall10302c02010-10-28 02:34:38 +00001405}
1406
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001407static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001408 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001409 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001410 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001411 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001412 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001413 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001414 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001415 if (IsProtocolMethodDecl &&
1416 (ImplVar->getObjCDeclQualifier() !=
1417 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001418 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001419 if (IsOverridingMode)
1420 S.Diag(ImplVar->getLocation(),
1421 diag::warn_conflicting_overriding_param_modifiers)
1422 << getTypeRange(ImplVar->getTypeSourceInfo())
1423 << MethodImpl->getDeclName();
1424 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001425 diag::warn_conflicting_param_modifiers)
1426 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001427 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001428 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1429 << getTypeRange(IfaceVar->getTypeSourceInfo());
1430 }
1431 else
1432 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001433 }
1434
John McCall10302c02010-10-28 02:34:38 +00001435 QualType ImplTy = ImplVar->getType();
1436 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001437
John McCall10302c02010-10-28 02:34:38 +00001438 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001439 return true;
1440
1441 if (!Warn)
1442 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001443 unsigned DiagID =
1444 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1445 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001446
1447 // Mismatches between ObjC pointers go into a different warning
1448 // category, and sometimes they're even completely whitelisted.
1449 if (const ObjCObjectPointerType *ImplPtrTy =
1450 ImplTy->getAs<ObjCObjectPointerType>()) {
1451 if (const ObjCObjectPointerType *IfacePtrTy =
1452 IfaceTy->getAs<ObjCObjectPointerType>()) {
1453 // Allow non-matching argument types as long as they don't
1454 // violate the principle of substitutability. Specifically, the
1455 // implementation must accept any objects that the superclass
1456 // accepts, however it may also accept others.
1457 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001458 return false;
John McCall10302c02010-10-28 02:34:38 +00001459
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001460 DiagID =
1461 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1462 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001463 }
1464 }
1465
1466 S.Diag(ImplVar->getLocation(), DiagID)
1467 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001468 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1469 S.Diag(IfaceVar->getLocation(),
1470 (IsOverridingMode ? diag::note_previous_declaration
1471 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001472 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001473 return false;
John McCall10302c02010-10-28 02:34:38 +00001474}
John McCallf85e1932011-06-15 23:02:42 +00001475
1476/// In ARC, check whether the conventional meanings of the two methods
1477/// match. If they don't, it's a hard error.
1478static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1479 ObjCMethodDecl *decl) {
1480 ObjCMethodFamily implFamily = impl->getMethodFamily();
1481 ObjCMethodFamily declFamily = decl->getMethodFamily();
1482 if (implFamily == declFamily) return false;
1483
1484 // Since conventions are sorted by selector, the only possibility is
1485 // that the types differ enough to cause one selector or the other
1486 // to fall out of the family.
1487 assert(implFamily == OMF_None || declFamily == OMF_None);
1488
1489 // No further diagnostics required on invalid declarations.
1490 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1491
1492 const ObjCMethodDecl *unmatched = impl;
1493 ObjCMethodFamily family = declFamily;
1494 unsigned errorID = diag::err_arc_lost_method_convention;
1495 unsigned noteID = diag::note_arc_lost_method_convention;
1496 if (declFamily == OMF_None) {
1497 unmatched = decl;
1498 family = implFamily;
1499 errorID = diag::err_arc_gained_method_convention;
1500 noteID = diag::note_arc_gained_method_convention;
1501 }
1502
1503 // Indexes into a %select clause in the diagnostic.
1504 enum FamilySelector {
1505 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1506 };
1507 FamilySelector familySelector = FamilySelector();
1508
1509 switch (family) {
1510 case OMF_None: llvm_unreachable("logic error, no method convention");
1511 case OMF_retain:
1512 case OMF_release:
1513 case OMF_autorelease:
1514 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001515 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001516 case OMF_retainCount:
1517 case OMF_self:
Stephen Hines176edba2014-12-01 14:53:08 -08001518 case OMF_initialize:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001519 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001520 // Mismatches for these methods don't change ownership
1521 // conventions, so we don't care.
1522 return false;
1523
1524 case OMF_init: familySelector = F_init; break;
1525 case OMF_alloc: familySelector = F_alloc; break;
1526 case OMF_copy: familySelector = F_copy; break;
1527 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1528 case OMF_new: familySelector = F_new; break;
1529 }
1530
1531 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1532 ReasonSelector reasonSelector;
1533
1534 // The only reason these methods don't fall within their families is
1535 // due to unusual result types.
Stephen Hines651f13c2014-04-23 16:59:28 -07001536 if (unmatched->getReturnType()->isObjCObjectPointerType()) {
John McCallf85e1932011-06-15 23:02:42 +00001537 reasonSelector = R_UnrelatedReturn;
1538 } else {
1539 reasonSelector = R_NonObjectReturn;
1540 }
1541
Joerg Sonnenberger73484542013-06-26 21:31:47 +00001542 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1543 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCallf85e1932011-06-15 23:02:42 +00001544
1545 return true;
1546}
John McCall10302c02010-10-28 02:34:38 +00001547
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001548void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001549 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001550 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001551 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001552 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1553 return;
1554
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001555 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001556 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001557 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Chris Lattner3aff9192009-04-11 19:58:42 +00001559 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001560 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1561 EF = MethodDecl->param_end();
1562 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001563 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001564 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001565 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001566
Fariborz Jahanian21121902011-08-08 18:03:17 +00001567 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001568 Diag(ImpMethodDecl->getLocation(),
1569 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001570 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001571 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001572}
1573
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001574void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1575 ObjCMethodDecl *Overridden,
1576 bool IsProtocolMethodDecl) {
1577
1578 CheckMethodOverrideReturn(*this, Method, Overridden,
1579 IsProtocolMethodDecl, true,
1580 true);
1581
1582 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001583 IF = Overridden->param_begin(), EM = Method->param_end(),
1584 EF = Overridden->param_end();
1585 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001586 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1587 IsProtocolMethodDecl, true, true);
1588 }
1589
1590 if (Method->isVariadic() != Overridden->isVariadic()) {
1591 Diag(Method->getLocation(),
1592 diag::warn_conflicting_overriding_variadic);
1593 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1594 }
1595}
1596
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001597/// WarnExactTypedMethods - This routine issues a warning if method
1598/// implementation declaration matches exactly that of its declaration.
1599void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1600 ObjCMethodDecl *MethodDecl,
1601 bool IsProtocolMethodDecl) {
1602 // don't issue warning when protocol method is optional because primary
1603 // class is not required to implement it and it is safe for protocol
1604 // to implement it.
1605 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1606 return;
1607 // don't issue warning when primary class's method is
1608 // depecated/unavailable.
1609 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1610 MethodDecl->hasAttr<DeprecatedAttr>())
1611 return;
1612
1613 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1614 IsProtocolMethodDecl, false, false);
1615 if (match)
1616 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001617 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1618 EF = MethodDecl->param_end();
1619 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001620 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1621 *IM, *IF,
1622 IsProtocolMethodDecl, false, false);
1623 if (!match)
1624 break;
1625 }
1626 if (match)
1627 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001628 if (match)
1629 match = !(MethodDecl->isClassMethod() &&
1630 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001631
1632 if (match) {
1633 Diag(ImpMethodDecl->getLocation(),
1634 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001635 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1636 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001637 }
1638}
1639
Mike Stump390b4cc2009-05-16 07:39:55 +00001640/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1641/// improve the efficiency of selector lookups and type checking by associating
1642/// with each protocol / interface / category the flattened instance tables. If
1643/// we used an immutable set to keep the table then it wouldn't add significant
1644/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001645
Stephen Hines651f13c2014-04-23 16:59:28 -07001646typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet;
1647typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet;
1648
1649static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl,
1650 ProtocolNameSet &PNS) {
1651 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1652 PNS.insert(PDecl->getIdentifier());
1653 for (const auto *PI : PDecl->protocols())
1654 findProtocolsWithExplicitImpls(PI, PNS);
1655}
1656
1657/// Recursively populates a set with all conformed protocols in a class
1658/// hierarchy that have the 'objc_protocol_requires_explicit_implementation'
1659/// attribute.
1660static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super,
1661 ProtocolNameSet &PNS) {
1662 if (!Super)
1663 return;
1664
1665 for (const auto *I : Super->all_referenced_protocols())
1666 findProtocolsWithExplicitImpls(I, PNS);
1667
1668 findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS);
1669}
1670
Steve Naroffefe7f362008-02-08 22:06:17 +00001671/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001672/// Declared in protocol, and those referenced by it.
Stephen Hines651f13c2014-04-23 16:59:28 -07001673static void CheckProtocolMethodDefs(Sema &S,
1674 SourceLocation ImpLoc,
1675 ObjCProtocolDecl *PDecl,
1676 bool& IncompleteImpl,
1677 const Sema::SelectorSet &InsMap,
1678 const Sema::SelectorSet &ClsMap,
1679 ObjCContainerDecl *CDecl,
1680 LazyProtocolNameSet &ProtocolsExplictImpl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001681 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1682 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1683 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001684 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1685
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001686 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001687 ObjCInterfaceDecl *NSIDecl = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001688
1689 // If this protocol is marked 'objc_protocol_requires_explicit_implementation'
1690 // then we should check if any class in the super class hierarchy also
1691 // conforms to this protocol, either directly or via protocol inheritance.
1692 // If so, we can skip checking this protocol completely because we
1693 // know that a parent class already satisfies this protocol.
1694 //
1695 // Note: we could generalize this logic for all protocols, and merely
1696 // add the limit on looking at the super class chain for just
1697 // specially marked protocols. This may be a good optimization. This
1698 // change is restricted to 'objc_protocol_requires_explicit_implementation'
1699 // protocols for now for controlled evaluation.
1700 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) {
1701 if (!ProtocolsExplictImpl) {
1702 ProtocolsExplictImpl.reset(new ProtocolNameSet);
1703 findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl);
1704 }
1705 if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) !=
1706 ProtocolsExplictImpl->end())
1707 return;
1708
1709 // If no super class conforms to the protocol, we should not search
1710 // for methods in the super class to implicitly satisfy the protocol.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001711 Super = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001712 }
1713
1714 if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001715 // check to see if class implements forwardInvocation method and objects
1716 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001717 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001718 // Under such conditions, which means that every method possible is
1719 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001720 // found" warnings.
1721 // FIXME: Use a general GetUnarySelector method for this.
Stephen Hines651f13c2014-04-23 16:59:28 -07001722 IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
1723 Selector fISelector = S.Context.Selectors.getSelector(1, &II);
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001724 if (InsMap.count(fISelector))
1725 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1726 // need be implemented in the implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001727 NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001728 }
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001730 // If this is a forward protocol declaration, get its definition.
1731 if (!PDecl->isThisDeclarationADefinition() &&
1732 PDecl->getDefinition())
1733 PDecl = PDecl->getDefinition();
1734
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001735 // If a method lookup fails locally we still need to look and see if
1736 // the method was implemented by a base class or an inherited
1737 // protocol. This lookup is slow, but occurs rarely in correct code
1738 // and otherwise would terminate in a warning.
1739
Chris Lattner4d391482007-12-12 07:09:47 +00001740 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001741 if (!NSIDecl)
Stephen Hines651f13c2014-04-23 16:59:28 -07001742 for (auto *method : PDecl->instance_methods()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001743 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001744 !method->isPropertyAccessor() &&
1745 !InsMap.count(method->getSelector()) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001746 (!Super || !Super->lookupMethod(method->getSelector(),
1747 true /* instance */,
1748 false /* shallowCategory */,
1749 true /* followsSuper */,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001750 nullptr /* category */))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001751 // If a method is not implemented in the category implementation but
1752 // has been declared in its primary class, superclass,
1753 // or in one of their protocols, no need to issue the warning.
1754 // This is because method will be implemented in the primary class
1755 // or one of its super class implementation.
1756
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001757 // Ugly, but necessary. Method declared in protcol might have
1758 // have been synthesized due to a property declared in the class which
1759 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001760 if (ObjCMethodDecl *MethodInClass =
Stephen Hines651f13c2014-04-23 16:59:28 -07001761 IDecl->lookupMethod(method->getSelector(),
1762 true /* instance */,
1763 true /* shallowCategoryLookup */,
1764 false /* followSuper */))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001765 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001766 continue;
1767 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001768 if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001769 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
1770 PDecl);
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001771 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001772 }
1773 }
Chris Lattner4d391482007-12-12 07:09:47 +00001774 // check unimplemented class methods
Stephen Hines651f13c2014-04-23 16:59:28 -07001775 for (auto *method : PDecl->class_methods()) {
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001776 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1777 !ClsMap.count(method->getSelector()) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001778 (!Super || !Super->lookupMethod(method->getSelector(),
1779 false /* class method */,
1780 false /* shallowCategoryLookup */,
1781 true /* followSuper */,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001782 nullptr /* category */))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001783 // See above comment for instance method lookups.
Stephen Hines651f13c2014-04-23 16:59:28 -07001784 if (C && IDecl->lookupMethod(method->getSelector(),
1785 false /* class */,
1786 true /* shallowCategoryLookup */,
1787 false /* followSuper */))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001788 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001789
Fariborz Jahanian52146832010-03-31 18:23:33 +00001790 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001791 if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001792 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001793 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001794 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001795 }
Chris Lattner780f3292008-07-21 21:32:27 +00001796 // Check on this protocols's referenced protocols, recursively.
Stephen Hines651f13c2014-04-23 16:59:28 -07001797 for (auto *PI : PDecl->protocols())
1798 CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap,
1799 CDecl, ProtocolsExplictImpl);
Chris Lattner4d391482007-12-12 07:09:47 +00001800}
1801
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001802/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001803/// or protocol against those declared in their implementations.
1804///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001805void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1806 const SelectorSet &ClsMap,
1807 SelectorSet &InsMapSeen,
1808 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001809 ObjCImplDecl* IMPDecl,
1810 ObjCContainerDecl* CDecl,
1811 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001812 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001813 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001814 // Check and see if instance methods in class interface have been
1815 // implemented in the implementation class. If so, their types match.
Stephen Hines651f13c2014-04-23 16:59:28 -07001816 for (auto *I : CDecl->instance_methods()) {
Stephen Hines176edba2014-12-01 14:53:08 -08001817 if (!InsMapSeen.insert(I->getSelector()).second)
Benjamin Kramer7dcff5b2013-10-14 15:16:10 +00001818 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001819 if (!I->isPropertyAccessor() &&
1820 !InsMap.count(I->getSelector())) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001821 if (ImmediateClass)
Stephen Hines651f13c2014-04-23 16:59:28 -07001822 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001823 diag::warn_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001824 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001825 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001826 ObjCMethodDecl *ImpMethodDecl =
Stephen Hines651f13c2014-04-23 16:59:28 -07001827 IMPDecl->getInstanceMethod(I->getSelector());
1828 assert(CDecl->getInstanceMethod(I->getSelector()) &&
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001829 "Expected to find the method through lookup as well");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001830 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001831 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001832 if (!WarnCategoryMethodImpl)
Stephen Hines651f13c2014-04-23 16:59:28 -07001833 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001834 isa<ObjCProtocolDecl>(CDecl));
Stephen Hines651f13c2014-04-23 16:59:28 -07001835 else if (!I->isPropertyAccessor())
1836 WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001837 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001838 }
1839 }
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001841 // Check and see if class methods in class interface have been
1842 // implemented in the implementation class. If so, their types match.
Stephen Hines651f13c2014-04-23 16:59:28 -07001843 for (auto *I : CDecl->class_methods()) {
Stephen Hines176edba2014-12-01 14:53:08 -08001844 if (!ClsMapSeen.insert(I->getSelector()).second)
Benjamin Kramer7dcff5b2013-10-14 15:16:10 +00001845 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001846 if (!ClsMap.count(I->getSelector())) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001847 if (ImmediateClass)
Stephen Hines651f13c2014-04-23 16:59:28 -07001848 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001849 diag::warn_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001850 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001851 ObjCMethodDecl *ImpMethodDecl =
Stephen Hines651f13c2014-04-23 16:59:28 -07001852 IMPDecl->getClassMethod(I->getSelector());
1853 assert(CDecl->getClassMethod(I->getSelector()) &&
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001854 "Expected to find the method through lookup as well");
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001855 if (!WarnCategoryMethodImpl)
Stephen Hines651f13c2014-04-23 16:59:28 -07001856 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001857 isa<ObjCProtocolDecl>(CDecl));
1858 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001859 WarnExactTypedMethods(ImpMethodDecl, I,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001860 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001861 }
1862 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001863
Fariborz Jahanian41594c52013-08-14 23:58:55 +00001864 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1865 // Also, check for methods declared in protocols inherited by
1866 // this protocol.
Stephen Hines651f13c2014-04-23 16:59:28 -07001867 for (auto *PI : PD->protocols())
Fariborz Jahanian41594c52013-08-14 23:58:55 +00001868 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Stephen Hines651f13c2014-04-23 16:59:28 -07001869 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanian41594c52013-08-14 23:58:55 +00001870 WarnCategoryMethodImpl);
1871 }
1872
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001873 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001874 // when checking that methods in implementation match their declaration,
1875 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1876 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001877 if (!WarnCategoryMethodImpl) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001878 for (auto *Cat : I->visible_categories())
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001879 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Stephen Hines651f13c2014-04-23 16:59:28 -07001880 IMPDecl, Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001881 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001882 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001883 // Also methods in class extensions need be looked at next.
Stephen Hines651f13c2014-04-23 16:59:28 -07001884 for (auto *Ext : I->visible_extensions())
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001885 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Stephen Hines651f13c2014-04-23 16:59:28 -07001886 IMPDecl, Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001887 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001888 }
1889
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001890 // Check for any implementation of a methods declared in protocol.
Stephen Hines651f13c2014-04-23 16:59:28 -07001891 for (auto *PI : I->all_referenced_protocols())
Mike Stump1eb44332009-09-09 15:08:12 +00001892 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Stephen Hines651f13c2014-04-23 16:59:28 -07001893 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001894 WarnCategoryMethodImpl);
Stephen Hines651f13c2014-04-23 16:59:28 -07001895
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001896 // FIXME. For now, we are not checking for extact match of methods
1897 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001898 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001899 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001900 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001901 I->getSuperClass(), IncompleteImpl, false);
1902 }
1903}
1904
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001905/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1906/// category matches with those implemented in its primary class and
1907/// warns each time an exact match is found.
1908void Sema::CheckCategoryVsClassMethodMatches(
1909 ObjCCategoryImplDecl *CatIMPDecl) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001910 // Get category's primary class.
1911 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1912 if (!CatDecl)
1913 return;
1914 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1915 if (!IDecl)
1916 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07001917 ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
1918 SelectorSet InsMap, ClsMap;
1919
1920 for (const auto *I : CatIMPDecl->instance_methods()) {
1921 Selector Sel = I->getSelector();
1922 // When checking for methods implemented in the category, skip over
1923 // those declared in category class's super class. This is because
1924 // the super class must implement the method.
1925 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
1926 continue;
1927 InsMap.insert(Sel);
1928 }
1929
1930 for (const auto *I : CatIMPDecl->class_methods()) {
1931 Selector Sel = I->getSelector();
1932 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
1933 continue;
1934 ClsMap.insert(Sel);
1935 }
1936 if (InsMap.empty() && ClsMap.empty())
1937 return;
1938
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001939 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001940 bool IncompleteImpl = false;
1941 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1942 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001943 IncompleteImpl, false,
1944 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001945}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001946
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001947void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001948 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001949 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001950 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001951 // Check and see if instance methods in class interface have been
1952 // implemented in the implementation class.
Stephen Hines651f13c2014-04-23 16:59:28 -07001953 for (const auto *I : IMPDecl->instance_methods())
1954 InsMap.insert(I->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001956 // Check and see if properties declared in the interface have either 1)
1957 // an implementation or 2) there is a @synthesize/@dynamic implementation
1958 // of the property in the @implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001959 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1960 bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties &&
1961 LangOpts.ObjCRuntime.isNonFragile() &&
1962 !IDecl->isObjCRequiresPropertyDefs();
1963 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties);
1964 }
1965
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001966 SelectorSet ClsMap;
Stephen Hines651f13c2014-04-23 16:59:28 -07001967 for (const auto *I : IMPDecl->class_methods())
1968 ClsMap.insert(I->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001970 // Check for type conflict of methods declared in a class/protocol and
1971 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001972 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001973 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1974 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001975 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001976
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001977 // check all methods implemented in category against those declared
1978 // in its primary class.
1979 if (ObjCCategoryImplDecl *CatDecl =
1980 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1981 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Chris Lattner4d391482007-12-12 07:09:47 +00001983 // Check the protocol list for unimplemented methods in the @implementation
1984 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001985 // Check and see if class methods in class interface have been
1986 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001987
Stephen Hines651f13c2014-04-23 16:59:28 -07001988 LazyProtocolNameSet ExplicitImplProtocols;
1989
Chris Lattnercddc8882009-03-01 00:56:52 +00001990 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001991 for (auto *PI : I->all_referenced_protocols())
1992 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl,
1993 InsMap, ClsMap, I, ExplicitImplProtocols);
Chris Lattnercddc8882009-03-01 00:56:52 +00001994 // Check class extensions (unnamed categories)
Stephen Hines651f13c2014-04-23 16:59:28 -07001995 for (auto *Ext : I->visible_extensions())
1996 ImplMethodsVsClassMethods(S, IMPDecl, Ext, IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001997 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001998 // For extended class, unimplemented methods in its protocols will
1999 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00002000 if (!C->IsClassExtension()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002001 for (auto *P : C->protocols())
2002 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P,
2003 IncompleteImpl, InsMap, ClsMap, CDecl,
2004 ExplicitImplProtocols);
2005 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002006 /*SynthesizeProperties=*/false);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00002007 }
Chris Lattnercddc8882009-03-01 00:56:52 +00002008 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002009 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00002010}
2011
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00002012Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00002013Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00002014 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00002015 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00002016 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00002017 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00002018 for (unsigned i = 0; i != NumElts; ++i) {
2019 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00002020 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00002021 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00002022 LookupOrdinaryName, ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002023 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00002024 // GCC apparently allows the following idiom:
2025 //
2026 // typedef NSObject < XCElementTogglerP > XCElementToggler;
2027 // @class XCElementToggler;
2028 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00002029 // Here we have chosen to ignore the forward class declaration
2030 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00002031 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00002032 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002033 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00002034 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00002035 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002036 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00002037 // to the underlying class. Just ignore the forward class with a warning
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002038 // as this will force the intended behavior which is to lookup the
2039 // typedef name.
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00002040 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002041 Diag(AtClassLoc, diag::warn_forward_class_redefinition)
2042 << IdentList[i];
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00002043 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2044 continue;
2045 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00002046 }
Chris Lattner4d391482007-12-12 07:09:47 +00002047 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00002048
2049 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00002050 ObjCInterfaceDecl *PrevIDecl
2051 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00002052
2053 IdentifierInfo *ClassName = IdentList[i];
2054 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2055 // A previous decl with a different name is because of
2056 // @compatibility_alias, for example:
2057 // \code
2058 // @class NewImage;
2059 // @compatibility_alias OldImage NewImage;
2060 // \endcode
2061 // A lookup for 'OldImage' will return the 'NewImage' decl.
2062 //
2063 // In such a case use the real declaration name, instead of the alias one,
2064 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2065 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2066 // has been aliased.
2067 ClassName = PrevIDecl->getIdentifier();
2068 }
2069
Douglas Gregor7723fec2011-12-15 20:29:51 +00002070 ObjCInterfaceDecl *IDecl
2071 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00002072 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00002073 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00002074
Douglas Gregor7723fec2011-12-15 20:29:51 +00002075 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00002076 CheckObjCDeclScope(IDecl);
2077 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002078 }
Rafael Espindola4549d7f2013-07-09 12:05:01 +00002079
2080 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002081}
2082
John McCall0f4c4c42011-06-16 01:15:19 +00002083static bool tryMatchRecordTypes(ASTContext &Context,
2084 Sema::MethodMatchStrategy strategy,
2085 const Type *left, const Type *right);
2086
John McCallf85e1932011-06-15 23:02:42 +00002087static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2088 QualType leftQT, QualType rightQT) {
2089 const Type *left =
2090 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2091 const Type *right =
2092 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2093
2094 if (left == right) return true;
2095
2096 // If we're doing a strict match, the types have to match exactly.
2097 if (strategy == Sema::MMS_strict) return false;
2098
2099 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2100
2101 // Otherwise, use this absurdly complicated algorithm to try to
2102 // validate the basic, low-level compatibility of the two types.
2103
2104 // As a minimum, require the sizes and alignments to match.
Stephen Hines176edba2014-12-01 14:53:08 -08002105 TypeInfo LeftTI = Context.getTypeInfo(left);
2106 TypeInfo RightTI = Context.getTypeInfo(right);
2107 if (LeftTI.Width != RightTI.Width)
2108 return false;
2109
2110 if (LeftTI.Align != RightTI.Align)
John McCallf85e1932011-06-15 23:02:42 +00002111 return false;
2112
2113 // Consider all the kinds of non-dependent canonical types:
2114 // - functions and arrays aren't possible as return and parameter types
2115
2116 // - vector types of equal size can be arbitrarily mixed
2117 if (isa<VectorType>(left)) return isa<VectorType>(right);
2118 if (isa<VectorType>(right)) return false;
2119
2120 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00002121 // - structs, unions, and Objective-C objects must match more-or-less
2122 // exactly
John McCallf85e1932011-06-15 23:02:42 +00002123 // - everything else should be a scalar
2124 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00002125 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00002126
John McCall1d9b3b22011-09-09 05:25:32 +00002127 // Make scalars agree in kind, except count bools as chars, and group
2128 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00002129 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2130 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2131 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2132 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00002133 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2134 leftSK = Type::STK_ObjCObjectPointer;
2135 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2136 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002137
2138 // Note that data member pointers and function member pointers don't
2139 // intermix because of the size differences.
2140
2141 return (leftSK == rightSK);
2142}
Chris Lattner4d391482007-12-12 07:09:47 +00002143
John McCall0f4c4c42011-06-16 01:15:19 +00002144static bool tryMatchRecordTypes(ASTContext &Context,
2145 Sema::MethodMatchStrategy strategy,
2146 const Type *lt, const Type *rt) {
2147 assert(lt && rt && lt != rt);
2148
2149 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2150 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2151 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2152
2153 // Require union-hood to match.
2154 if (left->isUnion() != right->isUnion()) return false;
2155
2156 // Require an exact match if either is non-POD.
2157 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2158 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2159 return false;
2160
2161 // Require size and alignment to match.
Stephen Hines176edba2014-12-01 14:53:08 -08002162 TypeInfo LeftTI = Context.getTypeInfo(lt);
2163 TypeInfo RightTI = Context.getTypeInfo(rt);
2164 if (LeftTI.Width != RightTI.Width)
2165 return false;
2166
2167 if (LeftTI.Align != RightTI.Align)
2168 return false;
John McCall0f4c4c42011-06-16 01:15:19 +00002169
2170 // Require fields to match.
2171 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2172 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2173 for (; li != le && ri != re; ++li, ++ri) {
2174 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2175 return false;
2176 }
2177 return (li == le && ri == re);
2178}
2179
Chris Lattner4d391482007-12-12 07:09:47 +00002180/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2181/// returns true, or false, accordingly.
2182/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002183bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2184 const ObjCMethodDecl *right,
2185 MethodMatchStrategy strategy) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002186 if (!matchTypes(Context, strategy, left->getReturnType(),
2187 right->getReturnType()))
John McCallf85e1932011-06-15 23:02:42 +00002188 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002189
Douglas Gregor7666b032013-02-07 19:13:24 +00002190 // If either is hidden, it is not considered to match.
2191 if (left->isHidden() || right->isHidden())
2192 return false;
2193
David Blaikie4e4d0842012-03-11 07:00:24 +00002194 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002195 (left->hasAttr<NSReturnsRetainedAttr>()
2196 != right->hasAttr<NSReturnsRetainedAttr>() ||
2197 left->hasAttr<NSConsumesSelfAttr>()
2198 != right->hasAttr<NSConsumesSelfAttr>()))
2199 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002201 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002202 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2203 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002204
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002205 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002206 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002207 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002208
2209 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2210 return false;
2211
David Blaikie4e4d0842012-03-11 07:00:24 +00002212 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002213 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2214 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002215 }
2216 return true;
2217}
2218
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002219void Sema::addMethodToGlobalList(ObjCMethodList *List,
2220 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.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002223 if (ObjCCategoryDecl *CD =
2224 dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00002225 if (!CD->IsClassExtension() && List->getBits() < 2)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002226 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 Hines0e2c34f2015-03-23 12:09:02 -07002229 if (List->getMethod() == nullptr) {
2230 List->setMethod(Method);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002231 List->setNext(nullptr);
Douglas Gregorff310c72012-05-01 23:37:00 +00002232 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002233 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002234
Douglas Gregor44fae522012-01-25 00:19:56 +00002235 // We've seen a method with this name, see if we have already seen this type
2236 // signature.
2237 ObjCMethodList *Previous = List;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002238 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregorfc46be92013-06-21 00:20:25 +00002239 // If we are building a module, keep all of the methods.
2240 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2241 continue;
2242
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002243 if (!MatchTwoMethodDeclarations(Method, List->getMethod())) {
2244 // Even if two method types do not match, we would like to say
2245 // there is more than one declaration so unavailability/deprecated
2246 // warning is not too noisy.
2247 if (!Method->isDefined())
2248 List->setHasMoreThanOneDecl(true);
Douglas Gregor44fae522012-01-25 00:19:56 +00002249 continue;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002250 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002251
2252 ObjCMethodDecl *PrevObjCMethod = List->getMethod();
Douglas Gregor44fae522012-01-25 00:19:56 +00002253
2254 // Propagate the 'defined' bit.
2255 if (Method->isDefined())
2256 PrevObjCMethod->setDefined(true);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002257 else {
2258 // Objective-C doesn't allow an @interface for a class after its
2259 // @implementation. So if Method is not defined and there already is
2260 // an entry for this type signature, Method has to be for a different
2261 // class than PrevObjCMethod.
2262 List->setHasMoreThanOneDecl(true);
2263 }
2264
Douglas Gregor44fae522012-01-25 00:19:56 +00002265 // If a method is deprecated, push it in the global pool.
2266 // This is used for better diagnostics.
2267 if (Method->isDeprecated()) {
2268 if (!PrevObjCMethod->isDeprecated())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002269 List->setMethod(Method);
Douglas Gregor44fae522012-01-25 00:19:56 +00002270 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002271 // If the new method is unavailable, push it into global pool
Douglas Gregor44fae522012-01-25 00:19:56 +00002272 // unless previous one is deprecated.
2273 if (Method->isUnavailable()) {
2274 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002275 List->setMethod(Method);
Douglas Gregor44fae522012-01-25 00:19:56 +00002276 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002277
Douglas Gregorff310c72012-05-01 23:37:00 +00002278 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002279 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002280
Douglas Gregor44fae522012-01-25 00:19:56 +00002281 // We have a new signature for an existing method - add it.
2282 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002283 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002284 Previous->setNext(new (Mem) ObjCMethodList(Method));
Douglas Gregor44fae522012-01-25 00:19:56 +00002285}
2286
Sebastian Redldb9d2142010-08-02 23:18:59 +00002287/// \brief Read the contents of the method pool for a given selector from
2288/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002289void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002290 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002291 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002292}
2293
Douglas Gregorff310c72012-05-01 23:37:00 +00002294void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002295 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002296 // Ignore methods of invalid containers.
2297 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002298 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002299
Douglas Gregor0d266d62012-01-25 00:59:09 +00002300 if (ExternalSource)
2301 ReadMethodPool(Method->getSelector());
2302
Sebastian Redldb9d2142010-08-02 23:18:59 +00002303 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002304 if (Pos == MethodPool.end())
2305 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2306 GlobalMethods())).first;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002307
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002308 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002309
Sebastian Redldb9d2142010-08-02 23:18:59 +00002310 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002311 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002312}
2313
John McCallf85e1932011-06-15 23:02:42 +00002314/// Determines if this is an "acceptable" loose mismatch in the global
2315/// method pool. This exists mostly as a hack to get around certain
2316/// global mismatches which we can't afford to make warnings / errors.
2317/// Really, what we want is a way to take a method out of the global
2318/// method pool.
2319static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2320 ObjCMethodDecl *other) {
2321 if (!chosen->isInstanceMethod())
2322 return false;
2323
2324 Selector sel = chosen->getSelector();
2325 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2326 return false;
2327
2328 // Don't complain about mismatches for -length if the method we
2329 // chose has an integral result type.
Stephen Hines651f13c2014-04-23 16:59:28 -07002330 return (chosen->getReturnType()->isIntegerType());
John McCallf85e1932011-06-15 23:02:42 +00002331}
2332
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002333bool Sema::CollectMultipleMethodsInGlobalPool(
2334 Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, bool instance) {
Stephen Hines176edba2014-12-01 14:53:08 -08002335 if (ExternalSource)
2336 ReadMethodPool(Sel);
2337
2338 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2339 if (Pos == MethodPool.end())
2340 return false;
2341 // Gather the non-hidden methods.
2342 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
2343 for (ObjCMethodList *M = &MethList; M; M = M->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002344 if (M->getMethod() && !M->getMethod()->isHidden())
2345 Methods.push_back(M->getMethod());
2346 return Methods.size() > 1;
Stephen Hines176edba2014-12-01 14:53:08 -08002347}
2348
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002349bool Sema::AreMultipleMethodsInGlobalPool(Selector Sel, ObjCMethodDecl *BestMethod,
2350 SourceRange R,
2351 bool receiverIdOrClass) {
Stephen Hines176edba2014-12-01 14:53:08 -08002352 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002353 // Test for no method in the pool which should not trigger any warning by
2354 // caller.
Stephen Hines176edba2014-12-01 14:53:08 -08002355 if (Pos == MethodPool.end())
2356 return true;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002357 ObjCMethodList &MethList =
2358 BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second;
2359
2360 // Diagnose finding more than one method in global pool
2361 SmallVector<ObjCMethodDecl *, 4> Methods;
2362 Methods.push_back(BestMethod);
2363 for (ObjCMethodList *M = &MethList; M; M = M->getNext())
2364 if (M->getMethod() && !M->getMethod()->isHidden() &&
2365 M->getMethod() != BestMethod)
2366 Methods.push_back(M->getMethod());
2367 if (Methods.size() > 1)
2368 DiagnoseMultipleMethodInGlobalPool(Methods, Sel, R, receiverIdOrClass);
2369
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002370 return MethList.hasMoreThanOneDecl();
Stephen Hines176edba2014-12-01 14:53:08 -08002371}
2372
Sebastian Redldb9d2142010-08-02 23:18:59 +00002373ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002374 bool receiverIdOrClass,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002375 bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002376 if (ExternalSource)
2377 ReadMethodPool(Sel);
2378
Sebastian Redldb9d2142010-08-02 23:18:59 +00002379 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002380 if (Pos == MethodPool.end())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002381 return nullptr;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002382
Douglas Gregorf0e00042013-01-16 18:47:38 +00002383 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002384 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelme7205c02013-08-10 12:33:24 +00002385 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002386 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002387 if (M->getMethod() && !M->getMethod()->isHidden())
2388 return M->getMethod();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002389 }
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002390 return nullptr;
2391}
Douglas Gregorf0e00042013-01-16 18:47:38 +00002392
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002393void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods,
2394 Selector Sel, SourceRange R,
2395 bool receiverIdOrClass) {
Douglas Gregorf0e00042013-01-16 18:47:38 +00002396 // We found multiple methods, so we may have to complain.
2397 bool issueDiagnostic = false, issueError = false;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002398
Douglas Gregorf0e00042013-01-16 18:47:38 +00002399 // We support a warning which complains about *any* difference in
2400 // method signature.
2401 bool strictSelectorMatch =
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002402 receiverIdOrClass &&
2403 !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin());
Douglas Gregorf0e00042013-01-16 18:47:38 +00002404 if (strictSelectorMatch) {
2405 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2406 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2407 issueDiagnostic = true;
2408 break;
2409 }
2410 }
2411 }
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002412
Douglas Gregorf0e00042013-01-16 18:47:38 +00002413 // If we didn't see any strict differences, we won't see any loose
2414 // differences. In ARC, however, we also need to check for loose
2415 // mismatches, because most of them are errors.
2416 if (!strictSelectorMatch ||
2417 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2418 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2419 // This checks if the methods differ in type mismatch.
2420 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2421 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2422 issueDiagnostic = true;
2423 if (getLangOpts().ObjCAutoRefCount)
2424 issueError = true;
2425 break;
2426 }
2427 }
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002428
Douglas Gregorf0e00042013-01-16 18:47:38 +00002429 if (issueDiagnostic) {
2430 if (issueError)
2431 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2432 else if (strictSelectorMatch)
2433 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2434 else
2435 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002436
Douglas Gregorf0e00042013-01-16 18:47:38 +00002437 Diag(Methods[0]->getLocStart(),
2438 issueError ? diag::note_possibility : diag::note_using)
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002439 << Methods[0]->getSourceRange();
Douglas Gregorf0e00042013-01-16 18:47:38 +00002440 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2441 Diag(Methods[I]->getLocStart(), diag::note_also_found)
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002442 << Methods[I]->getSourceRange();
2443 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002444 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002445}
2446
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002447ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002448 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2449 if (Pos == MethodPool.end())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002450 return nullptr;
Sebastian Redldb9d2142010-08-02 23:18:59 +00002451
2452 GlobalMethods &Methods = Pos->second;
Stephen Hines651f13c2014-04-23 16:59:28 -07002453 for (const ObjCMethodList *Method = &Methods.first; Method;
2454 Method = Method->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002455 if (Method->getMethod() &&
2456 (Method->getMethod()->isDefined() ||
2457 Method->getMethod()->isPropertyAccessor()))
2458 return Method->getMethod();
Stephen Hines651f13c2014-04-23 16:59:28 -07002459
2460 for (const ObjCMethodList *Method = &Methods.second; Method;
2461 Method = Method->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002462 if (Method->getMethod() &&
2463 (Method->getMethod()->isDefined() ||
2464 Method->getMethod()->isPropertyAccessor()))
2465 return Method->getMethod();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002466 return nullptr;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002467}
2468
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002469static void
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002470HelperSelectorsForTypoCorrection(
2471 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2472 StringRef Typo, const ObjCMethodDecl * Method) {
2473 const unsigned MaxEditDistance = 1;
2474 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieu4fe96442013-06-06 02:22:29 +00002475 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002476
2477 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2478 if (MinPossibleEditDistance > 0 &&
2479 Typo.size() / MinPossibleEditDistance < 1)
2480 return;
2481 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2482 if (EditDistance > MaxEditDistance)
2483 return;
2484 if (EditDistance == BestEditDistance)
2485 BestMethod.push_back(Method);
2486 else if (EditDistance < BestEditDistance) {
2487 BestMethod.clear();
2488 BestMethod.push_back(Method);
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002489 }
2490}
2491
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002492static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2493 QualType ObjectType) {
2494 if (ObjectType.isNull())
2495 return true;
2496 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2497 return true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002498 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) !=
2499 nullptr;
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002500}
2501
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002502const ObjCMethodDecl *
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002503Sema::SelectorsForTypoCorrection(Selector Sel,
2504 QualType ObjectType) {
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002505 unsigned NumArgs = Sel.getNumArgs();
2506 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002507 bool ObjectIsId = true, ObjectIsClass = true;
2508 if (ObjectType.isNull())
2509 ObjectIsId = ObjectIsClass = false;
2510 else if (!ObjectType->isObjCObjectPointerType())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002511 return nullptr;
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002512 else if (const ObjCObjectPointerType *ObjCPtr =
2513 ObjectType->getAsObjCInterfacePointerType()) {
2514 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2515 ObjectIsId = ObjectIsClass = false;
2516 }
2517 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2518 ObjectIsClass = false;
2519 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2520 ObjectIsId = false;
2521 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002522 return nullptr;
2523
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002524 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2525 e = MethodPool.end(); b != e; b++) {
2526 // instance methods
2527 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002528 if (M->getMethod() &&
2529 (M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
2530 (M->getMethod()->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002531 if (ObjectIsId)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002532 Methods.push_back(M->getMethod());
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002533 else if (!ObjectIsClass &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002534 HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(),
2535 ObjectType))
2536 Methods.push_back(M->getMethod());
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002537 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002538 // class methods
2539 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002540 if (M->getMethod() &&
2541 (M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
2542 (M->getMethod()->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002543 if (ObjectIsClass)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002544 Methods.push_back(M->getMethod());
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002545 else if (!ObjectIsId &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002546 HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(),
2547 ObjectType))
2548 Methods.push_back(M->getMethod());
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002549 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002550 }
2551
2552 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2553 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2554 HelperSelectorsForTypoCorrection(SelectedMethods,
2555 Sel.getAsString(), Methods[i]);
2556 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002557 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr;
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002558}
2559
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002560/// DiagnoseDuplicateIvars -
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002561/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002562/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002563/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002564/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002565void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2566 ObjCInterfaceDecl *SID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002567 for (auto *Ivar : ID->ivars()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002568 if (Ivar->isInvalidDecl())
2569 continue;
2570 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2571 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2572 if (prevIvar) {
2573 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2574 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2575 Ivar->setInvalidDecl();
2576 }
2577 }
2578 }
2579}
2580
Erik Verbruggend64251f2011-12-06 09:25:23 +00002581Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2582 switch (CurContext->getDeclKind()) {
2583 case Decl::ObjCInterface:
2584 return Sema::OCK_Interface;
2585 case Decl::ObjCProtocol:
2586 return Sema::OCK_Protocol;
2587 case Decl::ObjCCategory:
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002588 if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
Erik Verbruggend64251f2011-12-06 09:25:23 +00002589 return Sema::OCK_ClassExtension;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002590 return Sema::OCK_Category;
Erik Verbruggend64251f2011-12-06 09:25:23 +00002591 case Decl::ObjCImplementation:
2592 return Sema::OCK_Implementation;
2593 case Decl::ObjCCategoryImpl:
2594 return Sema::OCK_CategoryImplementation;
2595
2596 default:
2597 return Sema::OCK_None;
2598 }
2599}
2600
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002601// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm0111e4d2013-07-17 21:14:35 +00002602Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahanian80f8aca2013-07-17 00:05:08 +00002603 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggend64251f2011-12-06 09:25:23 +00002604 if (getObjCContainerKind() == Sema::OCK_None)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002605 return nullptr;
Erik Verbruggend64251f2011-12-06 09:25:23 +00002606
2607 assert(AtEnd.isValid() && "Invalid location for '@end'");
2608
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002609 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2610 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002611
Mike Stump1eb44332009-09-09 15:08:12 +00002612 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002613 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2614 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002615 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002616
Steve Naroff0701bbb2009-01-08 17:28:14 +00002617 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2618 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2619 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2620
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002621 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002622 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002623 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002624
2625 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002626 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002627 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002628 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002629 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002630 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002631 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002632 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002633 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002634 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002635 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002636 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002637 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002638 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002639 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002640 if (!Context.getSourceManager().isInSystemHeader(
2641 Method->getLocation()))
2642 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2643 << Method->getDeclName();
2644 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2645 }
Chris Lattner4d391482007-12-12 07:09:47 +00002646 InsMap[Method->getSelector()] = Method;
2647 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002648 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002649 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002650 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002651 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002652 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002653 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002654 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002655 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002656 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002657 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002658 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002659 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002660 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002661 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002662 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002663 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002664 if (!Context.getSourceManager().isInSystemHeader(
2665 Method->getLocation()))
2666 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2667 << Method->getDeclName();
2668 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2669 }
Chris Lattner4d391482007-12-12 07:09:47 +00002670 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002671 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002672 }
2673 }
2674 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002675 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2676 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002677 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002678 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002679 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002680 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002681
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002682 if (C->IsClassExtension()) {
2683 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2684 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002685 }
Chris Lattner4d391482007-12-12 07:09:47 +00002686 }
Steve Naroff09c47192009-01-09 15:36:25 +00002687 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002688 if (CDecl->getIdentifier())
2689 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2690 // user-defined setter/getter. It also synthesizes setter/getter methods
2691 // and adds them to the DeclContext and global method pools.
Stephen Hines651f13c2014-04-23 16:59:28 -07002692 for (auto *I : CDecl->properties())
2693 ProcessPropertyDecl(I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002694 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002695 }
2696 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002697 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002698 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002699 // Any property declared in a class extension might have user
2700 // declared setter or getter in current class extension or one
2701 // of the other class extensions. Mark them as synthesized as
2702 // property will be synthesized when property with same name is
2703 // seen in the @implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07002704 for (const auto *Ext : IDecl->visible_extensions()) {
2705 for (const auto *Property : Ext->properties()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002706 // Skip over properties declared @dynamic
2707 if (const ObjCPropertyImplDecl *PIDecl
2708 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2709 if (PIDecl->getPropertyImplementation()
2710 == ObjCPropertyImplDecl::Dynamic)
2711 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002712
Stephen Hines651f13c2014-04-23 16:59:28 -07002713 for (const auto *Ext : IDecl->visible_extensions()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002714 if (ObjCMethodDecl *GetterMethod
2715 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002716 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002717 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002718 if (ObjCMethodDecl *SetterMethod
2719 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002720 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002721 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002722 }
2723 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002724 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002725 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002726 DiagnoseOwningPropertyGetterSynthesis(IC);
Stephen Hines651f13c2014-04-23 16:59:28 -07002727 DiagnoseUnusedBackingIvarInAccessor(S, IC);
2728 if (IDecl->hasDesignatedInitializers())
2729 DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
2730
Patrick Beardb2f68202012-04-06 18:12:22 +00002731 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002732 if (IDecl->getSuperClass() == nullptr) {
Patrick Beardb2f68202012-04-06 18:12:22 +00002733 // This class has no superclass, so check that it has been marked with
2734 // __attribute((objc_root_class)).
2735 if (!HasRootClassAttr) {
2736 SourceLocation DeclLoc(IDecl->getLocation());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002737 SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc));
Patrick Beardb2f68202012-04-06 18:12:22 +00002738 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2739 << IDecl->getIdentifier();
2740 // See if NSObject is in the current scope, and if it is, suggest
2741 // adding " : NSObject " to the class declaration.
2742 NamedDecl *IF = LookupSingleName(TUScope,
2743 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2744 DeclLoc, LookupOrdinaryName);
2745 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2746 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2747 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2748 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2749 } else {
2750 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2751 }
2752 }
2753 } else if (HasRootClassAttr) {
2754 // Complain that only root classes may have this attribute.
2755 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2756 }
2757
John McCall260611a2012-06-20 06:18:46 +00002758 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002759 while (IDecl->getSuperClass()) {
2760 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2761 IDecl = IDecl->getSuperClass();
2762 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002763 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002764 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002765 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002766 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002767 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002768 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002769
Chris Lattner4d391482007-12-12 07:09:47 +00002770 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002771 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002772 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002773 if (ObjCCategoryDecl *Cat
2774 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2775 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002776 }
2777 }
2778 }
Chris Lattner682bf922009-03-29 16:50:03 +00002779 if (isInterfaceDeclKind) {
2780 // Reject invalid vardecls.
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002781 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov18062392013-08-27 13:15:56 +00002782 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner682bf922009-03-29 16:50:03 +00002783 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2784 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002785 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002786 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002787 }
Chris Lattner682bf922009-03-29 16:50:03 +00002788 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002789 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002790 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002791
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002792 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov18062392013-08-27 13:15:56 +00002793 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002794 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2795 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002796 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2797 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002798
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002799 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002800 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002801}
2802
2803
2804/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2805/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002806static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002807CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002808 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002809}
2810
Douglas Gregor926df6c2011-06-11 01:09:30 +00002811/// \brief Check whether the declared result type of the given Objective-C
2812/// method declaration is compatible with the method's class.
2813///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002814static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002815CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2816 ObjCInterfaceDecl *CurrentClass) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002817 QualType ResultType = Method->getReturnType();
2818
Douglas Gregor926df6c2011-06-11 01:09:30 +00002819 // If an Objective-C method inherits its related result type, then its
2820 // declared result type must be compatible with its own class type. The
2821 // declared result type is compatible if:
2822 if (const ObjCObjectPointerType *ResultObjectType
2823 = ResultType->getAs<ObjCObjectPointerType>()) {
2824 // - it is id or qualified id, or
2825 if (ResultObjectType->isObjCIdType() ||
2826 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002827 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002828
2829 if (CurrentClass) {
2830 if (ObjCInterfaceDecl *ResultClass
2831 = ResultObjectType->getInterfaceDecl()) {
2832 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002833 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002834 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002835
2836 // - it is a superclass of the method's class type
2837 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002838 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002839 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002840 } else {
2841 // Any Objective-C pointer type might be acceptable for a protocol
2842 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002843 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002844 }
2845 }
2846
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002847 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002848}
2849
John McCall6c2c2502011-07-22 02:45:48 +00002850namespace {
2851/// A helper class for searching for methods which a particular method
2852/// overrides.
2853class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002854public:
John McCall6c2c2502011-07-22 02:45:48 +00002855 Sema &S;
2856 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002857 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002858 bool Recursive;
2859
2860public:
2861 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2862 Selector selector = method->getSelector();
2863
2864 // Bypass this search if we've never seen an instance/class method
2865 // with this selector before.
2866 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2867 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002868 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002869 S.ReadMethodPool(selector);
2870
2871 it = S.MethodPool.find(selector);
2872 if (it == S.MethodPool.end())
2873 return;
John McCall6c2c2502011-07-22 02:45:48 +00002874 }
2875 ObjCMethodList &list =
2876 method->isInstanceMethod() ? it->second.first : it->second.second;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002877 if (!list.getMethod()) return;
John McCall6c2c2502011-07-22 02:45:48 +00002878
2879 ObjCContainerDecl *container
2880 = cast<ObjCContainerDecl>(method->getDeclContext());
2881
2882 // Prevent the search from reaching this container again. This is
2883 // important with categories, which override methods from the
2884 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002885 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2886 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002887 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2888 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002889 } else {
2890 searchFromContainer(container);
2891 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002892 }
John McCall6c2c2502011-07-22 02:45:48 +00002893
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002894 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002895 iterator begin() const { return Overridden.begin(); }
2896 iterator end() const { return Overridden.end(); }
2897
2898private:
2899 void searchFromContainer(ObjCContainerDecl *container) {
2900 if (container->isInvalidDecl()) return;
2901
2902 switch (container->getDeclKind()) {
2903#define OBJCCONTAINER(type, base) \
2904 case Decl::type: \
2905 searchFrom(cast<type##Decl>(container)); \
2906 break;
2907#define ABSTRACT_DECL(expansion)
2908#define DECL(type, base) \
2909 case Decl::type:
2910#include "clang/AST/DeclNodes.inc"
2911 llvm_unreachable("not an ObjC container!");
2912 }
2913 }
2914
2915 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002916 if (!protocol->hasDefinition())
2917 return;
2918
John McCall6c2c2502011-07-22 02:45:48 +00002919 // A method in a protocol declaration overrides declarations from
2920 // referenced ("parent") protocols.
2921 search(protocol->getReferencedProtocols());
2922 }
2923
2924 void searchFrom(ObjCCategoryDecl *category) {
2925 // A method in a category declaration overrides declarations from
2926 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002927 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002928 search(category->getReferencedProtocols());
2929 }
2930
2931 void searchFrom(ObjCCategoryImplDecl *impl) {
2932 // A method in a category definition that has a category
2933 // declaration overrides declarations from the category
2934 // declaration.
2935 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2936 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002937 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2938 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002939
2940 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002941 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2942 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002943 }
2944 }
2945
2946 void searchFrom(ObjCInterfaceDecl *iface) {
2947 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002948 if (!iface->hasDefinition())
2949 return;
2950
John McCall6c2c2502011-07-22 02:45:48 +00002951 // - categories,
Stephen Hines651f13c2014-04-23 16:59:28 -07002952 for (auto *Cat : iface->known_categories())
2953 search(Cat);
John McCall6c2c2502011-07-22 02:45:48 +00002954
2955 // - the super class, and
2956 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2957 search(super);
2958
2959 // - any referenced protocols.
2960 search(iface->getReferencedProtocols());
2961 }
2962
2963 void searchFrom(ObjCImplementationDecl *impl) {
2964 // A method in a class implementation overrides declarations from
2965 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002966 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2967 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002968 }
2969
2970
2971 void search(const ObjCProtocolList &protocols) {
2972 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2973 i != e; ++i)
2974 search(*i);
2975 }
2976
2977 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002978 // Check for a method in this container which matches this selector.
2979 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002980 Method->isInstanceMethod(),
2981 /*AllowHidden=*/true);
John McCall6c2c2502011-07-22 02:45:48 +00002982
2983 // If we find one, record it and bail out.
2984 if (meth) {
2985 Overridden.insert(meth);
2986 return;
2987 }
2988
2989 // Otherwise, search for methods that a hypothetical method here
2990 // would have overridden.
2991
2992 // Note that we're now in a recursive case.
2993 Recursive = true;
2994
2995 searchFromContainer(container);
2996 }
2997};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002998}
2999
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003000void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
3001 ObjCInterfaceDecl *CurrentClass,
3002 ResultTypeCompatibilityKind RTC) {
3003 // Search for overridden methods and merge information down from them.
3004 OverrideSearch overrides(*this, ObjCMethod);
3005 // Keep track if the method overrides any method in the class's base classes,
3006 // its protocols, or its categories' protocols; we will keep that info
3007 // in the ObjCMethodDecl.
3008 // For this info, a method in an implementation is not considered as
3009 // overriding the same method in the interface or its categories.
3010 bool hasOverriddenMethodsInBaseOrProtocol = false;
3011 for (OverrideSearch::iterator
3012 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
3013 ObjCMethodDecl *overridden = *i;
3014
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00003015 if (!hasOverriddenMethodsInBaseOrProtocol) {
3016 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
3017 CurrentClass != overridden->getClassInterface() ||
3018 overridden->isOverriding()) {
3019 hasOverriddenMethodsInBaseOrProtocol = true;
3020
3021 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
3022 // OverrideSearch will return as "overridden" the same method in the
3023 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
3024 // check whether a category of a base class introduced a method with the
3025 // same selector, after the interface method declaration.
3026 // To avoid unnecessary lookups in the majority of cases, we use the
3027 // extra info bits in GlobalMethodPool to check whether there were any
3028 // category methods with this selector.
3029 GlobalMethodPool::iterator It =
3030 MethodPool.find(ObjCMethod->getSelector());
3031 if (It != MethodPool.end()) {
3032 ObjCMethodList &List =
3033 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
3034 unsigned CategCount = List.getBits();
3035 if (CategCount > 0) {
3036 // If the method is in a category we'll do lookup if there were at
3037 // least 2 category methods recorded, otherwise only one will do.
3038 if (CategCount > 1 ||
3039 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3040 OverrideSearch overrides(*this, overridden);
3041 for (OverrideSearch::iterator
3042 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3043 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00003044 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3045 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00003046 hasOverriddenMethodsInBaseOrProtocol = true;
3047 overridden->setOverriding(true);
3048 break;
3049 }
3050 }
3051 }
3052 }
3053 }
3054 }
3055 }
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003056
3057 // Propagate down the 'related result type' bit from overridden methods.
3058 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3059 ObjCMethod->SetRelatedResultType();
3060
3061 // Then merge the declarations.
3062 mergeObjCMethodDecls(ObjCMethod, overridden);
3063
3064 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3065 continue; // Conflicting properties are detected elsewhere.
3066
3067 // Check for overriding methods
3068 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3069 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3070 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3071 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3072
3073 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00003074 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3075 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003076 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3077 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00003078 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3079 PrevE = overridden->param_end();
3080 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003081 assert(PrevI != overridden->param_end() && "Param mismatch");
3082 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3083 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3084 // If type of argument of method in this class does not match its
3085 // respective argument type in the super class method, issue warning;
3086 if (!Context.typesAreCompatible(T1, T2)) {
3087 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3088 << T1 << T2;
3089 Diag(overridden->getLocation(), diag::note_previous_declaration);
3090 break;
3091 }
3092 }
3093 }
3094 }
3095
3096 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3097}
3098
John McCalld226f652010-08-21 09:40:31 +00003099Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003100 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00003101 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003102 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00003103 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003104 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00003105 Selector Sel,
3106 // optional arguments. The number of types/arguments is obtained
3107 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00003108 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003109 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00003110 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003111 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003112 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003113 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003114 Diag(MethodLoc, diag::error_missing_method_context);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003115 return nullptr;
Steve Naroffda323ad2008-02-29 21:48:07 +00003116 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003117 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3118 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00003119 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Douglas Gregore97179c2011-09-08 01:46:34 +00003121 bool HasRelatedResultType = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003122 TypeSourceInfo *ReturnTInfo = nullptr;
Steve Naroffccef3712009-02-20 22:59:16 +00003123 if (ReturnType) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003124 resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00003125
Eli Friedmanddb5a392013-06-14 21:14:10 +00003126 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003127 return nullptr;
Eli Friedmanddb5a392013-06-14 21:14:10 +00003128
Douglas Gregore97179c2011-09-08 01:46:34 +00003129 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003130 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003131 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00003132 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003133 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003134 }
Mike Stump1eb44332009-09-09 15:08:12 +00003135
Stephen Hines651f13c2014-04-23 16:59:28 -07003136 ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create(
3137 Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext,
3138 MethodType == tok::minus, isVariadic,
3139 /*isPropertyAccessor=*/false,
3140 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
3141 MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional
3142 : ObjCMethodDecl::Required,
3143 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00003144
Chris Lattner5f9e2722011-07-23 10:55:15 +00003145 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00003146
Chris Lattner7db638d2009-04-11 19:42:43 +00003147 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00003148 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00003149 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00003150
David Blaikie7247c882013-05-15 07:37:26 +00003151 if (!ArgInfo[i].Type) {
John McCall58e46772009-10-23 21:48:59 +00003152 ArgType = Context.getObjCIdType();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003153 DI = nullptr;
Chris Lattnere294d3f2009-04-11 18:57:04 +00003154 } else {
John McCall58e46772009-10-23 21:48:59 +00003155 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00003156 }
Mike Stump1eb44332009-09-09 15:08:12 +00003157
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003158 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3159 LookupOrdinaryName, ForRedeclaration);
3160 LookupName(R, S);
3161 if (R.isSingleResult()) {
3162 NamedDecl *PrevDecl = R.getFoundDecl();
3163 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003164 Diag(ArgInfo[i].NameLoc,
3165 (MethodDefinition ? diag::warn_method_param_redefinition
3166 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003167 << ArgInfo[i].Name;
3168 Diag(PrevDecl->getLocation(),
3169 diag::note_previous_declaration);
3170 }
3171 }
3172
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003173 SourceLocation StartLoc = DI
3174 ? DI->getTypeLoc().getBeginLoc()
3175 : ArgInfo[i].NameLoc;
3176
John McCall81ef3e62011-04-23 02:46:06 +00003177 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3178 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003179 ArgType, DI, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00003180
John McCall70798862011-05-02 00:30:12 +00003181 Param->setObjCMethodScopeInfo(i);
3182
Chris Lattner0ed844b2008-04-04 06:12:32 +00003183 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00003184 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00003185
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00003186 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003187 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00003189 if (Param->hasAttr<BlocksAttr>()) {
3190 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3191 Param->setInvalidDecl();
3192 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003193 S->AddDecl(Param);
3194 IdResolver.AddDecl(Param);
3195
Chris Lattner0ed844b2008-04-04 06:12:32 +00003196 Params.push_back(Param);
3197 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003198
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003199 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00003200 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003201 QualType ArgType = Param->getType();
3202 if (ArgType.isNull())
3203 ArgType = Context.getObjCIdType();
3204 else
3205 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00003206 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedmanddb5a392013-06-14 21:14:10 +00003207
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003208 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003209 Params.push_back(Param);
3210 }
3211
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003212 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003213 ObjCMethod->setObjCDeclQualifier(
3214 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00003215
3216 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003217 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003219 // Add the method now.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003220 const ObjCMethodDecl *PrevMethod = nullptr;
John McCall6c2c2502011-07-22 02:45:48 +00003221 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00003222 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003223 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3224 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003225 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003226 PrevMethod = ImpDecl->getClassMethod(Sel);
3227 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003228 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00003229
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003230 ObjCMethodDecl *IMD = nullptr;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00003231 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3232 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3233 ObjCMethod->isInstanceMethod());
Fariborz Jahanian38b3bd82013-07-09 22:02:20 +00003234 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3235 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3236 // merge the attribute into implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07003237 ObjCMethod->addAttr(ObjCRequiresSuperAttr::CreateImplicit(Context,
3238 ObjCMethod->getLocation()));
Fariborz Jahanian38b3bd82013-07-09 22:02:20 +00003239 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003240 if (isa<ObjCCategoryImplDecl>(ImpDecl)) {
3241 ObjCMethodFamily family =
3242 ObjCMethod->getSelector().getMethodFamily();
3243 if (family == OMF_dealloc && IMD && IMD->isOverriding())
3244 Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
Ted Kremenek3306ec12012-02-27 22:55:11 +00003245 << ObjCMethod->getDeclName();
Fariborz Jahanianec236782011-12-06 00:02:41 +00003246 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003247 } else {
3248 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003249 }
John McCall6c2c2502011-07-22 02:45:48 +00003250
Chris Lattner4d391482007-12-12 07:09:47 +00003251 if (PrevMethod) {
3252 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003253 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003254 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003255 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanianfbff0c42013-05-13 17:27:00 +00003256 ObjCMethod->setInvalidDecl();
3257 return ObjCMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00003258 }
John McCall54abf7d2009-11-04 02:18:39 +00003259
Douglas Gregor926df6c2011-06-11 01:09:30 +00003260 // If this Objective-C method does not have a related result type, but we
3261 // are allowed to infer related result types, try to do so based on the
3262 // method family.
3263 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3264 if (!CurrentClass) {
3265 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3266 CurrentClass = Cat->getClassInterface();
3267 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3268 CurrentClass = Impl->getClassInterface();
3269 else if (ObjCCategoryImplDecl *CatImpl
3270 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3271 CurrentClass = CatImpl->getClassInterface();
3272 }
John McCall6c2c2502011-07-22 02:45:48 +00003273
Douglas Gregore97179c2011-09-08 01:46:34 +00003274 ResultTypeCompatibilityKind RTC
3275 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003276
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003277 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003278
John McCallf85e1932011-06-15 23:02:42 +00003279 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003280 if (getLangOpts().ObjCAutoRefCount)
John McCallb8463812013-04-04 01:38:37 +00003281 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCallf85e1932011-06-15 23:02:42 +00003282
Douglas Gregore97179c2011-09-08 01:46:34 +00003283 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003284 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003285 !ObjCMethod->hasRelatedResultType() &&
3286 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003287 bool InferRelatedResultType = false;
3288 switch (ObjCMethod->getMethodFamily()) {
3289 case OMF_None:
3290 case OMF_copy:
3291 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003292 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003293 case OMF_mutableCopy:
3294 case OMF_release:
3295 case OMF_retainCount:
Stephen Hines176edba2014-12-01 14:53:08 -08003296 case OMF_initialize:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003297 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003298 break;
3299
3300 case OMF_alloc:
3301 case OMF_new:
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003302 InferRelatedResultType = ObjCMethod->isClassMethod();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003303 break;
3304
3305 case OMF_init:
3306 case OMF_autorelease:
3307 case OMF_retain:
3308 case OMF_self:
3309 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3310 break;
3311 }
3312
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003313 if (InferRelatedResultType &&
3314 !ObjCMethod->getReturnType()->isObjCIndependentClassType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00003315 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003316 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003317
3318 ActOnDocumentableDecl(ObjCMethod);
3319
John McCalld226f652010-08-21 09:40:31 +00003320 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003321}
3322
Chris Lattnercc98eac2008-12-17 07:13:27 +00003323bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003324 // Following is also an error. But it is caused by a missing @end
3325 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003326 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003327 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003328
3329 // If we switched context to translation unit while we are still lexically in
3330 // an objc container, it means the parser missed emitting an error.
3331 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3332 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003333
Anders Carlsson15281452008-11-04 16:57:32 +00003334 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3335 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003336
Anders Carlsson15281452008-11-04 16:57:32 +00003337 return true;
3338}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003339
James Dennett1dfbd922012-06-14 21:40:34 +00003340/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003341/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003342void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003343 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003344 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003345 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003346 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003347 if (!Class) {
3348 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3349 return;
3350 }
John McCall260611a2012-06-20 06:18:46 +00003351 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003352 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3353 return;
3354 }
Mike Stump1eb44332009-09-09 15:08:12 +00003355
Chris Lattnercc98eac2008-12-17 07:13:27 +00003356 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003357 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003358 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003359 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003360 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003361 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003362 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003363 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3364 /*FIXME: StartL=*/ID->getLocation(),
3365 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003366 ID->getIdentifier(), ID->getType(),
3367 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003368 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003369 }
Mike Stump1eb44332009-09-09 15:08:12 +00003370
Chris Lattnercc98eac2008-12-17 07:13:27 +00003371 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003372 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003373 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003374 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003375 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003376 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003377 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003378 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003379 }
3380}
3381
Douglas Gregor160b5632010-04-26 17:32:49 +00003382/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003383VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3384 SourceLocation StartLoc,
3385 SourceLocation IdLoc,
3386 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003387 bool Invalid) {
3388 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3389 // duration shall not be qualified by an address-space qualifier."
3390 // Since all parameters have automatic store duration, they can not have
3391 // an address space.
3392 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003393 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003394 Invalid = true;
3395 }
3396
3397 // An @catch parameter must be an unqualified object pointer type;
3398 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3399 if (Invalid) {
3400 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003401 } else if (T->isDependentType()) {
3402 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003403 } else if (!T->isObjCObjectPointerType()) {
3404 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003405 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003406 } else if (T->isObjCQualifiedIdType()) {
3407 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003408 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003409 }
3410
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003411 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003412 T, TInfo, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003413 New->setExceptionVariable(true);
3414
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003415 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003416 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003417 Invalid = true;
3418
Douglas Gregor160b5632010-04-26 17:32:49 +00003419 if (Invalid)
3420 New->setInvalidDecl();
3421 return New;
3422}
3423
John McCalld226f652010-08-21 09:40:31 +00003424Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003425 const DeclSpec &DS = D.getDeclSpec();
3426
3427 // We allow the "register" storage class on exception variables because
3428 // GCC did, but we drop it completely. Any other storage class is an error.
3429 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3430 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3431 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithec642442013-04-12 22:46:28 +00003432 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003433 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithec642442013-04-12 22:46:28 +00003434 << DeclSpec::getSpecifierName(SCS);
3435 }
3436 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3437 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3438 diag::err_invalid_thread)
3439 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregor160b5632010-04-26 17:32:49 +00003440 D.getMutableDeclSpec().ClearStorageClassSpecs();
3441
Richard Smithc7f81162013-03-18 22:52:47 +00003442 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregor160b5632010-04-26 17:32:49 +00003443
3444 // Check that there are no default arguments inside the type of this
3445 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003446 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003447 CheckExtraCXXDefaultArguments(D);
3448
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003449 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003450 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003451
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003452 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3453 D.getSourceRange().getBegin(),
3454 D.getIdentifierLoc(),
3455 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003456 D.isInvalidType());
3457
3458 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3459 if (D.getCXXScopeSpec().isSet()) {
3460 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3461 << D.getCXXScopeSpec().getRange();
3462 New->setInvalidDecl();
3463 }
3464
3465 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003466 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003467 if (D.getIdentifier())
3468 IdResolver.AddDecl(New);
3469
3470 ProcessDeclAttributes(S, New, D);
3471
3472 if (New->hasAttr<BlocksAttr>())
3473 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003474 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003475}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003476
3477/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003478/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003479void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003480 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003481 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3482 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003483 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003484 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003485 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003486 }
3487}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003488
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003489void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003490 // Load referenced selectors from the external source.
3491 if (ExternalSource) {
3492 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3493 ExternalSource->ReadReferencedSelectors(Sels);
3494 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3495 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3496 }
3497
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003498 // Warning will be issued only when selector table is
3499 // generated (which means there is at lease one implementation
3500 // in the TU). This is to match gcc's behavior.
3501 if (ReferencedSelectors.empty() ||
3502 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003503 return;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003504 for (auto &SelectorAndLocation : ReferencedSelectors) {
3505 Selector Sel = SelectorAndLocation.first;
3506 SourceLocation Loc = SelectorAndLocation.second;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003507 if (!LookupImplementedMethodInGlobalPool(Sel))
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003508 Diag(Loc, diag::warn_unimplemented_selector) << Sel;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003509 }
3510 return;
3511}
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003512
3513ObjCIvarDecl *
3514Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3515 const ObjCPropertyDecl *&PDecl) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07003516 if (Method->isClassMethod())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003517 return nullptr;
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003518 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3519 if (!IDecl)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003520 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07003521 Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true,
3522 /*shallowCategoryLookup=*/false,
3523 /*followSuper=*/false);
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003524 if (!Method || !Method->isPropertyAccessor())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003525 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07003526 if ((PDecl = Method->findPropertyDecl()))
3527 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) {
3528 // property backing ivar must belong to property's class
3529 // or be a private ivar in class's implementation.
3530 // FIXME. fix the const-ness issue.
3531 IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable(
3532 IV->getIdentifier());
3533 return IV;
3534 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003535 return nullptr;
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003536}
3537
Stephen Hines651f13c2014-04-23 16:59:28 -07003538namespace {
3539 /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property
3540 /// accessor references the backing ivar.
3541 class UnusedBackingIvarChecker :
3542 public DataRecursiveASTVisitor<UnusedBackingIvarChecker> {
3543 public:
3544 Sema &S;
3545 const ObjCMethodDecl *Method;
3546 const ObjCIvarDecl *IvarD;
3547 bool AccessedIvar;
3548 bool InvokedSelfMethod;
3549
3550 UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method,
3551 const ObjCIvarDecl *IvarD)
3552 : S(S), Method(Method), IvarD(IvarD),
3553 AccessedIvar(false), InvokedSelfMethod(false) {
3554 assert(IvarD);
3555 }
3556
3557 bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
3558 if (E->getDecl() == IvarD) {
3559 AccessedIvar = true;
3560 return false;
3561 }
3562 return true;
3563 }
3564
3565 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
3566 if (E->getReceiverKind() == ObjCMessageExpr::Instance &&
3567 S.isSelfExpr(E->getInstanceReceiver(), Method)) {
3568 InvokedSelfMethod = true;
3569 }
3570 return true;
3571 }
3572 };
3573}
3574
3575void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S,
3576 const ObjCImplementationDecl *ImplD) {
3577 if (S->hasUnrecoverableErrorOccurred())
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003578 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07003579
3580 for (const auto *CurMethod : ImplD->instance_methods()) {
3581 unsigned DIAG = diag::warn_unused_property_backing_ivar;
3582 SourceLocation Loc = CurMethod->getLocation();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003583 if (Diags.isIgnored(DIAG, Loc))
Stephen Hines651f13c2014-04-23 16:59:28 -07003584 continue;
3585
3586 const ObjCPropertyDecl *PDecl;
3587 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3588 if (!IV)
3589 continue;
3590
3591 UnusedBackingIvarChecker Checker(*this, CurMethod, IV);
3592 Checker.TraverseStmt(CurMethod->getBody());
3593 if (Checker.AccessedIvar)
3594 continue;
3595
3596 // Do not issue this warning if backing ivar is used somewhere and accessor
3597 // implementation makes a self call. This is to prevent false positive in
3598 // cases where the ivar is accessed by another method that the accessor
3599 // delegates to.
3600 if (!IV->isReferenced() || !Checker.InvokedSelfMethod) {
3601 Diag(Loc, DIAG) << IV;
3602 Diag(PDecl->getLocation(), diag::note_property_declare);
3603 }
Fariborz Jahanian4e7f00c2013-10-25 21:44:50 +00003604 }
3605}