blob: 160bc19b3f219ee3f5e5a8c794c479a189ebd250 [file] [log] [blame]
Chris Lattnerda463fe2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerda463fe2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregor35b0bac2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorc78d3462009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff157599f2009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000021using namespace clang;
22
Sebastian Redlab6a0882010-08-09 21:55:28 +000023/// \brief Look for an Objective-C class in the translation unit.
24///
25/// \param Id The name of the Objective-C class we're looking for. If
26/// typo-correction fixes this name, the Id will be updated
27/// to the fixed name.
28///
29/// \param IdLoc The location of the name in the translation unit.
30///
31/// \param TypoCorrection If true, this routine will attempt typo correction
32/// if there is no class with the given name.
33///
34/// \returns The declaration of the named Objective-C class, which is also the
35/// definition if one is available, or NULL if the class could not be found.
36ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id,
37 SourceLocation IdLoc,
38 bool TypoCorrection) {
39 // The third "scope" argument is 0 since we aren't enabling lazy built-in
40 // creation from this context.
41 NamedDecl *Decl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName);
42
43 if (!Decl && TypoCorrection) {
44 // Perform typo correction at the given location, but only if we
45 // find an Objective-C class name.
46 LookupResult R(*this, Id, IdLoc, LookupOrdinaryName);
47 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
48 (Decl = R.getAsSingle<ObjCInterfaceDecl>())) {
49 Diag(IdLoc, diag::err_undef_interface_suggest)
50 << Id << Decl->getDeclName()
51 << FixItHint::CreateReplacement(IdLoc, Decl->getNameAsString());
52 Diag(Decl->getLocation(), diag::note_previous_decl)
53 << Decl->getDeclName();
54
55 Id = Decl->getIdentifier();
56 }
57 }
58
59 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(Decl);
60 if (IDecl) {
61 if (ObjCInterfaceDecl *Def = IDecl->getDefinition())
62 IDecl = Def;
63 }
64 return IDecl;
65}
66
Steve Naroff70f41d62009-02-28 16:59:13 +000067/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattnerda463fe2007-12-12 07:09:47 +000068/// and user declared, in the method definition's AST.
Chris Lattner83f095c2009-03-28 19:18:32 +000069void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000070 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner83f095c2009-03-28 19:18:32 +000071 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump11289f42009-09-09 15:08:12 +000072
Steve Naroff542cd5d2008-07-25 17:57:26 +000073 // If we don't have a valid method decl, simply return.
74 if (!MDecl)
75 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +000076
77 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorffca3a22009-01-09 17:18:27 +000078 if (MDecl->isInstanceMethod())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +000079 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroff1d2538c2007-12-18 01:30:32 +000080 else
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +000081 AddFactoryMethodToGlobalPool(MDecl, true);
82
Chris Lattnerda463fe2007-12-12 07:09:47 +000083 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +000084 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9a28e842010-03-01 23:15:13 +000085 PushFunctionScope();
86
Chris Lattnerda463fe2007-12-12 07:09:47 +000087 // Create Decl objects for each parameter, entrring them in the scope for
88 // binding to their use.
Chris Lattnerda463fe2007-12-12 07:09:47 +000089
90 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +000091 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +000092
Daniel Dunbar279d1cc2008-08-26 06:07:48 +000093 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
94 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000095
Chris Lattner58258242008-04-10 02:22:51 +000096 // Introduce all of the other parameters into this scope.
Chris Lattnera4997152009-02-20 18:43:26 +000097 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
98 E = MDecl->param_end(); PI != E; ++PI)
99 if ((*PI)->getIdentifier())
100 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000101}
102
Chris Lattner83f095c2009-03-28 19:18:32 +0000103Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000104ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
105 IdentifierInfo *ClassName, SourceLocation ClassLoc,
106 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000107 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000108 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000109 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000110 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000111
Sebastian Redlab6a0882010-08-09 21:55:28 +0000112 bool Invalid = false;
113
Chris Lattnerda463fe2007-12-12 07:09:47 +0000114 // Check for another declaration kind with the same name.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000115 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000116 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5101c242008-12-05 18:15:24 +0000117
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000118 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000119 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000120 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Sebastian Redlab6a0882010-08-09 21:55:28 +0000121 // Set the new decl invalid and ignore the old.
122 Invalid = true;
123 PrevDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Sebastian Redlab6a0882010-08-09 21:55:28 +0000126 ObjCInterfaceDecl *ODecl = cast_or_null<ObjCInterfaceDecl>(PrevDecl);
127 if (ODecl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000128 // Class already seen. Is it a forward declaration?
Sebastian Redlab6a0882010-08-09 21:55:28 +0000129 if (ObjCInterfaceDecl *Def = ODecl->getDefinition()) {
130 Invalid = true;
131 Diag(AtInterfaceLoc, diag::err_duplicate_class_def) << Def->getDeclName();
132 Diag(Def->getLocation(), diag::note_previous_definition);
Chris Lattnere6447ef2008-11-23 22:46:27 +0000133
Sebastian Redlab6a0882010-08-09 21:55:28 +0000134 // Return the previous class interface and ignore the new one.
135 return DeclPtrTy::make(ODecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000136 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000137 }
Mike Stump11289f42009-09-09 15:08:12 +0000138
Sebastian Redlab6a0882010-08-09 21:55:28 +0000139 ObjCInterfaceDecl *IDecl =
140 ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
141 ClassName, ClassLoc, ODecl);
142 if (Invalid)
143 IDecl->setInvalidDecl();
144
145 if (AttrList)
146 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
147
148 PushOnScopeChains(IDecl, TUScope);
149
Chris Lattnerda463fe2007-12-12 07:09:47 +0000150 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000151 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000152 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
153 LookupOrdinaryName);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000154
155 if (!PrevDecl) {
156 // Try to correct for a typo in the superclass name.
157 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000158 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000159 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
160 Diag(SuperLoc, diag::err_undef_superclass_suggest)
161 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor6da83622010-01-07 00:17:44 +0000162 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
163 << PrevDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000164 }
165 }
166
Sebastian Redlab6a0882010-08-09 21:55:28 +0000167 // Since we just pushed IDecl on the scope chain, if PrevDecl is the same
168 // class, it will be the same declaration.
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000169 if (PrevDecl == IDecl) {
170 Diag(SuperLoc, diag::err_recursive_superclass)
171 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
172 IDecl->setLocEnd(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000173 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000174 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000175 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000176
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000177 // Diagnose classes that inherit from deprecated classes.
178 if (SuperClassDecl)
179 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000180
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000181 if (PrevDecl && SuperClassDecl == 0) {
182 // The previous declaration was not a class decl. Check if we have a
183 // typedef. If we do, get the underlying class type.
Sebastian Redlab6a0882010-08-09 21:55:28 +0000184 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)){
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000185 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000186 if (T->isObjCObjectType()) {
Sebastian Redlab6a0882010-08-09 21:55:28 +0000187 if (NamedDecl *NDecl = T->getAs<ObjCObjectType>()->getInterface())
188 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(NDecl);
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000189 }
190 }
Mike Stump11289f42009-09-09 15:08:12 +0000191
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000192 // This handles the following case:
193 //
194 // typedef int SuperClass;
195 // @interface MyClass : SuperClass {} @end
196 //
197 if (!SuperClassDecl) {
198 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
199 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000200 }
201 }
Mike Stump11289f42009-09-09 15:08:12 +0000202
Sebastian Redlab6a0882010-08-09 21:55:28 +0000203 if (SuperClassDecl) {
204 if (ObjCInterfaceDecl *Def = SuperClassDecl->getDefinition())
205 SuperClassDecl = Def;
206 }
207
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000208 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
209 if (!SuperClassDecl)
210 Diag(SuperLoc, diag::err_undef_superclass)
211 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
212 else if (SuperClassDecl->isForwardDecl())
213 Diag(SuperLoc, diag::err_undef_superclass)
214 << SuperClassDecl->getDeclName() << ClassName
215 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000216 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000217 IDecl->setSuperClass(SuperClassDecl);
218 IDecl->setSuperClassLoc(SuperLoc);
219 IDecl->setLocEnd(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000220 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000221 } else { // we have a root class.
222 IDecl->setLocEnd(ClassLoc);
223 }
Mike Stump11289f42009-09-09 15:08:12 +0000224
Steve Naroff119f60e2008-11-18 19:15:30 +0000225 /// Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000226 if (NumProtoRefs) {
Chris Lattner22298722009-02-20 21:35:13 +0000227 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000228 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000229 IDecl->setLocEnd(EndProtoLoc);
230 }
Mike Stump11289f42009-09-09 15:08:12 +0000231
Anders Carlssona6b508a2008-11-04 16:57:32 +0000232 CheckObjCDeclScope(IDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000233 return DeclPtrTy::make(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000234}
235
236/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000237/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner83f095c2009-03-28 19:18:32 +0000238Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000239 IdentifierInfo *AliasName,
Chris Lattner83f095c2009-03-28 19:18:32 +0000240 SourceLocation AliasLocation,
241 IdentifierInfo *ClassName,
242 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000243 // Look for previous declaration of alias name
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000244 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000245 LookupOrdinaryName, ForRedeclaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000246 if (ADecl) {
Chris Lattnerd0685032008-11-23 23:20:13 +0000247 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattnerda463fe2007-12-12 07:09:47 +0000248 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerd0685032008-11-23 23:20:13 +0000249 else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000250 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000251 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000252 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000253 }
254 // Check for class declaration
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000255 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000256 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000257 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
258 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000259 if (T->isObjCObjectType()) {
260 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000261 ClassName = IDecl->getIdentifier();
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000262 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000263 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000264 }
265 }
266 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000267 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
268 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000269 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000270 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000271 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000272 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattner219b3e92008-03-16 21:17:37 +0000275 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000276 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000277 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000278
Anders Carlssona6b508a2008-11-04 16:57:32 +0000279 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000280 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000281
Chris Lattner83f095c2009-03-28 19:18:32 +0000282 return DeclPtrTy::make(AliasDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000283}
284
Steve Naroff41d09ad2009-03-05 15:22:01 +0000285void Sema::CheckForwardProtocolDeclarationForCircularDependency(
286 IdentifierInfo *PName,
287 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000288 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000289 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
290 E = PList.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000291
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000292 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
293 Ploc)) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000294 if (PDecl->getIdentifier() == PName) {
295 Diag(Ploc, diag::err_protocol_has_circular_dependency);
296 Diag(PrevLoc, diag::note_previous_definition);
297 }
Mike Stump11289f42009-09-09 15:08:12 +0000298 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff41d09ad2009-03-05 15:22:01 +0000299 PDecl->getLocation(), PDecl->getReferencedProtocols());
300 }
301 }
302}
303
Chris Lattner83f095c2009-03-28 19:18:32 +0000304Sema::DeclPtrTy
Chris Lattner3bbae002008-07-26 04:03:38 +0000305Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
306 IdentifierInfo *ProtocolName,
307 SourceLocation ProtocolLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000308 const DeclPtrTy *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000309 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000310 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000311 SourceLocation EndProtoLoc,
312 AttributeList *AttrList) {
313 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000314 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000315 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000316 if (PDecl) {
317 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner5074f8f2008-03-16 01:25:17 +0000318 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian54d569c2009-04-06 23:43:32 +0000319 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnere6447ef2008-11-23 22:46:27 +0000320 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000321 // Just return the protocol we already had.
322 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000323 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000324 }
Steve Naroff41d09ad2009-03-05 15:22:01 +0000325 ObjCList<ObjCProtocolDecl> PList;
Mike Stump11289f42009-09-09 15:08:12 +0000326 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff41d09ad2009-03-05 15:22:01 +0000327 CheckForwardProtocolDeclarationForCircularDependency(
328 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump11289f42009-09-09 15:08:12 +0000329
Steve Naroffeb03dac2008-08-13 16:39:22 +0000330 // Make sure the cached decl gets a valid start location.
331 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000332 PDecl->setForwardDecl(false);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000333 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000334 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000335 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000336 PushOnScopeChains(PDecl, TUScope);
Chris Lattneracc04a92008-03-16 20:19:15 +0000337 PDecl->setForwardDecl(false);
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000338 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000339 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000340 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000341 if (NumProtoRefs) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000342 /// Check then save referenced protocols.
Douglas Gregor002b6712010-01-16 15:02:53 +0000343 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
344 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000345 PDecl->setLocEnd(EndProtoLoc);
346 }
Mike Stump11289f42009-09-09 15:08:12 +0000347
348 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000349 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000350}
351
352/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000353/// issues an error if they are not declared. It returns list of
354/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000355void
Chris Lattner3bbae002008-07-26 04:03:38 +0000356Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000357 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000358 unsigned NumProtocols,
Chris Lattner83f095c2009-03-28 19:18:32 +0000359 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000360 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000361 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
362 ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000363 if (!PDecl) {
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000364 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
365 LookupObjCProtocolName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000366 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000367 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
368 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
369 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor6da83622010-01-07 00:17:44 +0000370 Diag(PDecl->getLocation(), diag::note_previous_decl)
371 << PDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000372 }
373 }
374
375 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000376 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000377 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000378 continue;
379 }
Mike Stump11289f42009-09-09 15:08:12 +0000380
Douglas Gregor171c45a2009-02-18 21:56:37 +0000381 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000382
383 // If this is a forward declaration and we are supposed to warn in this
384 // case, do it.
385 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner3b054132008-11-19 05:08:23 +0000386 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000387 << ProtocolId[i].first;
Chris Lattner83f095c2009-03-28 19:18:32 +0000388 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattnerda463fe2007-12-12 07:09:47 +0000389 }
390}
391
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000392/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000393/// a class method in its extension.
394///
Mike Stump11289f42009-09-09 15:08:12 +0000395void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000396 ObjCInterfaceDecl *ID) {
397 if (!ID)
398 return; // Possibly due to previous error
399
400 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000401 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
402 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000403 ObjCMethodDecl *MD = *i;
404 MethodMap[MD->getSelector()] = MD;
405 }
406
407 if (MethodMap.empty())
408 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000409 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
410 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000411 ObjCMethodDecl *Method = *i;
412 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
413 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
414 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
415 << Method->getDeclName();
416 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
417 }
418 }
419}
420
Chris Lattnerdac168d2009-04-12 08:43:13 +0000421/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner83f095c2009-03-28 19:18:32 +0000422Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000423Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000424 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000425 unsigned NumElts,
426 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000427 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor002b6712010-01-16 15:02:53 +0000428 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump11289f42009-09-09 15:08:12 +0000429
Chris Lattnerda463fe2007-12-12 07:09:47 +0000430 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000431 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000432 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000433 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000434 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000435 IdentList[i].second, Ident);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000436 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000437 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000438 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000439 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000440 Protocols.push_back(PDecl);
Douglas Gregor002b6712010-01-16 15:02:53 +0000441 ProtoLocs.push_back(IdentList[i].second);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
444 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000445 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor002b6712010-01-16 15:02:53 +0000446 Protocols.data(), Protocols.size(),
447 ProtoLocs.data());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000448 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000449 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000450 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000451}
452
Chris Lattner83f095c2009-03-28 19:18:32 +0000453Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000454ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
455 IdentifierInfo *ClassName, SourceLocation ClassLoc,
456 IdentifierInfo *CategoryName,
457 SourceLocation CategoryLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000458 const DeclPtrTy *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000459 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000460 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000461 SourceLocation EndProtoLoc) {
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000462 ObjCCategoryDecl *CDecl;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000463 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek514ff702010-02-23 19:39:46 +0000464
465 /// Check that class of this category is already completely declared.
466 if (!IDecl || IDecl->isForwardDecl()) {
467 // Create an invalid ObjCCategoryDecl to serve as context for
468 // the enclosing method declarations. We mark the decl invalid
469 // to make it clear that this isn't a valid AST.
470 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
471 ClassLoc, CategoryLoc, CategoryName);
472 CDecl->setInvalidDecl();
473 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
474 return DeclPtrTy::make(CDecl);
475 }
476
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000477 if (!CategoryName && IDecl->getImplementation()) {
478 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
479 Diag(IDecl->getImplementation()->getLocation(),
480 diag::note_implementation_declared);
Ted Kremenek514ff702010-02-23 19:39:46 +0000481 }
482
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000483 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
484 ClassLoc, CategoryLoc, CategoryName);
485 // FIXME: PushOnScopeChains?
486 CurContext->addDecl(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000487
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000488 CDecl->setClassInterface(IDecl);
489 // Insert class extension to the list of class's categories.
490 if (!CategoryName)
491 CDecl->insertNextClassCategory();
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattnerced903b2009-02-16 21:30:01 +0000493 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000494 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000495
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000496 if (CategoryName) {
497 /// Check for duplicate interface declaration for this category
498 ObjCCategoryDecl *CDeclChain;
499 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
500 CDeclChain = CDeclChain->getNextClassCategory()) {
501 if (CDeclChain->getIdentifier() == CategoryName) {
502 // Class extensions can be declared multiple times.
503 Diag(CategoryLoc, diag::warn_dup_category_def)
504 << ClassName << CategoryName;
505 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
506 break;
507 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000508 }
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000509 if (!CDeclChain)
510 CDecl->insertNextClassCategory();
Chris Lattner9018ca82009-02-16 21:26:43 +0000511 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000512
Chris Lattnerda463fe2007-12-12 07:09:47 +0000513 if (NumProtoRefs) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000514 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000515 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000516 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000517 if (CDecl->IsClassExtension())
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000518 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000519 NumProtoRefs, ProtoLocs,
520 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000521 }
Mike Stump11289f42009-09-09 15:08:12 +0000522
Anders Carlssona6b508a2008-11-04 16:57:32 +0000523 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000524 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000525}
526
527/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000528/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000529/// object.
Chris Lattner83f095c2009-03-28 19:18:32 +0000530Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000531 SourceLocation AtCatImplLoc,
532 IdentifierInfo *ClassName, SourceLocation ClassLoc,
533 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000534 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000535 ObjCCategoryDecl *CatIDecl = 0;
536 if (IDecl) {
537 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
538 if (!CatIDecl) {
539 // Category @implementation with no corresponding @interface.
540 // Create and install one.
541 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor071676f2010-01-16 16:38:58 +0000542 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000543 CatName);
544 CatIDecl->setClassInterface(IDecl);
545 CatIDecl->insertNextClassCategory();
546 }
547 }
548
Mike Stump11289f42009-09-09 15:08:12 +0000549 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000550 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
551 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000552 /// Check that class of this category is already completely declared.
553 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000554 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000555
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000556 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000557 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000558
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000559 /// Check that CatName, category name, is not used in another implementation.
560 if (CatIDecl) {
561 if (CatIDecl->getImplementation()) {
562 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
563 << CatName;
564 Diag(CatIDecl->getImplementation()->getLocation(),
565 diag::note_previous_definition);
566 } else
567 CatIDecl->setImplementation(CDecl);
568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Anders Carlssona6b508a2008-11-04 16:57:32 +0000570 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000571 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000572}
573
Chris Lattner83f095c2009-03-28 19:18:32 +0000574Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000575 SourceLocation AtClassImplLoc,
576 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000577 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000578 SourceLocation SuperClassLoc) {
Sebastian Redlab6a0882010-08-09 21:55:28 +0000579 ObjCInterfaceDecl *IDecl = 0, *ODecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000580 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000581 NamedDecl *PrevDecl
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000582 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
583 ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000584 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000585 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000586 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Sebastian Redlab6a0882010-08-09 21:55:28 +0000587 } else if ((ODecl = cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
588 // If we can't find a definition of the interface, warn.
589 if (!(IDecl = ODecl->getDefinition())) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000590 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000591 }
Douglas Gregor40f7a002010-01-04 17:27:12 +0000592 } else {
593 // We did not find anything with the name ClassName; try to correct for
594 // typos in the class name.
595 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000596 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor40f7a002010-01-04 17:27:12 +0000597 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000598 // Suggest the (potentially) correct interface name. However, put the
599 // fix-it hint itself in a separate note, since changing the name in
Sebastian Redlab6a0882010-08-09 21:55:28 +0000600 // the warning would make the fix-it change semantics. Also, don't
Douglas Gregor40f7a002010-01-04 17:27:12 +0000601 // provide a code-modification hint or use the typo name for recovery,
602 // because this is just a warning. The program may actually be correct.
603 Diag(ClassLoc, diag::warn_undef_interface_suggest)
604 << ClassName << R.getLookupName();
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000605 Diag(IDecl->getLocation(), diag::note_previous_decl)
606 << R.getLookupName()
Douglas Gregora771f462010-03-31 17:46:05 +0000607 << FixItHint::CreateReplacement(ClassLoc,
608 R.getLookupName().getAsString());
Douglas Gregor40f7a002010-01-04 17:27:12 +0000609 IDecl = 0;
610 } else {
611 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
612 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000613 }
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattnerda463fe2007-12-12 07:09:47 +0000615 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000616 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000617 if (SuperClassname) {
618 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000619 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
620 LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000621 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000622 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
623 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000624 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000625 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000626 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000627 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000628 Diag(SuperClassLoc, diag::err_undef_superclass)
629 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000630 else if (IDecl && IDecl->getSuperClass() != SDecl) {
631 // This implementation and its interface do not have the same
632 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000633 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000634 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000635 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000636 }
637 }
638 }
Mike Stump11289f42009-09-09 15:08:12 +0000639
Chris Lattnerda463fe2007-12-12 07:09:47 +0000640 if (!IDecl) {
641 // Legacy case of @implementation with no corresponding @interface.
642 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000643
Mike Stump87c57ac2009-05-16 07:39:55 +0000644 // FIXME: Do we support attributes on the @implementation? If so we should
645 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000646 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Sebastian Redlab6a0882010-08-09 21:55:28 +0000647 ClassName, ClassLoc, ODecl, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000648 IDecl->setSuperClass(SDecl);
649 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000650
651 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000652 }
Mike Stump11289f42009-09-09 15:08:12 +0000653
654 ObjCImplementationDecl* IMPDecl =
655 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000656 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000657
Anders Carlssona6b508a2008-11-04 16:57:32 +0000658 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner83f095c2009-03-28 19:18:32 +0000659 return DeclPtrTy::make(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Chris Lattnerda463fe2007-12-12 07:09:47 +0000661 // Check that there is no duplicate implementation of this class.
Sebastian Redlab6a0882010-08-09 21:55:28 +0000662 if (IDecl && IDecl->getImplementation()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000663 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000664 Diag(IDecl->getImplementation()->getLocation(),
665 diag::note_previous_definition);
Sebastian Redlab6a0882010-08-09 21:55:28 +0000666 } else {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000667 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000668 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000669 }
Sebastian Redlab6a0882010-08-09 21:55:28 +0000670
Chris Lattner83f095c2009-03-28 19:18:32 +0000671 return DeclPtrTy::make(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000672}
673
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000674void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
675 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000676 SourceLocation RBrace) {
677 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000678 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000679 if (!IDecl)
680 return;
681 /// Check case of non-existing @interface decl.
682 /// (legacy objective-c @implementation decl without an @interface decl).
683 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000684 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000685 IDecl->setLocEnd(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000686 // Add ivar's to class's DeclContext.
687 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +0000688 ivars[i]->setLexicalDeclContext(ImpDecl);
689 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000690 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000691 }
692
Chris Lattnerda463fe2007-12-12 07:09:47 +0000693 return;
694 }
695 // If implementation has empty ivar list, just return.
696 if (numIvars == 0)
697 return;
Mike Stump11289f42009-09-09 15:08:12 +0000698
Chris Lattnerda463fe2007-12-12 07:09:47 +0000699 assert(ivars && "missing @implementation ivars");
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +0000700 if (LangOpts.ObjCNonFragileABI2) {
701 if (ImpDecl->getSuperClass())
702 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
703 for (unsigned i = 0; i < numIvars; i++) {
704 ObjCIvarDecl* ImplIvar = ivars[i];
705 if (const ObjCIvarDecl *ClsIvar =
706 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
707 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
708 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
709 continue;
710 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +0000711 // Instance ivar to Implementation's DeclContext.
712 ImplIvar->setLexicalDeclContext(ImpDecl);
713 IDecl->makeDeclVisibleInContext(ImplIvar, false);
714 ImpDecl->addDecl(ImplIvar);
715 }
716 return;
717 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000718 // Check interface's Ivar list against those in the implementation.
719 // names and types must match.
720 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000721 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000722 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000723 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
724 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000725 ObjCIvarDecl* ImplIvar = ivars[j++];
726 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000727 assert (ImplIvar && "missing implementation ivar");
728 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000729
Steve Naroff157599f2009-03-03 14:49:36 +0000730 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000731 if (Context.getCanonicalType(ImplIvar->getType()) !=
732 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000733 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000734 << ImplIvar->getIdentifier()
735 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000736 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000737 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
738 Expr *ImplBitWidth = ImplIvar->getBitWidth();
739 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000740 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
741 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000742 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
743 << ImplIvar->getIdentifier();
744 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
745 }
Mike Stump11289f42009-09-09 15:08:12 +0000746 }
Steve Naroff157599f2009-03-03 14:49:36 +0000747 // Make sure the names are identical.
748 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000749 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000750 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000751 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000752 }
753 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattner0f29d982007-12-12 18:11:49 +0000756 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000757 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000758 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000759 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000760}
761
Steve Naroff15833ed2008-02-10 21:38:56 +0000762void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000763 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff15833ed2008-02-10 21:38:56 +0000764 if (!IncompleteImpl) {
765 Diag(ImpLoc, diag::warn_incomplete_impl);
766 IncompleteImpl = true;
767 }
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000768 Diag(method->getLocation(), DiagID)
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000769 << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000770}
771
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000772void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
773 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000774 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000775 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000776 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
777 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000778 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000779 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
780 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000781 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
782 }
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattner67f35b02009-04-11 19:58:42 +0000784 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
785 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
786 IM != EM; ++IM, ++IF) {
Fariborz Jahanian41e803d2009-11-18 18:56:09 +0000787 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
788 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
789 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
790 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner67f35b02009-04-11 19:58:42 +0000791 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000792
793 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000794 << ImpMethodDecl->getDeclName() << (*IF)->getType()
795 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000796 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000797 }
Fariborz Jahanian5981b042010-05-21 23:28:58 +0000798 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
799 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
800 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
801 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000802}
803
Mike Stump87c57ac2009-05-16 07:39:55 +0000804/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
805/// improve the efficiency of selector lookups and type checking by associating
806/// with each protocol / interface / category the flattened instance tables. If
807/// we used an immutable set to keep the table then it wouldn't add significant
808/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000809
Steve Naroffa36992242008-02-08 22:06:17 +0000810/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000811/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000812void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
813 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000814 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000815 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000816 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +0000817 ObjCContainerDecl *CDecl) {
818 ObjCInterfaceDecl *IDecl;
819 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
820 IDecl = C->getClassInterface();
821 else
822 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
823 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
824
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000825 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000826 ObjCInterfaceDecl *NSIDecl = 0;
827 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000828 // check to see if class implements forwardInvocation method and objects
829 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000830 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000831 // Under such conditions, which means that every method possible is
832 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000833 // found" warnings.
834 // FIXME: Use a general GetUnarySelector method for this.
835 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
836 Selector fISelector = Context.Selectors.getSelector(1, &II);
837 if (InsMap.count(fISelector))
838 // Is IDecl derived from 'NSProxy'? If so, no instance methods
839 // need be implemented in the implementation.
840 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
841 }
Mike Stump11289f42009-09-09 15:08:12 +0000842
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000843 // If a method lookup fails locally we still need to look and see if
844 // the method was implemented by a base class or an inherited
845 // protocol. This lookup is slow, but occurs rarely in correct code
846 // and otherwise would terminate in a warning.
847
Chris Lattnerda463fe2007-12-12 07:09:47 +0000848 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000849 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000850 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000851 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000852 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000853 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000854 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000855 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000856 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000857 // Ugly, but necessary. Method declared in protcol might have
858 // have been synthesized due to a property declared in the class which
859 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000860 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000861 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000862 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000863 unsigned DIAG = diag::warn_unimplemented_protocol_method;
864 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
865 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
866 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
867 << PDecl->getDeclName();
868 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000869 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000870 }
871 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000872 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +0000873 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000874 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000875 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000876 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000877 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
878 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000879 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000880 unsigned DIAG = diag::warn_unimplemented_protocol_method;
881 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
882 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
883 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
884 PDecl->getDeclName();
885 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +0000886 }
Steve Naroff3ce37a62007-12-14 23:37:57 +0000887 }
Chris Lattner390d39a2008-07-21 21:32:27 +0000888 // Check on this protocols's referenced protocols, recursively.
889 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
890 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000891 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000892}
893
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000894/// MatchAllMethodDeclarations - Check methods declaraed in interface or
895/// or protocol against those declared in their implementations.
896///
897void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
898 const llvm::DenseSet<Selector> &ClsMap,
899 llvm::DenseSet<Selector> &InsMapSeen,
900 llvm::DenseSet<Selector> &ClsMapSeen,
901 ObjCImplDecl* IMPDecl,
902 ObjCContainerDecl* CDecl,
903 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +0000904 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000905 // Check and see if instance methods in class interface have been
906 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000907 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
908 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000909 if (InsMapSeen.count((*I)->getSelector()))
910 continue;
911 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000912 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000913 !InsMap.count((*I)->getSelector())) {
914 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000915 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
916 diag::note_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000917 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +0000918 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000919 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000920 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000921 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000922 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000923 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000924 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
925 // ImpMethodDecl may be null as in a @dynamic property.
926 if (ImpMethodDecl)
927 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
928 }
929 }
Mike Stump11289f42009-09-09 15:08:12 +0000930
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000931 // Check and see if class methods in class interface have been
932 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +0000933 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000934 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000935 if (ClsMapSeen.count((*I)->getSelector()))
936 continue;
937 ClsMapSeen.insert((*I)->getSelector());
938 if (!ClsMap.count((*I)->getSelector())) {
939 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +0000940 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
941 diag::note_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +0000942 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000943 ObjCMethodDecl *ImpMethodDecl =
944 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000945 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000946 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000947 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
948 }
949 }
950 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
951 // Check for any implementation of a methods declared in protocol.
952 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
953 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +0000954 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
955 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000956 (*PI), IncompleteImpl, false);
957 if (I->getSuperClass())
958 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +0000959 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000960 I->getSuperClass(), IncompleteImpl, false);
961 }
962}
963
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000964void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000965 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +0000966 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000967 llvm::DenseSet<Selector> InsMap;
968 // Check and see if instance methods in class interface have been
969 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000970 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000971 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000972 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000973
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +0000974 // Check and see if properties declared in the interface have either 1)
975 // an implementation or 2) there is a @synthesize/@dynamic implementation
976 // of the property in the @implementation.
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +0000977 if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000978 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian98609b32010-01-20 01:51:55 +0000979
Chris Lattnerda463fe2007-12-12 07:09:47 +0000980 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +0000981 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000982 I = IMPDecl->classmeth_begin(),
983 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +0000984 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000985
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000986 // Check for type conflict of methods declared in a class/protocol and
987 // its implementation; if any.
988 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +0000989 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
990 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000991 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +0000992
Chris Lattnerda463fe2007-12-12 07:09:47 +0000993 // Check the protocol list for unimplemented methods in the @implementation
994 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000995 // Check and see if class methods in class interface have been
996 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +0000997
Chris Lattner9ef10f42009-03-01 00:56:52 +0000998 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000999 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +00001000 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001001 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001002 InsMap, ClsMap, I);
1003 // Check class extensions (unnamed categories)
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001004 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1005 Categories; Categories = Categories->getNextClassExtension())
1006 ImplMethodsVsClassMethods(S, IMPDecl,
1007 const_cast<ObjCCategoryDecl*>(Categories),
1008 IncompleteImpl);
Chris Lattner9ef10f42009-03-01 00:56:52 +00001009 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001010 // For extended class, unimplemented methods in its protocols will
1011 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001012 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001013 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1014 E = C->protocol_end(); PI != E; ++PI)
1015 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001016 InsMap, ClsMap, CDecl);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001017 // Report unimplemented properties in the category as well.
1018 // When reporting on missing setter/getters, do not report when
1019 // setter/getter is implemented in category's primary class
1020 // implementation.
1021 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1022 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1023 for (ObjCImplementationDecl::instmeth_iterator
1024 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1025 InsMap.insert((*I)->getSelector());
1026 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001027 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001028 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001029 } else
1030 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001031}
1032
Mike Stump11289f42009-09-09 15:08:12 +00001033/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +00001034Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001035Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001036 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001037 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001038 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001039 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +00001040
Chris Lattnerda463fe2007-12-12 07:09:47 +00001041 for (unsigned i = 0; i != NumElts; ++i) {
1042 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001043 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001044 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001045 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5daeee22008-12-08 18:40:42 +00001046 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001047 // Maybe we will complain about the shadowed template parameter.
1048 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1049 // Just pretend that we didn't see the previous declaration.
1050 PrevDecl = 0;
1051 }
1052
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001053 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001054 // GCC apparently allows the following idiom:
1055 //
1056 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1057 // @class XCElementToggler;
1058 //
Mike Stump11289f42009-09-09 15:08:12 +00001059 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001060 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00001061 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001062 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001063 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00001064 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00001065 // a forward class declaration matching a typedef name of a class refers
1066 // to the underlying class.
John McCall8b07ec22010-05-15 11:32:37 +00001067 if (const ObjCObjectType *OI =
1068 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1069 PrevDecl = OI->getInterface();
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001070 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001071 }
Sebastian Redlab6a0882010-08-09 21:55:28 +00001072 ObjCInterfaceDecl *ODecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1073 ObjCInterfaceDecl *IDecl =
1074 ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1075 IdentList[i], IdentLocs[i], ODecl, true);
1076
1077 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1078 // the current DeclContext. This prevents clients that walk DeclContext
1079 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1080 // declared later (if at all). We also take care to explicitly make
1081 // sure this declaration is visible for name lookup.
1082 PushOnScopeChains(IDecl, TUScope, false);
1083 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001084
1085 Interfaces.push_back(IDecl);
1086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
Ted Kremenek9b124e12009-11-18 00:28:11 +00001088 assert(Interfaces.size() == NumElts);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001089 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001090 Interfaces.data(), IdentLocs,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001091 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001092 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001093 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001094 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001095}
1096
1097
1098/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1099/// returns true, or false, accordingly.
1100/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001101bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001102 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001103 bool matchBasedOnSizeAndAlignment,
1104 bool matchBasedOnStrictEqulity) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001105 QualType T1 = Context.getCanonicalType(Method->getResultType());
1106 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001107
Steve Naroffa0ed1652008-10-21 10:37:50 +00001108 if (T1 != T2) {
1109 // The result types are different.
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001110 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001111 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001112 // Incomplete types don't have a size and alignment.
1113 if (T1->isIncompleteType() || T2->isIncompleteType())
1114 return false;
1115 // Check is based on size and alignment.
1116 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1117 return false;
1118 }
Mike Stump11289f42009-09-09 15:08:12 +00001119
Chris Lattnera4997152009-02-20 18:43:26 +00001120 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1121 E = Method->param_end();
1122 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001123
Chris Lattnera4997152009-02-20 18:43:26 +00001124 for (; ParamI != E; ++ParamI, ++PrevI) {
1125 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1126 T1 = Context.getCanonicalType((*ParamI)->getType());
1127 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001128 if (T1 != T2) {
1129 // The result types are different.
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001130 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Naroffa0ed1652008-10-21 10:37:50 +00001131 return false;
1132 // Incomplete types don't have a size and alignment.
1133 if (T1->isIncompleteType() || T2->isIncompleteType())
1134 return false;
1135 // Check is based on size and alignment.
1136 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1137 return false;
1138 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001139 }
1140 return true;
1141}
1142
Sebastian Redl75d8a322010-08-02 23:18:59 +00001143/// \brief Read the contents of the method pool for a given selector from
1144/// external storage.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001145///
Sebastian Redl75d8a322010-08-02 23:18:59 +00001146/// This routine should only be called once, when the method pool has no entry
1147/// for this selector.
1148Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001149 assert(ExternalSource && "We need an external AST source");
Sebastian Redl75d8a322010-08-02 23:18:59 +00001150 assert(MethodPool.find(Sel) == MethodPool.end() &&
1151 "Selector data already loaded into the method pool");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001152
1153 // Read the method list from the external source.
Sebastian Redl75d8a322010-08-02 23:18:59 +00001154 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001155
Sebastian Redl75d8a322010-08-02 23:18:59 +00001156 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001157}
1158
Sebastian Redl75d8a322010-08-02 23:18:59 +00001159void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1160 bool instance) {
1161 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1162 if (Pos == MethodPool.end()) {
1163 if (ExternalSource)
1164 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001165 else
Sebastian Redl75d8a322010-08-02 23:18:59 +00001166 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1167 GlobalMethods())).first;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001168 }
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001169 Method->setDefined(impl);
Sebastian Redl75d8a322010-08-02 23:18:59 +00001170 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001171 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001172 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001173 Entry.Method = Method;
1174 Entry.Next = 0;
1175 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001176 }
Mike Stump11289f42009-09-09 15:08:12 +00001177
Chris Lattner7b26b292009-03-04 05:16:45 +00001178 // We've seen a method with this name, see if we have already seen this type
1179 // signature.
1180 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001181 if (MatchTwoMethodDeclarations(Method, List->Method)) {
1182 List->Method->setDefined(impl);
Chris Lattner7b26b292009-03-04 05:16:45 +00001183 return;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001184 }
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattner7b26b292009-03-04 05:16:45 +00001186 // We have a new signature for an existing method - add it.
1187 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001188 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1189 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001190}
1191
Sebastian Redl75d8a322010-08-02 23:18:59 +00001192ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001193 bool receiverIdOrClass,
Sebastian Redl75d8a322010-08-02 23:18:59 +00001194 bool warn, bool instance) {
1195 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1196 if (Pos == MethodPool.end()) {
1197 if (ExternalSource)
1198 Pos = ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001199 else
1200 return 0;
1201 }
1202
Sebastian Redl75d8a322010-08-02 23:18:59 +00001203 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump11289f42009-09-09 15:08:12 +00001204
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001205 bool strictSelectorMatch = receiverIdOrClass && warn &&
1206 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl) !=
1207 Diagnostic::Ignored);
Sebastian Redl75d8a322010-08-02 23:18:59 +00001208 if (warn && MethList.Method && MethList.Next) {
1209 bool issueWarning = false;
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001210 if (strictSelectorMatch)
1211 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1212 // This checks if the methods differ in type mismatch.
1213 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1214 issueWarning = true;
1215 }
1216
1217 if (!issueWarning)
1218 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1219 // This checks if the methods differ by size & alignment.
1220 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1221 issueWarning = true;
1222 }
1223
Sebastian Redl75d8a322010-08-02 23:18:59 +00001224 if (issueWarning) {
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001225 if (strictSelectorMatch)
1226 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1227 else
1228 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redl75d8a322010-08-02 23:18:59 +00001229 Diag(MethList.Method->getLocStart(), diag::note_using)
1230 << MethList.Method->getSourceRange();
1231 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1232 Diag(Next->Method->getLocStart(), diag::note_also_found)
1233 << Next->Method->getSourceRange();
1234 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001235 }
1236 return MethList.Method;
1237}
1238
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001239ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00001240 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1241 if (Pos == MethodPool.end())
1242 return 0;
1243
1244 GlobalMethods &Methods = Pos->second;
1245
1246 if (Methods.first.Method && Methods.first.Method->isDefined())
1247 return Methods.first.Method;
1248 if (Methods.second.Method && Methods.second.Method->isDefined())
1249 return Methods.second.Method;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001250 return 0;
1251}
1252
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001253/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1254/// identical selector names in current and its super classes and issues
1255/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001256void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1257 ObjCMethodDecl *Method,
1258 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001259 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1260 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001261
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001262 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001263 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001264 SD->lookupMethod(Method->getSelector(), IsInstance);
1265 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001266 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001267 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001268 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001269 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1270 E = Method->param_end();
1271 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1272 for (; ParamI != E; ++ParamI, ++PrevI) {
1273 // Number of parameters are the same and is guaranteed by selector match.
1274 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1275 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1276 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1277 // If type of arguement of method in this class does not match its
1278 // respective argument type in the super class method, issue warning;
1279 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001280 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001281 << T1 << T2;
1282 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1283 return;
1284 }
1285 }
1286 ID = SD;
1287 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001288}
1289
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001290/// DiagnoseDuplicateIvars -
1291/// Check for duplicate ivars in the entire class at the start of
1292/// @implementation. This becomes necesssary because class extension can
1293/// add ivars to a class in random order which will not be known until
1294/// class's @implementation is seen.
1295void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1296 ObjCInterfaceDecl *SID) {
1297 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1298 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1299 ObjCIvarDecl* Ivar = (*IVI);
1300 if (Ivar->isInvalidDecl())
1301 continue;
1302 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1303 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1304 if (prevIvar) {
1305 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1306 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1307 Ivar->setInvalidDecl();
1308 }
1309 }
1310 }
1311}
1312
Steve Naroff1d2538c2007-12-18 01:30:32 +00001313// Note: For class/category implemenations, allMethods/allProperties is
1314// always null.
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001315void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
Ted Kremenekc7c64312010-01-07 01:20:12 +00001316 DeclPtrTy classDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +00001317 DeclPtrTy *allMethods, unsigned allNum,
1318 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001319 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001320 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001321
Steve Naroff1d2538c2007-12-18 01:30:32 +00001322 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1323 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001324 // should be true.
1325 if (!ClassDecl)
1326 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001327
Mike Stump11289f42009-09-09 15:08:12 +00001328 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001329 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1330 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001331 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001332
Ted Kremenekc7c64312010-01-07 01:20:12 +00001333 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1334 // FIXME: This is wrong. We shouldn't be pretending that there is
1335 // an '@end' in the declaration.
1336 SourceLocation L = ClassDecl->getLocation();
1337 AtEnd.setBegin(L);
1338 AtEnd.setEnd(L);
1339 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001340 }
1341
Steve Naroff35c62ae2009-01-08 17:28:14 +00001342 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001343
1344 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1345 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1346 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1347
Chris Lattnerda463fe2007-12-12 07:09:47 +00001348 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001349 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001350 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001351
1352 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001353 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001354 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001355 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001356 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001357 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001358 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001359 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001360 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001361 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001362 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001363 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001364 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001365 InsMap[Method->getSelector()] = Method;
1366 /// The following allows us to typecheck messages to "id".
1367 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001368 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001369 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001370 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001371 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001372 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001373 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001374 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001375 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001376 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001377 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001378 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001379 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001380 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001381 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001382 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001383 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001384 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001385 /// The following allows us to typecheck messages to "Class".
1386 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001387 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001388 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001389 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001390 }
1391 }
1392 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001393 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001394 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001395 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001396 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001397 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001398 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001399 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001400 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001401 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001402
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001403 // Compare protocol properties with those in category
1404 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001405 if (C->IsClassExtension())
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001406 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001407 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001408 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001409 if (CDecl->getIdentifier())
1410 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1411 // user-defined setter/getter. It also synthesizes setter/getter methods
1412 // and adds them to the DeclContext and global method pools.
1413 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1414 E = CDecl->prop_end();
1415 I != E; ++I)
1416 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001417 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00001418 }
1419 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001420 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001421 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001422 if (LangOpts.ObjCNonFragileABI2)
1423 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001424 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001425 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian545643c2010-02-23 23:41:11 +00001426 if (LangOpts.ObjCNonFragileABI2)
1427 while (IDecl->getSuperClass()) {
1428 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1429 IDecl = IDecl->getSuperClass();
1430 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001431 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001432 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00001433 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001434 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001435 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00001436
Chris Lattnerda463fe2007-12-12 07:09:47 +00001437 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001438 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001439 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001440 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001441 Categories; Categories = Categories->getNextClassCategory()) {
1442 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001443 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001444 break;
1445 }
1446 }
1447 }
1448 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001449 if (isInterfaceDeclKind) {
1450 // Reject invalid vardecls.
1451 for (unsigned i = 0; i != tuvNum; i++) {
1452 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1453 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1454 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001455 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001456 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001457 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001458 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001459 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001460}
1461
1462
1463/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1464/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001465static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001466CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1467 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1468 if (PQTVal & ObjCDeclSpec::DQ_In)
1469 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1470 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1471 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1472 if (PQTVal & ObjCDeclSpec::DQ_Out)
1473 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1474 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1475 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1476 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1477 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1478 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1479 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001480
1481 return ret;
1482}
1483
Ted Kremenek36712b22010-04-18 04:59:38 +00001484static inline
1485bool containsInvalidMethodImplAttribute(const AttributeList *A) {
1486 // The 'ibaction' attribute is allowed on method definitions because of
1487 // how the IBAction macro is used on both method declarations and definitions.
1488 // If the method definitions contains any other attributes, return true.
1489 while (A && A->getKind() == AttributeList::AT_IBAction)
1490 A = A->getNext();
1491 return A != NULL;
1492}
1493
Chris Lattner83f095c2009-03-28 19:18:32 +00001494Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001495 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001496 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001497 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001498 Selector Sel,
1499 // optional arguments. The number of types/arguments is obtained
1500 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001501 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001502 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00001503 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1504 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001505 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001506
1507 // Make sure we can establish a context for the method.
1508 if (!ClassDecl) {
1509 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9a28e842010-03-01 23:15:13 +00001510 getLabelMap().clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001511 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001512 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001513 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001514
Douglas Gregor12852d92010-03-08 14:59:44 +00001515 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00001516 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00001517 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001518
Steve Naroff32606412009-02-20 22:59:16 +00001519 // Methods cannot return interface types. All ObjC objects are
1520 // passed by reference.
John McCall8b07ec22010-05-15 11:32:37 +00001521 if (resultDeclType->isObjCObjectType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001522 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1523 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001524 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001525 }
1526 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001527 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001528
1529 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001530 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00001531 ResultTInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001532 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001533 MethodType == tok::minus, isVariadic,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001534 false, false,
Mike Stump11289f42009-09-09 15:08:12 +00001535 MethodDeclKind == tok::objc_optional ?
1536 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001537 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001538
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001539 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001540
Chris Lattner23b0faf2009-04-11 19:42:43 +00001541 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001542 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00001543 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001544
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001545 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001546 ArgType = Context.getObjCIdType();
1547 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001548 } else {
John McCall856bbea2009-10-23 21:48:59 +00001549 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001550 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001551 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001552 }
Mike Stump11289f42009-09-09 15:08:12 +00001553
John McCall856bbea2009-10-23 21:48:59 +00001554 ParmVarDecl* Param
1555 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1556 ArgInfo[i].Name, ArgType, DI,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001557 VarDecl::None, VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001558
John McCall8b07ec22010-05-15 11:32:37 +00001559 if (ArgType->isObjCObjectType()) {
Chris Lattner9713a1c2009-04-11 19:34:56 +00001560 Diag(ArgInfo[i].NameLoc,
1561 diag::err_object_cannot_be_passed_returned_by_value)
1562 << 1 << ArgType;
1563 Param->setInvalidDecl();
1564 }
Mike Stump11289f42009-09-09 15:08:12 +00001565
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001566 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001567 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001568
Chris Lattner9713a1c2009-04-11 19:34:56 +00001569 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001570 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001571
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001572 Params.push_back(Param);
1573 }
1574
Fariborz Jahanian60462092010-04-08 00:30:06 +00001575 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
1576 ParmVarDecl *Param = CParamInfo[i].Param.getAs<ParmVarDecl>();
1577 QualType ArgType = Param->getType();
1578 if (ArgType.isNull())
1579 ArgType = Context.getObjCIdType();
1580 else
1581 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1582 ArgType = adjustParameterType(ArgType);
John McCall8b07ec22010-05-15 11:32:37 +00001583 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian60462092010-04-08 00:30:06 +00001584 Diag(Param->getLocation(),
1585 diag::err_object_cannot_be_passed_returned_by_value)
1586 << 1 << ArgType;
1587 Param->setInvalidDecl();
1588 }
1589 Param->setDeclContext(ObjCMethod);
Douglas Gregor114e55d2010-08-07 12:29:18 +00001590 if (Param->getDeclName())
1591 IdResolver.RemoveDecl(Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001592 Params.push_back(Param);
1593 }
1594
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00001595 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1596 Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001597 ObjCMethod->setObjCDeclQualifier(
1598 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1599 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001600
1601 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001602 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001603
John McCall28a6aea2009-11-04 02:18:39 +00001604 const ObjCMethodDecl *InterfaceMD = 0;
1605
Mike Stump11289f42009-09-09 15:08:12 +00001606 // For implementations (which can be very "coarse grain"), we add the
1607 // method now. This allows the AST to implement lookup methods that work
1608 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001609 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001610 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001611 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001612 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001613 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1614 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001615 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001616 PrevMethod = ImpDecl->getClassMethod(Sel);
1617 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001618 }
John McCall28a6aea2009-11-04 02:18:39 +00001619 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1620 MethodType == tok::minus);
Ted Kremenek36712b22010-04-18 04:59:38 +00001621 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001622 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001623 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001624 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001625 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001626 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1627 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001628 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001629 PrevMethod = CatImpDecl->getClassMethod(Sel);
1630 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001631 }
Ted Kremenek36712b22010-04-18 04:59:38 +00001632 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001633 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001634 }
1635 if (PrevMethod) {
1636 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001637 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001638 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001639 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001640 }
John McCall28a6aea2009-11-04 02:18:39 +00001641
1642 // If the interface declared this method, and it was deprecated there,
1643 // mark it deprecated here.
1644 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1645 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1646
Chris Lattner83f095c2009-03-28 19:18:32 +00001647 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001648}
1649
Chris Lattner438e5012008-12-17 07:13:27 +00001650bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001651 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00001652 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001653
Anders Carlssona6b508a2008-11-04 16:57:32 +00001654 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1655 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001656
Anders Carlssona6b508a2008-11-04 16:57:32 +00001657 return true;
1658}
Chris Lattner438e5012008-12-17 07:13:27 +00001659
Chris Lattner438e5012008-12-17 07:13:27 +00001660/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1661/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00001662void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00001663 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00001664 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00001665 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001666 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00001667 if (!Class) {
1668 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1669 return;
1670 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00001671 if (LangOpts.ObjCNonFragileABI) {
1672 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1673 return;
1674 }
Mike Stump11289f42009-09-09 15:08:12 +00001675
Chris Lattner438e5012008-12-17 07:13:27 +00001676 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00001677 llvm::SmallVector<FieldDecl*, 32> RecFields;
1678 Context.CollectObjCIvars(Class, RecFields);
1679 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1680 for (unsigned i = 0; i < RecFields.size(); i++) {
1681 FieldDecl* ID = RecFields[i];
1682 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1683 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1684 ID->getIdentifier(), ID->getType(),
1685 ID->getBitWidth());
1686 Decls.push_back(Sema::DeclPtrTy::make(FD));
1687 }
Mike Stump11289f42009-09-09 15:08:12 +00001688
Chris Lattner438e5012008-12-17 07:13:27 +00001689 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00001690 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00001691 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001692 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00001693 if (getLangOptions().CPlusPlus)
1694 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00001695 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001696 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00001697 }
1698}
1699
Douglas Gregorf3564192010-04-26 17:32:49 +00001700/// \brief Build a type-check a new Objective-C exception variable declaration.
1701VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1702 QualType T,
1703 IdentifierInfo *Name,
1704 SourceLocation NameLoc,
1705 bool Invalid) {
1706 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1707 // duration shall not be qualified by an address-space qualifier."
1708 // Since all parameters have automatic store duration, they can not have
1709 // an address space.
1710 if (T.getAddressSpace() != 0) {
1711 Diag(NameLoc, diag::err_arg_with_address_space);
1712 Invalid = true;
1713 }
1714
1715 // An @catch parameter must be an unqualified object pointer type;
1716 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1717 if (Invalid) {
1718 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00001719 } else if (T->isDependentType()) {
1720 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00001721 } else if (!T->isObjCObjectPointerType()) {
1722 Invalid = true;
1723 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1724 } else if (T->isObjCQualifiedIdType()) {
1725 Invalid = true;
1726 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1727 }
1728
1729 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
Douglas Gregor3f324d562010-05-03 18:51:14 +00001730 VarDecl::None, VarDecl::None);
1731 New->setExceptionVariable(true);
1732
Douglas Gregorf3564192010-04-26 17:32:49 +00001733 if (Invalid)
1734 New->setInvalidDecl();
1735 return New;
1736}
1737
Douglas Gregore11ee112010-04-23 23:01:43 +00001738Sema::DeclPtrTy Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00001739 const DeclSpec &DS = D.getDeclSpec();
1740
1741 // We allow the "register" storage class on exception variables because
1742 // GCC did, but we drop it completely. Any other storage class is an error.
1743 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1744 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1745 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1746 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1747 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1748 << DS.getStorageClassSpec();
1749 }
1750 if (D.getDeclSpec().isThreadSpecified())
1751 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1752 D.getMutableDeclSpec().ClearStorageClassSpecs();
1753
1754 DiagnoseFunctionSpecifiers(D);
1755
1756 // Check that there are no default arguments inside the type of this
1757 // exception object (C++ only).
1758 if (getLangOptions().CPlusPlus)
1759 CheckExtraCXXDefaultArguments(D);
1760
Douglas Gregorf3564192010-04-26 17:32:49 +00001761 TagDecl *OwnedDecl = 0;
John McCall8cb7bdf2010-06-04 23:28:52 +00001762 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1763 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00001764
1765 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1766 // Objective-C++: Types shall not be defined in exception types.
1767 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1768 << Context.getTypeDeclType(OwnedDecl);
1769 }
1770
1771 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
1772 D.getIdentifierLoc(),
1773 D.isInvalidType());
1774
1775 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
1776 if (D.getCXXScopeSpec().isSet()) {
1777 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
1778 << D.getCXXScopeSpec().getRange();
1779 New->setInvalidDecl();
1780 }
1781
1782 // Add the parameter declaration into this scope.
1783 S->AddDecl(DeclPtrTy::make(New));
1784 if (D.getIdentifier())
1785 IdResolver.AddDecl(New);
1786
1787 ProcessDeclAttributes(S, New, D);
1788
1789 if (New->hasAttr<BlocksAttr>())
1790 Diag(New->getLocation(), diag::err_block_on_nonlocal);
1791 return DeclPtrTy::make(New);
Douglas Gregore11ee112010-04-23 23:01:43 +00001792}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001793
1794/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001795/// initialization.
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001796void Sema::CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI,
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001797 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001798 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1799 E = OI->ivar_end(); I != E; ++I) {
1800 ObjCIvarDecl *Iv = (*I);
1801 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00001802 if (QT->isRecordType())
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001803 Ivars.push_back(*I);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001804 }
1805
1806 // Find ivars to construct/destruct in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001807 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
1808 CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001809 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1810 E = CDecl->ivar_end(); I != E; ++I) {
1811 ObjCIvarDecl *Iv = (*I);
1812 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00001813 if (QT->isRecordType())
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001814 Ivars.push_back(*I);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001815 }
1816 }
1817
1818 // Also add any ivar defined in this class's implementation. This
1819 // includes synthesized ivars.
1820 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
1821 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1822 E = ImplDecl->ivar_end(); I != E; ++I) {
1823 ObjCIvarDecl *Iv = (*I);
1824 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00001825 if (QT->isRecordType())
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001826 Ivars.push_back(*I);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00001827 }
1828 }
1829}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00001830
1831void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1832 CXXBaseOrMemberInitializer ** initializers,
1833 unsigned numInitializers) {
1834 if (numInitializers > 0) {
1835 NumIvarInitializers = numInitializers;
1836 CXXBaseOrMemberInitializer **ivarInitializers =
1837 new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
1838 memcpy(ivarInitializers, initializers,
1839 numInitializers * sizeof(CXXBaseOrMemberInitializer*));
1840 IvarInitializers = ivarInitializers;
1841 }
1842}
1843
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001844void Sema::DiagnoseUseOfUnimplementedSelectors() {
1845 if (ReferencedSelectors.empty())
1846 return;
1847 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
1848 ReferencedSelectors.begin(),
1849 E = ReferencedSelectors.end(); S != E; ++S) {
1850 Selector Sel = (*S).first;
1851 if (!LookupImplementedMethodInGlobalPool(Sel))
1852 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
1853 }
1854 return;
1855}