blob: d28a910d0a53e8243379fb87ef21522c4a62f2f8 [file] [log] [blame]
Chris Lattner1e03a562008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +000017#include "clang/AST/ASTMutationListener.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000018#include "llvm/ADT/STLExtras.h"
Anna Zaksad0ce532012-09-27 19:45:11 +000019#include "llvm/ADT/SmallString.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000020using namespace clang;
21
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000022//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000023// ObjCListBase
24//===----------------------------------------------------------------------===//
25
Chris Lattner38af2de2009-02-20 21:35:13 +000026void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000027 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000028 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000029
30
Chris Lattner4ee413b2009-02-20 21:44:01 +000031 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000032 NumElts = Elts;
33 memcpy(List, InList, sizeof(void*)*Elts);
34}
35
Douglas Gregor18df52b2010-01-16 15:02:53 +000036void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
37 const SourceLocation *Locs, ASTContext &Ctx) {
38 if (Elts == 0)
39 return;
40
41 Locations = new (Ctx) SourceLocation[Elts];
42 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
43 set(InList, Elts, Ctx);
44}
45
Chris Lattner11e1e1a2009-02-20 21:16:26 +000046//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000047// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000048//===----------------------------------------------------------------------===//
49
David Blaikie99ba9e32011-12-20 02:48:34 +000050void ObjCContainerDecl::anchor() { }
51
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000052/// getIvarDecl - This method looks up an ivar in this ContextDecl.
53///
54ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000055ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000056 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000057 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000058 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
59 return ivar;
60 }
61 return 0;
62}
63
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000064// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000065ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000066ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000067 // Since instance & class methods can have the same name, the loop below
68 // ensures we get the correct method.
69 //
70 // @interface Whatever
71 // - (int) class_method;
72 // + (float) class_method;
73 // @end
74 //
75 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000076 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000077 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000078 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000079 return MD;
80 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000081 return 0;
82}
83
Ted Kremenek9f550ff2010-03-15 20:11:46 +000084ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000085ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000086 IdentifierInfo *propertyID) {
87
Ted Kremenekde09d0c2010-03-15 20:11:53 +000088 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000089 llvm::tie(I, E) = DC->lookup(propertyID);
90 for ( ; I != E; ++I)
91 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
92 return PD;
93
94 return 0;
95}
96
Anna Zaksad0ce532012-09-27 19:45:11 +000097IdentifierInfo *
98ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
99 SmallString<128> ivarName;
100 {
101 llvm::raw_svector_ostream os(ivarName);
102 os << '_' << getIdentifier()->getName();
103 }
104 return &Ctx.Idents.get(ivarName.str());
105}
106
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000107/// FindPropertyDeclaration - Finds declaration of the property given its name
108/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000109ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000110ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000112 if (ObjCPropertyDecl *PD =
113 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
114 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000116 switch (getKind()) {
117 default:
118 break;
119 case Decl::ObjCProtocol: {
120 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
121 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
122 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000123 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
124 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000125 break;
126 }
127 case Decl::ObjCInterface: {
128 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
129 // Look through categories.
130 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
131 Cat; Cat = Cat->getNextClassCategory())
132 if (!Cat->IsClassExtension())
133 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
134 return P;
135
136 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000137 for (ObjCInterfaceDecl::all_protocol_iterator
138 I = OID->all_referenced_protocol_begin(),
139 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000140 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
141 return P;
142
143 // Finally, check the super class.
144 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
145 return superClass->FindPropertyDeclaration(PropertyId);
146 break;
147 }
148 case Decl::ObjCCategory: {
149 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
150 // Look through protocols.
151 if (!OCD->IsClassExtension())
152 for (ObjCCategoryDecl::protocol_iterator
153 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
154 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
155 return P;
156
157 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000158 }
159 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000160 return 0;
161}
162
David Blaikie99ba9e32011-12-20 02:48:34 +0000163void ObjCInterfaceDecl::anchor() { }
164
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000165/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
166/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000167/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000168///
169ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000170ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000171 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000172 // FIXME: Should make sure no callers ever do this.
173 if (!hasDefinition())
174 return 0;
175
176 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000177 LoadExternalDefinition();
178
Ted Kremenek37cafb02010-03-15 20:30:07 +0000179 if (ObjCPropertyDecl *PD =
180 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
181 return PD;
182
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000183 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000184 for (ObjCInterfaceDecl::all_protocol_iterator
185 I = all_referenced_protocol_begin(),
186 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000187 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
188 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000189
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000190 return 0;
191}
192
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000193void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
194 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
195 ASTContext &C)
196{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000197 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000198 LoadExternalDefinition();
199
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000200 if (data().AllReferencedProtocols.empty() &&
201 data().ReferencedProtocols.empty()) {
202 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000203 return;
204 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000205
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000206 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000207 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000208 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000210 for (unsigned i = 0; i < ExtNum; i++) {
211 bool protocolExists = false;
212 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000213 for (all_protocol_iterator
214 p = all_referenced_protocol_begin(),
215 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000216 ObjCProtocolDecl *Proto = (*p);
217 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
218 protocolExists = true;
219 break;
220 }
221 }
222 // Do we want to warn on a protocol in extension class which
223 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000224 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000225 ProtocolRefs.push_back(ProtoInExtension);
226 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000227
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000228 if (ProtocolRefs.empty())
229 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000230
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000231 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000232 for (all_protocol_iterator p = all_referenced_protocol_begin(),
233 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000234 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000235 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000236
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000237 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000238}
239
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000240void ObjCInterfaceDecl::allocateDefinitionData() {
241 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor26fec632011-12-15 18:17:27 +0000242 Data = new (getASTContext()) DefinitionData();
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000243 Data->Definition = this;
244
245 // Make the type point at the definition, now that we have one.
246 if (TypeForDecl)
247 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000248}
249
250void ObjCInterfaceDecl::startDefinition() {
251 allocateDefinitionData();
252
Douglas Gregor53df7a12011-12-15 18:03:09 +0000253 // Update all of the declarations with a pointer to the definition.
254 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
255 RD != RDEnd; ++RD) {
256 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000257 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000258 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000259}
260
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000261/// getFirstClassExtension - Find first class extension of the given class.
262ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
263 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000264 CDecl = CDecl->getNextClassCategory())
265 if (CDecl->IsClassExtension())
266 return CDecl;
267 return 0;
268}
269
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000270/// getNextClassCategory - Find next class extension in list of categories.
271const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
272 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
273 CDecl = CDecl->getNextClassCategory())
274 if (CDecl->IsClassExtension())
275 return CDecl;
276 return 0;
277}
278
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000279ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
280 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000281 // FIXME: Should make sure no callers ever do this.
282 if (!hasDefinition())
283 return 0;
284
285 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000286 LoadExternalDefinition();
287
Chris Lattner1e03a562008-03-16 00:19:01 +0000288 ObjCInterfaceDecl* ClassDecl = this;
289 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000290 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000291 clsDeclared = ClassDecl;
292 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000293 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000294 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
295 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000296 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
297 clsDeclared = ClassDecl;
298 return I;
299 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000300 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000301
Chris Lattner1e03a562008-03-16 00:19:01 +0000302 ClassDecl = ClassDecl->getSuperClass();
303 }
304 return NULL;
305}
306
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000307/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
308/// class whose name is passed as argument. If it is not one of the super classes
309/// the it returns NULL.
310ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
311 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000312 // FIXME: Should make sure no callers ever do this.
313 if (!hasDefinition())
314 return 0;
315
316 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000317 LoadExternalDefinition();
318
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000319 ObjCInterfaceDecl* ClassDecl = this;
320 while (ClassDecl != NULL) {
321 if (ClassDecl->getIdentifier() == ICName)
322 return ClassDecl;
323 ClassDecl = ClassDecl->getSuperClass();
324 }
325 return NULL;
326}
327
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000328/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000329/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000330ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
331 bool isInstance,
332 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000333 // FIXME: Should make sure no callers ever do this.
334 if (!hasDefinition())
335 return 0;
336
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000337 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000338 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000340 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000341 LoadExternalDefinition();
342
Chris Lattner1e03a562008-03-16 00:19:01 +0000343 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000344 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000345 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner1e03a562008-03-16 00:19:01 +0000347 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000348 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
349 E = ClassDecl->protocol_end();
350 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000351 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000352 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000353
354 // Didn't find one yet - now look through categories.
355 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
356 while (CatDecl) {
357 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
358 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000359
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000360 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000361 // Didn't find one yet - look through protocols.
362 const ObjCList<ObjCProtocolDecl> &Protocols =
363 CatDecl->getReferencedProtocols();
364 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
365 E = Protocols.end(); I != E; ++I)
366 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
367 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000368 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000369 CatDecl = CatDecl->getNextClassCategory();
Chris Lattner1e03a562008-03-16 00:19:01 +0000370 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000371
Chris Lattner1e03a562008-03-16 00:19:01 +0000372 ClassDecl = ClassDecl->getSuperClass();
373 }
374 return NULL;
375}
376
Anna Zakse61354b2012-07-27 19:07:44 +0000377// Will search "local" class/category implementations for a method decl.
378// If failed, then we search in class's root for an instance method.
379// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000380ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
381 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000382 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000383 // FIXME: Should make sure no callers ever do this.
384 if (!hasDefinition())
385 return 0;
386
387 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000388 LoadExternalDefinition();
389
Steve Naroffd789d3d2009-10-01 23:46:04 +0000390 ObjCMethodDecl *Method = 0;
391 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000392 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
393 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000394
395 // Look through local category implementations associated with the class.
396 if (!Method)
397 Method = Instance ? getCategoryInstanceMethod(Sel)
398 : getCategoryClassMethod(Sel);
399
400 // Before we give up, check if the selector is an instance method.
401 // But only in the root. This matches gcc's behavior and what the
402 // runtime expects.
403 if (!Instance && !Method && !getSuperClass()) {
404 Method = lookupInstanceMethod(Sel);
405 // Look through local category implementations associated
406 // with the root class.
407 if (!Method)
408 Method = lookupPrivateMethod(Sel, true);
409 }
410
Steve Naroffd789d3d2009-10-01 23:46:04 +0000411 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000412 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000413 return Method;
414}
Chris Lattnerab351632009-02-20 20:59:54 +0000415
416//===----------------------------------------------------------------------===//
417// ObjCMethodDecl
418//===----------------------------------------------------------------------===//
419
420ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000421 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000422 SourceLocation endLoc,
423 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000424 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000425 DeclContext *contextDecl,
426 bool isInstance,
427 bool isVariadic,
428 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000429 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000430 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000431 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000432 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000433 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000434 SelInfo, T, ResultTInfo, contextDecl,
435 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000436 isVariadic, isSynthesized, isImplicitlyDeclared,
437 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000438 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000439 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000440}
441
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000442ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
443 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
444 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
445 Selector(), QualType(), 0, 0);
446}
447
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000448void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
449 assert(PrevMethod);
450 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
451 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000452 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000453}
454
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000455void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
456 ArrayRef<ParmVarDecl*> Params,
457 ArrayRef<SourceLocation> SelLocs) {
458 ParamsAndSelLocs = 0;
459 NumParams = Params.size();
460 if (Params.empty() && SelLocs.empty())
461 return;
462
463 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
464 sizeof(SourceLocation) * SelLocs.size();
465 ParamsAndSelLocs = C.Allocate(Size);
466 std::copy(Params.begin(), Params.end(), getParams());
467 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
468}
469
470void ObjCMethodDecl::getSelectorLocs(
471 SmallVectorImpl<SourceLocation> &SelLocs) const {
472 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
473 SelLocs.push_back(getSelectorLoc(i));
474}
475
476void ObjCMethodDecl::setMethodParams(ASTContext &C,
477 ArrayRef<ParmVarDecl*> Params,
478 ArrayRef<SourceLocation> SelLocs) {
479 assert((!SelLocs.empty() || isImplicit()) &&
480 "No selector locs for non-implicit method");
481 if (isImplicit())
482 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
483
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000484 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
485 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000486 if (SelLocsKind != SelLoc_NonStandard)
487 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
488
489 setParamsAndSelLocs(C, Params, SelLocs);
490}
491
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000492/// \brief A definition will return its interface declaration.
493/// An interface declaration will return its definition.
494/// Otherwise it will return itself.
495ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
496 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000497 ObjCMethodDecl *Redecl = 0;
498 if (HasRedeclaration)
499 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000500 if (Redecl)
501 return Redecl;
502
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000503 Decl *CtxD = cast<Decl>(getDeclContext());
504
505 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
506 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
507 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
508
509 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
510 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
511 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
512
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000513 } else if (ObjCImplementationDecl *ImplD =
514 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000515 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
516 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000517
518 } else if (ObjCCategoryImplDecl *CImplD =
519 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000520 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000521 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000522 }
523
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000524 if (!Redecl && isRedeclaration()) {
525 // This is the last redeclaration, go back to the first method.
526 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
527 isInstanceMethod());
528 }
529
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000530 return Redecl ? Redecl : this;
531}
532
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000533ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
534 Decl *CtxD = cast<Decl>(getDeclContext());
535
536 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
537 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
538 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
539 isInstanceMethod()))
540 return MD;
541
542 } else if (ObjCCategoryImplDecl *CImplD =
543 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000544 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000545 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
546 isInstanceMethod()))
547 return MD;
548 }
549
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000550 if (isRedeclaration())
551 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
552 isInstanceMethod());
553
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000554 return this;
555}
556
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000557SourceLocation ObjCMethodDecl::getLocEnd() const {
558 if (Stmt *Body = getBody())
559 return Body->getLocEnd();
560 return DeclEndLoc;
561}
562
John McCall85f3d762011-03-02 01:50:55 +0000563ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
564 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000565 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000566 return family;
567
John McCalld5313b02011-03-02 11:33:24 +0000568 // Check for an explicit attribute.
569 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
570 // The unfortunate necessity of mapping between enums here is due
571 // to the attributes framework.
572 switch (attr->getFamily()) {
573 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
574 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
575 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
576 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
577 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
578 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
579 }
580 Family = static_cast<unsigned>(family);
581 return family;
582 }
583
John McCall85f3d762011-03-02 01:50:55 +0000584 family = getSelector().getMethodFamily();
585 switch (family) {
586 case OMF_None: break;
587
588 // init only has a conventional meaning for an instance method, and
589 // it has to return an object.
590 case OMF_init:
591 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
592 family = OMF_None;
593 break;
594
595 // alloc/copy/new have a conventional meaning for both class and
596 // instance methods, but they require an object return.
597 case OMF_alloc:
598 case OMF_copy:
599 case OMF_mutableCopy:
600 case OMF_new:
601 if (!getResultType()->isObjCObjectPointerType())
602 family = OMF_None;
603 break;
604
605 // These selectors have a conventional meaning only for instance methods.
606 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000607 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000608 case OMF_retain:
609 case OMF_release:
610 case OMF_autorelease:
611 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000612 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000613 if (!isInstanceMethod())
614 family = OMF_None;
615 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000616
617 case OMF_performSelector:
618 if (!isInstanceMethod() ||
619 !getResultType()->isObjCIdType())
620 family = OMF_None;
621 else {
622 unsigned noParams = param_size();
623 if (noParams < 1 || noParams > 3)
624 family = OMF_None;
625 else {
626 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
627 QualType ArgT = (*it);
628 if (!ArgT->isObjCSelType()) {
629 family = OMF_None;
630 break;
631 }
632 while (--noParams) {
633 it++;
634 ArgT = (*it);
635 if (!ArgT->isObjCIdType()) {
636 family = OMF_None;
637 break;
638 }
639 }
640 }
641 }
642 break;
643
John McCall85f3d762011-03-02 01:50:55 +0000644 }
645
646 // Cache the result.
647 Family = static_cast<unsigned>(family);
648 return family;
649}
650
Mike Stump1eb44332009-09-09 15:08:12 +0000651void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000652 const ObjCInterfaceDecl *OID) {
653 QualType selfTy;
654 if (isInstanceMethod()) {
655 // There may be no interface context due to error in declaration
656 // of the interface (which has been reported). Recover gracefully.
657 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000658 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000659 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000660 } else {
661 selfTy = Context.getObjCIdType();
662 }
663 } else // we have a factory method.
664 selfTy = Context.getObjCClassType();
665
John McCall7acddac2011-06-17 06:42:21 +0000666 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000667 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000668
David Blaikie4e4d0842012-03-11 07:00:24 +0000669 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000670 if (isInstanceMethod()) {
671 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000672
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000673 // 'self' is always __strong. It's actually pseudo-strong except
674 // in init methods (or methods labeled ns_consumes_self), though.
675 Qualifiers qs;
676 qs.setObjCLifetime(Qualifiers::OCL_Strong);
677 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000678
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000679 // In addition, 'self' is const unless this is an init method.
680 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
681 selfTy = selfTy.withConst();
682 selfIsPseudoStrong = true;
683 }
684 }
685 else {
686 assert(isClassMethod());
687 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000688 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000689 selfIsPseudoStrong = true;
690 }
John McCallf85e1932011-06-15 23:02:42 +0000691 }
692
693 ImplicitParamDecl *self
694 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
695 &Context.Idents.get("self"), selfTy);
696 setSelfDecl(self);
697
698 if (selfIsConsumed)
699 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000700
John McCall7acddac2011-06-17 06:42:21 +0000701 if (selfIsPseudoStrong)
702 self->setARCPseudoStrong(true);
703
Mike Stump1eb44332009-09-09 15:08:12 +0000704 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
705 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000706 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000707}
708
Chris Lattnerab351632009-02-20 20:59:54 +0000709ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
710 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
711 return ID;
712 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
713 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000714 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000715 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000716
717 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000718 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000719}
720
Chris Lattnerab351632009-02-20 20:59:54 +0000721//===----------------------------------------------------------------------===//
722// ObjCInterfaceDecl
723//===----------------------------------------------------------------------===//
724
Douglas Gregora6ea10e2012-01-17 18:09:05 +0000725ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +0000726 DeclContext *DC,
727 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000728 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000729 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000730 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000731 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000732 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000733 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000734 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000735 return Result;
736}
737
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000738ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
739 unsigned ID) {
740 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
741 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
742 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +0000743}
744
745ObjCInterfaceDecl::
746ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000747 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
748 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000749 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +0000750 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000751{
Douglas Gregorfd002a72011-12-16 22:37:11 +0000752 setPreviousDeclaration(PrevDecl);
753
754 // Copy the 'data' pointer over.
755 if (PrevDecl)
756 Data = PrevDecl->Data;
757
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +0000758 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000759}
760
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000761void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000762 assert(data().ExternallyCompleted && "Class is not externally completed");
763 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000764 getASTContext().getExternalSource()->CompleteType(
765 const_cast<ObjCInterfaceDecl *>(this));
766}
767
768void ObjCInterfaceDecl::setExternallyCompleted() {
769 assert(getASTContext().getExternalSource() &&
770 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000771 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000772 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000773 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000774}
775
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000776ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000777 if (const ObjCInterfaceDecl *Def = getDefinition()) {
778 if (data().ExternallyCompleted)
779 LoadExternalDefinition();
780
781 return getASTContext().getObjCImplementation(
782 const_cast<ObjCInterfaceDecl*>(Def));
783 }
784
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000785 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +0000786 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000787}
788
789void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000790 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000791}
792
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000793/// all_declared_ivar_begin - return first ivar declared in this class,
794/// its extensions and its implementation. Lazily build the list on first
795/// access.
796ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000797 // FIXME: Should make sure no callers ever do this.
798 if (!hasDefinition())
799 return 0;
800
801 if (data().IvarList)
802 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000803
804 ObjCIvarDecl *curIvar = 0;
805 if (!ivar_empty()) {
806 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
David Blaikie581deb32012-06-06 20:45:41 +0000807 data().IvarList = *I; ++I;
808 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
809 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000810 }
811
812 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
813 CDecl = CDecl->getNextClassExtension()) {
814 if (!CDecl->ivar_empty()) {
815 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
816 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000817 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +0000818 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000819 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000820 }
David Blaikie581deb32012-06-06 20:45:41 +0000821 for ( ;I != E; curIvar = *I, ++I)
822 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000823 }
824 }
825
826 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
827 if (!ImplDecl->ivar_empty()) {
828 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
829 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000830 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +0000831 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000832 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000833 }
David Blaikie581deb32012-06-06 20:45:41 +0000834 for ( ;I != E; curIvar = *I, ++I)
835 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000836 }
837 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000838 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000839}
Chris Lattnerab351632009-02-20 20:59:54 +0000840
841/// FindCategoryDeclaration - Finds category declaration in the list of
842/// categories for this class and returns it. Name of the category is passed
843/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000844///
Chris Lattnerab351632009-02-20 20:59:54 +0000845ObjCCategoryDecl *
846ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000847 // FIXME: Should make sure no callers ever do this.
848 if (!hasDefinition())
849 return 0;
850
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000851 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000852 LoadExternalDefinition();
853
Chris Lattnerab351632009-02-20 20:59:54 +0000854 for (ObjCCategoryDecl *Category = getCategoryList();
855 Category; Category = Category->getNextClassCategory())
856 if (Category->getIdentifier() == CategoryId)
857 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000858 return 0;
859}
860
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000861ObjCMethodDecl *
862ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
863 for (ObjCCategoryDecl *Category = getCategoryList();
864 Category; Category = Category->getNextClassCategory())
865 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
866 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
867 return MD;
868 return 0;
869}
870
871ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
872 for (ObjCCategoryDecl *Category = getCategoryList();
873 Category; Category = Category->getNextClassCategory())
874 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
875 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
876 return MD;
877 return 0;
878}
879
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000880/// ClassImplementsProtocol - Checks that 'lProto' protocol
881/// has been implemented in IDecl class, its super class or categories (if
882/// lookupCategory is true).
883bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
884 bool lookupCategory,
885 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000886 if (!hasDefinition())
887 return false;
888
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000889 ObjCInterfaceDecl *IDecl = this;
890 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000891 for (ObjCInterfaceDecl::protocol_iterator
892 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000893 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
894 return true;
895 // This is dubious and is added to be compatible with gcc. In gcc, it is
896 // also allowed assigning a protocol-qualified 'id' type to a LHS object
897 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
898 // object. This IMO, should be a bug.
899 // FIXME: Treat this as an extension, and flag this as an error when GCC
900 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000901 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000902 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
903 return true;
904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000906 // 2nd, look up the category.
907 if (lookupCategory)
908 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
909 CDecl = CDecl->getNextClassCategory()) {
910 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
911 E = CDecl->protocol_end(); PI != E; ++PI)
912 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
913 return true;
914 }
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000916 // 3rd, look up the super class(s)
917 if (IDecl->getSuperClass())
918 return
919 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
920 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000922 return false;
923}
924
Chris Lattnerab351632009-02-20 20:59:54 +0000925//===----------------------------------------------------------------------===//
926// ObjCIvarDecl
927//===----------------------------------------------------------------------===//
928
David Blaikie99ba9e32011-12-20 02:48:34 +0000929void ObjCIvarDecl::anchor() { }
930
Daniel Dunbara0654922010-04-02 20:10:03 +0000931ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000932 SourceLocation StartLoc,
933 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000934 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000935 AccessControl ac, Expr *BW,
936 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000937 if (DC) {
938 // Ivar's can only appear in interfaces, implementations (via synthesized
939 // properties), and class extensions (via direct declaration, or synthesized
940 // properties).
941 //
942 // FIXME: This should really be asserting this:
943 // (isa<ObjCCategoryDecl>(DC) &&
944 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
945 // but unfortunately we sometimes place ivars into non-class extension
946 // categories on error. This breaks an AST invariant, and should not be
947 // fixed.
948 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
949 isa<ObjCCategoryDecl>(DC)) &&
950 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000951 // Once a new ivar is created in any of class/class-extension/implementation
952 // decl contexts, the previously built IvarList must be rebuilt.
953 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
954 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +0000955 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000956 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +0000957 else
958 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000959 }
960 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000961 }
962
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000963 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
964 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000965}
966
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000967ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
968 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
969 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
970 QualType(), 0, ObjCIvarDecl::None, 0, false);
971}
972
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000973const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
974 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000975
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000976 switch (DC->getKind()) {
977 default:
978 case ObjCCategoryImpl:
979 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000980 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000981
982 // Ivars can only appear in class extension categories.
983 case ObjCCategory: {
984 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
985 assert(CD->IsClassExtension() && "invalid container for ivar!");
986 return CD->getClassInterface();
987 }
988
989 case ObjCImplementation:
990 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
991
992 case ObjCInterface:
993 return cast<ObjCInterfaceDecl>(DC);
994 }
995}
Chris Lattnerab351632009-02-20 20:59:54 +0000996
997//===----------------------------------------------------------------------===//
998// ObjCAtDefsFieldDecl
999//===----------------------------------------------------------------------===//
1000
David Blaikie99ba9e32011-12-20 02:48:34 +00001001void ObjCAtDefsFieldDecl::anchor() { }
1002
Chris Lattnerab351632009-02-20 20:59:54 +00001003ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001004*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1005 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001006 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001007 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001008}
1009
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001010ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1011 unsigned ID) {
1012 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1013 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1014 0, QualType(), 0);
1015}
1016
Chris Lattnerab351632009-02-20 20:59:54 +00001017//===----------------------------------------------------------------------===//
1018// ObjCProtocolDecl
1019//===----------------------------------------------------------------------===//
1020
David Blaikie99ba9e32011-12-20 02:48:34 +00001021void ObjCProtocolDecl::anchor() { }
1022
Douglas Gregor27c6da22012-01-01 20:30:41 +00001023ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1024 SourceLocation nameLoc,
1025 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001026 ObjCProtocolDecl *PrevDecl)
1027 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001028{
1029 setPreviousDeclaration(PrevDecl);
1030 if (PrevDecl)
1031 Data = PrevDecl->Data;
1032}
1033
Chris Lattnerab351632009-02-20 20:59:54 +00001034ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001035 IdentifierInfo *Id,
1036 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001037 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001038 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001039 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001040 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001041
1042 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001043}
1044
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001045ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1046 unsigned ID) {
1047 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1048 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1049 0);
1050}
1051
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001052ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1053 ObjCProtocolDecl *PDecl = this;
1054
1055 if (Name == getIdentifier())
1056 return PDecl;
1057
1058 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1059 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1060 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001062 return NULL;
1063}
1064
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001065// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001066// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001067ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1068 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001069 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001071 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001072 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Chris Lattnerab351632009-02-20 20:59:54 +00001074 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001075 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001076 return MethodDecl;
1077 return NULL;
1078}
1079
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001080void ObjCProtocolDecl::allocateDefinitionData() {
1081 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001082 Data = new (getASTContext()) DefinitionData;
1083 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001084}
1085
1086void ObjCProtocolDecl::startDefinition() {
1087 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001088
1089 // Update all of the declarations with a pointer to the definition.
1090 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1091 RD != RDEnd; ++RD)
1092 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001093}
1094
Chris Lattnerab351632009-02-20 20:59:54 +00001095//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001096// ObjCCategoryDecl
1097//===----------------------------------------------------------------------===//
1098
David Blaikie99ba9e32011-12-20 02:48:34 +00001099void ObjCCategoryDecl::anchor() { }
1100
Chris Lattnerab351632009-02-20 20:59:54 +00001101ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001102 SourceLocation AtLoc,
1103 SourceLocation ClassNameLoc,
1104 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001105 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001106 ObjCInterfaceDecl *IDecl,
1107 SourceLocation IvarLBraceLoc,
1108 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001109 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1110 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001111 IDecl,
1112 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001113 if (IDecl) {
1114 // Link this category into its class's category list.
1115 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001116 if (IDecl->hasDefinition()) {
1117 IDecl->setCategoryList(CatDecl);
1118 if (ASTMutationListener *L = C.getASTMutationListener())
1119 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1120 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001121 }
1122
1123 return CatDecl;
1124}
1125
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001126ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1127 unsigned ID) {
1128 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1129 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1130 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001131}
1132
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001133ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1134 return getASTContext().getObjCImplementation(
1135 const_cast<ObjCCategoryDecl*>(this));
1136}
1137
1138void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1139 getASTContext().setObjCImplementation(this, ImplD);
1140}
1141
1142
Chris Lattnerab351632009-02-20 20:59:54 +00001143//===----------------------------------------------------------------------===//
1144// ObjCCategoryImplDecl
1145//===----------------------------------------------------------------------===//
1146
David Blaikie99ba9e32011-12-20 02:48:34 +00001147void ObjCCategoryImplDecl::anchor() { }
1148
Chris Lattnerab351632009-02-20 20:59:54 +00001149ObjCCategoryImplDecl *
1150ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001151 IdentifierInfo *Id,
1152 ObjCInterfaceDecl *ClassInterface,
1153 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001154 SourceLocation atStartLoc,
1155 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001156 if (ClassInterface && ClassInterface->hasDefinition())
1157 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001158 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001159 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001160}
1161
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001162ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1163 unsigned ID) {
1164 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1165 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1166 SourceLocation(), SourceLocation());
1167}
1168
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001169ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001170 // The class interface might be NULL if we are working with invalid code.
1171 if (const ObjCInterfaceDecl *ID = getClassInterface())
1172 return ID->FindCategoryDeclaration(getIdentifier());
1173 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001174}
1175
Chris Lattnerab351632009-02-20 20:59:54 +00001176
David Blaikie99ba9e32011-12-20 02:48:34 +00001177void ObjCImplDecl::anchor() { }
1178
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001179void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001180 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001181 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001182 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001183}
1184
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001185void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1186 ASTContext &Ctx = getASTContext();
1187
1188 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001189 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001190 if (IFace)
1191 Ctx.setObjCImplementation(IFace, ImplD);
1192
Duncan Sands98f2cca2009-07-21 07:56:29 +00001193 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001194 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1195 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1196 Ctx.setObjCImplementation(CD, ImplD);
1197 }
1198
1199 ClassInterface = IFace;
1200}
1201
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001202/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
James Dennett1c3a46a2012-06-15 22:30:14 +00001203/// properties implemented in this category \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001204/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001205///
Chris Lattner3aa18612009-02-28 18:42:10 +00001206ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001207FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1208 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001209 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001210 if (PID->getPropertyIvarDecl() &&
1211 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1212 return PID;
1213 }
1214 return 0;
1215}
1216
1217/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001218/// added to the list of those properties \@synthesized/\@dynamic in this
1219/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001220///
Chris Lattner3aa18612009-02-28 18:42:10 +00001221ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001222FindPropertyImplDecl(IdentifierInfo *Id) const {
1223 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001224 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001225 if (PID->getPropertyDecl()->getIdentifier() == Id)
1226 return PID;
1227 }
1228 return 0;
1229}
1230
Chris Lattner5f9e2722011-07-23 10:55:15 +00001231raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001232 const ObjCCategoryImplDecl &CID) {
1233 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001234 return OS;
1235}
1236
Chris Lattnerab351632009-02-20 20:59:54 +00001237//===----------------------------------------------------------------------===//
1238// ObjCImplementationDecl
1239//===----------------------------------------------------------------------===//
1240
David Blaikie99ba9e32011-12-20 02:48:34 +00001241void ObjCImplementationDecl::anchor() { }
1242
Chris Lattnerab351632009-02-20 20:59:54 +00001243ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001244ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001245 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001246 ObjCInterfaceDecl *SuperDecl,
1247 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001248 SourceLocation atStartLoc,
1249 SourceLocation IvarLBraceLoc,
1250 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001251 if (ClassInterface && ClassInterface->hasDefinition())
1252 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001253 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001254 nameLoc, atStartLoc,
1255 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001256}
1257
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001258ObjCImplementationDecl *
1259ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1260 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1261 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1262 SourceLocation());
1263}
1264
John McCallda6d9762011-07-22 04:15:06 +00001265void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1266 CXXCtorInitializer ** initializers,
1267 unsigned numInitializers) {
1268 if (numInitializers > 0) {
1269 NumIvarInitializers = numInitializers;
1270 CXXCtorInitializer **ivarInitializers =
1271 new (C) CXXCtorInitializer*[NumIvarInitializers];
1272 memcpy(ivarInitializers, initializers,
1273 numInitializers * sizeof(CXXCtorInitializer*));
1274 IvarInitializers = ivarInitializers;
1275 }
1276}
1277
Chris Lattner5f9e2722011-07-23 10:55:15 +00001278raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001279 const ObjCImplementationDecl &ID) {
1280 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001281 return OS;
1282}
1283
Chris Lattnerab351632009-02-20 20:59:54 +00001284//===----------------------------------------------------------------------===//
1285// ObjCCompatibleAliasDecl
1286//===----------------------------------------------------------------------===//
1287
David Blaikie99ba9e32011-12-20 02:48:34 +00001288void ObjCCompatibleAliasDecl::anchor() { }
1289
Chris Lattnerab351632009-02-20 20:59:54 +00001290ObjCCompatibleAliasDecl *
1291ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1292 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001293 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001294 ObjCInterfaceDecl* AliasedClass) {
1295 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1296}
1297
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001298ObjCCompatibleAliasDecl *
1299ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1300 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1301 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1302}
1303
Chris Lattnerab351632009-02-20 20:59:54 +00001304//===----------------------------------------------------------------------===//
1305// ObjCPropertyDecl
1306//===----------------------------------------------------------------------===//
1307
David Blaikie99ba9e32011-12-20 02:48:34 +00001308void ObjCPropertyDecl::anchor() { }
1309
Chris Lattnerab351632009-02-20 20:59:54 +00001310ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1311 SourceLocation L,
1312 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001313 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001314 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001315 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001316 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001317 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001318}
1319
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001320ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1321 unsigned ID) {
1322 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1323 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001324 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001325 0);
1326}
1327
Chris Lattnerab351632009-02-20 20:59:54 +00001328//===----------------------------------------------------------------------===//
1329// ObjCPropertyImplDecl
1330//===----------------------------------------------------------------------===//
1331
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001332ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001333 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001334 SourceLocation atLoc,
1335 SourceLocation L,
1336 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001337 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001338 ObjCIvarDecl *ivar,
1339 SourceLocation ivarLoc) {
1340 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1341 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001342}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001343
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001344ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1345 unsigned ID) {
1346 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1347 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1348 0, Dynamic, 0, SourceLocation());
1349}
1350
Douglas Gregora4ffd852010-11-17 01:03:52 +00001351SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1352 SourceLocation EndLoc = getLocation();
1353 if (IvarLoc.isValid())
1354 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001355
Douglas Gregora4ffd852010-11-17 01:03:52 +00001356 return SourceRange(AtLoc, EndLoc);
1357}