blob: de9097e98b4fd5855b9552707783ecee53330821 [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"
Steve Naroffca331292009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclObjC.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
24
Chris Lattner4d391482007-12-12 07:09:47 +000025using namespace clang;
26
Douglas Gregor926df6c2011-06-11 01:09:30 +000027bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
28 const ObjCMethodDecl *Overridden,
29 bool IsImplementation) {
30 if (Overridden->hasRelatedResultType() &&
31 !NewMethod->hasRelatedResultType()) {
32 // This can only happen when the method follows a naming convention that
33 // implies a related result type, and the original (overridden) method has
34 // a suitable return type, but the new (overriding) method does not have
35 // a suitable return type.
36 QualType ResultType = NewMethod->getResultType();
37 SourceRange ResultTypeRange;
38 if (const TypeSourceInfo *ResultTypeInfo
39 = NewMethod->getResultTypeSourceInfo())
40 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
41
42 // Figure out which class this method is part of, if any.
43 ObjCInterfaceDecl *CurrentClass
44 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
45 if (!CurrentClass) {
46 DeclContext *DC = NewMethod->getDeclContext();
47 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
48 CurrentClass = Cat->getClassInterface();
49 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
50 CurrentClass = Impl->getClassInterface();
51 else if (ObjCCategoryImplDecl *CatImpl
52 = dyn_cast<ObjCCategoryImplDecl>(DC))
53 CurrentClass = CatImpl->getClassInterface();
54 }
55
56 if (CurrentClass) {
57 Diag(NewMethod->getLocation(),
58 diag::warn_related_result_type_compatibility_class)
59 << Context.getObjCInterfaceType(CurrentClass)
60 << ResultType
61 << ResultTypeRange;
62 } else {
63 Diag(NewMethod->getLocation(),
64 diag::warn_related_result_type_compatibility_protocol)
65 << ResultType
66 << ResultTypeRange;
67 }
68
69 Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
70 << Overridden->getMethodFamily();
71 }
72
73 return false;
74}
75
76
77static bool CheckObjCMethodOverrides(Sema &S, ObjCMethodDecl *NewMethod,
78 DeclContext *DC,
79 bool SkipCurrent = true) {
80 if (!DC)
81 return false;
82
83 if (!SkipCurrent) {
84 // Look for this method. If we find it, we're done.
85 Selector Sel = NewMethod->getSelector();
86 bool IsInstance = NewMethod->isInstanceMethod();
87 DeclContext::lookup_const_iterator Meth, MethEnd;
88 for (llvm::tie(Meth, MethEnd) = DC->lookup(Sel); Meth != MethEnd; ++Meth) {
89 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
90 if (MD && MD->isInstanceMethod() == IsInstance)
91 return S.CheckObjCMethodOverride(NewMethod, MD, false);
92 }
93 }
94
95 if (ObjCInterfaceDecl *Class = llvm::dyn_cast<ObjCInterfaceDecl>(DC)) {
96 // Look through categories.
97 for (ObjCCategoryDecl *Category = Class->getCategoryList();
98 Category; Category = Category->getNextClassCategory()) {
99 if (CheckObjCMethodOverrides(S, NewMethod, Category, false))
100 return true;
101 }
102
103 // Look through protocols.
104 for (ObjCList<ObjCProtocolDecl>::iterator I = Class->protocol_begin(),
105 IEnd = Class->protocol_end();
106 I != IEnd; ++I)
107 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
108 return true;
109
110 // Look in our superclass.
111 return CheckObjCMethodOverrides(S, NewMethod, Class->getSuperClass(),
112 false);
113 }
114
115 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC)) {
116 // Look through protocols.
117 for (ObjCList<ObjCProtocolDecl>::iterator I = Category->protocol_begin(),
118 IEnd = Category->protocol_end();
119 I != IEnd; ++I)
120 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
121 return true;
122
123 return false;
124 }
125
126 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC)) {
127 // Look through protocols.
128 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocol->protocol_begin(),
129 IEnd = Protocol->protocol_end();
130 I != IEnd; ++I)
131 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
132 return true;
133
134 return false;
135 }
136
137 return false;
138}
139
140bool Sema::CheckObjCMethodOverrides(ObjCMethodDecl *NewMethod,
141 DeclContext *DC) {
142 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(DC))
143 return ::CheckObjCMethodOverrides(*this, NewMethod, Class);
144
145 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC))
146 return ::CheckObjCMethodOverrides(*this, NewMethod, Category);
147
148 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC))
149 return ::CheckObjCMethodOverrides(*this, NewMethod, Protocol);
150
151 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(DC))
152 return ::CheckObjCMethodOverrides(*this, NewMethod,
153 Impl->getClassInterface());
154
155 if (ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(DC))
156 return ::CheckObjCMethodOverrides(*this, NewMethod,
157 CatImpl->getClassInterface());
158
159 return ::CheckObjCMethodOverrides(*this, NewMethod, CurContext);
160}
161
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000162static void DiagnoseObjCImplementedDeprecations(Sema &S,
163 NamedDecl *ND,
164 SourceLocation ImplLoc,
165 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000166 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000167 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000168 if (select == 0)
169 S.Diag(ND->getLocation(), diag::note_method_declared_at);
170 else
171 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
172 }
173}
174
Steve Naroffebf64432009-02-28 16:59:13 +0000175/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000176/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000177void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000178 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000179 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Steve Naroff394f3f42008-07-25 17:57:26 +0000181 // If we don't have a valid method decl, simply return.
182 if (!MDecl)
183 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000184
185 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000186 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000187 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000188 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000189 AddFactoryMethodToGlobalPool(MDecl, true);
190
Chris Lattner4d391482007-12-12 07:09:47 +0000191 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000192 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000193 PushFunctionScope();
194
Chris Lattner4d391482007-12-12 07:09:47 +0000195 // Create Decl objects for each parameter, entrring them in the scope for
196 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000197
198 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000199 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Daniel Dunbar451318c2008-08-26 06:07:48 +0000201 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
202 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000203
Chris Lattner8123a952008-04-10 02:22:51 +0000204 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000205 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000206 E = MDecl->param_end(); PI != E; ++PI) {
207 ParmVarDecl *Param = (*PI);
208 if (!Param->isInvalidDecl() &&
209 RequireCompleteType(Param->getLocation(), Param->getType(),
210 diag::err_typecheck_decl_incomplete_type))
211 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000212 if ((*PI)->getIdentifier())
213 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000214 }
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000215 // Warn on implementating deprecated methods under
216 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000217 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
218 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000219 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000220 DiagnoseObjCImplementedDeprecations(*this,
221 dyn_cast<NamedDecl>(IMD),
222 MDecl->getLocation(), 0);
Chris Lattner4d391482007-12-12 07:09:47 +0000223}
224
John McCalld226f652010-08-21 09:40:31 +0000225Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000226ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
227 IdentifierInfo *ClassName, SourceLocation ClassLoc,
228 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000229 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000230 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000231 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000232 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattner4d391482007-12-12 07:09:47 +0000234 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000235 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000236 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000237
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000238 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000239 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000240 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000241 }
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000243 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
244 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000245 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000246 if (!IDecl->isForwardDecl()) {
247 IDecl->setInvalidDecl();
248 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
249 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000250
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000251 // Return the previous class interface.
252 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000253 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000254 } else {
255 IDecl->setLocation(AtInterfaceLoc);
256 IDecl->setForwardDecl(false);
257 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000258 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000259 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000260 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000261
262 // Since this ObjCInterfaceDecl was created by a forward declaration,
263 // we now add it to the DeclContext since it wasn't added before
264 // (see ActOnForwardClassDeclaration).
265 IDecl->setLexicalDeclContext(CurContext);
266 CurContext->addDecl(IDecl);
267
268 if (AttrList)
269 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000270 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000271 } else {
272 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
273 ClassName, ClassLoc);
274 if (AttrList)
275 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
276
277 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner4d391482007-12-12 07:09:47 +0000280 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000281 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000282 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
283 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000284
285 if (!PrevDecl) {
286 // Try to correct for a typo in the superclass name.
287 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000288 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000289 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
290 Diag(SuperLoc, diag::err_undef_superclass_suggest)
291 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000292 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
293 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000294 }
295 }
296
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000297 if (PrevDecl == IDecl) {
298 Diag(SuperLoc, diag::err_recursive_superclass)
299 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
300 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000301 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000302 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000303 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000304
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000305 // Diagnose classes that inherit from deprecated classes.
306 if (SuperClassDecl)
307 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000309 if (PrevDecl && SuperClassDecl == 0) {
310 // The previous declaration was not a class decl. Check if we have a
311 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000312 if (const TypedefNameDecl *TDecl =
313 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000314 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000315 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000316 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
317 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000318 }
319 }
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000321 // This handles the following case:
322 //
323 // typedef int SuperClass;
324 // @interface MyClass : SuperClass {} @end
325 //
326 if (!SuperClassDecl) {
327 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
328 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000329 }
330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Richard Smith162e1c12011-04-15 14:24:37 +0000332 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000333 if (!SuperClassDecl)
334 Diag(SuperLoc, diag::err_undef_superclass)
335 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
336 else if (SuperClassDecl->isForwardDecl())
337 Diag(SuperLoc, diag::err_undef_superclass)
338 << SuperClassDecl->getDeclName() << ClassName
339 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000340 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000341 IDecl->setSuperClass(SuperClassDecl);
342 IDecl->setSuperClassLoc(SuperLoc);
343 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000344 }
Chris Lattner4d391482007-12-12 07:09:47 +0000345 } else { // we have a root class.
346 IDecl->setLocEnd(ClassLoc);
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Sebastian Redl0b17c612010-08-13 00:28:03 +0000349 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000350 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000351 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000352 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000353 IDecl->setLocEnd(EndProtoLoc);
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlsson15281452008-11-04 16:57:32 +0000356 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000357 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000358}
359
360/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000361/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000362Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
363 IdentifierInfo *AliasName,
364 SourceLocation AliasLocation,
365 IdentifierInfo *ClassName,
366 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000367 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000368 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000369 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000370 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000371 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000372 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000373 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000374 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000375 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000376 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000377 }
378 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000379 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000380 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000381 if (const TypedefNameDecl *TDecl =
382 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000383 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000384 if (T->isObjCObjectType()) {
385 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000386 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000387 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000388 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000389 }
390 }
391 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000392 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
393 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000394 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000395 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000396 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000397 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000400 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000401 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000402 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Anders Carlsson15281452008-11-04 16:57:32 +0000404 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000405 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000406
John McCalld226f652010-08-21 09:40:31 +0000407 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000408}
409
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000410bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000411 IdentifierInfo *PName,
412 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000413 const ObjCList<ObjCProtocolDecl> &PList) {
414
415 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000416 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
417 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000418 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
419 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000420 if (PDecl->getIdentifier() == PName) {
421 Diag(Ploc, diag::err_protocol_has_circular_dependency);
422 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000423 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000424 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000425 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
426 PDecl->getLocation(), PDecl->getReferencedProtocols()))
427 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000428 }
429 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000430 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000431}
432
John McCalld226f652010-08-21 09:40:31 +0000433Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000434Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
435 IdentifierInfo *ProtocolName,
436 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000437 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000438 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000439 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000440 SourceLocation EndProtoLoc,
441 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000442 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000443 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000444 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000445 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000446 if (PDecl) {
447 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000448 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000449 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000450 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000451 // Just return the protocol we already had.
452 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000453 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000454 }
Steve Naroff61d68522009-03-05 15:22:01 +0000455 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000456 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000457 err = CheckForwardProtocolDeclarationForCircularDependency(
458 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Steve Narofff11b5082008-08-13 16:39:22 +0000460 // Make sure the cached decl gets a valid start location.
461 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000462 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000463 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000464 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000465 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000466 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000467 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000468 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000469 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000470 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000471 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000472 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000473 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000474 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000475 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000476 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
477 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000478 PDecl->setLocEnd(EndProtoLoc);
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
481 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000482 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000483}
484
485/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000486/// issues an error if they are not declared. It returns list of
487/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000488void
Chris Lattnere13b9592008-07-26 04:03:38 +0000489Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000490 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000491 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000492 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000493 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000494 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
495 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000496 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000497 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
498 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000499 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000500 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
501 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
502 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000503 Diag(PDecl->getLocation(), diag::note_previous_decl)
504 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000505 }
506 }
507
508 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000509 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000510 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000511 continue;
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000514 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000515
516 // If this is a forward declaration and we are supposed to warn in this
517 // case, do it.
518 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000519 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000520 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000521 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000522 }
523}
524
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000525/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000526/// a class method in its extension.
527///
Mike Stump1eb44332009-09-09 15:08:12 +0000528void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000529 ObjCInterfaceDecl *ID) {
530 if (!ID)
531 return; // Possibly due to previous error
532
533 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000534 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
535 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000536 ObjCMethodDecl *MD = *i;
537 MethodMap[MD->getSelector()] = MD;
538 }
539
540 if (MethodMap.empty())
541 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000542 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
543 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000544 ObjCMethodDecl *Method = *i;
545 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
546 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
547 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
548 << Method->getDeclName();
549 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
550 }
551 }
552}
553
Chris Lattner58fe03b2009-04-12 08:43:13 +0000554/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000555Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000556Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000557 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000558 unsigned NumElts,
559 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000560 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000561 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Chris Lattner4d391482007-12-12 07:09:47 +0000563 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000564 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000565 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000566 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000567 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000568 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000569 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000570 PushOnScopeChains(PDecl, TUScope, false);
571 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000572 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000573 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000574 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000575 if (!isNew)
576 PDecl->setChangedSinceDeserialization(true);
577 }
Chris Lattner4d391482007-12-12 07:09:47 +0000578 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000579 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
582 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000583 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000584 Protocols.data(), Protocols.size(),
585 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000586 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000587 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000588 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000589}
590
John McCalld226f652010-08-21 09:40:31 +0000591Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000592ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
593 IdentifierInfo *ClassName, SourceLocation ClassLoc,
594 IdentifierInfo *CategoryName,
595 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000596 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000597 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000598 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000599 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000600 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000601 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000602
603 /// Check that class of this category is already completely declared.
604 if (!IDecl || IDecl->isForwardDecl()) {
605 // Create an invalid ObjCCategoryDecl to serve as context for
606 // the enclosing method declarations. We mark the decl invalid
607 // to make it clear that this isn't a valid AST.
608 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
609 ClassLoc, CategoryLoc, CategoryName);
610 CDecl->setInvalidDecl();
611 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000612 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000613 }
614
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000615 if (!CategoryName && IDecl->getImplementation()) {
616 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
617 Diag(IDecl->getImplementation()->getLocation(),
618 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000619 }
620
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000621 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
622 ClassLoc, CategoryLoc, CategoryName);
623 // FIXME: PushOnScopeChains?
624 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000625
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000626 CDecl->setClassInterface(IDecl);
627 // Insert class extension to the list of class's categories.
628 if (!CategoryName)
629 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Chris Lattner16b34b42009-02-16 21:30:01 +0000631 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000632 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000633
Fariborz Jahanian25760612010-02-15 21:55:26 +0000634 if (CategoryName) {
635 /// Check for duplicate interface declaration for this category
636 ObjCCategoryDecl *CDeclChain;
637 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
638 CDeclChain = CDeclChain->getNextClassCategory()) {
639 if (CDeclChain->getIdentifier() == CategoryName) {
640 // Class extensions can be declared multiple times.
641 Diag(CategoryLoc, diag::warn_dup_category_def)
642 << ClassName << CategoryName;
643 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
644 break;
645 }
Chris Lattner70f19542009-02-16 21:26:43 +0000646 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000647 if (!CDeclChain)
648 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000649 }
Chris Lattner70f19542009-02-16 21:26:43 +0000650
Chris Lattner4d391482007-12-12 07:09:47 +0000651 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000652 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000653 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000654 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000655 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000656 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000657 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Anders Carlsson15281452008-11-04 16:57:32 +0000660 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000661 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000662}
663
664/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000665/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000666/// object.
John McCalld226f652010-08-21 09:40:31 +0000667Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000668 SourceLocation AtCatImplLoc,
669 IdentifierInfo *ClassName, SourceLocation ClassLoc,
670 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000671 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000672 ObjCCategoryDecl *CatIDecl = 0;
673 if (IDecl) {
674 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
675 if (!CatIDecl) {
676 // Category @implementation with no corresponding @interface.
677 // Create and install one.
678 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000679 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000680 CatName);
681 CatIDecl->setClassInterface(IDecl);
682 CatIDecl->insertNextClassCategory();
683 }
684 }
685
Mike Stump1eb44332009-09-09 15:08:12 +0000686 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000687 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
688 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000689 /// Check that class of this category is already completely declared.
690 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000691 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000692
Douglas Gregord0434102009-01-09 00:49:46 +0000693 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000694 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000695
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000696 /// Check that CatName, category name, is not used in another implementation.
697 if (CatIDecl) {
698 if (CatIDecl->getImplementation()) {
699 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
700 << CatName;
701 Diag(CatIDecl->getImplementation()->getLocation(),
702 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000703 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000704 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000705 // Warn on implementating category of deprecated class under
706 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000707 DiagnoseObjCImplementedDeprecations(*this,
708 dyn_cast<NamedDecl>(IDecl),
709 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000710 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Anders Carlsson15281452008-11-04 16:57:32 +0000713 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000714 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000715}
716
John McCalld226f652010-08-21 09:40:31 +0000717Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000718 SourceLocation AtClassImplLoc,
719 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000720 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000721 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000722 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000723 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000724 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000725 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
726 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000727 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000728 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000729 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000730 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
731 // If this is a forward declaration of an interface, warn.
732 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000733 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000734 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000735 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000736 } else {
737 // We did not find anything with the name ClassName; try to correct for
738 // typos in the class name.
739 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000740 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000741 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000742 // Suggest the (potentially) correct interface name. However, put the
743 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000744 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000745 // provide a code-modification hint or use the typo name for recovery,
746 // because this is just a warning. The program may actually be correct.
747 Diag(ClassLoc, diag::warn_undef_interface_suggest)
748 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000749 Diag(IDecl->getLocation(), diag::note_previous_decl)
750 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000751 << FixItHint::CreateReplacement(ClassLoc,
752 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000753 IDecl = 0;
754 } else {
755 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
756 }
Chris Lattner4d391482007-12-12 07:09:47 +0000757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattner4d391482007-12-12 07:09:47 +0000759 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000760 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000761 if (SuperClassname) {
762 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000763 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
764 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000765 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000766 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
767 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000768 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000769 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000770 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000771 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000772 Diag(SuperClassLoc, diag::err_undef_superclass)
773 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000774 else if (IDecl && IDecl->getSuperClass() != SDecl) {
775 // This implementation and its interface do not have the same
776 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000777 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000778 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000779 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000780 }
781 }
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattner4d391482007-12-12 07:09:47 +0000784 if (!IDecl) {
785 // Legacy case of @implementation with no corresponding @interface.
786 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000787
Mike Stump390b4cc2009-05-16 07:39:55 +0000788 // FIXME: Do we support attributes on the @implementation? If so we should
789 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000790 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000791 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000792 IDecl->setSuperClass(SDecl);
793 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000794
795 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000796 } else {
797 // Mark the interface as being completed, even if it was just as
798 // @class ....;
799 // declaration; the user cannot reopen it.
800 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
803 ObjCImplementationDecl* IMPDecl =
804 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000805 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Anders Carlsson15281452008-11-04 16:57:32 +0000807 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000808 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Chris Lattner4d391482007-12-12 07:09:47 +0000810 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000811 if (IDecl->getImplementation()) {
812 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000813 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000814 Diag(IDecl->getImplementation()->getLocation(),
815 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000816 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000817 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000818 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000819 // Warn on implementating deprecated class under
820 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000821 DiagnoseObjCImplementedDeprecations(*this,
822 dyn_cast<NamedDecl>(IDecl),
823 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000824 }
John McCalld226f652010-08-21 09:40:31 +0000825 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000826}
827
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
829 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000830 SourceLocation RBrace) {
831 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000832 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000833 if (!IDecl)
834 return;
835 /// Check case of non-existing @interface decl.
836 /// (legacy objective-c @implementation decl without an @interface decl).
837 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000838 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000839 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000840 // Add ivar's to class's DeclContext.
841 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000842 ivars[i]->setLexicalDeclContext(ImpDecl);
843 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000844 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000845 }
846
Chris Lattner4d391482007-12-12 07:09:47 +0000847 return;
848 }
849 // If implementation has empty ivar list, just return.
850 if (numIvars == 0)
851 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattner4d391482007-12-12 07:09:47 +0000853 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000854 if (LangOpts.ObjCNonFragileABI2) {
855 if (ImpDecl->getSuperClass())
856 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
857 for (unsigned i = 0; i < numIvars; i++) {
858 ObjCIvarDecl* ImplIvar = ivars[i];
859 if (const ObjCIvarDecl *ClsIvar =
860 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
861 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
862 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
863 continue;
864 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000865 // Instance ivar to Implementation's DeclContext.
866 ImplIvar->setLexicalDeclContext(ImpDecl);
867 IDecl->makeDeclVisibleInContext(ImplIvar, false);
868 ImpDecl->addDecl(ImplIvar);
869 }
870 return;
871 }
Chris Lattner4d391482007-12-12 07:09:47 +0000872 // Check interface's Ivar list against those in the implementation.
873 // names and types must match.
874 //
Chris Lattner4d391482007-12-12 07:09:47 +0000875 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000876 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000877 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
878 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000879 ObjCIvarDecl* ImplIvar = ivars[j++];
880 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000881 assert (ImplIvar && "missing implementation ivar");
882 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Steve Naroffca331292009-03-03 14:49:36 +0000884 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000885 if (Context.getCanonicalType(ImplIvar->getType()) !=
886 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000887 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000888 << ImplIvar->getIdentifier()
889 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000890 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000891 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
892 Expr *ImplBitWidth = ImplIvar->getBitWidth();
893 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000894 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
895 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000896 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
897 << ImplIvar->getIdentifier();
898 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900 }
Steve Naroffca331292009-03-03 14:49:36 +0000901 // Make sure the names are identical.
902 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000903 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000904 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000905 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000906 }
907 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Chris Lattner609e4c72007-12-12 18:11:49 +0000910 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000911 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000912 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000913 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000914}
915
Steve Naroff3c2eb662008-02-10 21:38:56 +0000916void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000917 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000918 if (!IncompleteImpl) {
919 Diag(ImpLoc, diag::warn_incomplete_impl);
920 IncompleteImpl = true;
921 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000922 if (DiagID == diag::warn_unimplemented_protocol_method)
923 Diag(ImpLoc, DiagID) << method->getDeclName();
924 else
925 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000926}
927
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000928/// Determines if type B can be substituted for type A. Returns true if we can
929/// guarantee that anything that the user will do to an object of type A can
930/// also be done to an object of type B. This is trivially true if the two
931/// types are the same, or if B is a subclass of A. It becomes more complex
932/// in cases where protocols are involved.
933///
934/// Object types in Objective-C describe the minimum requirements for an
935/// object, rather than providing a complete description of a type. For
936/// example, if A is a subclass of B, then B* may refer to an instance of A.
937/// The principle of substitutability means that we may use an instance of A
938/// anywhere that we may use an instance of B - it will implement all of the
939/// ivars of B and all of the methods of B.
940///
941/// This substitutability is important when type checking methods, because
942/// the implementation may have stricter type definitions than the interface.
943/// The interface specifies minimum requirements, but the implementation may
944/// have more accurate ones. For example, a method may privately accept
945/// instances of B, but only publish that it accepts instances of A. Any
946/// object passed to it will be type checked against B, and so will implicitly
947/// by a valid A*. Similarly, a method may return a subclass of the class that
948/// it is declared as returning.
949///
950/// This is most important when considering subclassing. A method in a
951/// subclass must accept any object as an argument that its superclass's
952/// implementation accepts. It may, however, accept a more general type
953/// without breaking substitutability (i.e. you can still use the subclass
954/// anywhere that you can use the superclass, but not vice versa). The
955/// converse requirement applies to return types: the return type for a
956/// subclass method must be a valid object of the kind that the superclass
957/// advertises, but it may be specified more accurately. This avoids the need
958/// for explicit down-casting by callers.
959///
960/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +0000961static bool isObjCTypeSubstitutable(ASTContext &Context,
962 const ObjCObjectPointerType *A,
963 const ObjCObjectPointerType *B,
964 bool rejectId) {
965 // Reject a protocol-unqualified id.
966 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000967
968 // If B is a qualified id, then A must also be a qualified id and it must
969 // implement all of the protocols in B. It may not be a qualified class.
970 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
971 // stricter definition so it is not substitutable for id<A>.
972 if (B->isObjCQualifiedIdType()) {
973 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +0000974 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
975 QualType(B,0),
976 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000977 }
978
979 /*
980 // id is a special type that bypasses type checking completely. We want a
981 // warning when it is used in one place but not another.
982 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
983
984
985 // If B is a qualified id, then A must also be a qualified id (which it isn't
986 // if we've got this far)
987 if (B->isObjCQualifiedIdType()) return false;
988 */
989
990 // Now we know that A and B are (potentially-qualified) class types. The
991 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +0000992 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000993}
994
John McCall10302c02010-10-28 02:34:38 +0000995static SourceRange getTypeRange(TypeSourceInfo *TSI) {
996 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
997}
998
999static void CheckMethodOverrideReturn(Sema &S,
1000 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001001 ObjCMethodDecl *MethodDecl,
1002 bool IsProtocolMethodDecl) {
1003 if (IsProtocolMethodDecl &&
1004 (MethodDecl->getObjCDeclQualifier() !=
1005 MethodImpl->getObjCDeclQualifier())) {
1006 S.Diag(MethodImpl->getLocation(),
1007 diag::warn_conflicting_ret_type_modifiers)
1008 << MethodImpl->getDeclName()
1009 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1010 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1011 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1012 }
1013
John McCall10302c02010-10-28 02:34:38 +00001014 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001015 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +00001016 return;
1017
1018 unsigned DiagID = diag::warn_conflicting_ret_types;
1019
1020 // Mismatches between ObjC pointers go into a different warning
1021 // category, and sometimes they're even completely whitelisted.
1022 if (const ObjCObjectPointerType *ImplPtrTy =
1023 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1024 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001025 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001026 // Allow non-matching return types as long as they don't violate
1027 // the principle of substitutability. Specifically, we permit
1028 // return types that are subclasses of the declared return type,
1029 // or that are more-qualified versions of the declared type.
1030 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1031 return;
1032
1033 DiagID = diag::warn_non_covariant_ret_types;
1034 }
1035 }
1036
1037 S.Diag(MethodImpl->getLocation(), DiagID)
1038 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001039 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001040 << MethodImpl->getResultType()
1041 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001042 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
1043 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +00001044}
1045
1046static void CheckMethodOverrideParam(Sema &S,
1047 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001048 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001049 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001050 ParmVarDecl *IfaceVar,
1051 bool IsProtocolMethodDecl) {
1052 if (IsProtocolMethodDecl &&
1053 (ImplVar->getObjCDeclQualifier() !=
1054 IfaceVar->getObjCDeclQualifier())) {
1055 S.Diag(ImplVar->getLocation(),
1056 diag::warn_conflicting_param_modifiers)
1057 << getTypeRange(ImplVar->getTypeSourceInfo())
1058 << MethodImpl->getDeclName();
1059 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1060 << getTypeRange(IfaceVar->getTypeSourceInfo());
1061 }
1062
John McCall10302c02010-10-28 02:34:38 +00001063 QualType ImplTy = ImplVar->getType();
1064 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001065
John McCall10302c02010-10-28 02:34:38 +00001066 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
1067 return;
1068
1069 unsigned DiagID = diag::warn_conflicting_param_types;
1070
1071 // Mismatches between ObjC pointers go into a different warning
1072 // category, and sometimes they're even completely whitelisted.
1073 if (const ObjCObjectPointerType *ImplPtrTy =
1074 ImplTy->getAs<ObjCObjectPointerType>()) {
1075 if (const ObjCObjectPointerType *IfacePtrTy =
1076 IfaceTy->getAs<ObjCObjectPointerType>()) {
1077 // Allow non-matching argument types as long as they don't
1078 // violate the principle of substitutability. Specifically, the
1079 // implementation must accept any objects that the superclass
1080 // accepts, however it may also accept others.
1081 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
1082 return;
1083
1084 DiagID = diag::warn_non_contravariant_param_types;
1085 }
1086 }
1087
1088 S.Diag(ImplVar->getLocation(), DiagID)
1089 << getTypeRange(ImplVar->getTypeSourceInfo())
1090 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1091 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
1092 << getTypeRange(IfaceVar->getTypeSourceInfo());
1093}
1094
1095
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001096void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001097 ObjCMethodDecl *MethodDecl,
1098 bool IsProtocolMethodDecl) {
1099 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1100 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Chris Lattner3aff9192009-04-11 19:58:42 +00001102 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001103 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +00001104 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001105 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1106 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001108 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001109 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001110 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001111 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001112}
1113
Mike Stump390b4cc2009-05-16 07:39:55 +00001114/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1115/// improve the efficiency of selector lookups and type checking by associating
1116/// with each protocol / interface / category the flattened instance tables. If
1117/// we used an immutable set to keep the table then it wouldn't add significant
1118/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001119
Steve Naroffefe7f362008-02-08 22:06:17 +00001120/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001121/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001122void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1123 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001124 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001125 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001126 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001127 ObjCContainerDecl *CDecl) {
1128 ObjCInterfaceDecl *IDecl;
1129 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1130 IDecl = C->getClassInterface();
1131 else
1132 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1133 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1134
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001135 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001136 ObjCInterfaceDecl *NSIDecl = 0;
1137 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001138 // check to see if class implements forwardInvocation method and objects
1139 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001140 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001141 // Under such conditions, which means that every method possible is
1142 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001143 // found" warnings.
1144 // FIXME: Use a general GetUnarySelector method for this.
1145 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1146 Selector fISelector = Context.Selectors.getSelector(1, &II);
1147 if (InsMap.count(fISelector))
1148 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1149 // need be implemented in the implementation.
1150 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001153 // If a method lookup fails locally we still need to look and see if
1154 // the method was implemented by a base class or an inherited
1155 // protocol. This lookup is slow, but occurs rarely in correct code
1156 // and otherwise would terminate in a warning.
1157
Chris Lattner4d391482007-12-12 07:09:47 +00001158 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001159 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001160 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001161 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001162 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001163 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001164 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001165 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001166 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001167 // Ugly, but necessary. Method declared in protcol might have
1168 // have been synthesized due to a property declared in the class which
1169 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001170 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001171 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001172 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001173 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001174 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1175 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001176 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001177 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001178 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1179 << PDecl->getDeclName();
1180 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001181 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001182 }
1183 }
Chris Lattner4d391482007-12-12 07:09:47 +00001184 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001185 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001186 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001187 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001188 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001189 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1190 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001191 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001192 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001193 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001194 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001195 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001196 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1197 PDecl->getDeclName();
1198 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001199 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001200 }
Chris Lattner780f3292008-07-21 21:32:27 +00001201 // Check on this protocols's referenced protocols, recursively.
1202 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1203 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001204 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001205}
1206
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001207/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1208/// or protocol against those declared in their implementations.
1209///
1210void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1211 const llvm::DenseSet<Selector> &ClsMap,
1212 llvm::DenseSet<Selector> &InsMapSeen,
1213 llvm::DenseSet<Selector> &ClsMapSeen,
1214 ObjCImplDecl* IMPDecl,
1215 ObjCContainerDecl* CDecl,
1216 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001217 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001218 // Check and see if instance methods in class interface have been
1219 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001220 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1221 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001222 if (InsMapSeen.count((*I)->getSelector()))
1223 continue;
1224 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001225 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001226 !InsMap.count((*I)->getSelector())) {
1227 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001228 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1229 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001230 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001231 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001232 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001233 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001234 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001235 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001236 assert(MethodDecl &&
1237 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001238 // ImpMethodDecl may be null as in a @dynamic property.
1239 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001240 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1241 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001242 }
1243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001245 // Check and see if class methods in class interface have been
1246 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001247 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001248 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001249 if (ClsMapSeen.count((*I)->getSelector()))
1250 continue;
1251 ClsMapSeen.insert((*I)->getSelector());
1252 if (!ClsMap.count((*I)->getSelector())) {
1253 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001254 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1255 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001256 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001257 ObjCMethodDecl *ImpMethodDecl =
1258 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001259 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001260 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001261 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1262 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001263 }
1264 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001265
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001266 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001267 // Also methods in class extensions need be looked at next.
1268 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1269 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1270 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1271 IMPDecl,
1272 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1273 IncompleteImpl, false);
1274
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001275 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001276 for (ObjCInterfaceDecl::all_protocol_iterator
1277 PI = I->all_referenced_protocol_begin(),
1278 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001279 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1280 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001281 (*PI), IncompleteImpl, false);
1282 if (I->getSuperClass())
1283 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001284 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001285 I->getSuperClass(), IncompleteImpl, false);
1286 }
1287}
1288
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001289void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001290 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001291 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001292 llvm::DenseSet<Selector> InsMap;
1293 // Check and see if instance methods in class interface have been
1294 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001295 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001296 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001297 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001299 // Check and see if properties declared in the interface have either 1)
1300 // an implementation or 2) there is a @synthesize/@dynamic implementation
1301 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001302 if (isa<ObjCInterfaceDecl>(CDecl) &&
1303 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001304 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001305
Chris Lattner4d391482007-12-12 07:09:47 +00001306 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001307 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001308 I = IMPDecl->classmeth_begin(),
1309 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001310 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001312 // Check for type conflict of methods declared in a class/protocol and
1313 // its implementation; if any.
1314 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001315 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1316 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001317 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chris Lattner4d391482007-12-12 07:09:47 +00001319 // Check the protocol list for unimplemented methods in the @implementation
1320 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001321 // Check and see if class methods in class interface have been
1322 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Chris Lattnercddc8882009-03-01 00:56:52 +00001324 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001325 for (ObjCInterfaceDecl::all_protocol_iterator
1326 PI = I->all_referenced_protocol_begin(),
1327 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001328 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001329 InsMap, ClsMap, I);
1330 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001331 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1332 Categories; Categories = Categories->getNextClassExtension())
1333 ImplMethodsVsClassMethods(S, IMPDecl,
1334 const_cast<ObjCCategoryDecl*>(Categories),
1335 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001336 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001337 // For extended class, unimplemented methods in its protocols will
1338 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001339 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001340 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1341 E = C->protocol_end(); PI != E; ++PI)
1342 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001343 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001344 // Report unimplemented properties in the category as well.
1345 // When reporting on missing setter/getters, do not report when
1346 // setter/getter is implemented in category's primary class
1347 // implementation.
1348 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1349 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1350 for (ObjCImplementationDecl::instmeth_iterator
1351 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1352 InsMap.insert((*I)->getSelector());
1353 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001354 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001355 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001356 } else
1357 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001358}
1359
Mike Stump1eb44332009-09-09 15:08:12 +00001360/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001361Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001362Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001363 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001364 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001365 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001366 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Chris Lattner4d391482007-12-12 07:09:47 +00001368 for (unsigned i = 0; i != NumElts; ++i) {
1369 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001370 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001371 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001372 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001373 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001374 // Maybe we will complain about the shadowed template parameter.
1375 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1376 // Just pretend that we didn't see the previous declaration.
1377 PrevDecl = 0;
1378 }
1379
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001380 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001381 // GCC apparently allows the following idiom:
1382 //
1383 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1384 // @class XCElementToggler;
1385 //
Mike Stump1eb44332009-09-09 15:08:12 +00001386 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001387 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001388 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001389 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001390 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001391 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001392 // a forward class declaration matching a typedef name of a class refers
1393 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001394 if (const ObjCObjectType *OI =
1395 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1396 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001397 }
Chris Lattner4d391482007-12-12 07:09:47 +00001398 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001399 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1400 if (!IDecl) { // Not already seen? Make a forward decl.
1401 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1402 IdentList[i], IdentLocs[i], true);
1403
1404 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1405 // the current DeclContext. This prevents clients that walk DeclContext
1406 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1407 // declared later (if at all). We also take care to explicitly make
1408 // sure this declaration is visible for name lookup.
1409 PushOnScopeChains(IDecl, TUScope, false);
1410 CurContext->makeDeclVisibleInContext(IDecl, true);
1411 }
Chris Lattner4d391482007-12-12 07:09:47 +00001412
1413 Interfaces.push_back(IDecl);
1414 }
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Ted Kremenek321c22f2009-11-18 00:28:11 +00001416 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001417 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001418 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001419 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001420 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001421 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001422 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001423}
1424
1425
1426/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1427/// returns true, or false, accordingly.
1428/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001429bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001430 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001431 bool matchBasedOnSizeAndAlignment,
1432 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001433 QualType T1 = Context.getCanonicalType(Method->getResultType());
1434 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001436 if (T1 != T2) {
1437 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001438 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001439 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001440 // Incomplete types don't have a size and alignment.
1441 if (T1->isIncompleteType() || T2->isIncompleteType())
1442 return false;
1443 // Check is based on size and alignment.
1444 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1445 return false;
1446 }
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Chris Lattner89951a82009-02-20 18:43:26 +00001448 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1449 E = Method->param_end();
1450 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Chris Lattner89951a82009-02-20 18:43:26 +00001452 for (; ParamI != E; ++ParamI, ++PrevI) {
1453 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1454 T1 = Context.getCanonicalType((*ParamI)->getType());
1455 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001456 if (T1 != T2) {
1457 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001458 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001459 return false;
1460 // Incomplete types don't have a size and alignment.
1461 if (T1->isIncompleteType() || T2->isIncompleteType())
1462 return false;
1463 // Check is based on size and alignment.
1464 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1465 return false;
1466 }
Chris Lattner4d391482007-12-12 07:09:47 +00001467 }
1468 return true;
1469}
1470
Sebastian Redldb9d2142010-08-02 23:18:59 +00001471/// \brief Read the contents of the method pool for a given selector from
1472/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001473///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001474/// This routine should only be called once, when the method pool has no entry
1475/// for this selector.
1476Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001477 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001478 assert(MethodPool.find(Sel) == MethodPool.end() &&
1479 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001480
1481 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001482 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Sebastian Redldb9d2142010-08-02 23:18:59 +00001484 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001485}
1486
Sebastian Redldb9d2142010-08-02 23:18:59 +00001487void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1488 bool instance) {
1489 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1490 if (Pos == MethodPool.end()) {
1491 if (ExternalSource)
1492 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001493 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001494 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1495 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001496 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001497 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001498 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001499 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001500 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001501 Entry.Method = Method;
1502 Entry.Next = 0;
1503 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Chris Lattnerb25df352009-03-04 05:16:45 +00001506 // We've seen a method with this name, see if we have already seen this type
1507 // signature.
1508 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001509 if (MatchTwoMethodDeclarations(Method, List->Method)) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001510 ObjCMethodDecl *PrevObjCMethod = List->Method;
1511 PrevObjCMethod->setDefined(impl);
1512 // If a method is deprecated, push it in the global pool.
1513 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001514 if (Method->isDeprecated()) {
1515 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001516 List->Method = Method;
1517 }
1518 // If new method is unavailable, push it into global pool
1519 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001520 if (Method->isUnavailable()) {
1521 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001522 List->Method = Method;
1523 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001524 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Chris Lattnerb25df352009-03-04 05:16:45 +00001527 // We have a new signature for an existing method - add it.
1528 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001529 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1530 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001531}
1532
Sebastian Redldb9d2142010-08-02 23:18:59 +00001533ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001534 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001535 bool warn, bool instance) {
1536 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1537 if (Pos == MethodPool.end()) {
1538 if (ExternalSource)
1539 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001540 else
1541 return 0;
1542 }
1543
Sebastian Redldb9d2142010-08-02 23:18:59 +00001544 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001546 bool strictSelectorMatch = receiverIdOrClass && warn &&
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001547 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1548 R.getBegin()) !=
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001549 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001550 if (warn && MethList.Method && MethList.Next) {
1551 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001552 if (strictSelectorMatch)
1553 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1554 // This checks if the methods differ in type mismatch.
1555 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1556 issueWarning = true;
1557 }
1558
1559 if (!issueWarning)
1560 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1561 // This checks if the methods differ by size & alignment.
1562 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1563 issueWarning = true;
1564 }
1565
Sebastian Redldb9d2142010-08-02 23:18:59 +00001566 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001567 if (strictSelectorMatch)
1568 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1569 else
1570 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001571 Diag(MethList.Method->getLocStart(), diag::note_using)
1572 << MethList.Method->getSourceRange();
1573 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1574 Diag(Next->Method->getLocStart(), diag::note_also_found)
1575 << Next->Method->getSourceRange();
1576 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001577 }
1578 return MethList.Method;
1579}
1580
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001581ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001582 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1583 if (Pos == MethodPool.end())
1584 return 0;
1585
1586 GlobalMethods &Methods = Pos->second;
1587
1588 if (Methods.first.Method && Methods.first.Method->isDefined())
1589 return Methods.first.Method;
1590 if (Methods.second.Method && Methods.second.Method->isDefined())
1591 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001592 return 0;
1593}
1594
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001595/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1596/// identical selector names in current and its super classes and issues
1597/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001598void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1599 ObjCMethodDecl *Method,
1600 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001601 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1602 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001604 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001605 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001606 SD->lookupMethod(Method->getSelector(), IsInstance);
1607 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001608 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001609 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001610 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001611 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1612 E = Method->param_end();
1613 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1614 for (; ParamI != E; ++ParamI, ++PrevI) {
1615 // Number of parameters are the same and is guaranteed by selector match.
1616 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1617 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1618 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001619 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001620 // respective argument type in the super class method, issue warning;
1621 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001622 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001623 << T1 << T2;
1624 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1625 return;
1626 }
1627 }
1628 ID = SD;
1629 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001630}
1631
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001632/// DiagnoseDuplicateIvars -
1633/// Check for duplicate ivars in the entire class at the start of
1634/// @implementation. This becomes necesssary because class extension can
1635/// add ivars to a class in random order which will not be known until
1636/// class's @implementation is seen.
1637void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1638 ObjCInterfaceDecl *SID) {
1639 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1640 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1641 ObjCIvarDecl* Ivar = (*IVI);
1642 if (Ivar->isInvalidDecl())
1643 continue;
1644 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1645 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1646 if (prevIvar) {
1647 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1648 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1649 Ivar->setInvalidDecl();
1650 }
1651 }
1652 }
1653}
1654
Steve Naroffa56f6162007-12-18 01:30:32 +00001655// Note: For class/category implemenations, allMethods/allProperties is
1656// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001657void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001658 Decl *ClassDecl,
1659 Decl **allMethods, unsigned allNum,
1660 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001661 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001662 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1663 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001664 // should be true.
1665 if (!ClassDecl)
1666 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001667
Mike Stump1eb44332009-09-09 15:08:12 +00001668 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001669 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1670 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001671 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001672
Ted Kremenek782f2f52010-01-07 01:20:12 +00001673 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1674 // FIXME: This is wrong. We shouldn't be pretending that there is
1675 // an '@end' in the declaration.
1676 SourceLocation L = ClassDecl->getLocation();
1677 AtEnd.setBegin(L);
1678 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001679 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001680 }
1681
Steve Naroff0701bbb2009-01-08 17:28:14 +00001682 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1683 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1684 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1685
Chris Lattner4d391482007-12-12 07:09:47 +00001686 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001687 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001688 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001689
1690 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001691 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001692 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001693 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001694 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001695 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001696 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001697 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001698 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001699 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001700 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001701 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001702 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001703 InsMap[Method->getSelector()] = Method;
1704 /// The following allows us to typecheck messages to "id".
1705 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001706 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001707 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001708 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001709 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001710 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001711 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001712 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001713 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001714 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001715 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001716 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001717 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001718 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001719 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001720 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001721 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001722 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001723 /// The following allows us to typecheck messages to "Class".
1724 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001725 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001726 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001727 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001728 }
1729 }
1730 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001731 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001732 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001733 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001734 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00001735 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001736 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001737 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001738 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001739 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001740
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001741 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00001742 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001743 if (C->IsClassExtension()) {
1744 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
1745 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001746 }
Chris Lattner4d391482007-12-12 07:09:47 +00001747 }
Steve Naroff09c47192009-01-09 15:36:25 +00001748 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001749 if (CDecl->getIdentifier())
1750 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1751 // user-defined setter/getter. It also synthesizes setter/getter methods
1752 // and adds them to the DeclContext and global method pools.
1753 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1754 E = CDecl->prop_end();
1755 I != E; ++I)
1756 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001757 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001758 }
1759 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001760 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001761 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00001762 // Any property declared in a class extension might have user
1763 // declared setter or getter in current class extension or one
1764 // of the other class extensions. Mark them as synthesized as
1765 // property will be synthesized when property with same name is
1766 // seen in the @implementation.
1767 for (const ObjCCategoryDecl *ClsExtDecl =
1768 IDecl->getFirstClassExtension();
1769 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
1770 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
1771 E = ClsExtDecl->prop_end(); I != E; ++I) {
1772 ObjCPropertyDecl *Property = (*I);
1773 // Skip over properties declared @dynamic
1774 if (const ObjCPropertyImplDecl *PIDecl
1775 = IC->FindPropertyImplDecl(Property->getIdentifier()))
1776 if (PIDecl->getPropertyImplementation()
1777 == ObjCPropertyImplDecl::Dynamic)
1778 continue;
1779
1780 for (const ObjCCategoryDecl *CExtDecl =
1781 IDecl->getFirstClassExtension();
1782 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
1783 if (ObjCMethodDecl *GetterMethod =
1784 CExtDecl->getInstanceMethod(Property->getGetterName()))
1785 GetterMethod->setSynthesized(true);
1786 if (!Property->isReadOnly())
1787 if (ObjCMethodDecl *SetterMethod =
1788 CExtDecl->getInstanceMethod(Property->getSetterName()))
1789 SetterMethod->setSynthesized(true);
1790 }
1791 }
1792 }
1793
Ted Kremenekc32647d2010-12-23 21:35:43 +00001794 if (LangOpts.ObjCDefaultSynthProperties &&
1795 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001796 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001797 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001798 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001799
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001800 if (LangOpts.ObjCNonFragileABI2)
1801 while (IDecl->getSuperClass()) {
1802 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1803 IDecl = IDecl->getSuperClass();
1804 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001805 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001806 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001807 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001808 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001809 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Chris Lattner4d391482007-12-12 07:09:47 +00001811 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001812 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001813 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001814 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001815 Categories; Categories = Categories->getNextClassCategory()) {
1816 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001817 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001818 break;
1819 }
1820 }
1821 }
1822 }
Chris Lattner682bf922009-03-29 16:50:03 +00001823 if (isInterfaceDeclKind) {
1824 // Reject invalid vardecls.
1825 for (unsigned i = 0; i != tuvNum; i++) {
1826 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1827 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1828 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001829 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001830 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001831 }
Chris Lattner682bf922009-03-29 16:50:03 +00001832 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001833 }
Chris Lattner4d391482007-12-12 07:09:47 +00001834}
1835
1836
1837/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1838/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001839static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001840CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00001841 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00001842}
1843
Ted Kremenek422bae72010-04-18 04:59:38 +00001844static inline
Sean Huntcf807c42010-08-18 23:23:40 +00001845bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00001846 // The 'ibaction' attribute is allowed on method definitions because of
1847 // how the IBAction macro is used on both method declarations and definitions.
1848 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00001849 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1850 if ((*i)->getKind() != attr::IBAction)
1851 return true;
1852 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00001853}
1854
Douglas Gregor926df6c2011-06-11 01:09:30 +00001855/// \brief Check whether the declared result type of the given Objective-C
1856/// method declaration is compatible with the method's class.
1857///
1858static bool
1859CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
1860 ObjCInterfaceDecl *CurrentClass) {
1861 QualType ResultType = Method->getResultType();
1862 SourceRange ResultTypeRange;
1863 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
1864 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
1865
1866 // If an Objective-C method inherits its related result type, then its
1867 // declared result type must be compatible with its own class type. The
1868 // declared result type is compatible if:
1869 if (const ObjCObjectPointerType *ResultObjectType
1870 = ResultType->getAs<ObjCObjectPointerType>()) {
1871 // - it is id or qualified id, or
1872 if (ResultObjectType->isObjCIdType() ||
1873 ResultObjectType->isObjCQualifiedIdType())
1874 return false;
1875
1876 if (CurrentClass) {
1877 if (ObjCInterfaceDecl *ResultClass
1878 = ResultObjectType->getInterfaceDecl()) {
1879 // - it is the same as the method's class type, or
1880 if (CurrentClass == ResultClass)
1881 return false;
1882
1883 // - it is a superclass of the method's class type
1884 if (ResultClass->isSuperClassOf(CurrentClass))
1885 return false;
1886 }
1887 }
1888 }
1889
1890 return true;
1891}
1892
1893/// \brief Determine if any method in the global method pool has an inferred
1894/// result type.
1895static bool
1896anyMethodInfersRelatedResultType(Sema &S, Selector Sel, bool IsInstance) {
1897 Sema::GlobalMethodPool::iterator Pos = S.MethodPool.find(Sel);
1898 if (Pos == S.MethodPool.end()) {
1899 if (S.ExternalSource)
1900 Pos = S.ReadMethodPool(Sel);
1901 else
1902 return 0;
1903 }
1904
1905 ObjCMethodList &List = IsInstance ? Pos->second.first : Pos->second.second;
1906 for (ObjCMethodList *M = &List; M; M = M->Next) {
1907 if (M->Method && M->Method->hasRelatedResultType())
1908 return true;
1909 }
1910
1911 return false;
1912}
1913
John McCalld226f652010-08-21 09:40:31 +00001914Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001915 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00001916 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00001917 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00001918 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001919 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00001920 Selector Sel,
1921 // optional arguments. The number of types/arguments is obtained
1922 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001923 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001924 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001925 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001926 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001927 // Make sure we can establish a context for the method.
1928 if (!ClassDecl) {
1929 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00001930 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00001931 }
Chris Lattner4d391482007-12-12 07:09:47 +00001932 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001934 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001935 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001936 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Steve Naroffccef3712009-02-20 22:59:16 +00001938 // Methods cannot return interface types. All ObjC objects are
1939 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001940 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001941 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1942 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00001943 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00001944 }
Steve Naroffccef3712009-02-20 22:59:16 +00001945 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001946 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001947
1948 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001949 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001950 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001951 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001952 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001953 false, false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001954 MethodDeclKind == tok::objc_optional
1955 ? ObjCMethodDecl::Optional
1956 : ObjCMethodDecl::Required,
1957 false);
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Chris Lattner0ed844b2008-04-04 06:12:32 +00001959 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Chris Lattner7db638d2009-04-11 19:42:43 +00001961 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001962 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001963 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Chris Lattnere294d3f2009-04-11 18:57:04 +00001965 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001966 ArgType = Context.getObjCIdType();
1967 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001968 } else {
John McCall58e46772009-10-23 21:48:59 +00001969 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001970 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001971 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001972 }
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001974 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
1975 LookupOrdinaryName, ForRedeclaration);
1976 LookupName(R, S);
1977 if (R.isSingleResult()) {
1978 NamedDecl *PrevDecl = R.getFoundDecl();
1979 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001980 Diag(ArgInfo[i].NameLoc,
1981 (MethodDefinition ? diag::warn_method_param_redefinition
1982 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001983 << ArgInfo[i].Name;
1984 Diag(PrevDecl->getLocation(),
1985 diag::note_previous_declaration);
1986 }
1987 }
1988
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001989 SourceLocation StartLoc = DI
1990 ? DI->getTypeLoc().getBeginLoc()
1991 : ArgInfo[i].NameLoc;
1992
John McCall81ef3e62011-04-23 02:46:06 +00001993 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
1994 ArgInfo[i].NameLoc, ArgInfo[i].Name,
1995 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00001996
John McCall70798862011-05-02 00:30:12 +00001997 Param->setObjCMethodScopeInfo(i);
1998
Chris Lattner0ed844b2008-04-04 06:12:32 +00001999 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002000 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002002 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002003 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002005 S->AddDecl(Param);
2006 IdResolver.AddDecl(Param);
2007
Chris Lattner0ed844b2008-04-04 06:12:32 +00002008 Params.push_back(Param);
2009 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002010
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002011 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002012 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002013 QualType ArgType = Param->getType();
2014 if (ArgType.isNull())
2015 ArgType = Context.getObjCIdType();
2016 else
2017 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
2018 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002019 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002020 Diag(Param->getLocation(),
2021 diag::err_object_cannot_be_passed_returned_by_value)
2022 << 1 << ArgType;
2023 Param->setInvalidDecl();
2024 }
2025 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002026
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002027 Params.push_back(Param);
2028 }
2029
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002030 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2031 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002032 ObjCMethod->setObjCDeclQualifier(
2033 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2034 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002035
2036 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002037 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002038
John McCall54abf7d2009-11-04 02:18:39 +00002039 const ObjCMethodDecl *InterfaceMD = 0;
2040
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002041 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00002042 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002043 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002044 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002045 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2046 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002047 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002048 PrevMethod = ImpDecl->getClassMethod(Sel);
2049 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002050 }
John McCall54abf7d2009-11-04 02:18:39 +00002051 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2052 MethodType == tok::minus);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002053
Sean Huntcf807c42010-08-18 23:23:40 +00002054 if (ObjCMethod->hasAttrs() &&
2055 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002056 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002057 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002058 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002059 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002060 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2061 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002062 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002063 PrevMethod = CatImpDecl->getClassMethod(Sel);
2064 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002065 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002066
2067 if (ObjCCategoryDecl *Cat = CatImpDecl->getCategoryDecl())
2068 InterfaceMD = Cat->getMethod(Sel, MethodType == tok::minus);
2069
Sean Huntcf807c42010-08-18 23:23:40 +00002070 if (ObjCMethod->hasAttrs() &&
2071 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002072 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002073 } else {
2074 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002075 }
2076 if (PrevMethod) {
2077 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002078 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002079 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002080 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002081 }
John McCall54abf7d2009-11-04 02:18:39 +00002082
Douglas Gregor926df6c2011-06-11 01:09:30 +00002083 // If this Objective-C method does not have a related result type, but we
2084 // are allowed to infer related result types, try to do so based on the
2085 // method family.
2086 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2087 if (!CurrentClass) {
2088 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2089 CurrentClass = Cat->getClassInterface();
2090 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2091 CurrentClass = Impl->getClassInterface();
2092 else if (ObjCCategoryImplDecl *CatImpl
2093 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2094 CurrentClass = CatImpl->getClassInterface();
2095 }
2096
John McCalleca5d222011-03-02 04:00:57 +00002097 // Merge information down from the interface declaration if we have one.
Douglas Gregor926df6c2011-06-11 01:09:30 +00002098 if (InterfaceMD) {
2099 // Inherit the related result type, if we can.
2100 if (InterfaceMD->hasRelatedResultType() &&
2101 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2102 ObjCMethod->SetRelatedResultType();
2103
John McCalleca5d222011-03-02 04:00:57 +00002104 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002105 }
2106
2107 if (!ObjCMethod->hasRelatedResultType() &&
2108 getLangOptions().ObjCInferRelatedResultType) {
2109 bool InferRelatedResultType = false;
2110 switch (ObjCMethod->getMethodFamily()) {
2111 case OMF_None:
2112 case OMF_copy:
2113 case OMF_dealloc:
2114 case OMF_mutableCopy:
2115 case OMF_release:
2116 case OMF_retainCount:
2117 break;
2118
2119 case OMF_alloc:
2120 case OMF_new:
2121 InferRelatedResultType = ObjCMethod->isClassMethod();
2122 break;
2123
2124 case OMF_init:
2125 case OMF_autorelease:
2126 case OMF_retain:
2127 case OMF_self:
2128 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2129 break;
2130 }
2131
2132 if (InferRelatedResultType &&
2133 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2134 ObjCMethod->SetRelatedResultType();
2135
2136 if (!InterfaceMD &&
2137 anyMethodInfersRelatedResultType(*this, ObjCMethod->getSelector(),
2138 ObjCMethod->isInstanceMethod()))
2139 CheckObjCMethodOverrides(ObjCMethod, cast<DeclContext>(ClassDecl));
2140 }
2141
John McCalld226f652010-08-21 09:40:31 +00002142 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002143}
2144
Chris Lattnercc98eac2008-12-17 07:13:27 +00002145bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002146 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002147 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002148
Anders Carlsson15281452008-11-04 16:57:32 +00002149 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2150 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Anders Carlsson15281452008-11-04 16:57:32 +00002152 return true;
2153}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002154
Chris Lattnercc98eac2008-12-17 07:13:27 +00002155/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2156/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002157void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002158 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00002159 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002160 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002161 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002162 if (!Class) {
2163 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2164 return;
2165 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002166 if (LangOpts.ObjCNonFragileABI) {
2167 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2168 return;
2169 }
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Chris Lattnercc98eac2008-12-17 07:13:27 +00002171 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002172 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
2173 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002174 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002175 for (unsigned i = 0; i < Ivars.size(); i++) {
2176 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002177 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002178 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2179 /*FIXME: StartL=*/ID->getLocation(),
2180 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002181 ID->getIdentifier(), ID->getType(),
2182 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002183 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002184 }
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Chris Lattnercc98eac2008-12-17 07:13:27 +00002186 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00002187 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002188 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002189 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002190 if (getLangOptions().CPlusPlus)
2191 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002192 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002193 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002194 }
2195}
2196
Douglas Gregor160b5632010-04-26 17:32:49 +00002197/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002198VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2199 SourceLocation StartLoc,
2200 SourceLocation IdLoc,
2201 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002202 bool Invalid) {
2203 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2204 // duration shall not be qualified by an address-space qualifier."
2205 // Since all parameters have automatic store duration, they can not have
2206 // an address space.
2207 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002208 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002209 Invalid = true;
2210 }
2211
2212 // An @catch parameter must be an unqualified object pointer type;
2213 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2214 if (Invalid) {
2215 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002216 } else if (T->isDependentType()) {
2217 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002218 } else if (!T->isObjCObjectPointerType()) {
2219 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002220 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002221 } else if (T->isObjCQualifiedIdType()) {
2222 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002223 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002224 }
2225
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002226 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2227 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002228 New->setExceptionVariable(true);
2229
Douglas Gregor160b5632010-04-26 17:32:49 +00002230 if (Invalid)
2231 New->setInvalidDecl();
2232 return New;
2233}
2234
John McCalld226f652010-08-21 09:40:31 +00002235Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002236 const DeclSpec &DS = D.getDeclSpec();
2237
2238 // We allow the "register" storage class on exception variables because
2239 // GCC did, but we drop it completely. Any other storage class is an error.
2240 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2241 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2242 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2243 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2244 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2245 << DS.getStorageClassSpec();
2246 }
2247 if (D.getDeclSpec().isThreadSpecified())
2248 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2249 D.getMutableDeclSpec().ClearStorageClassSpecs();
2250
2251 DiagnoseFunctionSpecifiers(D);
2252
2253 // Check that there are no default arguments inside the type of this
2254 // exception object (C++ only).
2255 if (getLangOptions().CPlusPlus)
2256 CheckExtraCXXDefaultArguments(D);
2257
Douglas Gregor160b5632010-04-26 17:32:49 +00002258 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002259 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
2260 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002261
2262 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
2263 // Objective-C++: Types shall not be defined in exception types.
2264 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
2265 << Context.getTypeDeclType(OwnedDecl);
2266 }
2267
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002268 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2269 D.getSourceRange().getBegin(),
2270 D.getIdentifierLoc(),
2271 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002272 D.isInvalidType());
2273
2274 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2275 if (D.getCXXScopeSpec().isSet()) {
2276 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2277 << D.getCXXScopeSpec().getRange();
2278 New->setInvalidDecl();
2279 }
2280
2281 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002282 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002283 if (D.getIdentifier())
2284 IdResolver.AddDecl(New);
2285
2286 ProcessDeclAttributes(S, New, D);
2287
2288 if (New->hasAttr<BlocksAttr>())
2289 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002290 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002291}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002292
2293/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002294/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002295void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002296 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002297 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2298 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002299 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002300 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002301 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002302 }
2303}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002304
2305void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002306 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002307 unsigned numInitializers) {
2308 if (numInitializers > 0) {
2309 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002310 CXXCtorInitializer **ivarInitializers =
2311 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002312 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002313 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002314 IvarInitializers = ivarInitializers;
2315 }
2316}
2317
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002318void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002319 // Warning will be issued only when selector table is
2320 // generated (which means there is at lease one implementation
2321 // in the TU). This is to match gcc's behavior.
2322 if (ReferencedSelectors.empty() ||
2323 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002324 return;
2325 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2326 ReferencedSelectors.begin(),
2327 E = ReferencedSelectors.end(); S != E; ++S) {
2328 Selector Sel = (*S).first;
2329 if (!LookupImplementedMethodInGlobalPool(Sel))
2330 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2331 }
2332 return;
2333}