blob: ef978f0730a1b398c962314b54e46507ce1fa0b2 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000026#include "llvm/ADT/DenseSet.h"
27
Chris Lattner4d391482007-12-12 07:09:47 +000028using namespace clang;
29
John McCallf85e1932011-06-15 23:02:42 +000030/// Check whether the given method, which must be in the 'init'
31/// family, is a valid member of that family.
32///
33/// \param receiverTypeIfCall - if null, check this as if declaring it;
34/// if non-null, check this as if making a call to it with the given
35/// receiver type
36///
37/// \return true to indicate that there was an error and appropriate
38/// actions were taken
39bool Sema::checkInitMethod(ObjCMethodDecl *method,
40 QualType receiverTypeIfCall) {
41 if (method->isInvalidDecl()) return true;
42
43 // This castAs is safe: methods that don't return an object
44 // pointer won't be inferred as inits and will reject an explicit
45 // objc_method_family(init).
46
47 // We ignore protocols here. Should we? What about Class?
48
49 const ObjCObjectType *result = method->getResultType()
50 ->castAs<ObjCObjectPointerType>()->getObjectType();
51
52 if (result->isObjCId()) {
53 return false;
54 } else if (result->isObjCClass()) {
55 // fall through: always an error
56 } else {
57 ObjCInterfaceDecl *resultClass = result->getInterface();
58 assert(resultClass && "unexpected object type!");
59
60 // It's okay for the result type to still be a forward declaration
61 // if we're checking an interface declaration.
62 if (resultClass->isForwardDecl()) {
63 if (receiverTypeIfCall.isNull() &&
64 !isa<ObjCImplementationDecl>(method->getDeclContext()))
65 return false;
66
67 // Otherwise, we try to compare class types.
68 } else {
69 // If this method was declared in a protocol, we can't check
70 // anything unless we have a receiver type that's an interface.
71 const ObjCInterfaceDecl *receiverClass = 0;
72 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
73 if (receiverTypeIfCall.isNull())
74 return false;
75
76 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
77 ->getInterfaceDecl();
78
79 // This can be null for calls to e.g. id<Foo>.
80 if (!receiverClass) return false;
81 } else {
82 receiverClass = method->getClassInterface();
83 assert(receiverClass && "method not associated with a class!");
84 }
85
86 // If either class is a subclass of the other, it's fine.
87 if (receiverClass->isSuperClassOf(resultClass) ||
88 resultClass->isSuperClassOf(receiverClass))
89 return false;
90 }
91 }
92
93 SourceLocation loc = method->getLocation();
94
95 // If we're in a system header, and this is not a call, just make
96 // the method unusable.
97 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
98 method->addAttr(new (Context) UnavailableAttr(loc, Context,
99 "init method returns a type unrelated to its receiver type"));
100 return true;
101 }
102
103 // Otherwise, it's an error.
104 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
105 method->setInvalidDecl();
106 return true;
107}
108
Douglas Gregor926df6c2011-06-11 01:09:30 +0000109bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
110 const ObjCMethodDecl *Overridden,
111 bool IsImplementation) {
112 if (Overridden->hasRelatedResultType() &&
113 !NewMethod->hasRelatedResultType()) {
114 // This can only happen when the method follows a naming convention that
115 // implies a related result type, and the original (overridden) method has
116 // a suitable return type, but the new (overriding) method does not have
117 // a suitable return type.
118 QualType ResultType = NewMethod->getResultType();
119 SourceRange ResultTypeRange;
120 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000121 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000122 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
123
124 // Figure out which class this method is part of, if any.
125 ObjCInterfaceDecl *CurrentClass
126 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
127 if (!CurrentClass) {
128 DeclContext *DC = NewMethod->getDeclContext();
129 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
130 CurrentClass = Cat->getClassInterface();
131 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
132 CurrentClass = Impl->getClassInterface();
133 else if (ObjCCategoryImplDecl *CatImpl
134 = dyn_cast<ObjCCategoryImplDecl>(DC))
135 CurrentClass = CatImpl->getClassInterface();
136 }
137
138 if (CurrentClass) {
139 Diag(NewMethod->getLocation(),
140 diag::warn_related_result_type_compatibility_class)
141 << Context.getObjCInterfaceType(CurrentClass)
142 << ResultType
143 << ResultTypeRange;
144 } else {
145 Diag(NewMethod->getLocation(),
146 diag::warn_related_result_type_compatibility_protocol)
147 << ResultType
148 << ResultTypeRange;
149 }
150
151 Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
152 << Overridden->getMethodFamily();
153 }
154
155 return false;
156}
157
John McCallf85e1932011-06-15 23:02:42 +0000158/// \brief Check a method declaration for compatibility with the Objective-C
159/// ARC conventions.
160static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
161 ObjCMethodFamily family = method->getMethodFamily();
162 switch (family) {
163 case OMF_None:
164 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000165 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000166 case OMF_retain:
167 case OMF_release:
168 case OMF_autorelease:
169 case OMF_retainCount:
170 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000171 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000172 return false;
173
174 case OMF_init:
175 // If the method doesn't obey the init rules, don't bother annotating it.
176 if (S.checkInitMethod(method, QualType()))
177 return true;
178
179 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
180 S.Context));
181
182 // Don't add a second copy of this attribute, but otherwise don't
183 // let it be suppressed.
184 if (method->hasAttr<NSReturnsRetainedAttr>())
185 return false;
186 break;
187
188 case OMF_alloc:
189 case OMF_copy:
190 case OMF_mutableCopy:
191 case OMF_new:
192 if (method->hasAttr<NSReturnsRetainedAttr>() ||
193 method->hasAttr<NSReturnsNotRetainedAttr>() ||
194 method->hasAttr<NSReturnsAutoreleasedAttr>())
195 return false;
196 break;
197 }
198
199 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
200 S.Context));
201 return false;
202}
203
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000204static void DiagnoseObjCImplementedDeprecations(Sema &S,
205 NamedDecl *ND,
206 SourceLocation ImplLoc,
207 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000208 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000209 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000210 if (select == 0)
211 S.Diag(ND->getLocation(), diag::note_method_declared_at);
212 else
213 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
214 }
215}
216
Steve Naroffebf64432009-02-28 16:59:13 +0000217/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000218/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000219void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000220 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000221 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Steve Naroff394f3f42008-07-25 17:57:26 +0000223 // If we don't have a valid method decl, simply return.
224 if (!MDecl)
225 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000226
227 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000228 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000229 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000230 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000231 AddFactoryMethodToGlobalPool(MDecl, true);
232
Chris Lattner4d391482007-12-12 07:09:47 +0000233 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000234 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000235 PushFunctionScope();
236
Chris Lattner4d391482007-12-12 07:09:47 +0000237 // Create Decl objects for each parameter, entrring them in the scope for
238 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000239
240 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000241 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Daniel Dunbar451318c2008-08-26 06:07:48 +0000243 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
244 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000245
Chris Lattner8123a952008-04-10 02:22:51 +0000246 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000247 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000248 E = MDecl->param_end(); PI != E; ++PI) {
249 ParmVarDecl *Param = (*PI);
250 if (!Param->isInvalidDecl() &&
251 RequireCompleteType(Param->getLocation(), Param->getType(),
252 diag::err_typecheck_decl_incomplete_type))
253 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000254 if ((*PI)->getIdentifier())
255 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000256 }
John McCallf85e1932011-06-15 23:02:42 +0000257
258 // In ARC, disallow definition of retain/release/autorelease/retainCount
259 if (getLangOptions().ObjCAutoRefCount) {
260 switch (MDecl->getMethodFamily()) {
261 case OMF_retain:
262 case OMF_retainCount:
263 case OMF_release:
264 case OMF_autorelease:
265 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
266 << MDecl->getSelector();
267 break;
268
269 case OMF_None:
270 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000271 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000272 case OMF_alloc:
273 case OMF_init:
274 case OMF_mutableCopy:
275 case OMF_copy:
276 case OMF_new:
277 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000278 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000279 break;
280 }
281 }
282
Nico Weber9a1ecf02011-08-22 17:25:57 +0000283 // Warn on deprecated methods under -Wdeprecated-implementations,
284 // and prepare for warning on missing super calls.
285 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000286 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000287 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000288 DiagnoseObjCImplementedDeprecations(*this,
289 dyn_cast<NamedDecl>(IMD),
290 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000291
Nico Weber80cb6e62011-08-28 22:35:17 +0000292 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000293 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
294 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
295 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000296 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000297 ObjCShouldCallSuperDealloc =
298 !Context.getLangOptions().ObjCAutoRefCount &&
299 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000300 ObjCShouldCallSuperFinalize =
301 !Context.getLangOptions().ObjCAutoRefCount &&
302 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000303 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000304 }
Chris Lattner4d391482007-12-12 07:09:47 +0000305}
306
John McCalld226f652010-08-21 09:40:31 +0000307Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000308ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
309 IdentifierInfo *ClassName, SourceLocation ClassLoc,
310 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000311 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000312 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000313 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000314 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattner4d391482007-12-12 07:09:47 +0000316 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000317 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000318 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000319
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000320 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000321 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000322 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000325 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
326 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000327 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000328 if (!IDecl->isForwardDecl()) {
329 IDecl->setInvalidDecl();
330 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
331 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000332
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000333 // Return the previous class interface.
334 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000335 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000336 } else {
337 IDecl->setLocation(AtInterfaceLoc);
338 IDecl->setForwardDecl(false);
339 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000340 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000341 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000342 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000343
344 // Since this ObjCInterfaceDecl was created by a forward declaration,
345 // we now add it to the DeclContext since it wasn't added before
346 // (see ActOnForwardClassDeclaration).
347 IDecl->setLexicalDeclContext(CurContext);
348 CurContext->addDecl(IDecl);
349
350 if (AttrList)
351 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000352 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000353 } else {
354 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
355 ClassName, ClassLoc);
356 if (AttrList)
357 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
358
359 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattner4d391482007-12-12 07:09:47 +0000362 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000363 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000364 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
365 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000366
367 if (!PrevDecl) {
368 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000369 TypoCorrection Corrected = CorrectTypo(
370 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
371 NULL, NULL, false, CTC_NoKeywords);
372 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000373 Diag(SuperLoc, diag::err_undef_superclass_suggest)
374 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000375 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
376 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000377 }
378 }
379
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000380 if (PrevDecl == IDecl) {
381 Diag(SuperLoc, diag::err_recursive_superclass)
382 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
383 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000384 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000385 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000386 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000387
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000388 // Diagnose classes that inherit from deprecated classes.
389 if (SuperClassDecl)
390 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000392 if (PrevDecl && SuperClassDecl == 0) {
393 // The previous declaration was not a class decl. Check if we have a
394 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000395 if (const TypedefNameDecl *TDecl =
396 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000397 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000398 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000399 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
400 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000401 }
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000404 // This handles the following case:
405 //
406 // typedef int SuperClass;
407 // @interface MyClass : SuperClass {} @end
408 //
409 if (!SuperClassDecl) {
410 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
411 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000412 }
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Richard Smith162e1c12011-04-15 14:24:37 +0000415 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000416 if (!SuperClassDecl)
417 Diag(SuperLoc, diag::err_undef_superclass)
418 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000419 else if (SuperClassDecl->isForwardDecl()) {
420 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000421 << SuperClassDecl->getDeclName() << ClassName
422 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000423 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
424 SuperClassDecl = 0;
425 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000426 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000427 IDecl->setSuperClass(SuperClassDecl);
428 IDecl->setSuperClassLoc(SuperLoc);
429 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000430 }
Chris Lattner4d391482007-12-12 07:09:47 +0000431 } else { // we have a root class.
432 IDecl->setLocEnd(ClassLoc);
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Sebastian Redl0b17c612010-08-13 00:28:03 +0000435 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000436 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000437 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000438 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000439 IDecl->setLocEnd(EndProtoLoc);
440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlsson15281452008-11-04 16:57:32 +0000442 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000443 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000444}
445
446/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000447/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000448Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
449 IdentifierInfo *AliasName,
450 SourceLocation AliasLocation,
451 IdentifierInfo *ClassName,
452 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000453 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000454 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000455 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000456 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000457 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000458 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000459 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000460 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000461 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000462 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000463 }
464 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000465 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000466 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000467 if (const TypedefNameDecl *TDecl =
468 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000469 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000470 if (T->isObjCObjectType()) {
471 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000472 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000473 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000474 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000475 }
476 }
477 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000478 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
479 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000480 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000481 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000482 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000483 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000486 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000487 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000488 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Anders Carlsson15281452008-11-04 16:57:32 +0000490 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000491 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000492
John McCalld226f652010-08-21 09:40:31 +0000493 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000494}
495
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000496bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000497 IdentifierInfo *PName,
498 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000499 const ObjCList<ObjCProtocolDecl> &PList) {
500
501 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000502 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
503 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000504 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
505 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000506 if (PDecl->getIdentifier() == PName) {
507 Diag(Ploc, diag::err_protocol_has_circular_dependency);
508 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000509 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000510 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000511 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
512 PDecl->getLocation(), PDecl->getReferencedProtocols()))
513 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000514 }
515 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000516 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000517}
518
John McCalld226f652010-08-21 09:40:31 +0000519Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000520Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
521 IdentifierInfo *ProtocolName,
522 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000523 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000524 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000525 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000526 SourceLocation EndProtoLoc,
527 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000528 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000529 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000530 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000531 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000532 if (PDecl) {
533 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000534 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000535 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000536 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000537 // Just return the protocol we already had.
538 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000539 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000540 }
Steve Naroff61d68522009-03-05 15:22:01 +0000541 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000542 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000543 err = CheckForwardProtocolDeclarationForCircularDependency(
544 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Steve Narofff11b5082008-08-13 16:39:22 +0000546 // Make sure the cached decl gets a valid start location.
547 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000548 PDecl->setForwardDecl(false);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000549 // Since this ObjCProtocolDecl was created by a forward declaration,
550 // we now add it to the DeclContext since it wasn't added before
551 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000552 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000553 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000554 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000555 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000556 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000557 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000558 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000559 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000560 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000561 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000562 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000563 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000564 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000565 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
566 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000567 PDecl->setLocEnd(EndProtoLoc);
568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
570 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000571 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000572}
573
574/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000575/// issues an error if they are not declared. It returns list of
576/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000577void
Chris Lattnere13b9592008-07-26 04:03:38 +0000578Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000579 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000580 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000581 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000582 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000583 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
584 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000585 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000586 TypoCorrection Corrected = CorrectTypo(
587 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
588 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
589 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000590 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000591 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000592 Diag(PDecl->getLocation(), diag::note_previous_decl)
593 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000594 }
595 }
596
597 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000598 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000599 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000600 continue;
601 }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000603 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000604
605 // If this is a forward declaration and we are supposed to warn in this
606 // case, do it.
607 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000608 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000609 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000610 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000611 }
612}
613
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000614/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000615/// a class method in its extension.
616///
Mike Stump1eb44332009-09-09 15:08:12 +0000617void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000618 ObjCInterfaceDecl *ID) {
619 if (!ID)
620 return; // Possibly due to previous error
621
622 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000623 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
624 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000625 ObjCMethodDecl *MD = *i;
626 MethodMap[MD->getSelector()] = MD;
627 }
628
629 if (MethodMap.empty())
630 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000631 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
632 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000633 ObjCMethodDecl *Method = *i;
634 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
635 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
636 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
637 << Method->getDeclName();
638 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
639 }
640 }
641}
642
Chris Lattner58fe03b2009-04-12 08:43:13 +0000643/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000644Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000645Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000646 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000647 unsigned NumElts,
648 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000649 SmallVector<ObjCProtocolDecl*, 32> Protocols;
650 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattner4d391482007-12-12 07:09:47 +0000652 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000653 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000654 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000655 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000656 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000657 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000658 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000659 PushOnScopeChains(PDecl, TUScope, false);
660 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000661 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000662 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000663 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000664 if (!isNew)
665 PDecl->setChangedSinceDeserialization(true);
666 }
Chris Lattner4d391482007-12-12 07:09:47 +0000667 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000668 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
671 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000672 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000673 Protocols.data(), Protocols.size(),
674 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000675 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000676 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000677 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000678}
679
John McCalld226f652010-08-21 09:40:31 +0000680Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000681ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
682 IdentifierInfo *ClassName, SourceLocation ClassLoc,
683 IdentifierInfo *CategoryName,
684 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000685 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000686 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000687 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000688 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000689 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000690 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000691
692 /// Check that class of this category is already completely declared.
693 if (!IDecl || IDecl->isForwardDecl()) {
694 // Create an invalid ObjCCategoryDecl to serve as context for
695 // the enclosing method declarations. We mark the decl invalid
696 // to make it clear that this isn't a valid AST.
697 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
698 ClassLoc, CategoryLoc, CategoryName);
699 CDecl->setInvalidDecl();
700 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000701 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000702 }
703
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000704 if (!CategoryName && IDecl->getImplementation()) {
705 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
706 Diag(IDecl->getImplementation()->getLocation(),
707 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000708 }
709
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000710 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
711 ClassLoc, CategoryLoc, CategoryName);
712 // FIXME: PushOnScopeChains?
713 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000714
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000715 CDecl->setClassInterface(IDecl);
716 // Insert class extension to the list of class's categories.
717 if (!CategoryName)
718 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Chris Lattner16b34b42009-02-16 21:30:01 +0000720 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000721 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000722
Fariborz Jahanian25760612010-02-15 21:55:26 +0000723 if (CategoryName) {
724 /// Check for duplicate interface declaration for this category
725 ObjCCategoryDecl *CDeclChain;
726 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
727 CDeclChain = CDeclChain->getNextClassCategory()) {
728 if (CDeclChain->getIdentifier() == CategoryName) {
729 // Class extensions can be declared multiple times.
730 Diag(CategoryLoc, diag::warn_dup_category_def)
731 << ClassName << CategoryName;
732 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
733 break;
734 }
Chris Lattner70f19542009-02-16 21:26:43 +0000735 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000736 if (!CDeclChain)
737 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000738 }
Chris Lattner70f19542009-02-16 21:26:43 +0000739
Chris Lattner4d391482007-12-12 07:09:47 +0000740 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000741 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000742 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000743 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000744 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000745 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000746 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Anders Carlsson15281452008-11-04 16:57:32 +0000749 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000750 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000751}
752
753/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000754/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000755/// object.
John McCalld226f652010-08-21 09:40:31 +0000756Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000757 SourceLocation AtCatImplLoc,
758 IdentifierInfo *ClassName, SourceLocation ClassLoc,
759 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000760 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000761 ObjCCategoryDecl *CatIDecl = 0;
762 if (IDecl) {
763 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
764 if (!CatIDecl) {
765 // Category @implementation with no corresponding @interface.
766 // Create and install one.
767 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000768 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000769 CatName);
770 CatIDecl->setClassInterface(IDecl);
771 CatIDecl->insertNextClassCategory();
772 }
773 }
774
Mike Stump1eb44332009-09-09 15:08:12 +0000775 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000776 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
777 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000778 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000779 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000780 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000781 CDecl->setInvalidDecl();
782 }
Chris Lattner4d391482007-12-12 07:09:47 +0000783
Douglas Gregord0434102009-01-09 00:49:46 +0000784 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000785 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000786
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000787 /// Check that CatName, category name, is not used in another implementation.
788 if (CatIDecl) {
789 if (CatIDecl->getImplementation()) {
790 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
791 << CatName;
792 Diag(CatIDecl->getImplementation()->getLocation(),
793 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000794 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000795 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000796 // Warn on implementating category of deprecated class under
797 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000798 DiagnoseObjCImplementedDeprecations(*this,
799 dyn_cast<NamedDecl>(IDecl),
800 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000801 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Anders Carlsson15281452008-11-04 16:57:32 +0000804 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000805 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000806}
807
John McCalld226f652010-08-21 09:40:31 +0000808Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000809 SourceLocation AtClassImplLoc,
810 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000811 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000812 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000813 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000814 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000815 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000816 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
817 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000819 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000820 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000821 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
822 // If this is a forward declaration of an interface, warn.
823 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000824 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000825 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000826 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000827 } else {
828 // We did not find anything with the name ClassName; try to correct for
829 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000830 TypoCorrection Corrected = CorrectTypo(
831 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
832 NULL, NULL, false, CTC_NoKeywords);
833 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000834 // Suggest the (potentially) correct interface name. However, put the
835 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000836 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000837 // provide a code-modification hint or use the typo name for recovery,
838 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000839 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000840 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000841 << ClassName << CorrectedName;
842 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
843 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000844 IDecl = 0;
845 } else {
846 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
847 }
Chris Lattner4d391482007-12-12 07:09:47 +0000848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Chris Lattner4d391482007-12-12 07:09:47 +0000850 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000852 if (SuperClassname) {
853 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000854 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
855 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000856 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000857 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
858 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000859 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000860 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000861 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000862 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000863 Diag(SuperClassLoc, diag::err_undef_superclass)
864 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000865 else if (IDecl && IDecl->getSuperClass() != SDecl) {
866 // This implementation and its interface do not have the same
867 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000868 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000869 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000870 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000871 }
872 }
873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Chris Lattner4d391482007-12-12 07:09:47 +0000875 if (!IDecl) {
876 // Legacy case of @implementation with no corresponding @interface.
877 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000878
Mike Stump390b4cc2009-05-16 07:39:55 +0000879 // FIXME: Do we support attributes on the @implementation? If so we should
880 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000881 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000882 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000883 IDecl->setSuperClass(SDecl);
884 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000885
886 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000887 } else {
888 // Mark the interface as being completed, even if it was just as
889 // @class ....;
890 // declaration; the user cannot reopen it.
891 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
894 ObjCImplementationDecl* IMPDecl =
895 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000896 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Anders Carlsson15281452008-11-04 16:57:32 +0000898 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000899 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattner4d391482007-12-12 07:09:47 +0000901 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000902 if (IDecl->getImplementation()) {
903 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000904 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000905 Diag(IDecl->getImplementation()->getLocation(),
906 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000907 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000908 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000909 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000910 // Warn on implementating deprecated class under
911 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000912 DiagnoseObjCImplementedDeprecations(*this,
913 dyn_cast<NamedDecl>(IDecl),
914 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000915 }
John McCalld226f652010-08-21 09:40:31 +0000916 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000917}
918
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000919void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
920 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000921 SourceLocation RBrace) {
922 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000923 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000924 if (!IDecl)
925 return;
926 /// Check case of non-existing @interface decl.
927 /// (legacy objective-c @implementation decl without an @interface decl).
928 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000929 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000930 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000931 // Add ivar's to class's DeclContext.
932 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000933 ivars[i]->setLexicalDeclContext(ImpDecl);
934 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000935 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000936 }
937
Chris Lattner4d391482007-12-12 07:09:47 +0000938 return;
939 }
940 // If implementation has empty ivar list, just return.
941 if (numIvars == 0)
942 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Chris Lattner4d391482007-12-12 07:09:47 +0000944 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000945 if (LangOpts.ObjCNonFragileABI2) {
946 if (ImpDecl->getSuperClass())
947 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
948 for (unsigned i = 0; i < numIvars; i++) {
949 ObjCIvarDecl* ImplIvar = ivars[i];
950 if (const ObjCIvarDecl *ClsIvar =
951 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
952 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
953 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
954 continue;
955 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000956 // Instance ivar to Implementation's DeclContext.
957 ImplIvar->setLexicalDeclContext(ImpDecl);
958 IDecl->makeDeclVisibleInContext(ImplIvar, false);
959 ImpDecl->addDecl(ImplIvar);
960 }
961 return;
962 }
Chris Lattner4d391482007-12-12 07:09:47 +0000963 // Check interface's Ivar list against those in the implementation.
964 // names and types must match.
965 //
Chris Lattner4d391482007-12-12 07:09:47 +0000966 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000967 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000968 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
969 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000970 ObjCIvarDecl* ImplIvar = ivars[j++];
971 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000972 assert (ImplIvar && "missing implementation ivar");
973 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Steve Naroffca331292009-03-03 14:49:36 +0000975 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000976 if (Context.getCanonicalType(ImplIvar->getType()) !=
977 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000978 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000979 << ImplIvar->getIdentifier()
980 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000981 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000982 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
983 Expr *ImplBitWidth = ImplIvar->getBitWidth();
984 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000985 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
986 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000987 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
988 << ImplIvar->getIdentifier();
989 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991 }
Steve Naroffca331292009-03-03 14:49:36 +0000992 // Make sure the names are identical.
993 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000994 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000995 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000996 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000997 }
998 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattner609e4c72007-12-12 18:11:49 +00001001 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001002 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001003 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001004 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001005}
1006
Steve Naroff3c2eb662008-02-10 21:38:56 +00001007void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001008 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001009 // No point warning no definition of method which is 'unavailable'.
1010 if (method->hasAttr<UnavailableAttr>())
1011 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001012 if (!IncompleteImpl) {
1013 Diag(ImpLoc, diag::warn_incomplete_impl);
1014 IncompleteImpl = true;
1015 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001016 if (DiagID == diag::warn_unimplemented_protocol_method)
1017 Diag(ImpLoc, DiagID) << method->getDeclName();
1018 else
1019 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001020}
1021
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001022/// Determines if type B can be substituted for type A. Returns true if we can
1023/// guarantee that anything that the user will do to an object of type A can
1024/// also be done to an object of type B. This is trivially true if the two
1025/// types are the same, or if B is a subclass of A. It becomes more complex
1026/// in cases where protocols are involved.
1027///
1028/// Object types in Objective-C describe the minimum requirements for an
1029/// object, rather than providing a complete description of a type. For
1030/// example, if A is a subclass of B, then B* may refer to an instance of A.
1031/// The principle of substitutability means that we may use an instance of A
1032/// anywhere that we may use an instance of B - it will implement all of the
1033/// ivars of B and all of the methods of B.
1034///
1035/// This substitutability is important when type checking methods, because
1036/// the implementation may have stricter type definitions than the interface.
1037/// The interface specifies minimum requirements, but the implementation may
1038/// have more accurate ones. For example, a method may privately accept
1039/// instances of B, but only publish that it accepts instances of A. Any
1040/// object passed to it will be type checked against B, and so will implicitly
1041/// by a valid A*. Similarly, a method may return a subclass of the class that
1042/// it is declared as returning.
1043///
1044/// This is most important when considering subclassing. A method in a
1045/// subclass must accept any object as an argument that its superclass's
1046/// implementation accepts. It may, however, accept a more general type
1047/// without breaking substitutability (i.e. you can still use the subclass
1048/// anywhere that you can use the superclass, but not vice versa). The
1049/// converse requirement applies to return types: the return type for a
1050/// subclass method must be a valid object of the kind that the superclass
1051/// advertises, but it may be specified more accurately. This avoids the need
1052/// for explicit down-casting by callers.
1053///
1054/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001055static bool isObjCTypeSubstitutable(ASTContext &Context,
1056 const ObjCObjectPointerType *A,
1057 const ObjCObjectPointerType *B,
1058 bool rejectId) {
1059 // Reject a protocol-unqualified id.
1060 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001061
1062 // If B is a qualified id, then A must also be a qualified id and it must
1063 // implement all of the protocols in B. It may not be a qualified class.
1064 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1065 // stricter definition so it is not substitutable for id<A>.
1066 if (B->isObjCQualifiedIdType()) {
1067 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001068 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1069 QualType(B,0),
1070 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001071 }
1072
1073 /*
1074 // id is a special type that bypasses type checking completely. We want a
1075 // warning when it is used in one place but not another.
1076 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1077
1078
1079 // If B is a qualified id, then A must also be a qualified id (which it isn't
1080 // if we've got this far)
1081 if (B->isObjCQualifiedIdType()) return false;
1082 */
1083
1084 // Now we know that A and B are (potentially-qualified) class types. The
1085 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001086 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001087}
1088
John McCall10302c02010-10-28 02:34:38 +00001089static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1090 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1091}
1092
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001093static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001094 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001095 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001096 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001097 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001098 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001099 if (IsProtocolMethodDecl &&
1100 (MethodDecl->getObjCDeclQualifier() !=
1101 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001102 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001103 S.Diag(MethodImpl->getLocation(),
1104 (IsOverridingMode ?
1105 diag::warn_conflicting_overriding_ret_type_modifiers
1106 : diag::warn_conflicting_ret_type_modifiers))
1107 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001108 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1109 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1110 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1111 }
1112 else
1113 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001114 }
1115
John McCall10302c02010-10-28 02:34:38 +00001116 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001117 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001118 return true;
1119 if (!Warn)
1120 return false;
John McCall10302c02010-10-28 02:34:38 +00001121
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001122 unsigned DiagID =
1123 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1124 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001125
1126 // Mismatches between ObjC pointers go into a different warning
1127 // category, and sometimes they're even completely whitelisted.
1128 if (const ObjCObjectPointerType *ImplPtrTy =
1129 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1130 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001131 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001132 // Allow non-matching return types as long as they don't violate
1133 // the principle of substitutability. Specifically, we permit
1134 // return types that are subclasses of the declared return type,
1135 // or that are more-qualified versions of the declared type.
1136 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001137 return false;
John McCall10302c02010-10-28 02:34:38 +00001138
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001139 DiagID =
1140 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1141 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001142 }
1143 }
1144
1145 S.Diag(MethodImpl->getLocation(), DiagID)
1146 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001147 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001148 << MethodImpl->getResultType()
1149 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001150 S.Diag(MethodDecl->getLocation(),
1151 IsOverridingMode ? diag::note_previous_declaration
1152 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001153 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001154 return false;
John McCall10302c02010-10-28 02:34:38 +00001155}
1156
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001157static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001158 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001159 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001160 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001161 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001162 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001163 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001164 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001165 if (IsProtocolMethodDecl &&
1166 (ImplVar->getObjCDeclQualifier() !=
1167 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001168 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001169 if (IsOverridingMode)
1170 S.Diag(ImplVar->getLocation(),
1171 diag::warn_conflicting_overriding_param_modifiers)
1172 << getTypeRange(ImplVar->getTypeSourceInfo())
1173 << MethodImpl->getDeclName();
1174 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001175 diag::warn_conflicting_param_modifiers)
1176 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001177 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001178 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1179 << getTypeRange(IfaceVar->getTypeSourceInfo());
1180 }
1181 else
1182 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001183 }
1184
John McCall10302c02010-10-28 02:34:38 +00001185 QualType ImplTy = ImplVar->getType();
1186 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001187
John McCall10302c02010-10-28 02:34:38 +00001188 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001189 return true;
1190
1191 if (!Warn)
1192 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001193 unsigned DiagID =
1194 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1195 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001196
1197 // Mismatches between ObjC pointers go into a different warning
1198 // category, and sometimes they're even completely whitelisted.
1199 if (const ObjCObjectPointerType *ImplPtrTy =
1200 ImplTy->getAs<ObjCObjectPointerType>()) {
1201 if (const ObjCObjectPointerType *IfacePtrTy =
1202 IfaceTy->getAs<ObjCObjectPointerType>()) {
1203 // Allow non-matching argument types as long as they don't
1204 // violate the principle of substitutability. Specifically, the
1205 // implementation must accept any objects that the superclass
1206 // accepts, however it may also accept others.
1207 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001208 return false;
John McCall10302c02010-10-28 02:34:38 +00001209
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001210 DiagID =
1211 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1212 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001213 }
1214 }
1215
1216 S.Diag(ImplVar->getLocation(), DiagID)
1217 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001218 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1219 S.Diag(IfaceVar->getLocation(),
1220 (IsOverridingMode ? diag::note_previous_declaration
1221 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001222 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001223 return false;
John McCall10302c02010-10-28 02:34:38 +00001224}
John McCallf85e1932011-06-15 23:02:42 +00001225
1226/// In ARC, check whether the conventional meanings of the two methods
1227/// match. If they don't, it's a hard error.
1228static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1229 ObjCMethodDecl *decl) {
1230 ObjCMethodFamily implFamily = impl->getMethodFamily();
1231 ObjCMethodFamily declFamily = decl->getMethodFamily();
1232 if (implFamily == declFamily) return false;
1233
1234 // Since conventions are sorted by selector, the only possibility is
1235 // that the types differ enough to cause one selector or the other
1236 // to fall out of the family.
1237 assert(implFamily == OMF_None || declFamily == OMF_None);
1238
1239 // No further diagnostics required on invalid declarations.
1240 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1241
1242 const ObjCMethodDecl *unmatched = impl;
1243 ObjCMethodFamily family = declFamily;
1244 unsigned errorID = diag::err_arc_lost_method_convention;
1245 unsigned noteID = diag::note_arc_lost_method_convention;
1246 if (declFamily == OMF_None) {
1247 unmatched = decl;
1248 family = implFamily;
1249 errorID = diag::err_arc_gained_method_convention;
1250 noteID = diag::note_arc_gained_method_convention;
1251 }
1252
1253 // Indexes into a %select clause in the diagnostic.
1254 enum FamilySelector {
1255 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1256 };
1257 FamilySelector familySelector = FamilySelector();
1258
1259 switch (family) {
1260 case OMF_None: llvm_unreachable("logic error, no method convention");
1261 case OMF_retain:
1262 case OMF_release:
1263 case OMF_autorelease:
1264 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001265 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001266 case OMF_retainCount:
1267 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001268 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001269 // Mismatches for these methods don't change ownership
1270 // conventions, so we don't care.
1271 return false;
1272
1273 case OMF_init: familySelector = F_init; break;
1274 case OMF_alloc: familySelector = F_alloc; break;
1275 case OMF_copy: familySelector = F_copy; break;
1276 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1277 case OMF_new: familySelector = F_new; break;
1278 }
1279
1280 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1281 ReasonSelector reasonSelector;
1282
1283 // The only reason these methods don't fall within their families is
1284 // due to unusual result types.
1285 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1286 reasonSelector = R_UnrelatedReturn;
1287 } else {
1288 reasonSelector = R_NonObjectReturn;
1289 }
1290
1291 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1292 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1293
1294 return true;
1295}
John McCall10302c02010-10-28 02:34:38 +00001296
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001297void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001298 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001299 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001300 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001301 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001302 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001303 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1304 return;
1305
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001306 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001307 IsProtocolMethodDecl, IsOverridingMode,
1308 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Chris Lattner3aff9192009-04-11 19:58:42 +00001310 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001311 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001312 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001313 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1314 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001315 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001316
Fariborz Jahanian21121902011-08-08 18:03:17 +00001317 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001318 if (IsOverridingMode)
1319 Diag(ImpMethodDecl->getLocation(),
1320 diag::warn_conflicting_overriding_variadic);
1321 else
1322 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001323 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001324 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001325}
1326
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001327/// WarnExactTypedMethods - This routine issues a warning if method
1328/// implementation declaration matches exactly that of its declaration.
1329void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1330 ObjCMethodDecl *MethodDecl,
1331 bool IsProtocolMethodDecl) {
1332 // don't issue warning when protocol method is optional because primary
1333 // class is not required to implement it and it is safe for protocol
1334 // to implement it.
1335 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1336 return;
1337 // don't issue warning when primary class's method is
1338 // depecated/unavailable.
1339 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1340 MethodDecl->hasAttr<DeprecatedAttr>())
1341 return;
1342
1343 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1344 IsProtocolMethodDecl, false, false);
1345 if (match)
1346 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1347 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1348 IM != EM; ++IM, ++IF) {
1349 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1350 *IM, *IF,
1351 IsProtocolMethodDecl, false, false);
1352 if (!match)
1353 break;
1354 }
1355 if (match)
1356 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001357 if (match)
1358 match = !(MethodDecl->isClassMethod() &&
1359 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001360
1361 if (match) {
1362 Diag(ImpMethodDecl->getLocation(),
1363 diag::warn_category_method_impl_match);
1364 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1365 }
1366}
1367
Mike Stump390b4cc2009-05-16 07:39:55 +00001368/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1369/// improve the efficiency of selector lookups and type checking by associating
1370/// with each protocol / interface / category the flattened instance tables. If
1371/// we used an immutable set to keep the table then it wouldn't add significant
1372/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001373
Steve Naroffefe7f362008-02-08 22:06:17 +00001374/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001375/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001376void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1377 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001378 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001379 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001380 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001381 ObjCContainerDecl *CDecl) {
1382 ObjCInterfaceDecl *IDecl;
1383 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1384 IDecl = C->getClassInterface();
1385 else
1386 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1387 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1388
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001389 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001390 ObjCInterfaceDecl *NSIDecl = 0;
1391 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001392 // check to see if class implements forwardInvocation method and objects
1393 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001394 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001395 // Under such conditions, which means that every method possible is
1396 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001397 // found" warnings.
1398 // FIXME: Use a general GetUnarySelector method for this.
1399 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1400 Selector fISelector = Context.Selectors.getSelector(1, &II);
1401 if (InsMap.count(fISelector))
1402 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1403 // need be implemented in the implementation.
1404 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1405 }
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001407 // If a method lookup fails locally we still need to look and see if
1408 // the method was implemented by a base class or an inherited
1409 // protocol. This lookup is slow, but occurs rarely in correct code
1410 // and otherwise would terminate in a warning.
1411
Chris Lattner4d391482007-12-12 07:09:47 +00001412 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001413 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001414 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001415 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001416 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001417 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001418 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001419 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001420 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001421 // Ugly, but necessary. Method declared in protcol might have
1422 // have been synthesized due to a property declared in the class which
1423 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001424 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001425 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001426 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001427 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001428 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1429 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001430 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001431 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001432 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1433 << PDecl->getDeclName();
1434 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001435 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001436 }
1437 }
Chris Lattner4d391482007-12-12 07:09:47 +00001438 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001439 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001440 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001441 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001442 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001443 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1444 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001445 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001446 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001447 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001448 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001449 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001450 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1451 PDecl->getDeclName();
1452 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001453 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001454 }
Chris Lattner780f3292008-07-21 21:32:27 +00001455 // Check on this protocols's referenced protocols, recursively.
1456 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1457 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001458 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001459}
1460
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001461/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001462/// or protocol against those declared in their implementations.
1463///
1464void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1465 const llvm::DenseSet<Selector> &ClsMap,
1466 llvm::DenseSet<Selector> &InsMapSeen,
1467 llvm::DenseSet<Selector> &ClsMapSeen,
1468 ObjCImplDecl* IMPDecl,
1469 ObjCContainerDecl* CDecl,
1470 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001471 bool ImmediateClass,
1472 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001473 // Check and see if instance methods in class interface have been
1474 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001475 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1476 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001477 if (InsMapSeen.count((*I)->getSelector()))
1478 continue;
1479 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001480 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001481 !InsMap.count((*I)->getSelector())) {
1482 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001483 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1484 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001485 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001486 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001487 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001488 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001489 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001490 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001491 assert(MethodDecl &&
1492 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001493 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001494 if (ImpMethodDecl) {
1495 if (!WarnExactMatch)
1496 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1497 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001498 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001499 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1500 isa<ObjCProtocolDecl>(CDecl));
1501 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001502 }
1503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001505 // Check and see if class methods in class interface have been
1506 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001507 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001508 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001509 if (ClsMapSeen.count((*I)->getSelector()))
1510 continue;
1511 ClsMapSeen.insert((*I)->getSelector());
1512 if (!ClsMap.count((*I)->getSelector())) {
1513 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001514 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1515 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001516 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001517 ObjCMethodDecl *ImpMethodDecl =
1518 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001519 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001520 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001521 if (!WarnExactMatch)
1522 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1523 isa<ObjCProtocolDecl>(CDecl));
1524 else
1525 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1526 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001527 }
1528 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001529
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001530 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001531 // Also methods in class extensions need be looked at next.
1532 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1533 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1534 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1535 IMPDecl,
1536 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001537 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001538
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001539 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001540 for (ObjCInterfaceDecl::all_protocol_iterator
1541 PI = I->all_referenced_protocol_begin(),
1542 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001543 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1544 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001545 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001546
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001547 // FIXME. For now, we are not checking for extact match of methods
1548 // in category implementation and its primary class's super class.
1549 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001550 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001551 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001552 I->getSuperClass(), IncompleteImpl, false);
1553 }
1554}
1555
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001556/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1557/// category matches with those implemented in its primary class and
1558/// warns each time an exact match is found.
1559void Sema::CheckCategoryVsClassMethodMatches(
1560 ObjCCategoryImplDecl *CatIMPDecl) {
1561 llvm::DenseSet<Selector> InsMap, ClsMap;
1562
1563 for (ObjCImplementationDecl::instmeth_iterator
1564 I = CatIMPDecl->instmeth_begin(),
1565 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1566 InsMap.insert((*I)->getSelector());
1567
1568 for (ObjCImplementationDecl::classmeth_iterator
1569 I = CatIMPDecl->classmeth_begin(),
1570 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1571 ClsMap.insert((*I)->getSelector());
1572 if (InsMap.empty() && ClsMap.empty())
1573 return;
1574
1575 // Get category's primary class.
1576 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1577 if (!CatDecl)
1578 return;
1579 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1580 if (!IDecl)
1581 return;
1582 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1583 bool IncompleteImpl = false;
1584 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1585 CatIMPDecl, IDecl,
1586 IncompleteImpl, false, true /*WarnExactMatch*/);
1587}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001588
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001589void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001590 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001591 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001592 llvm::DenseSet<Selector> InsMap;
1593 // Check and see if instance methods in class interface have been
1594 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001595 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001596 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001597 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001599 // Check and see if properties declared in the interface have either 1)
1600 // an implementation or 2) there is a @synthesize/@dynamic implementation
1601 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001602 if (isa<ObjCInterfaceDecl>(CDecl) &&
1603 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001604 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001605
Chris Lattner4d391482007-12-12 07:09:47 +00001606 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001607 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001608 I = IMPDecl->classmeth_begin(),
1609 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001610 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001612 // Check for type conflict of methods declared in a class/protocol and
1613 // its implementation; if any.
1614 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001615 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1616 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001617 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001618
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001619 // check all methods implemented in category against those declared
1620 // in its primary class.
1621 if (ObjCCategoryImplDecl *CatDecl =
1622 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1623 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Chris Lattner4d391482007-12-12 07:09:47 +00001625 // Check the protocol list for unimplemented methods in the @implementation
1626 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001627 // Check and see if class methods in class interface have been
1628 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Chris Lattnercddc8882009-03-01 00:56:52 +00001630 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001631 for (ObjCInterfaceDecl::all_protocol_iterator
1632 PI = I->all_referenced_protocol_begin(),
1633 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001634 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001635 InsMap, ClsMap, I);
1636 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001637 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1638 Categories; Categories = Categories->getNextClassExtension())
1639 ImplMethodsVsClassMethods(S, IMPDecl,
1640 const_cast<ObjCCategoryDecl*>(Categories),
1641 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001642 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001643 // For extended class, unimplemented methods in its protocols will
1644 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001645 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001646 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1647 E = C->protocol_end(); PI != E; ++PI)
1648 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001649 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001650 // Report unimplemented properties in the category as well.
1651 // When reporting on missing setter/getters, do not report when
1652 // setter/getter is implemented in category's primary class
1653 // implementation.
1654 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1655 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1656 for (ObjCImplementationDecl::instmeth_iterator
1657 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1658 InsMap.insert((*I)->getSelector());
1659 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001660 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001661 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001662 } else
1663 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001664}
1665
Mike Stump1eb44332009-09-09 15:08:12 +00001666/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001667Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001668Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001669 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001670 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001671 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001672 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001673 for (unsigned i = 0; i != NumElts; ++i) {
1674 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001675 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001676 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001677 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001678 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001679 // Maybe we will complain about the shadowed template parameter.
1680 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1681 // Just pretend that we didn't see the previous declaration.
1682 PrevDecl = 0;
1683 }
1684
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001685 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001686 // GCC apparently allows the following idiom:
1687 //
1688 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1689 // @class XCElementToggler;
1690 //
Mike Stump1eb44332009-09-09 15:08:12 +00001691 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001692 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001693 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001694 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001695 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001696 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001697 // a forward class declaration matching a typedef name of a class refers
1698 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001699 if (const ObjCObjectType *OI =
1700 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1701 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001702 }
Chris Lattner4d391482007-12-12 07:09:47 +00001703 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001704 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1705 if (!IDecl) { // Not already seen? Make a forward decl.
1706 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1707 IdentList[i], IdentLocs[i], true);
1708
1709 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1710 // the current DeclContext. This prevents clients that walk DeclContext
1711 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1712 // declared later (if at all). We also take care to explicitly make
1713 // sure this declaration is visible for name lookup.
1714 PushOnScopeChains(IDecl, TUScope, false);
1715 CurContext->makeDeclVisibleInContext(IDecl, true);
1716 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001717 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1718 IDecl, IdentLocs[i]);
1719 CurContext->addDecl(CDecl);
1720 CheckObjCDeclScope(CDecl);
1721 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001722 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001723
1724 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001725}
1726
John McCall0f4c4c42011-06-16 01:15:19 +00001727static bool tryMatchRecordTypes(ASTContext &Context,
1728 Sema::MethodMatchStrategy strategy,
1729 const Type *left, const Type *right);
1730
John McCallf85e1932011-06-15 23:02:42 +00001731static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1732 QualType leftQT, QualType rightQT) {
1733 const Type *left =
1734 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1735 const Type *right =
1736 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1737
1738 if (left == right) return true;
1739
1740 // If we're doing a strict match, the types have to match exactly.
1741 if (strategy == Sema::MMS_strict) return false;
1742
1743 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1744
1745 // Otherwise, use this absurdly complicated algorithm to try to
1746 // validate the basic, low-level compatibility of the two types.
1747
1748 // As a minimum, require the sizes and alignments to match.
1749 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1750 return false;
1751
1752 // Consider all the kinds of non-dependent canonical types:
1753 // - functions and arrays aren't possible as return and parameter types
1754
1755 // - vector types of equal size can be arbitrarily mixed
1756 if (isa<VectorType>(left)) return isa<VectorType>(right);
1757 if (isa<VectorType>(right)) return false;
1758
1759 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001760 // - structs, unions, and Objective-C objects must match more-or-less
1761 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001762 // - everything else should be a scalar
1763 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001764 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001765
1766 // Make scalars agree in kind, except count bools as chars.
1767 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1768 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1769 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1770 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1771
1772 // Note that data member pointers and function member pointers don't
1773 // intermix because of the size differences.
1774
1775 return (leftSK == rightSK);
1776}
Chris Lattner4d391482007-12-12 07:09:47 +00001777
John McCall0f4c4c42011-06-16 01:15:19 +00001778static bool tryMatchRecordTypes(ASTContext &Context,
1779 Sema::MethodMatchStrategy strategy,
1780 const Type *lt, const Type *rt) {
1781 assert(lt && rt && lt != rt);
1782
1783 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1784 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1785 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1786
1787 // Require union-hood to match.
1788 if (left->isUnion() != right->isUnion()) return false;
1789
1790 // Require an exact match if either is non-POD.
1791 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1792 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1793 return false;
1794
1795 // Require size and alignment to match.
1796 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1797
1798 // Require fields to match.
1799 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1800 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1801 for (; li != le && ri != re; ++li, ++ri) {
1802 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1803 return false;
1804 }
1805 return (li == le && ri == re);
1806}
1807
Chris Lattner4d391482007-12-12 07:09:47 +00001808/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1809/// returns true, or false, accordingly.
1810/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001811bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1812 const ObjCMethodDecl *right,
1813 MethodMatchStrategy strategy) {
1814 if (!matchTypes(Context, strategy,
1815 left->getResultType(), right->getResultType()))
1816 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001817
John McCallf85e1932011-06-15 23:02:42 +00001818 if (getLangOptions().ObjCAutoRefCount &&
1819 (left->hasAttr<NSReturnsRetainedAttr>()
1820 != right->hasAttr<NSReturnsRetainedAttr>() ||
1821 left->hasAttr<NSConsumesSelfAttr>()
1822 != right->hasAttr<NSConsumesSelfAttr>()))
1823 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001824
John McCallf85e1932011-06-15 23:02:42 +00001825 ObjCMethodDecl::param_iterator
1826 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001827
John McCallf85e1932011-06-15 23:02:42 +00001828 for (; li != le; ++li, ++ri) {
1829 assert(ri != right->param_end() && "Param mismatch");
1830 ParmVarDecl *lparm = *li, *rparm = *ri;
1831
1832 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1833 return false;
1834
1835 if (getLangOptions().ObjCAutoRefCount &&
1836 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1837 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001838 }
1839 return true;
1840}
1841
Sebastian Redldb9d2142010-08-02 23:18:59 +00001842/// \brief Read the contents of the method pool for a given selector from
1843/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001844///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001845/// This routine should only be called once, when the method pool has no entry
1846/// for this selector.
1847Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001848 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001849 assert(MethodPool.find(Sel) == MethodPool.end() &&
1850 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001851
1852 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001853 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Sebastian Redldb9d2142010-08-02 23:18:59 +00001855 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001856}
1857
Sebastian Redldb9d2142010-08-02 23:18:59 +00001858void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1859 bool instance) {
1860 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1861 if (Pos == MethodPool.end()) {
1862 if (ExternalSource)
1863 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001864 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001865 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1866 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001867 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001868 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001869 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001870 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001871 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001872 Entry.Method = Method;
1873 Entry.Next = 0;
1874 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001875 }
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Chris Lattnerb25df352009-03-04 05:16:45 +00001877 // We've seen a method with this name, see if we have already seen this type
1878 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001879 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1880 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1881
1882 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001883 ObjCMethodDecl *PrevObjCMethod = List->Method;
1884 PrevObjCMethod->setDefined(impl);
1885 // If a method is deprecated, push it in the global pool.
1886 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001887 if (Method->isDeprecated()) {
1888 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001889 List->Method = Method;
1890 }
1891 // If new method is unavailable, push it into global pool
1892 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001893 if (Method->isUnavailable()) {
1894 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001895 List->Method = Method;
1896 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001897 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001898 }
John McCallf85e1932011-06-15 23:02:42 +00001899 }
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Chris Lattnerb25df352009-03-04 05:16:45 +00001901 // We have a new signature for an existing method - add it.
1902 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001903 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1904 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001905}
1906
John McCallf85e1932011-06-15 23:02:42 +00001907/// Determines if this is an "acceptable" loose mismatch in the global
1908/// method pool. This exists mostly as a hack to get around certain
1909/// global mismatches which we can't afford to make warnings / errors.
1910/// Really, what we want is a way to take a method out of the global
1911/// method pool.
1912static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1913 ObjCMethodDecl *other) {
1914 if (!chosen->isInstanceMethod())
1915 return false;
1916
1917 Selector sel = chosen->getSelector();
1918 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1919 return false;
1920
1921 // Don't complain about mismatches for -length if the method we
1922 // chose has an integral result type.
1923 return (chosen->getResultType()->isIntegerType());
1924}
1925
Sebastian Redldb9d2142010-08-02 23:18:59 +00001926ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001927 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001928 bool warn, bool instance) {
1929 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1930 if (Pos == MethodPool.end()) {
1931 if (ExternalSource)
1932 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001933 else
1934 return 0;
1935 }
1936
Sebastian Redldb9d2142010-08-02 23:18:59 +00001937 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Sebastian Redldb9d2142010-08-02 23:18:59 +00001939 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001940 bool issueDiagnostic = false, issueError = false;
1941
1942 // We support a warning which complains about *any* difference in
1943 // method signature.
1944 bool strictSelectorMatch =
1945 (receiverIdOrClass && warn &&
1946 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1947 R.getBegin()) !=
1948 Diagnostic::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001949 if (strictSelectorMatch)
1950 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001951 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1952 MMS_strict)) {
1953 issueDiagnostic = true;
1954 break;
1955 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001956 }
1957
John McCallf85e1932011-06-15 23:02:42 +00001958 // If we didn't see any strict differences, we won't see any loose
1959 // differences. In ARC, however, we also need to check for loose
1960 // mismatches, because most of them are errors.
1961 if (!strictSelectorMatch ||
1962 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001963 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001964 // This checks if the methods differ in type mismatch.
1965 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1966 MMS_loose) &&
1967 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
1968 issueDiagnostic = true;
1969 if (getLangOptions().ObjCAutoRefCount)
1970 issueError = true;
1971 break;
1972 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001973 }
1974
John McCallf85e1932011-06-15 23:02:42 +00001975 if (issueDiagnostic) {
1976 if (issueError)
1977 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
1978 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001979 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1980 else
1981 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00001982
1983 Diag(MethList.Method->getLocStart(),
1984 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00001985 << MethList.Method->getSourceRange();
1986 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1987 Diag(Next->Method->getLocStart(), diag::note_also_found)
1988 << Next->Method->getSourceRange();
1989 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001990 }
1991 return MethList.Method;
1992}
1993
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001994ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001995 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1996 if (Pos == MethodPool.end())
1997 return 0;
1998
1999 GlobalMethods &Methods = Pos->second;
2000
2001 if (Methods.first.Method && Methods.first.Method->isDefined())
2002 return Methods.first.Method;
2003 if (Methods.second.Method && Methods.second.Method->isDefined())
2004 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002005 return 0;
2006}
2007
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002008/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2009/// identical selector names in current and its super classes and issues
2010/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002011void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2012 ObjCMethodDecl *Method,
2013 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002014 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2015 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002017 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002018 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002019 SD->lookupMethod(Method->getSelector(), IsInstance);
2020 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002021 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002022 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002023 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002024 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2025 E = Method->param_end();
2026 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2027 for (; ParamI != E; ++ParamI, ++PrevI) {
2028 // Number of parameters are the same and is guaranteed by selector match.
2029 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2030 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2031 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002032 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002033 // respective argument type in the super class method, issue warning;
2034 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002035 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002036 << T1 << T2;
2037 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2038 return;
2039 }
2040 }
2041 ID = SD;
2042 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002043}
2044
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002045/// DiagnoseDuplicateIvars -
2046/// Check for duplicate ivars in the entire class at the start of
2047/// @implementation. This becomes necesssary because class extension can
2048/// add ivars to a class in random order which will not be known until
2049/// class's @implementation is seen.
2050void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2051 ObjCInterfaceDecl *SID) {
2052 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2053 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2054 ObjCIvarDecl* Ivar = (*IVI);
2055 if (Ivar->isInvalidDecl())
2056 continue;
2057 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2058 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2059 if (prevIvar) {
2060 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2061 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2062 Ivar->setInvalidDecl();
2063 }
2064 }
2065 }
2066}
2067
Steve Naroffa56f6162007-12-18 01:30:32 +00002068// Note: For class/category implemenations, allMethods/allProperties is
2069// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002070void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002071 Decl **allMethods, unsigned allNum,
2072 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002073 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002074
2075 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002076 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002077 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2078 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002079
Mike Stump1eb44332009-09-09 15:08:12 +00002080 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002081 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2082 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002083 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002084
Ted Kremenek782f2f52010-01-07 01:20:12 +00002085 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2086 // FIXME: This is wrong. We shouldn't be pretending that there is
2087 // an '@end' in the declaration.
2088 SourceLocation L = ClassDecl->getLocation();
2089 AtEnd.setBegin(L);
2090 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002091 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002092 }
2093
Steve Naroff0701bbb2009-01-08 17:28:14 +00002094 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2095 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2096 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2097
Chris Lattner4d391482007-12-12 07:09:47 +00002098 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002099 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002100 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002101
2102 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002103 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002104 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002105 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002106 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002107 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002108 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002109 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002110 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002111 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002112 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002113 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002114 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002115 InsMap[Method->getSelector()] = Method;
2116 /// The following allows us to typecheck messages to "id".
2117 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002118 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002119 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002120 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002121 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002122 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002123 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002124 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002125 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002126 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002127 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002128 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002129 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002130 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002131 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002132 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002133 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002134 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002135 /// The following allows us to typecheck messages to "Class".
2136 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002137 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002138 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002139 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002140 }
2141 }
2142 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002143 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002144 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002145 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002146 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002147 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002148 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002149 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002150 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002151 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002152
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002153 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002154 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002155 if (C->IsClassExtension()) {
2156 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2157 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002158 }
Chris Lattner4d391482007-12-12 07:09:47 +00002159 }
Steve Naroff09c47192009-01-09 15:36:25 +00002160 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002161 if (CDecl->getIdentifier())
2162 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2163 // user-defined setter/getter. It also synthesizes setter/getter methods
2164 // and adds them to the DeclContext and global method pools.
2165 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2166 E = CDecl->prop_end();
2167 I != E; ++I)
2168 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002169 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002170 }
2171 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002172 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002173 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002174 // Any property declared in a class extension might have user
2175 // declared setter or getter in current class extension or one
2176 // of the other class extensions. Mark them as synthesized as
2177 // property will be synthesized when property with same name is
2178 // seen in the @implementation.
2179 for (const ObjCCategoryDecl *ClsExtDecl =
2180 IDecl->getFirstClassExtension();
2181 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2182 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2183 E = ClsExtDecl->prop_end(); I != E; ++I) {
2184 ObjCPropertyDecl *Property = (*I);
2185 // Skip over properties declared @dynamic
2186 if (const ObjCPropertyImplDecl *PIDecl
2187 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2188 if (PIDecl->getPropertyImplementation()
2189 == ObjCPropertyImplDecl::Dynamic)
2190 continue;
2191
2192 for (const ObjCCategoryDecl *CExtDecl =
2193 IDecl->getFirstClassExtension();
2194 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2195 if (ObjCMethodDecl *GetterMethod =
2196 CExtDecl->getInstanceMethod(Property->getGetterName()))
2197 GetterMethod->setSynthesized(true);
2198 if (!Property->isReadOnly())
2199 if (ObjCMethodDecl *SetterMethod =
2200 CExtDecl->getInstanceMethod(Property->getSetterName()))
2201 SetterMethod->setSynthesized(true);
2202 }
2203 }
2204 }
2205
Ted Kremenekc32647d2010-12-23 21:35:43 +00002206 if (LangOpts.ObjCDefaultSynthProperties &&
2207 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00002208 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002209 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002210 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002211 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002212
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002213 if (LangOpts.ObjCNonFragileABI2)
2214 while (IDecl->getSuperClass()) {
2215 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2216 IDecl = IDecl->getSuperClass();
2217 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002218 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002219 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002220 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002221 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002222 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Chris Lattner4d391482007-12-12 07:09:47 +00002224 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002225 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002226 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002227 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002228 Categories; Categories = Categories->getNextClassCategory()) {
2229 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002230 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002231 break;
2232 }
2233 }
2234 }
2235 }
Chris Lattner682bf922009-03-29 16:50:03 +00002236 if (isInterfaceDeclKind) {
2237 // Reject invalid vardecls.
2238 for (unsigned i = 0; i != tuvNum; i++) {
2239 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2240 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2241 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002242 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002243 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002244 }
Chris Lattner682bf922009-03-29 16:50:03 +00002245 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002246 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002247 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002248}
2249
2250
2251/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2252/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002253static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002254CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002255 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002256}
2257
Ted Kremenek422bae72010-04-18 04:59:38 +00002258static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002259bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002260 // The 'ibaction' attribute is allowed on method definitions because of
2261 // how the IBAction macro is used on both method declarations and definitions.
2262 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002263 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2264 if ((*i)->getKind() != attr::IBAction)
2265 return true;
2266 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002267}
2268
Douglas Gregor926df6c2011-06-11 01:09:30 +00002269/// \brief Check whether the declared result type of the given Objective-C
2270/// method declaration is compatible with the method's class.
2271///
2272static bool
2273CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2274 ObjCInterfaceDecl *CurrentClass) {
2275 QualType ResultType = Method->getResultType();
2276 SourceRange ResultTypeRange;
2277 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2278 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2279
2280 // If an Objective-C method inherits its related result type, then its
2281 // declared result type must be compatible with its own class type. The
2282 // declared result type is compatible if:
2283 if (const ObjCObjectPointerType *ResultObjectType
2284 = ResultType->getAs<ObjCObjectPointerType>()) {
2285 // - it is id or qualified id, or
2286 if (ResultObjectType->isObjCIdType() ||
2287 ResultObjectType->isObjCQualifiedIdType())
2288 return false;
2289
2290 if (CurrentClass) {
2291 if (ObjCInterfaceDecl *ResultClass
2292 = ResultObjectType->getInterfaceDecl()) {
2293 // - it is the same as the method's class type, or
2294 if (CurrentClass == ResultClass)
2295 return false;
2296
2297 // - it is a superclass of the method's class type
2298 if (ResultClass->isSuperClassOf(CurrentClass))
2299 return false;
2300 }
2301 }
2302 }
2303
2304 return true;
2305}
2306
John McCall6c2c2502011-07-22 02:45:48 +00002307namespace {
2308/// A helper class for searching for methods which a particular method
2309/// overrides.
2310class OverrideSearch {
2311 Sema &S;
2312 ObjCMethodDecl *Method;
2313 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2314 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2315 bool Recursive;
2316
2317public:
2318 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2319 Selector selector = method->getSelector();
2320
2321 // Bypass this search if we've never seen an instance/class method
2322 // with this selector before.
2323 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2324 if (it == S.MethodPool.end()) {
2325 if (!S.ExternalSource) return;
2326 it = S.ReadMethodPool(selector);
2327 }
2328 ObjCMethodList &list =
2329 method->isInstanceMethod() ? it->second.first : it->second.second;
2330 if (!list.Method) return;
2331
2332 ObjCContainerDecl *container
2333 = cast<ObjCContainerDecl>(method->getDeclContext());
2334
2335 // Prevent the search from reaching this container again. This is
2336 // important with categories, which override methods from the
2337 // interface and each other.
2338 Searched.insert(container);
2339 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002340 }
John McCall6c2c2502011-07-22 02:45:48 +00002341
2342 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2343 iterator begin() const { return Overridden.begin(); }
2344 iterator end() const { return Overridden.end(); }
2345
2346private:
2347 void searchFromContainer(ObjCContainerDecl *container) {
2348 if (container->isInvalidDecl()) return;
2349
2350 switch (container->getDeclKind()) {
2351#define OBJCCONTAINER(type, base) \
2352 case Decl::type: \
2353 searchFrom(cast<type##Decl>(container)); \
2354 break;
2355#define ABSTRACT_DECL(expansion)
2356#define DECL(type, base) \
2357 case Decl::type:
2358#include "clang/AST/DeclNodes.inc"
2359 llvm_unreachable("not an ObjC container!");
2360 }
2361 }
2362
2363 void searchFrom(ObjCProtocolDecl *protocol) {
2364 // A method in a protocol declaration overrides declarations from
2365 // referenced ("parent") protocols.
2366 search(protocol->getReferencedProtocols());
2367 }
2368
2369 void searchFrom(ObjCCategoryDecl *category) {
2370 // A method in a category declaration overrides declarations from
2371 // the main class and from protocols the category references.
2372 search(category->getClassInterface());
2373 search(category->getReferencedProtocols());
2374 }
2375
2376 void searchFrom(ObjCCategoryImplDecl *impl) {
2377 // A method in a category definition that has a category
2378 // declaration overrides declarations from the category
2379 // declaration.
2380 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2381 search(category);
2382
2383 // Otherwise it overrides declarations from the class.
2384 } else {
2385 search(impl->getClassInterface());
2386 }
2387 }
2388
2389 void searchFrom(ObjCInterfaceDecl *iface) {
2390 // A method in a class declaration overrides declarations from
2391
2392 // - categories,
2393 for (ObjCCategoryDecl *category = iface->getCategoryList();
2394 category; category = category->getNextClassCategory())
2395 search(category);
2396
2397 // - the super class, and
2398 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2399 search(super);
2400
2401 // - any referenced protocols.
2402 search(iface->getReferencedProtocols());
2403 }
2404
2405 void searchFrom(ObjCImplementationDecl *impl) {
2406 // A method in a class implementation overrides declarations from
2407 // the class interface.
2408 search(impl->getClassInterface());
2409 }
2410
2411
2412 void search(const ObjCProtocolList &protocols) {
2413 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2414 i != e; ++i)
2415 search(*i);
2416 }
2417
2418 void search(ObjCContainerDecl *container) {
2419 // Abort if we've already searched this container.
2420 if (!Searched.insert(container)) return;
2421
2422 // Check for a method in this container which matches this selector.
2423 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2424 Method->isInstanceMethod());
2425
2426 // If we find one, record it and bail out.
2427 if (meth) {
2428 Overridden.insert(meth);
2429 return;
2430 }
2431
2432 // Otherwise, search for methods that a hypothetical method here
2433 // would have overridden.
2434
2435 // Note that we're now in a recursive case.
2436 Recursive = true;
2437
2438 searchFromContainer(container);
2439 }
2440};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002441}
2442
John McCalld226f652010-08-21 09:40:31 +00002443Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002444 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002445 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002446 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002447 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002448 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00002449 Selector Sel,
2450 // optional arguments. The number of types/arguments is obtained
2451 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002452 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002453 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002454 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002455 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002456 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002457 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002458 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002459 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002460 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002461 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2462 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002463 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002464
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002465 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002466 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002467 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Steve Naroffccef3712009-02-20 22:59:16 +00002469 // Methods cannot return interface types. All ObjC objects are
2470 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002471 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002472 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2473 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002474 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002475 }
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002476 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002477 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002478 Diag(MethodLoc, diag::warn_missing_method_return_type)
2479 << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002480 }
Mike Stump1eb44332009-09-09 15:08:12 +00002481
2482 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002483 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002484 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002485 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002486 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002487 /*isSynthesized=*/false,
2488 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002489 MethodDeclKind == tok::objc_optional
2490 ? ObjCMethodDecl::Optional
2491 : ObjCMethodDecl::Required,
2492 false);
Mike Stump1eb44332009-09-09 15:08:12 +00002493
Chris Lattner5f9e2722011-07-23 10:55:15 +00002494 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Chris Lattner7db638d2009-04-11 19:42:43 +00002496 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002497 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002498 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002499
Chris Lattnere294d3f2009-04-11 18:57:04 +00002500 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002501 ArgType = Context.getObjCIdType();
2502 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002503 } else {
John McCall58e46772009-10-23 21:48:59 +00002504 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002505 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002506 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002507 }
Mike Stump1eb44332009-09-09 15:08:12 +00002508
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002509 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2510 LookupOrdinaryName, ForRedeclaration);
2511 LookupName(R, S);
2512 if (R.isSingleResult()) {
2513 NamedDecl *PrevDecl = R.getFoundDecl();
2514 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002515 Diag(ArgInfo[i].NameLoc,
2516 (MethodDefinition ? diag::warn_method_param_redefinition
2517 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002518 << ArgInfo[i].Name;
2519 Diag(PrevDecl->getLocation(),
2520 diag::note_previous_declaration);
2521 }
2522 }
2523
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002524 SourceLocation StartLoc = DI
2525 ? DI->getTypeLoc().getBeginLoc()
2526 : ArgInfo[i].NameLoc;
2527
John McCall81ef3e62011-04-23 02:46:06 +00002528 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2529 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2530 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002531
John McCall70798862011-05-02 00:30:12 +00002532 Param->setObjCMethodScopeInfo(i);
2533
Chris Lattner0ed844b2008-04-04 06:12:32 +00002534 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002535 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002536
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002537 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002538 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002539
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002540 S->AddDecl(Param);
2541 IdResolver.AddDecl(Param);
2542
Chris Lattner0ed844b2008-04-04 06:12:32 +00002543 Params.push_back(Param);
2544 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002545
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002546 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002547 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002548 QualType ArgType = Param->getType();
2549 if (ArgType.isNull())
2550 ArgType = Context.getObjCIdType();
2551 else
2552 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002553 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002554 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002555 Diag(Param->getLocation(),
2556 diag::err_object_cannot_be_passed_returned_by_value)
2557 << 1 << ArgType;
2558 Param->setInvalidDecl();
2559 }
2560 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002561
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002562 Params.push_back(Param);
2563 }
2564
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002565 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2566 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002567 ObjCMethod->setObjCDeclQualifier(
2568 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002569
2570 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002571 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002572
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002573 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002574 const ObjCMethodDecl *PrevMethod = 0;
2575 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002576 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002577 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2578 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002579 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002580 PrevMethod = ImpDecl->getClassMethod(Sel);
2581 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002582 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002583
Sean Huntcf807c42010-08-18 23:23:40 +00002584 if (ObjCMethod->hasAttrs() &&
2585 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002586 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002587 } else {
2588 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002589 }
John McCall6c2c2502011-07-22 02:45:48 +00002590
Chris Lattner4d391482007-12-12 07:09:47 +00002591 if (PrevMethod) {
2592 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002593 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002594 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002595 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002596 }
John McCall54abf7d2009-11-04 02:18:39 +00002597
Douglas Gregor926df6c2011-06-11 01:09:30 +00002598 // If this Objective-C method does not have a related result type, but we
2599 // are allowed to infer related result types, try to do so based on the
2600 // method family.
2601 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2602 if (!CurrentClass) {
2603 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2604 CurrentClass = Cat->getClassInterface();
2605 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2606 CurrentClass = Impl->getClassInterface();
2607 else if (ObjCCategoryImplDecl *CatImpl
2608 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2609 CurrentClass = CatImpl->getClassInterface();
2610 }
John McCall6c2c2502011-07-22 02:45:48 +00002611
2612 bool isRelatedResultTypeCompatible =
2613 (getLangOptions().ObjCInferRelatedResultType &&
2614 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass));
2615
2616 // Search for overridden methods and merge information down from them.
2617 OverrideSearch overrides(*this, ObjCMethod);
2618 for (OverrideSearch::iterator
2619 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2620 ObjCMethodDecl *overridden = *i;
2621
2622 // Propagate down the 'related result type' bit from overridden methods.
2623 if (isRelatedResultTypeCompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002624 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002625
2626 // Then merge the declarations.
2627 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002628
2629 // Check for overriding methods
2630 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2631 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2632 WarnConflictingTypedMethods(ObjCMethod, overridden,
2633 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2634 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002635 }
2636
John McCallf85e1932011-06-15 23:02:42 +00002637 bool ARCError = false;
2638 if (getLangOptions().ObjCAutoRefCount)
2639 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2640
John McCall6c2c2502011-07-22 02:45:48 +00002641 if (!ARCError && isRelatedResultTypeCompatible &&
2642 !ObjCMethod->hasRelatedResultType()) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002643 bool InferRelatedResultType = false;
2644 switch (ObjCMethod->getMethodFamily()) {
2645 case OMF_None:
2646 case OMF_copy:
2647 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002648 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002649 case OMF_mutableCopy:
2650 case OMF_release:
2651 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002652 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002653 break;
2654
2655 case OMF_alloc:
2656 case OMF_new:
2657 InferRelatedResultType = ObjCMethod->isClassMethod();
2658 break;
2659
2660 case OMF_init:
2661 case OMF_autorelease:
2662 case OMF_retain:
2663 case OMF_self:
2664 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2665 break;
2666 }
2667
John McCall6c2c2502011-07-22 02:45:48 +00002668 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002669 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002670 }
2671
John McCalld226f652010-08-21 09:40:31 +00002672 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002673}
2674
Chris Lattnercc98eac2008-12-17 07:13:27 +00002675bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002676 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002677 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002678 // Following is also an error. But it is caused by a missing @end
2679 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002680 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2681 return false;
2682 }
2683
Anders Carlsson15281452008-11-04 16:57:32 +00002684 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2685 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Anders Carlsson15281452008-11-04 16:57:32 +00002687 return true;
2688}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002689
Chris Lattnercc98eac2008-12-17 07:13:27 +00002690/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2691/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002692void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002693 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002694 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002695 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002696 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002697 if (!Class) {
2698 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2699 return;
2700 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002701 if (LangOpts.ObjCNonFragileABI) {
2702 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2703 return;
2704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Chris Lattnercc98eac2008-12-17 07:13:27 +00002706 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002707 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002708 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002709 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002710 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002711 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002712 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002713 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2714 /*FIXME: StartL=*/ID->getLocation(),
2715 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002716 ID->getIdentifier(), ID->getType(),
2717 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002718 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002719 }
Mike Stump1eb44332009-09-09 15:08:12 +00002720
Chris Lattnercc98eac2008-12-17 07:13:27 +00002721 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002722 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002723 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002724 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002725 if (getLangOptions().CPlusPlus)
2726 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002727 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002728 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002729 }
2730}
2731
Douglas Gregor160b5632010-04-26 17:32:49 +00002732/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002733VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2734 SourceLocation StartLoc,
2735 SourceLocation IdLoc,
2736 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002737 bool Invalid) {
2738 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2739 // duration shall not be qualified by an address-space qualifier."
2740 // Since all parameters have automatic store duration, they can not have
2741 // an address space.
2742 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002743 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002744 Invalid = true;
2745 }
2746
2747 // An @catch parameter must be an unqualified object pointer type;
2748 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2749 if (Invalid) {
2750 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002751 } else if (T->isDependentType()) {
2752 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002753 } else if (!T->isObjCObjectPointerType()) {
2754 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002755 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002756 } else if (T->isObjCQualifiedIdType()) {
2757 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002758 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002759 }
2760
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002761 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2762 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002763 New->setExceptionVariable(true);
2764
Douglas Gregor160b5632010-04-26 17:32:49 +00002765 if (Invalid)
2766 New->setInvalidDecl();
2767 return New;
2768}
2769
John McCalld226f652010-08-21 09:40:31 +00002770Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002771 const DeclSpec &DS = D.getDeclSpec();
2772
2773 // We allow the "register" storage class on exception variables because
2774 // GCC did, but we drop it completely. Any other storage class is an error.
2775 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2776 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2777 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2778 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2779 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2780 << DS.getStorageClassSpec();
2781 }
2782 if (D.getDeclSpec().isThreadSpecified())
2783 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2784 D.getMutableDeclSpec().ClearStorageClassSpecs();
2785
2786 DiagnoseFunctionSpecifiers(D);
2787
2788 // Check that there are no default arguments inside the type of this
2789 // exception object (C++ only).
2790 if (getLangOptions().CPlusPlus)
2791 CheckExtraCXXDefaultArguments(D);
2792
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002793 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002794 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002795
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002796 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2797 D.getSourceRange().getBegin(),
2798 D.getIdentifierLoc(),
2799 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002800 D.isInvalidType());
2801
2802 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2803 if (D.getCXXScopeSpec().isSet()) {
2804 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2805 << D.getCXXScopeSpec().getRange();
2806 New->setInvalidDecl();
2807 }
2808
2809 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002810 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002811 if (D.getIdentifier())
2812 IdResolver.AddDecl(New);
2813
2814 ProcessDeclAttributes(S, New, D);
2815
2816 if (New->hasAttr<BlocksAttr>())
2817 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002818 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002819}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002820
2821/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002822/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002823void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002824 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002825 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2826 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002827 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002828 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002829 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002830 }
2831}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002832
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002833void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002834 // Load referenced selectors from the external source.
2835 if (ExternalSource) {
2836 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2837 ExternalSource->ReadReferencedSelectors(Sels);
2838 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2839 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2840 }
2841
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002842 // Warning will be issued only when selector table is
2843 // generated (which means there is at lease one implementation
2844 // in the TU). This is to match gcc's behavior.
2845 if (ReferencedSelectors.empty() ||
2846 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002847 return;
2848 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2849 ReferencedSelectors.begin(),
2850 E = ReferencedSelectors.end(); S != E; ++S) {
2851 Selector Sel = (*S).first;
2852 if (!LookupImplementedMethodInGlobalPool(Sel))
2853 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2854 }
2855 return;
2856}