blob: bfa2ef45d08de78a2d3324ea7d090bd4c1d524da [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
Douglas Gregor05a2e942011-06-13 16:07:18 +000076/// \brief Check for consistency between a given method declaration and the
77/// methods it overrides within the class hierarchy.
78///
79/// This method walks the inheritance hierarchy starting at the given
80/// declaration context (\p DC), invoking Sema::CheckObjCMethodOverride() with
81/// the given new method (\p NewMethod) and any method it directly overrides
82/// in the hierarchy. Sema::CheckObjCMethodOverride() is responsible for
83/// checking consistency, e.g., among return types for methods that return a
84/// related result type.
Douglas Gregor926df6c2011-06-11 01:09:30 +000085static bool CheckObjCMethodOverrides(Sema &S, ObjCMethodDecl *NewMethod,
86 DeclContext *DC,
87 bool SkipCurrent = true) {
88 if (!DC)
89 return false;
90
91 if (!SkipCurrent) {
92 // Look for this method. If we find it, we're done.
93 Selector Sel = NewMethod->getSelector();
94 bool IsInstance = NewMethod->isInstanceMethod();
95 DeclContext::lookup_const_iterator Meth, MethEnd;
96 for (llvm::tie(Meth, MethEnd) = DC->lookup(Sel); Meth != MethEnd; ++Meth) {
97 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
98 if (MD && MD->isInstanceMethod() == IsInstance)
99 return S.CheckObjCMethodOverride(NewMethod, MD, false);
100 }
101 }
102
103 if (ObjCInterfaceDecl *Class = llvm::dyn_cast<ObjCInterfaceDecl>(DC)) {
104 // Look through categories.
105 for (ObjCCategoryDecl *Category = Class->getCategoryList();
106 Category; Category = Category->getNextClassCategory()) {
107 if (CheckObjCMethodOverrides(S, NewMethod, Category, false))
108 return true;
109 }
110
111 // Look through protocols.
112 for (ObjCList<ObjCProtocolDecl>::iterator I = Class->protocol_begin(),
113 IEnd = Class->protocol_end();
114 I != IEnd; ++I)
115 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
116 return true;
117
118 // Look in our superclass.
119 return CheckObjCMethodOverrides(S, NewMethod, Class->getSuperClass(),
120 false);
121 }
122
123 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC)) {
124 // Look through protocols.
125 for (ObjCList<ObjCProtocolDecl>::iterator I = Category->protocol_begin(),
126 IEnd = Category->protocol_end();
127 I != IEnd; ++I)
128 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
129 return true;
130
131 return false;
132 }
133
134 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC)) {
135 // Look through protocols.
136 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocol->protocol_begin(),
137 IEnd = Protocol->protocol_end();
138 I != IEnd; ++I)
139 if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
140 return true;
141
142 return false;
143 }
144
145 return false;
146}
147
148bool Sema::CheckObjCMethodOverrides(ObjCMethodDecl *NewMethod,
149 DeclContext *DC) {
150 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(DC))
151 return ::CheckObjCMethodOverrides(*this, NewMethod, Class);
152
153 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC))
154 return ::CheckObjCMethodOverrides(*this, NewMethod, Category);
155
156 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC))
157 return ::CheckObjCMethodOverrides(*this, NewMethod, Protocol);
158
159 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(DC))
160 return ::CheckObjCMethodOverrides(*this, NewMethod,
161 Impl->getClassInterface());
162
163 if (ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(DC))
164 return ::CheckObjCMethodOverrides(*this, NewMethod,
165 CatImpl->getClassInterface());
166
167 return ::CheckObjCMethodOverrides(*this, NewMethod, CurContext);
168}
169
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000170static void DiagnoseObjCImplementedDeprecations(Sema &S,
171 NamedDecl *ND,
172 SourceLocation ImplLoc,
173 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000174 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000175 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000176 if (select == 0)
177 S.Diag(ND->getLocation(), diag::note_method_declared_at);
178 else
179 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
180 }
181}
182
Steve Naroffebf64432009-02-28 16:59:13 +0000183/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000184/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000185void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000186 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000187 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Steve Naroff394f3f42008-07-25 17:57:26 +0000189 // If we don't have a valid method decl, simply return.
190 if (!MDecl)
191 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000192
193 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000194 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000195 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +0000196 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000197 AddFactoryMethodToGlobalPool(MDecl, true);
198
Chris Lattner4d391482007-12-12 07:09:47 +0000199 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000200 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000201 PushFunctionScope();
202
Chris Lattner4d391482007-12-12 07:09:47 +0000203 // Create Decl objects for each parameter, entrring them in the scope for
204 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000205
206 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000207 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Daniel Dunbar451318c2008-08-26 06:07:48 +0000209 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
210 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000211
Chris Lattner8123a952008-04-10 02:22:51 +0000212 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000213 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000214 E = MDecl->param_end(); PI != E; ++PI) {
215 ParmVarDecl *Param = (*PI);
216 if (!Param->isInvalidDecl() &&
217 RequireCompleteType(Param->getLocation(), Param->getType(),
218 diag::err_typecheck_decl_incomplete_type))
219 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000220 if ((*PI)->getIdentifier())
221 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000222 }
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000223 // Warn on implementating deprecated methods under
224 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000225 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
226 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000227 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000228 DiagnoseObjCImplementedDeprecations(*this,
229 dyn_cast<NamedDecl>(IMD),
230 MDecl->getLocation(), 0);
Chris Lattner4d391482007-12-12 07:09:47 +0000231}
232
John McCalld226f652010-08-21 09:40:31 +0000233Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000234ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
235 IdentifierInfo *ClassName, SourceLocation ClassLoc,
236 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000237 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000238 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000239 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000240 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner4d391482007-12-12 07:09:47 +0000242 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000243 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000244 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000245
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000246 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000247 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000248 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000251 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
252 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000253 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000254 if (!IDecl->isForwardDecl()) {
255 IDecl->setInvalidDecl();
256 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
257 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000258
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000259 // Return the previous class interface.
260 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000261 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000262 } else {
263 IDecl->setLocation(AtInterfaceLoc);
264 IDecl->setForwardDecl(false);
265 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000266 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000267 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000268 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000269
270 // Since this ObjCInterfaceDecl was created by a forward declaration,
271 // we now add it to the DeclContext since it wasn't added before
272 // (see ActOnForwardClassDeclaration).
273 IDecl->setLexicalDeclContext(CurContext);
274 CurContext->addDecl(IDecl);
275
276 if (AttrList)
277 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000278 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000279 } else {
280 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
281 ClassName, ClassLoc);
282 if (AttrList)
283 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
284
285 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner4d391482007-12-12 07:09:47 +0000288 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000289 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000290 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
291 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000292
293 if (!PrevDecl) {
294 // Try to correct for a typo in the superclass name.
295 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000296 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000297 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
298 Diag(SuperLoc, diag::err_undef_superclass_suggest)
299 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000300 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
301 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000302 }
303 }
304
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000305 if (PrevDecl == IDecl) {
306 Diag(SuperLoc, diag::err_recursive_superclass)
307 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
308 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000309 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000310 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000311 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000312
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000313 // Diagnose classes that inherit from deprecated classes.
314 if (SuperClassDecl)
315 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000317 if (PrevDecl && SuperClassDecl == 0) {
318 // The previous declaration was not a class decl. Check if we have a
319 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000320 if (const TypedefNameDecl *TDecl =
321 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000322 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000323 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000324 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
325 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000326 }
327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000329 // This handles the following case:
330 //
331 // typedef int SuperClass;
332 // @interface MyClass : SuperClass {} @end
333 //
334 if (!SuperClassDecl) {
335 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
336 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000337 }
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Richard Smith162e1c12011-04-15 14:24:37 +0000340 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000341 if (!SuperClassDecl)
342 Diag(SuperLoc, diag::err_undef_superclass)
343 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
344 else if (SuperClassDecl->isForwardDecl())
345 Diag(SuperLoc, diag::err_undef_superclass)
346 << SuperClassDecl->getDeclName() << ClassName
347 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000348 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000349 IDecl->setSuperClass(SuperClassDecl);
350 IDecl->setSuperClassLoc(SuperLoc);
351 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000352 }
Chris Lattner4d391482007-12-12 07:09:47 +0000353 } else { // we have a root class.
354 IDecl->setLocEnd(ClassLoc);
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Sebastian Redl0b17c612010-08-13 00:28:03 +0000357 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000358 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000359 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000360 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000361 IDecl->setLocEnd(EndProtoLoc);
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Anders Carlsson15281452008-11-04 16:57:32 +0000364 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000365 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000366}
367
368/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000369/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000370Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
371 IdentifierInfo *AliasName,
372 SourceLocation AliasLocation,
373 IdentifierInfo *ClassName,
374 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000375 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000376 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000377 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000378 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000379 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000380 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000381 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000382 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000383 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000384 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000385 }
386 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000387 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000388 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000389 if (const TypedefNameDecl *TDecl =
390 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000391 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000392 if (T->isObjCObjectType()) {
393 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000394 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000395 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000396 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000397 }
398 }
399 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000400 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
401 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000402 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000403 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000404 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000405 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000408 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000409 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000410 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlsson15281452008-11-04 16:57:32 +0000412 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000413 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000414
John McCalld226f652010-08-21 09:40:31 +0000415 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000416}
417
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000418bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000419 IdentifierInfo *PName,
420 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000421 const ObjCList<ObjCProtocolDecl> &PList) {
422
423 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000424 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
425 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000426 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
427 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000428 if (PDecl->getIdentifier() == PName) {
429 Diag(Ploc, diag::err_protocol_has_circular_dependency);
430 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000431 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000432 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000433 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
434 PDecl->getLocation(), PDecl->getReferencedProtocols()))
435 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000436 }
437 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000438 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000439}
440
John McCalld226f652010-08-21 09:40:31 +0000441Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000442Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
443 IdentifierInfo *ProtocolName,
444 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000445 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000446 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000447 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000448 SourceLocation EndProtoLoc,
449 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000450 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000451 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000452 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000453 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000454 if (PDecl) {
455 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000456 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000457 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000458 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000459 // Just return the protocol we already had.
460 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000461 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000462 }
Steve Naroff61d68522009-03-05 15:22:01 +0000463 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000464 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000465 err = CheckForwardProtocolDeclarationForCircularDependency(
466 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Steve Narofff11b5082008-08-13 16:39:22 +0000468 // Make sure the cached decl gets a valid start location.
469 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000470 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000471 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000472 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000473 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000474 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000475 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000476 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000477 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000478 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000479 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000480 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000481 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000482 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000483 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000484 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
485 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000486 PDecl->setLocEnd(EndProtoLoc);
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
489 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000490 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000491}
492
493/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000494/// issues an error if they are not declared. It returns list of
495/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000496void
Chris Lattnere13b9592008-07-26 04:03:38 +0000497Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000498 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000499 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000500 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000501 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000502 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
503 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000504 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000505 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
506 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000507 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000508 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
509 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
510 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000511 Diag(PDecl->getLocation(), diag::note_previous_decl)
512 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000513 }
514 }
515
516 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000517 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000518 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000519 continue;
520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000522 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000523
524 // If this is a forward declaration and we are supposed to warn in this
525 // case, do it.
526 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000527 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000528 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000529 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000530 }
531}
532
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000533/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000534/// a class method in its extension.
535///
Mike Stump1eb44332009-09-09 15:08:12 +0000536void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000537 ObjCInterfaceDecl *ID) {
538 if (!ID)
539 return; // Possibly due to previous error
540
541 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000542 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
543 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000544 ObjCMethodDecl *MD = *i;
545 MethodMap[MD->getSelector()] = MD;
546 }
547
548 if (MethodMap.empty())
549 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000550 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
551 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000552 ObjCMethodDecl *Method = *i;
553 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
554 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
555 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
556 << Method->getDeclName();
557 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
558 }
559 }
560}
561
Chris Lattner58fe03b2009-04-12 08:43:13 +0000562/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000563Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000564Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000565 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000566 unsigned NumElts,
567 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000568 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000569 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner4d391482007-12-12 07:09:47 +0000571 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000572 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000573 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000574 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000575 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000576 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000577 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000578 PushOnScopeChains(PDecl, TUScope, false);
579 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000580 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000581 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000582 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000583 if (!isNew)
584 PDecl->setChangedSinceDeserialization(true);
585 }
Chris Lattner4d391482007-12-12 07:09:47 +0000586 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000587 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
590 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000591 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000592 Protocols.data(), Protocols.size(),
593 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000594 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000595 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000596 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000597}
598
John McCalld226f652010-08-21 09:40:31 +0000599Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000600ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
601 IdentifierInfo *ClassName, SourceLocation ClassLoc,
602 IdentifierInfo *CategoryName,
603 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000604 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000605 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000606 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000607 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000608 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000609 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000610
611 /// Check that class of this category is already completely declared.
612 if (!IDecl || IDecl->isForwardDecl()) {
613 // Create an invalid ObjCCategoryDecl to serve as context for
614 // the enclosing method declarations. We mark the decl invalid
615 // to make it clear that this isn't a valid AST.
616 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
617 ClassLoc, CategoryLoc, CategoryName);
618 CDecl->setInvalidDecl();
619 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000620 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000621 }
622
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000623 if (!CategoryName && IDecl->getImplementation()) {
624 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
625 Diag(IDecl->getImplementation()->getLocation(),
626 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000627 }
628
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000629 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
630 ClassLoc, CategoryLoc, CategoryName);
631 // FIXME: PushOnScopeChains?
632 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000633
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000634 CDecl->setClassInterface(IDecl);
635 // Insert class extension to the list of class's categories.
636 if (!CategoryName)
637 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Chris Lattner16b34b42009-02-16 21:30:01 +0000639 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000640 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000641
Fariborz Jahanian25760612010-02-15 21:55:26 +0000642 if (CategoryName) {
643 /// Check for duplicate interface declaration for this category
644 ObjCCategoryDecl *CDeclChain;
645 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
646 CDeclChain = CDeclChain->getNextClassCategory()) {
647 if (CDeclChain->getIdentifier() == CategoryName) {
648 // Class extensions can be declared multiple times.
649 Diag(CategoryLoc, diag::warn_dup_category_def)
650 << ClassName << CategoryName;
651 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
652 break;
653 }
Chris Lattner70f19542009-02-16 21:26:43 +0000654 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000655 if (!CDeclChain)
656 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000657 }
Chris Lattner70f19542009-02-16 21:26:43 +0000658
Chris Lattner4d391482007-12-12 07:09:47 +0000659 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000660 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000661 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000662 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000663 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000664 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000665 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000666 }
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Anders Carlsson15281452008-11-04 16:57:32 +0000668 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000669 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000670}
671
672/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000673/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000674/// object.
John McCalld226f652010-08-21 09:40:31 +0000675Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000676 SourceLocation AtCatImplLoc,
677 IdentifierInfo *ClassName, SourceLocation ClassLoc,
678 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000679 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000680 ObjCCategoryDecl *CatIDecl = 0;
681 if (IDecl) {
682 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
683 if (!CatIDecl) {
684 // Category @implementation with no corresponding @interface.
685 // Create and install one.
686 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000687 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000688 CatName);
689 CatIDecl->setClassInterface(IDecl);
690 CatIDecl->insertNextClassCategory();
691 }
692 }
693
Mike Stump1eb44332009-09-09 15:08:12 +0000694 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000695 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
696 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000697 /// Check that class of this category is already completely declared.
698 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000699 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000700
Douglas Gregord0434102009-01-09 00:49:46 +0000701 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000702 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000703
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000704 /// Check that CatName, category name, is not used in another implementation.
705 if (CatIDecl) {
706 if (CatIDecl->getImplementation()) {
707 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
708 << CatName;
709 Diag(CatIDecl->getImplementation()->getLocation(),
710 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000711 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000712 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000713 // Warn on implementating category of deprecated class under
714 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000715 DiagnoseObjCImplementedDeprecations(*this,
716 dyn_cast<NamedDecl>(IDecl),
717 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000718 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Anders Carlsson15281452008-11-04 16:57:32 +0000721 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000722 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000723}
724
John McCalld226f652010-08-21 09:40:31 +0000725Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000726 SourceLocation AtClassImplLoc,
727 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000728 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000729 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000730 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000731 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000732 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000733 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
734 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000735 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000736 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000737 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000738 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
739 // If this is a forward declaration of an interface, warn.
740 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000741 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000742 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000743 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000744 } else {
745 // We did not find anything with the name ClassName; try to correct for
746 // typos in the class name.
747 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000748 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000749 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000750 // Suggest the (potentially) correct interface name. However, put the
751 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000752 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000753 // provide a code-modification hint or use the typo name for recovery,
754 // because this is just a warning. The program may actually be correct.
755 Diag(ClassLoc, diag::warn_undef_interface_suggest)
756 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000757 Diag(IDecl->getLocation(), diag::note_previous_decl)
758 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000759 << FixItHint::CreateReplacement(ClassLoc,
760 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000761 IDecl = 0;
762 } else {
763 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
764 }
Chris Lattner4d391482007-12-12 07:09:47 +0000765 }
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Chris Lattner4d391482007-12-12 07:09:47 +0000767 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000768 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000769 if (SuperClassname) {
770 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000771 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
772 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000773 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000774 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
775 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000776 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000777 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000778 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000779 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000780 Diag(SuperClassLoc, diag::err_undef_superclass)
781 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000782 else if (IDecl && IDecl->getSuperClass() != SDecl) {
783 // This implementation and its interface do not have the same
784 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000785 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000786 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000787 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000788 }
789 }
790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattner4d391482007-12-12 07:09:47 +0000792 if (!IDecl) {
793 // Legacy case of @implementation with no corresponding @interface.
794 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000795
Mike Stump390b4cc2009-05-16 07:39:55 +0000796 // FIXME: Do we support attributes on the @implementation? If so we should
797 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000798 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000799 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000800 IDecl->setSuperClass(SDecl);
801 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000802
803 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000804 } else {
805 // Mark the interface as being completed, even if it was just as
806 // @class ....;
807 // declaration; the user cannot reopen it.
808 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000809 }
Mike Stump1eb44332009-09-09 15:08:12 +0000810
811 ObjCImplementationDecl* IMPDecl =
812 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000813 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Anders Carlsson15281452008-11-04 16:57:32 +0000815 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000816 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Chris Lattner4d391482007-12-12 07:09:47 +0000818 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000819 if (IDecl->getImplementation()) {
820 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000821 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000822 Diag(IDecl->getImplementation()->getLocation(),
823 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000824 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000825 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000826 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000827 // Warn on implementating deprecated class under
828 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000829 DiagnoseObjCImplementedDeprecations(*this,
830 dyn_cast<NamedDecl>(IDecl),
831 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000832 }
John McCalld226f652010-08-21 09:40:31 +0000833 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000834}
835
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
837 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000838 SourceLocation RBrace) {
839 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000840 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000841 if (!IDecl)
842 return;
843 /// Check case of non-existing @interface decl.
844 /// (legacy objective-c @implementation decl without an @interface decl).
845 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000846 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000847 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000848 // Add ivar's to class's DeclContext.
849 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000850 ivars[i]->setLexicalDeclContext(ImpDecl);
851 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000852 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000853 }
854
Chris Lattner4d391482007-12-12 07:09:47 +0000855 return;
856 }
857 // If implementation has empty ivar list, just return.
858 if (numIvars == 0)
859 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Chris Lattner4d391482007-12-12 07:09:47 +0000861 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000862 if (LangOpts.ObjCNonFragileABI2) {
863 if (ImpDecl->getSuperClass())
864 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
865 for (unsigned i = 0; i < numIvars; i++) {
866 ObjCIvarDecl* ImplIvar = ivars[i];
867 if (const ObjCIvarDecl *ClsIvar =
868 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
869 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
870 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
871 continue;
872 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000873 // Instance ivar to Implementation's DeclContext.
874 ImplIvar->setLexicalDeclContext(ImpDecl);
875 IDecl->makeDeclVisibleInContext(ImplIvar, false);
876 ImpDecl->addDecl(ImplIvar);
877 }
878 return;
879 }
Chris Lattner4d391482007-12-12 07:09:47 +0000880 // Check interface's Ivar list against those in the implementation.
881 // names and types must match.
882 //
Chris Lattner4d391482007-12-12 07:09:47 +0000883 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000884 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000885 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
886 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000887 ObjCIvarDecl* ImplIvar = ivars[j++];
888 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000889 assert (ImplIvar && "missing implementation ivar");
890 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Steve Naroffca331292009-03-03 14:49:36 +0000892 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000893 if (Context.getCanonicalType(ImplIvar->getType()) !=
894 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000895 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000896 << ImplIvar->getIdentifier()
897 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000898 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000899 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
900 Expr *ImplBitWidth = ImplIvar->getBitWidth();
901 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000902 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
903 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000904 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
905 << ImplIvar->getIdentifier();
906 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908 }
Steve Naroffca331292009-03-03 14:49:36 +0000909 // Make sure the names are identical.
910 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000911 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000912 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000913 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000914 }
915 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Chris Lattner609e4c72007-12-12 18:11:49 +0000918 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000919 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000920 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000921 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000922}
923
Steve Naroff3c2eb662008-02-10 21:38:56 +0000924void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000925 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000926 if (!IncompleteImpl) {
927 Diag(ImpLoc, diag::warn_incomplete_impl);
928 IncompleteImpl = true;
929 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +0000930 if (DiagID == diag::warn_unimplemented_protocol_method)
931 Diag(ImpLoc, DiagID) << method->getDeclName();
932 else
933 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000934}
935
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000936/// Determines if type B can be substituted for type A. Returns true if we can
937/// guarantee that anything that the user will do to an object of type A can
938/// also be done to an object of type B. This is trivially true if the two
939/// types are the same, or if B is a subclass of A. It becomes more complex
940/// in cases where protocols are involved.
941///
942/// Object types in Objective-C describe the minimum requirements for an
943/// object, rather than providing a complete description of a type. For
944/// example, if A is a subclass of B, then B* may refer to an instance of A.
945/// The principle of substitutability means that we may use an instance of A
946/// anywhere that we may use an instance of B - it will implement all of the
947/// ivars of B and all of the methods of B.
948///
949/// This substitutability is important when type checking methods, because
950/// the implementation may have stricter type definitions than the interface.
951/// The interface specifies minimum requirements, but the implementation may
952/// have more accurate ones. For example, a method may privately accept
953/// instances of B, but only publish that it accepts instances of A. Any
954/// object passed to it will be type checked against B, and so will implicitly
955/// by a valid A*. Similarly, a method may return a subclass of the class that
956/// it is declared as returning.
957///
958/// This is most important when considering subclassing. A method in a
959/// subclass must accept any object as an argument that its superclass's
960/// implementation accepts. It may, however, accept a more general type
961/// without breaking substitutability (i.e. you can still use the subclass
962/// anywhere that you can use the superclass, but not vice versa). The
963/// converse requirement applies to return types: the return type for a
964/// subclass method must be a valid object of the kind that the superclass
965/// advertises, but it may be specified more accurately. This avoids the need
966/// for explicit down-casting by callers.
967///
968/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +0000969static bool isObjCTypeSubstitutable(ASTContext &Context,
970 const ObjCObjectPointerType *A,
971 const ObjCObjectPointerType *B,
972 bool rejectId) {
973 // Reject a protocol-unqualified id.
974 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000975
976 // If B is a qualified id, then A must also be a qualified id and it must
977 // implement all of the protocols in B. It may not be a qualified class.
978 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
979 // stricter definition so it is not substitutable for id<A>.
980 if (B->isObjCQualifiedIdType()) {
981 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +0000982 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
983 QualType(B,0),
984 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +0000985 }
986
987 /*
988 // id is a special type that bypasses type checking completely. We want a
989 // warning when it is used in one place but not another.
990 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
991
992
993 // If B is a qualified id, then A must also be a qualified id (which it isn't
994 // if we've got this far)
995 if (B->isObjCQualifiedIdType()) return false;
996 */
997
998 // Now we know that A and B are (potentially-qualified) class types. The
999 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001000 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001001}
1002
John McCall10302c02010-10-28 02:34:38 +00001003static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1004 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1005}
1006
1007static void CheckMethodOverrideReturn(Sema &S,
1008 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001009 ObjCMethodDecl *MethodDecl,
1010 bool IsProtocolMethodDecl) {
1011 if (IsProtocolMethodDecl &&
1012 (MethodDecl->getObjCDeclQualifier() !=
1013 MethodImpl->getObjCDeclQualifier())) {
1014 S.Diag(MethodImpl->getLocation(),
1015 diag::warn_conflicting_ret_type_modifiers)
1016 << MethodImpl->getDeclName()
1017 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1018 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1019 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1020 }
1021
John McCall10302c02010-10-28 02:34:38 +00001022 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001023 MethodDecl->getResultType()))
John McCall10302c02010-10-28 02:34:38 +00001024 return;
1025
1026 unsigned DiagID = diag::warn_conflicting_ret_types;
1027
1028 // Mismatches between ObjC pointers go into a different warning
1029 // category, and sometimes they're even completely whitelisted.
1030 if (const ObjCObjectPointerType *ImplPtrTy =
1031 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1032 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001033 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001034 // Allow non-matching return types as long as they don't violate
1035 // the principle of substitutability. Specifically, we permit
1036 // return types that are subclasses of the declared return type,
1037 // or that are more-qualified versions of the declared type.
1038 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1039 return;
1040
1041 DiagID = diag::warn_non_covariant_ret_types;
1042 }
1043 }
1044
1045 S.Diag(MethodImpl->getLocation(), DiagID)
1046 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001047 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001048 << MethodImpl->getResultType()
1049 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001050 S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
1051 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
John McCall10302c02010-10-28 02:34:38 +00001052}
1053
1054static void CheckMethodOverrideParam(Sema &S,
1055 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001056 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001057 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001058 ParmVarDecl *IfaceVar,
1059 bool IsProtocolMethodDecl) {
1060 if (IsProtocolMethodDecl &&
1061 (ImplVar->getObjCDeclQualifier() !=
1062 IfaceVar->getObjCDeclQualifier())) {
1063 S.Diag(ImplVar->getLocation(),
1064 diag::warn_conflicting_param_modifiers)
1065 << getTypeRange(ImplVar->getTypeSourceInfo())
1066 << MethodImpl->getDeclName();
1067 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1068 << getTypeRange(IfaceVar->getTypeSourceInfo());
1069 }
1070
John McCall10302c02010-10-28 02:34:38 +00001071 QualType ImplTy = ImplVar->getType();
1072 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001073
John McCall10302c02010-10-28 02:34:38 +00001074 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
1075 return;
1076
1077 unsigned DiagID = diag::warn_conflicting_param_types;
1078
1079 // Mismatches between ObjC pointers go into a different warning
1080 // category, and sometimes they're even completely whitelisted.
1081 if (const ObjCObjectPointerType *ImplPtrTy =
1082 ImplTy->getAs<ObjCObjectPointerType>()) {
1083 if (const ObjCObjectPointerType *IfacePtrTy =
1084 IfaceTy->getAs<ObjCObjectPointerType>()) {
1085 // Allow non-matching argument types as long as they don't
1086 // violate the principle of substitutability. Specifically, the
1087 // implementation must accept any objects that the superclass
1088 // accepts, however it may also accept others.
1089 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
1090 return;
1091
1092 DiagID = diag::warn_non_contravariant_param_types;
1093 }
1094 }
1095
1096 S.Diag(ImplVar->getLocation(), DiagID)
1097 << getTypeRange(ImplVar->getTypeSourceInfo())
1098 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1099 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
1100 << getTypeRange(IfaceVar->getTypeSourceInfo());
1101}
1102
1103
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001104void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001105 ObjCMethodDecl *MethodDecl,
1106 bool IsProtocolMethodDecl) {
1107 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1108 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Chris Lattner3aff9192009-04-11 19:58:42 +00001110 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001111 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
John McCall10302c02010-10-28 02:34:38 +00001112 IM != EM; ++IM, ++IF)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001113 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1114 IsProtocolMethodDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001116 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001117 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001118 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian561da7e2010-05-21 23:28:58 +00001119 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001120}
1121
Mike Stump390b4cc2009-05-16 07:39:55 +00001122/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1123/// improve the efficiency of selector lookups and type checking by associating
1124/// with each protocol / interface / category the flattened instance tables. If
1125/// we used an immutable set to keep the table then it wouldn't add significant
1126/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001127
Steve Naroffefe7f362008-02-08 22:06:17 +00001128/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001129/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001130void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1131 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001132 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001133 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001134 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001135 ObjCContainerDecl *CDecl) {
1136 ObjCInterfaceDecl *IDecl;
1137 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1138 IDecl = C->getClassInterface();
1139 else
1140 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1141 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1142
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001143 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001144 ObjCInterfaceDecl *NSIDecl = 0;
1145 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001146 // check to see if class implements forwardInvocation method and objects
1147 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001148 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001149 // Under such conditions, which means that every method possible is
1150 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001151 // found" warnings.
1152 // FIXME: Use a general GetUnarySelector method for this.
1153 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1154 Selector fISelector = Context.Selectors.getSelector(1, &II);
1155 if (InsMap.count(fISelector))
1156 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1157 // need be implemented in the implementation.
1158 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001161 // If a method lookup fails locally we still need to look and see if
1162 // the method was implemented by a base class or an inherited
1163 // protocol. This lookup is slow, but occurs rarely in correct code
1164 // and otherwise would terminate in a warning.
1165
Chris Lattner4d391482007-12-12 07:09:47 +00001166 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001167 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001168 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001169 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001170 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001171 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001172 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001173 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001174 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001175 // Ugly, but necessary. Method declared in protcol might have
1176 // have been synthesized due to a property declared in the class which
1177 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001178 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001179 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001180 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001181 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001182 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1183 != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001184 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001185 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001186 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1187 << PDecl->getDeclName();
1188 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001189 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001190 }
1191 }
Chris Lattner4d391482007-12-12 07:09:47 +00001192 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001193 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001194 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001195 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001196 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001197 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1198 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001199 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001200 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001201 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001202 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001203 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001204 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1205 PDecl->getDeclName();
1206 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001207 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001208 }
Chris Lattner780f3292008-07-21 21:32:27 +00001209 // Check on this protocols's referenced protocols, recursively.
1210 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1211 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001212 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001213}
1214
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001215/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1216/// or protocol against those declared in their implementations.
1217///
1218void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1219 const llvm::DenseSet<Selector> &ClsMap,
1220 llvm::DenseSet<Selector> &InsMapSeen,
1221 llvm::DenseSet<Selector> &ClsMapSeen,
1222 ObjCImplDecl* IMPDecl,
1223 ObjCContainerDecl* CDecl,
1224 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001225 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001226 // Check and see if instance methods in class interface have been
1227 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001228 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1229 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001230 if (InsMapSeen.count((*I)->getSelector()))
1231 continue;
1232 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001233 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001234 !InsMap.count((*I)->getSelector())) {
1235 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001236 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1237 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001238 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001239 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001240 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001241 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001242 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001243 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001244 assert(MethodDecl &&
1245 "MethodDecl is null in ImplMethodsVsClassMethods");
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001246 // ImpMethodDecl may be null as in a @dynamic property.
1247 if (ImpMethodDecl)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001248 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1249 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001250 }
1251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001253 // Check and see if class methods in class interface have been
1254 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001255 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001256 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001257 if (ClsMapSeen.count((*I)->getSelector()))
1258 continue;
1259 ClsMapSeen.insert((*I)->getSelector());
1260 if (!ClsMap.count((*I)->getSelector())) {
1261 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001262 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1263 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001264 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001265 ObjCMethodDecl *ImpMethodDecl =
1266 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001267 ObjCMethodDecl *MethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001268 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001269 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1270 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001271 }
1272 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001273
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001274 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001275 // Also methods in class extensions need be looked at next.
1276 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1277 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1278 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1279 IMPDecl,
1280 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1281 IncompleteImpl, false);
1282
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001283 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001284 for (ObjCInterfaceDecl::all_protocol_iterator
1285 PI = I->all_referenced_protocol_begin(),
1286 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001287 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1288 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001289 (*PI), IncompleteImpl, false);
1290 if (I->getSuperClass())
1291 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001292 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001293 I->getSuperClass(), IncompleteImpl, false);
1294 }
1295}
1296
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001297void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001298 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001299 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001300 llvm::DenseSet<Selector> InsMap;
1301 // Check and see if instance methods in class interface have been
1302 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001303 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001304 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001305 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001307 // Check and see if properties declared in the interface have either 1)
1308 // an implementation or 2) there is a @synthesize/@dynamic implementation
1309 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001310 if (isa<ObjCInterfaceDecl>(CDecl) &&
1311 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001312 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001313
Chris Lattner4d391482007-12-12 07:09:47 +00001314 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001315 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001316 I = IMPDecl->classmeth_begin(),
1317 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001318 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001320 // Check for type conflict of methods declared in a class/protocol and
1321 // its implementation; if any.
1322 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001323 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1324 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001325 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Chris Lattner4d391482007-12-12 07:09:47 +00001327 // Check the protocol list for unimplemented methods in the @implementation
1328 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001329 // Check and see if class methods in class interface have been
1330 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Chris Lattnercddc8882009-03-01 00:56:52 +00001332 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001333 for (ObjCInterfaceDecl::all_protocol_iterator
1334 PI = I->all_referenced_protocol_begin(),
1335 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001336 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001337 InsMap, ClsMap, I);
1338 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001339 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1340 Categories; Categories = Categories->getNextClassExtension())
1341 ImplMethodsVsClassMethods(S, IMPDecl,
1342 const_cast<ObjCCategoryDecl*>(Categories),
1343 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001344 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001345 // For extended class, unimplemented methods in its protocols will
1346 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001347 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001348 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1349 E = C->protocol_end(); PI != E; ++PI)
1350 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001351 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001352 // Report unimplemented properties in the category as well.
1353 // When reporting on missing setter/getters, do not report when
1354 // setter/getter is implemented in category's primary class
1355 // implementation.
1356 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1357 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1358 for (ObjCImplementationDecl::instmeth_iterator
1359 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1360 InsMap.insert((*I)->getSelector());
1361 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001362 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001363 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001364 } else
1365 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001366}
1367
Mike Stump1eb44332009-09-09 15:08:12 +00001368/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001369Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001370Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001371 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001372 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001373 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001374 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Chris Lattner4d391482007-12-12 07:09:47 +00001376 for (unsigned i = 0; i != NumElts; ++i) {
1377 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001378 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001379 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001380 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001381 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001382 // Maybe we will complain about the shadowed template parameter.
1383 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1384 // Just pretend that we didn't see the previous declaration.
1385 PrevDecl = 0;
1386 }
1387
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001388 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001389 // GCC apparently allows the following idiom:
1390 //
1391 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1392 // @class XCElementToggler;
1393 //
Mike Stump1eb44332009-09-09 15:08:12 +00001394 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001395 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001396 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001397 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001398 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001399 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001400 // a forward class declaration matching a typedef name of a class refers
1401 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001402 if (const ObjCObjectType *OI =
1403 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1404 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001405 }
Chris Lattner4d391482007-12-12 07:09:47 +00001406 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001407 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1408 if (!IDecl) { // Not already seen? Make a forward decl.
1409 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1410 IdentList[i], IdentLocs[i], true);
1411
1412 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1413 // the current DeclContext. This prevents clients that walk DeclContext
1414 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1415 // declared later (if at all). We also take care to explicitly make
1416 // sure this declaration is visible for name lookup.
1417 PushOnScopeChains(IDecl, TUScope, false);
1418 CurContext->makeDeclVisibleInContext(IDecl, true);
1419 }
Chris Lattner4d391482007-12-12 07:09:47 +00001420
1421 Interfaces.push_back(IDecl);
1422 }
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Ted Kremenek321c22f2009-11-18 00:28:11 +00001424 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001425 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001426 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001427 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001428 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001429 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001430 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001431}
1432
1433
1434/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1435/// returns true, or false, accordingly.
1436/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001437bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001438 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001439 bool matchBasedOnSizeAndAlignment,
1440 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001441 QualType T1 = Context.getCanonicalType(Method->getResultType());
1442 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001444 if (T1 != T2) {
1445 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001446 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001447 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001448 // Incomplete types don't have a size and alignment.
1449 if (T1->isIncompleteType() || T2->isIncompleteType())
1450 return false;
1451 // Check is based on size and alignment.
1452 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1453 return false;
1454 }
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Chris Lattner89951a82009-02-20 18:43:26 +00001456 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1457 E = Method->param_end();
1458 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Chris Lattner89951a82009-02-20 18:43:26 +00001460 for (; ParamI != E; ++ParamI, ++PrevI) {
1461 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1462 T1 = Context.getCanonicalType((*ParamI)->getType());
1463 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001464 if (T1 != T2) {
1465 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001466 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001467 return false;
1468 // Incomplete types don't have a size and alignment.
1469 if (T1->isIncompleteType() || T2->isIncompleteType())
1470 return false;
1471 // Check is based on size and alignment.
1472 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1473 return false;
1474 }
Chris Lattner4d391482007-12-12 07:09:47 +00001475 }
1476 return true;
1477}
1478
Sebastian Redldb9d2142010-08-02 23:18:59 +00001479/// \brief Read the contents of the method pool for a given selector from
1480/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001481///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001482/// This routine should only be called once, when the method pool has no entry
1483/// for this selector.
1484Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001485 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001486 assert(MethodPool.find(Sel) == MethodPool.end() &&
1487 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001488
1489 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001490 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Sebastian Redldb9d2142010-08-02 23:18:59 +00001492 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001493}
1494
Sebastian Redldb9d2142010-08-02 23:18:59 +00001495void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1496 bool instance) {
1497 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1498 if (Pos == MethodPool.end()) {
1499 if (ExternalSource)
1500 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001501 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001502 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1503 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001504 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001505 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001506 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001507 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001508 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001509 Entry.Method = Method;
1510 Entry.Next = 0;
1511 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001512 }
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Chris Lattnerb25df352009-03-04 05:16:45 +00001514 // We've seen a method with this name, see if we have already seen this type
1515 // signature.
1516 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001517 if (MatchTwoMethodDeclarations(Method, List->Method)) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001518 ObjCMethodDecl *PrevObjCMethod = List->Method;
1519 PrevObjCMethod->setDefined(impl);
1520 // If a method is deprecated, push it in the global pool.
1521 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001522 if (Method->isDeprecated()) {
1523 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001524 List->Method = Method;
1525 }
1526 // If new method is unavailable, push it into global pool
1527 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001528 if (Method->isUnavailable()) {
1529 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001530 List->Method = Method;
1531 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001532 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Chris Lattnerb25df352009-03-04 05:16:45 +00001535 // We have a new signature for an existing method - add it.
1536 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001537 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1538 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001539}
1540
Sebastian Redldb9d2142010-08-02 23:18:59 +00001541ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001542 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001543 bool warn, bool instance) {
1544 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1545 if (Pos == MethodPool.end()) {
1546 if (ExternalSource)
1547 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001548 else
1549 return 0;
1550 }
1551
Sebastian Redldb9d2142010-08-02 23:18:59 +00001552 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001554 bool strictSelectorMatch = receiverIdOrClass && warn &&
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001555 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1556 R.getBegin()) !=
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001557 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001558 if (warn && MethList.Method && MethList.Next) {
1559 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001560 if (strictSelectorMatch)
1561 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1562 // This checks if the methods differ in type mismatch.
1563 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1564 issueWarning = true;
1565 }
1566
1567 if (!issueWarning)
1568 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1569 // This checks if the methods differ by size & alignment.
1570 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1571 issueWarning = true;
1572 }
1573
Sebastian Redldb9d2142010-08-02 23:18:59 +00001574 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001575 if (strictSelectorMatch)
1576 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1577 else
1578 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001579 Diag(MethList.Method->getLocStart(), diag::note_using)
1580 << MethList.Method->getSourceRange();
1581 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1582 Diag(Next->Method->getLocStart(), diag::note_also_found)
1583 << Next->Method->getSourceRange();
1584 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001585 }
1586 return MethList.Method;
1587}
1588
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001589ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001590 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1591 if (Pos == MethodPool.end())
1592 return 0;
1593
1594 GlobalMethods &Methods = Pos->second;
1595
1596 if (Methods.first.Method && Methods.first.Method->isDefined())
1597 return Methods.first.Method;
1598 if (Methods.second.Method && Methods.second.Method->isDefined())
1599 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001600 return 0;
1601}
1602
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001603/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1604/// identical selector names in current and its super classes and issues
1605/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001606void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1607 ObjCMethodDecl *Method,
1608 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001609 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1610 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001612 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001613 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001614 SD->lookupMethod(Method->getSelector(), IsInstance);
1615 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001616 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001617 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001618 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001619 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1620 E = Method->param_end();
1621 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1622 for (; ParamI != E; ++ParamI, ++PrevI) {
1623 // Number of parameters are the same and is guaranteed by selector match.
1624 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1625 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1626 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001627 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001628 // respective argument type in the super class method, issue warning;
1629 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001630 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001631 << T1 << T2;
1632 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1633 return;
1634 }
1635 }
1636 ID = SD;
1637 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001638}
1639
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001640/// DiagnoseDuplicateIvars -
1641/// Check for duplicate ivars in the entire class at the start of
1642/// @implementation. This becomes necesssary because class extension can
1643/// add ivars to a class in random order which will not be known until
1644/// class's @implementation is seen.
1645void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1646 ObjCInterfaceDecl *SID) {
1647 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1648 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1649 ObjCIvarDecl* Ivar = (*IVI);
1650 if (Ivar->isInvalidDecl())
1651 continue;
1652 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1653 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1654 if (prevIvar) {
1655 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1656 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1657 Ivar->setInvalidDecl();
1658 }
1659 }
1660 }
1661}
1662
Steve Naroffa56f6162007-12-18 01:30:32 +00001663// Note: For class/category implemenations, allMethods/allProperties is
1664// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001665void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001666 Decl *ClassDecl,
1667 Decl **allMethods, unsigned allNum,
1668 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001669 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001670 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1671 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001672 // should be true.
1673 if (!ClassDecl)
1674 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001675
Mike Stump1eb44332009-09-09 15:08:12 +00001676 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001677 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1678 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001679 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001680
Ted Kremenek782f2f52010-01-07 01:20:12 +00001681 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1682 // FIXME: This is wrong. We shouldn't be pretending that there is
1683 // an '@end' in the declaration.
1684 SourceLocation L = ClassDecl->getLocation();
1685 AtEnd.setBegin(L);
1686 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001687 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001688 }
1689
Steve Naroff0701bbb2009-01-08 17:28:14 +00001690 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1691 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1692 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1693
Chris Lattner4d391482007-12-12 07:09:47 +00001694 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001695 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001696 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001697
1698 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001699 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001700 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001701 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001702 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001703 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001704 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001705 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001706 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001707 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001708 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001709 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001710 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001711 InsMap[Method->getSelector()] = Method;
1712 /// The following allows us to typecheck messages to "id".
1713 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001714 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001715 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001716 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001717 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001718 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001719 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001720 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001721 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001722 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001723 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001724 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001725 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001726 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001727 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00001728 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00001729 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001730 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001731 /// The following allows us to typecheck messages to "Class".
1732 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001733 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001734 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001735 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001736 }
1737 }
1738 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001739 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001740 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001741 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001742 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00001743 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001744 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001745 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001746 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001747 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001748
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001749 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00001750 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001751 if (C->IsClassExtension()) {
1752 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
1753 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00001754 }
Chris Lattner4d391482007-12-12 07:09:47 +00001755 }
Steve Naroff09c47192009-01-09 15:36:25 +00001756 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001757 if (CDecl->getIdentifier())
1758 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1759 // user-defined setter/getter. It also synthesizes setter/getter methods
1760 // and adds them to the DeclContext and global method pools.
1761 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1762 E = CDecl->prop_end();
1763 I != E; ++I)
1764 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001765 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001766 }
1767 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001768 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001769 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00001770 // Any property declared in a class extension might have user
1771 // declared setter or getter in current class extension or one
1772 // of the other class extensions. Mark them as synthesized as
1773 // property will be synthesized when property with same name is
1774 // seen in the @implementation.
1775 for (const ObjCCategoryDecl *ClsExtDecl =
1776 IDecl->getFirstClassExtension();
1777 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
1778 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
1779 E = ClsExtDecl->prop_end(); I != E; ++I) {
1780 ObjCPropertyDecl *Property = (*I);
1781 // Skip over properties declared @dynamic
1782 if (const ObjCPropertyImplDecl *PIDecl
1783 = IC->FindPropertyImplDecl(Property->getIdentifier()))
1784 if (PIDecl->getPropertyImplementation()
1785 == ObjCPropertyImplDecl::Dynamic)
1786 continue;
1787
1788 for (const ObjCCategoryDecl *CExtDecl =
1789 IDecl->getFirstClassExtension();
1790 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
1791 if (ObjCMethodDecl *GetterMethod =
1792 CExtDecl->getInstanceMethod(Property->getGetterName()))
1793 GetterMethod->setSynthesized(true);
1794 if (!Property->isReadOnly())
1795 if (ObjCMethodDecl *SetterMethod =
1796 CExtDecl->getInstanceMethod(Property->getSetterName()))
1797 SetterMethod->setSynthesized(true);
1798 }
1799 }
1800 }
1801
Ted Kremenekc32647d2010-12-23 21:35:43 +00001802 if (LangOpts.ObjCDefaultSynthProperties &&
1803 LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001804 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001805 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001806 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001807
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001808 if (LangOpts.ObjCNonFragileABI2)
1809 while (IDecl->getSuperClass()) {
1810 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1811 IDecl = IDecl->getSuperClass();
1812 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001813 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001814 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001815 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001816 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001817 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Chris Lattner4d391482007-12-12 07:09:47 +00001819 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001820 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001821 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001822 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001823 Categories; Categories = Categories->getNextClassCategory()) {
1824 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001825 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001826 break;
1827 }
1828 }
1829 }
1830 }
Chris Lattner682bf922009-03-29 16:50:03 +00001831 if (isInterfaceDeclKind) {
1832 // Reject invalid vardecls.
1833 for (unsigned i = 0; i != tuvNum; i++) {
1834 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1835 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1836 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001837 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001838 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001839 }
Chris Lattner682bf922009-03-29 16:50:03 +00001840 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001841 }
Chris Lattner4d391482007-12-12 07:09:47 +00001842}
1843
1844
1845/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1846/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001847static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001848CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00001849 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00001850}
1851
Ted Kremenek422bae72010-04-18 04:59:38 +00001852static inline
Sean Huntcf807c42010-08-18 23:23:40 +00001853bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00001854 // The 'ibaction' attribute is allowed on method definitions because of
1855 // how the IBAction macro is used on both method declarations and definitions.
1856 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00001857 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1858 if ((*i)->getKind() != attr::IBAction)
1859 return true;
1860 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00001861}
1862
Douglas Gregor926df6c2011-06-11 01:09:30 +00001863/// \brief Check whether the declared result type of the given Objective-C
1864/// method declaration is compatible with the method's class.
1865///
1866static bool
1867CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
1868 ObjCInterfaceDecl *CurrentClass) {
1869 QualType ResultType = Method->getResultType();
1870 SourceRange ResultTypeRange;
1871 if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
1872 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
1873
1874 // If an Objective-C method inherits its related result type, then its
1875 // declared result type must be compatible with its own class type. The
1876 // declared result type is compatible if:
1877 if (const ObjCObjectPointerType *ResultObjectType
1878 = ResultType->getAs<ObjCObjectPointerType>()) {
1879 // - it is id or qualified id, or
1880 if (ResultObjectType->isObjCIdType() ||
1881 ResultObjectType->isObjCQualifiedIdType())
1882 return false;
1883
1884 if (CurrentClass) {
1885 if (ObjCInterfaceDecl *ResultClass
1886 = ResultObjectType->getInterfaceDecl()) {
1887 // - it is the same as the method's class type, or
1888 if (CurrentClass == ResultClass)
1889 return false;
1890
1891 // - it is a superclass of the method's class type
1892 if (ResultClass->isSuperClassOf(CurrentClass))
1893 return false;
1894 }
1895 }
1896 }
1897
1898 return true;
1899}
1900
1901/// \brief Determine if any method in the global method pool has an inferred
1902/// result type.
1903static bool
1904anyMethodInfersRelatedResultType(Sema &S, Selector Sel, bool IsInstance) {
1905 Sema::GlobalMethodPool::iterator Pos = S.MethodPool.find(Sel);
1906 if (Pos == S.MethodPool.end()) {
1907 if (S.ExternalSource)
1908 Pos = S.ReadMethodPool(Sel);
1909 else
1910 return 0;
1911 }
1912
1913 ObjCMethodList &List = IsInstance ? Pos->second.first : Pos->second.second;
1914 for (ObjCMethodList *M = &List; M; M = M->Next) {
1915 if (M->Method && M->Method->hasRelatedResultType())
1916 return true;
1917 }
1918
1919 return false;
1920}
1921
John McCalld226f652010-08-21 09:40:31 +00001922Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001923 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00001924 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00001925 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00001926 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001927 SourceLocation SelectorStartLoc,
Chris Lattner4d391482007-12-12 07:09:47 +00001928 Selector Sel,
1929 // optional arguments. The number of types/arguments is obtained
1930 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001931 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001932 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001933 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001934 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001935 // Make sure we can establish a context for the method.
1936 if (!ClassDecl) {
1937 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00001938 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00001939 }
Chris Lattner4d391482007-12-12 07:09:47 +00001940 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001942 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001943 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001944 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Steve Naroffccef3712009-02-20 22:59:16 +00001946 // Methods cannot return interface types. All ObjC objects are
1947 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001948 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001949 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1950 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00001951 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00001952 }
Steve Naroffccef3712009-02-20 22:59:16 +00001953 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001954 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001955
1956 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001957 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001958 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001959 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001960 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001961 false, false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001962 MethodDeclKind == tok::objc_optional
1963 ? ObjCMethodDecl::Optional
1964 : ObjCMethodDecl::Required,
1965 false);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Chris Lattner0ed844b2008-04-04 06:12:32 +00001967 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Chris Lattner7db638d2009-04-11 19:42:43 +00001969 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001970 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001971 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Chris Lattnere294d3f2009-04-11 18:57:04 +00001973 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001974 ArgType = Context.getObjCIdType();
1975 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001976 } else {
John McCall58e46772009-10-23 21:48:59 +00001977 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001978 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001979 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001980 }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001982 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
1983 LookupOrdinaryName, ForRedeclaration);
1984 LookupName(R, S);
1985 if (R.isSingleResult()) {
1986 NamedDecl *PrevDecl = R.getFoundDecl();
1987 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001988 Diag(ArgInfo[i].NameLoc,
1989 (MethodDefinition ? diag::warn_method_param_redefinition
1990 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001991 << ArgInfo[i].Name;
1992 Diag(PrevDecl->getLocation(),
1993 diag::note_previous_declaration);
1994 }
1995 }
1996
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001997 SourceLocation StartLoc = DI
1998 ? DI->getTypeLoc().getBeginLoc()
1999 : ArgInfo[i].NameLoc;
2000
John McCall81ef3e62011-04-23 02:46:06 +00002001 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2002 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2003 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002004
John McCall70798862011-05-02 00:30:12 +00002005 Param->setObjCMethodScopeInfo(i);
2006
Chris Lattner0ed844b2008-04-04 06:12:32 +00002007 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002008 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002009
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002010 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002011 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002013 S->AddDecl(Param);
2014 IdResolver.AddDecl(Param);
2015
Chris Lattner0ed844b2008-04-04 06:12:32 +00002016 Params.push_back(Param);
2017 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002018
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002019 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002020 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002021 QualType ArgType = Param->getType();
2022 if (ArgType.isNull())
2023 ArgType = Context.getObjCIdType();
2024 else
2025 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
2026 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002027 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002028 Diag(Param->getLocation(),
2029 diag::err_object_cannot_be_passed_returned_by_value)
2030 << 1 << ArgType;
2031 Param->setInvalidDecl();
2032 }
2033 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002034
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002035 Params.push_back(Param);
2036 }
2037
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002038 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2039 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002040 ObjCMethod->setObjCDeclQualifier(
2041 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2042 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002043
2044 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002045 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002046
John McCall54abf7d2009-11-04 02:18:39 +00002047 const ObjCMethodDecl *InterfaceMD = 0;
2048
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002049 // Add the method now.
Mike Stump1eb44332009-09-09 15:08:12 +00002050 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002051 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002052 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002053 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2054 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002055 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002056 PrevMethod = ImpDecl->getClassMethod(Sel);
2057 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002058 }
John McCall54abf7d2009-11-04 02:18:39 +00002059 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2060 MethodType == tok::minus);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002061
Sean Huntcf807c42010-08-18 23:23:40 +00002062 if (ObjCMethod->hasAttrs() &&
2063 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002064 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002065 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002066 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002067 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002068 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2069 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002070 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002071 PrevMethod = CatImpDecl->getClassMethod(Sel);
2072 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002073 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002074
2075 if (ObjCCategoryDecl *Cat = CatImpDecl->getCategoryDecl())
2076 InterfaceMD = Cat->getMethod(Sel, MethodType == tok::minus);
2077
Sean Huntcf807c42010-08-18 23:23:40 +00002078 if (ObjCMethod->hasAttrs() &&
2079 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002080 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002081 } else {
2082 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002083 }
2084 if (PrevMethod) {
2085 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002086 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002087 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002088 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002089 }
John McCall54abf7d2009-11-04 02:18:39 +00002090
Douglas Gregor926df6c2011-06-11 01:09:30 +00002091 // If this Objective-C method does not have a related result type, but we
2092 // are allowed to infer related result types, try to do so based on the
2093 // method family.
2094 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2095 if (!CurrentClass) {
2096 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2097 CurrentClass = Cat->getClassInterface();
2098 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2099 CurrentClass = Impl->getClassInterface();
2100 else if (ObjCCategoryImplDecl *CatImpl
2101 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2102 CurrentClass = CatImpl->getClassInterface();
2103 }
2104
John McCalleca5d222011-03-02 04:00:57 +00002105 // Merge information down from the interface declaration if we have one.
Douglas Gregor926df6c2011-06-11 01:09:30 +00002106 if (InterfaceMD) {
2107 // Inherit the related result type, if we can.
2108 if (InterfaceMD->hasRelatedResultType() &&
2109 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2110 ObjCMethod->SetRelatedResultType();
2111
John McCalleca5d222011-03-02 04:00:57 +00002112 mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002113 }
2114
2115 if (!ObjCMethod->hasRelatedResultType() &&
2116 getLangOptions().ObjCInferRelatedResultType) {
2117 bool InferRelatedResultType = false;
2118 switch (ObjCMethod->getMethodFamily()) {
2119 case OMF_None:
2120 case OMF_copy:
2121 case OMF_dealloc:
2122 case OMF_mutableCopy:
2123 case OMF_release:
2124 case OMF_retainCount:
2125 break;
2126
2127 case OMF_alloc:
2128 case OMF_new:
2129 InferRelatedResultType = ObjCMethod->isClassMethod();
2130 break;
2131
2132 case OMF_init:
2133 case OMF_autorelease:
2134 case OMF_retain:
2135 case OMF_self:
2136 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2137 break;
2138 }
2139
2140 if (InferRelatedResultType &&
2141 !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
2142 ObjCMethod->SetRelatedResultType();
2143
2144 if (!InterfaceMD &&
2145 anyMethodInfersRelatedResultType(*this, ObjCMethod->getSelector(),
2146 ObjCMethod->isInstanceMethod()))
2147 CheckObjCMethodOverrides(ObjCMethod, cast<DeclContext>(ClassDecl));
2148 }
2149
John McCalld226f652010-08-21 09:40:31 +00002150 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002151}
2152
Chris Lattnercc98eac2008-12-17 07:13:27 +00002153bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002154 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002155 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Anders Carlsson15281452008-11-04 16:57:32 +00002157 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2158 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002159
Anders Carlsson15281452008-11-04 16:57:32 +00002160 return true;
2161}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002162
Chris Lattnercc98eac2008-12-17 07:13:27 +00002163/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2164/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002165void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002166 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00002167 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002168 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002169 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002170 if (!Class) {
2171 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2172 return;
2173 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002174 if (LangOpts.ObjCNonFragileABI) {
2175 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2176 return;
2177 }
Mike Stump1eb44332009-09-09 15:08:12 +00002178
Chris Lattnercc98eac2008-12-17 07:13:27 +00002179 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002180 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
2181 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002182 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002183 for (unsigned i = 0; i < Ivars.size(); i++) {
2184 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002185 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002186 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2187 /*FIXME: StartL=*/ID->getLocation(),
2188 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002189 ID->getIdentifier(), ID->getType(),
2190 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002191 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002192 }
Mike Stump1eb44332009-09-09 15:08:12 +00002193
Chris Lattnercc98eac2008-12-17 07:13:27 +00002194 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00002195 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002196 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002197 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002198 if (getLangOptions().CPlusPlus)
2199 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002200 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002201 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002202 }
2203}
2204
Douglas Gregor160b5632010-04-26 17:32:49 +00002205/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002206VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2207 SourceLocation StartLoc,
2208 SourceLocation IdLoc,
2209 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002210 bool Invalid) {
2211 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2212 // duration shall not be qualified by an address-space qualifier."
2213 // Since all parameters have automatic store duration, they can not have
2214 // an address space.
2215 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002216 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002217 Invalid = true;
2218 }
2219
2220 // An @catch parameter must be an unqualified object pointer type;
2221 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2222 if (Invalid) {
2223 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002224 } else if (T->isDependentType()) {
2225 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002226 } else if (!T->isObjCObjectPointerType()) {
2227 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002228 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002229 } else if (T->isObjCQualifiedIdType()) {
2230 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002231 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002232 }
2233
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002234 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2235 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002236 New->setExceptionVariable(true);
2237
Douglas Gregor160b5632010-04-26 17:32:49 +00002238 if (Invalid)
2239 New->setInvalidDecl();
2240 return New;
2241}
2242
John McCalld226f652010-08-21 09:40:31 +00002243Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002244 const DeclSpec &DS = D.getDeclSpec();
2245
2246 // We allow the "register" storage class on exception variables because
2247 // GCC did, but we drop it completely. Any other storage class is an error.
2248 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2249 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2250 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2251 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2252 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2253 << DS.getStorageClassSpec();
2254 }
2255 if (D.getDeclSpec().isThreadSpecified())
2256 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2257 D.getMutableDeclSpec().ClearStorageClassSpecs();
2258
2259 DiagnoseFunctionSpecifiers(D);
2260
2261 // Check that there are no default arguments inside the type of this
2262 // exception object (C++ only).
2263 if (getLangOptions().CPlusPlus)
2264 CheckExtraCXXDefaultArguments(D);
2265
Douglas Gregor160b5632010-04-26 17:32:49 +00002266 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002267 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
2268 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002269
2270 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
2271 // Objective-C++: Types shall not be defined in exception types.
2272 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
2273 << Context.getTypeDeclType(OwnedDecl);
2274 }
2275
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002276 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2277 D.getSourceRange().getBegin(),
2278 D.getIdentifierLoc(),
2279 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002280 D.isInvalidType());
2281
2282 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2283 if (D.getCXXScopeSpec().isSet()) {
2284 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2285 << D.getCXXScopeSpec().getRange();
2286 New->setInvalidDecl();
2287 }
2288
2289 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002290 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002291 if (D.getIdentifier())
2292 IdResolver.AddDecl(New);
2293
2294 ProcessDeclAttributes(S, New, D);
2295
2296 if (New->hasAttr<BlocksAttr>())
2297 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002298 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002299}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002300
2301/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002302/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002303void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002304 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002305 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2306 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002307 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002308 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002309 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002310 }
2311}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002312
2313void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Sean Huntcbb67482011-01-08 20:30:50 +00002314 CXXCtorInitializer ** initializers,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002315 unsigned numInitializers) {
2316 if (numInitializers > 0) {
2317 NumIvarInitializers = numInitializers;
Sean Huntcbb67482011-01-08 20:30:50 +00002318 CXXCtorInitializer **ivarInitializers =
2319 new (C) CXXCtorInitializer*[NumIvarInitializers];
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002320 memcpy(ivarInitializers, initializers,
Sean Huntcbb67482011-01-08 20:30:50 +00002321 numInitializers * sizeof(CXXCtorInitializer*));
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002322 IvarInitializers = ivarInitializers;
2323 }
2324}
2325
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002326void Sema::DiagnoseUseOfUnimplementedSelectors() {
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002327 // Warning will be issued only when selector table is
2328 // generated (which means there is at lease one implementation
2329 // in the TU). This is to match gcc's behavior.
2330 if (ReferencedSelectors.empty() ||
2331 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002332 return;
2333 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2334 ReferencedSelectors.begin(),
2335 E = ReferencedSelectors.end(); S != E; ++S) {
2336 Selector Sel = (*S).first;
2337 if (!LookupImplementedMethodInGlobalPool(Sel))
2338 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2339 }
2340 return;
2341}