blob: 18be26def90560fef5bc6baefa46edd71dfd0e4b [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"
Chris Lattner1e03a562008-03-16 00:19:01 +000019using namespace clang;
20
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000021//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000022// ObjCListBase
23//===----------------------------------------------------------------------===//
24
Chris Lattner38af2de2009-02-20 21:35:13 +000025void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000026 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000027 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000028
29
Chris Lattner4ee413b2009-02-20 21:44:01 +000030 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000031 NumElts = Elts;
32 memcpy(List, InList, sizeof(void*)*Elts);
33}
34
Douglas Gregor18df52b2010-01-16 15:02:53 +000035void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
36 const SourceLocation *Locs, ASTContext &Ctx) {
37 if (Elts == 0)
38 return;
39
40 Locations = new (Ctx) SourceLocation[Elts];
41 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
42 set(InList, Elts, Ctx);
43}
44
Chris Lattner11e1e1a2009-02-20 21:16:26 +000045//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000046// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000047//===----------------------------------------------------------------------===//
48
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000049/// getIvarDecl - This method looks up an ivar in this ContextDecl.
50///
51ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000052ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000053 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000054 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000055 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
56 return ivar;
57 }
58 return 0;
59}
60
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000061// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000062ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000063ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000064 // Since instance & class methods can have the same name, the loop below
65 // ensures we get the correct method.
66 //
67 // @interface Whatever
68 // - (int) class_method;
69 // + (float) class_method;
70 // @end
71 //
72 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000073 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000074 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000075 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000076 return MD;
77 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000078 return 0;
79}
80
Ted Kremenek9f550ff2010-03-15 20:11:46 +000081ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000082ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000083 IdentifierInfo *propertyID) {
84
Ted Kremenekde09d0c2010-03-15 20:11:53 +000085 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000086 llvm::tie(I, E) = DC->lookup(propertyID);
87 for ( ; I != E; ++I)
88 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
89 return PD;
90
91 return 0;
92}
93
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000094/// FindPropertyDeclaration - Finds declaration of the property given its name
95/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000096ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000097ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenekde09d0c2010-03-15 20:11:53 +000099 if (ObjCPropertyDecl *PD =
100 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
101 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000103 switch (getKind()) {
104 default:
105 break;
106 case Decl::ObjCProtocol: {
107 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
108 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
109 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000110 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
111 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000112 break;
113 }
114 case Decl::ObjCInterface: {
115 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
116 // Look through categories.
117 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
118 Cat; Cat = Cat->getNextClassCategory())
119 if (!Cat->IsClassExtension())
120 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
121 return P;
122
123 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000124 for (ObjCInterfaceDecl::all_protocol_iterator
125 I = OID->all_referenced_protocol_begin(),
126 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000127 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
128 return P;
129
130 // Finally, check the super class.
131 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
132 return superClass->FindPropertyDeclaration(PropertyId);
133 break;
134 }
135 case Decl::ObjCCategory: {
136 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
137 // Look through protocols.
138 if (!OCD->IsClassExtension())
139 for (ObjCCategoryDecl::protocol_iterator
140 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
141 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
142 return P;
143
144 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000145 }
146 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000147 return 0;
148}
149
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000150/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
151/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000152/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000153///
154ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000155ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000156 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000157 // FIXME: Should make sure no callers ever do this.
158 if (!hasDefinition())
159 return 0;
160
161 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000162 LoadExternalDefinition();
163
Ted Kremenek37cafb02010-03-15 20:30:07 +0000164 if (ObjCPropertyDecl *PD =
165 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
166 return PD;
167
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000168 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000169 for (ObjCInterfaceDecl::all_protocol_iterator
170 I = all_referenced_protocol_begin(),
171 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000172 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
173 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000174
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000175 return 0;
176}
177
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000178void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
179 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
180 ASTContext &C)
181{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000182 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000183 LoadExternalDefinition();
184
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000185 if (data().AllReferencedProtocols.empty() &&
186 data().ReferencedProtocols.empty()) {
187 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000188 return;
189 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000190
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000191 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000192 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000193 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000194 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000195 for (unsigned i = 0; i < ExtNum; i++) {
196 bool protocolExists = false;
197 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000198 for (all_protocol_iterator
199 p = all_referenced_protocol_begin(),
200 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000201 ObjCProtocolDecl *Proto = (*p);
202 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
203 protocolExists = true;
204 break;
205 }
206 }
207 // Do we want to warn on a protocol in extension class which
208 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000209 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000210 ProtocolRefs.push_back(ProtoInExtension);
211 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000212
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000213 if (ProtocolRefs.empty())
214 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000215
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000216 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000217 for (all_protocol_iterator p = all_referenced_protocol_begin(),
218 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000219 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000220 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000221
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000222 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000223}
224
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000225void ObjCInterfaceDecl::allocateDefinitionData() {
226 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor26fec632011-12-15 18:17:27 +0000227 Data = new (getASTContext()) DefinitionData();
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000228 Data->Definition = this;
229
230 // Make the type point at the definition, now that we have one.
231 if (TypeForDecl)
232 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000233}
234
235void ObjCInterfaceDecl::startDefinition() {
236 allocateDefinitionData();
237
Douglas Gregor53df7a12011-12-15 18:03:09 +0000238 // Update all of the declarations with a pointer to the definition.
239 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
240 RD != RDEnd; ++RD) {
241 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000242 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000243 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000244
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000245 if (ASTMutationListener *L = getASTContext().getASTMutationListener())
246 L->CompletedObjCForwardRef(this);
247}
248
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000249/// getFirstClassExtension - Find first class extension of the given class.
250ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
251 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000252 CDecl = CDecl->getNextClassCategory())
253 if (CDecl->IsClassExtension())
254 return CDecl;
255 return 0;
256}
257
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000258/// getNextClassCategory - Find next class extension in list of categories.
259const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
260 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
261 CDecl = CDecl->getNextClassCategory())
262 if (CDecl->IsClassExtension())
263 return CDecl;
264 return 0;
265}
266
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000267ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
268 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000269 // FIXME: Should make sure no callers ever do this.
270 if (!hasDefinition())
271 return 0;
272
273 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000274 LoadExternalDefinition();
275
Chris Lattner1e03a562008-03-16 00:19:01 +0000276 ObjCInterfaceDecl* ClassDecl = this;
277 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000278 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000279 clsDeclared = ClassDecl;
280 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000281 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000282 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
283 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000284 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
285 clsDeclared = ClassDecl;
286 return I;
287 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000288 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000289
Chris Lattner1e03a562008-03-16 00:19:01 +0000290 ClassDecl = ClassDecl->getSuperClass();
291 }
292 return NULL;
293}
294
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000295/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
296/// class whose name is passed as argument. If it is not one of the super classes
297/// the it returns NULL.
298ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
299 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000300 // FIXME: Should make sure no callers ever do this.
301 if (!hasDefinition())
302 return 0;
303
304 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000305 LoadExternalDefinition();
306
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000307 ObjCInterfaceDecl* ClassDecl = this;
308 while (ClassDecl != NULL) {
309 if (ClassDecl->getIdentifier() == ICName)
310 return ClassDecl;
311 ClassDecl = ClassDecl->getSuperClass();
312 }
313 return NULL;
314}
315
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000316/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000317/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000318ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
319 bool isInstance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000320 // FIXME: Should make sure no callers ever do this.
321 if (!hasDefinition())
322 return 0;
323
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000324 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000325 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000327 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000328 LoadExternalDefinition();
329
Chris Lattner1e03a562008-03-16 00:19:01 +0000330 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000331 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000332 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattner1e03a562008-03-16 00:19:01 +0000334 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000335 const ObjCList<ObjCProtocolDecl> &Protocols =
336 ClassDecl->getReferencedProtocols();
337 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
338 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000339 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000340 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattner1e03a562008-03-16 00:19:01 +0000342 // Didn't find one yet - now look through categories.
343 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
344 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000345 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000346 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Steve Naroffb79c01e2008-12-08 20:57:28 +0000348 // Didn't find one yet - look through protocols.
349 const ObjCList<ObjCProtocolDecl> &Protocols =
350 CatDecl->getReferencedProtocols();
351 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
352 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000353 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000354 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000355 CatDecl = CatDecl->getNextClassCategory();
356 }
357 ClassDecl = ClassDecl->getSuperClass();
358 }
359 return NULL;
360}
361
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000362ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
363 const Selector &Sel,
364 bool Instance) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000365 // FIXME: Should make sure no callers ever do this.
366 if (!hasDefinition())
367 return 0;
368
369 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000370 LoadExternalDefinition();
371
Steve Naroffd789d3d2009-10-01 23:46:04 +0000372 ObjCMethodDecl *Method = 0;
373 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000374 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
375 : ImpDecl->getClassMethod(Sel);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000376
377 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000378 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000379 return Method;
380}
Chris Lattnerab351632009-02-20 20:59:54 +0000381
382//===----------------------------------------------------------------------===//
383// ObjCMethodDecl
384//===----------------------------------------------------------------------===//
385
386ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000387 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000388 SourceLocation endLoc,
389 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000390 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000391 DeclContext *contextDecl,
392 bool isInstance,
393 bool isVariadic,
394 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000395 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000396 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000397 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000398 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000399 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000400 SelInfo, T, ResultTInfo, contextDecl,
401 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000402 isVariadic, isSynthesized, isImplicitlyDeclared,
403 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000404 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000405 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000406}
407
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000408void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
409 assert(PrevMethod);
410 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
411 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000412 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000413}
414
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000415void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
416 ArrayRef<ParmVarDecl*> Params,
417 ArrayRef<SourceLocation> SelLocs) {
418 ParamsAndSelLocs = 0;
419 NumParams = Params.size();
420 if (Params.empty() && SelLocs.empty())
421 return;
422
423 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
424 sizeof(SourceLocation) * SelLocs.size();
425 ParamsAndSelLocs = C.Allocate(Size);
426 std::copy(Params.begin(), Params.end(), getParams());
427 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
428}
429
430void ObjCMethodDecl::getSelectorLocs(
431 SmallVectorImpl<SourceLocation> &SelLocs) const {
432 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
433 SelLocs.push_back(getSelectorLoc(i));
434}
435
436void ObjCMethodDecl::setMethodParams(ASTContext &C,
437 ArrayRef<ParmVarDecl*> Params,
438 ArrayRef<SourceLocation> SelLocs) {
439 assert((!SelLocs.empty() || isImplicit()) &&
440 "No selector locs for non-implicit method");
441 if (isImplicit())
442 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
443
444 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
445 if (SelLocsKind != SelLoc_NonStandard)
446 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
447
448 setParamsAndSelLocs(C, Params, SelLocs);
449}
450
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000451/// \brief A definition will return its interface declaration.
452/// An interface declaration will return its definition.
453/// Otherwise it will return itself.
454ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
455 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000456 ObjCMethodDecl *Redecl = 0;
457 if (HasRedeclaration)
458 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000459 if (Redecl)
460 return Redecl;
461
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000462 Decl *CtxD = cast<Decl>(getDeclContext());
463
464 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
465 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
466 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
467
468 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
469 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
470 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
471
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000472 } else if (ObjCImplementationDecl *ImplD =
473 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000474 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
475 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000476
477 } else if (ObjCCategoryImplDecl *CImplD =
478 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000479 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000480 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000481 }
482
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000483 if (!Redecl && isRedeclaration()) {
484 // This is the last redeclaration, go back to the first method.
485 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
486 isInstanceMethod());
487 }
488
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000489 return Redecl ? Redecl : this;
490}
491
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000492ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
493 Decl *CtxD = cast<Decl>(getDeclContext());
494
495 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
496 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
497 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
498 isInstanceMethod()))
499 return MD;
500
501 } else if (ObjCCategoryImplDecl *CImplD =
502 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000503 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000504 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
505 isInstanceMethod()))
506 return MD;
507 }
508
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000509 if (isRedeclaration())
510 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
511 isInstanceMethod());
512
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000513 return this;
514}
515
John McCall85f3d762011-03-02 01:50:55 +0000516ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
517 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000518 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000519 return family;
520
John McCalld5313b02011-03-02 11:33:24 +0000521 // Check for an explicit attribute.
522 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
523 // The unfortunate necessity of mapping between enums here is due
524 // to the attributes framework.
525 switch (attr->getFamily()) {
526 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
527 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
528 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
529 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
530 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
531 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
532 }
533 Family = static_cast<unsigned>(family);
534 return family;
535 }
536
John McCall85f3d762011-03-02 01:50:55 +0000537 family = getSelector().getMethodFamily();
538 switch (family) {
539 case OMF_None: break;
540
541 // init only has a conventional meaning for an instance method, and
542 // it has to return an object.
543 case OMF_init:
544 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
545 family = OMF_None;
546 break;
547
548 // alloc/copy/new have a conventional meaning for both class and
549 // instance methods, but they require an object return.
550 case OMF_alloc:
551 case OMF_copy:
552 case OMF_mutableCopy:
553 case OMF_new:
554 if (!getResultType()->isObjCObjectPointerType())
555 family = OMF_None;
556 break;
557
558 // These selectors have a conventional meaning only for instance methods.
559 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000560 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000561 case OMF_retain:
562 case OMF_release:
563 case OMF_autorelease:
564 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000565 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000566 if (!isInstanceMethod())
567 family = OMF_None;
568 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000569
570 case OMF_performSelector:
571 if (!isInstanceMethod() ||
572 !getResultType()->isObjCIdType())
573 family = OMF_None;
574 else {
575 unsigned noParams = param_size();
576 if (noParams < 1 || noParams > 3)
577 family = OMF_None;
578 else {
579 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
580 QualType ArgT = (*it);
581 if (!ArgT->isObjCSelType()) {
582 family = OMF_None;
583 break;
584 }
585 while (--noParams) {
586 it++;
587 ArgT = (*it);
588 if (!ArgT->isObjCIdType()) {
589 family = OMF_None;
590 break;
591 }
592 }
593 }
594 }
595 break;
596
John McCall85f3d762011-03-02 01:50:55 +0000597 }
598
599 // Cache the result.
600 Family = static_cast<unsigned>(family);
601 return family;
602}
603
Mike Stump1eb44332009-09-09 15:08:12 +0000604void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000605 const ObjCInterfaceDecl *OID) {
606 QualType selfTy;
607 if (isInstanceMethod()) {
608 // There may be no interface context due to error in declaration
609 // of the interface (which has been reported). Recover gracefully.
610 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000611 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000612 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000613 } else {
614 selfTy = Context.getObjCIdType();
615 }
616 } else // we have a factory method.
617 selfTy = Context.getObjCClassType();
618
John McCall7acddac2011-06-17 06:42:21 +0000619 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000620 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000621
622 if (Context.getLangOptions().ObjCAutoRefCount) {
623 if (isInstanceMethod()) {
624 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000625
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000626 // 'self' is always __strong. It's actually pseudo-strong except
627 // in init methods (or methods labeled ns_consumes_self), though.
628 Qualifiers qs;
629 qs.setObjCLifetime(Qualifiers::OCL_Strong);
630 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000631
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000632 // In addition, 'self' is const unless this is an init method.
633 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
634 selfTy = selfTy.withConst();
635 selfIsPseudoStrong = true;
636 }
637 }
638 else {
639 assert(isClassMethod());
640 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000641 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000642 selfIsPseudoStrong = true;
643 }
John McCallf85e1932011-06-15 23:02:42 +0000644 }
645
646 ImplicitParamDecl *self
647 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
648 &Context.Idents.get("self"), selfTy);
649 setSelfDecl(self);
650
651 if (selfIsConsumed)
652 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000653
John McCall7acddac2011-06-17 06:42:21 +0000654 if (selfIsPseudoStrong)
655 self->setARCPseudoStrong(true);
656
Mike Stump1eb44332009-09-09 15:08:12 +0000657 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
658 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000659 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000660}
661
Chris Lattnerab351632009-02-20 20:59:54 +0000662ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
663 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
664 return ID;
665 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
666 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000667 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000668 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000669
670 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000671 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000672}
673
Chris Lattnerab351632009-02-20 20:59:54 +0000674//===----------------------------------------------------------------------===//
675// ObjCInterfaceDecl
676//===----------------------------------------------------------------------===//
677
678ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
679 DeclContext *DC,
680 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000681 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000682 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000683 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000684 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000685 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000686 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000687 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000688 return Result;
689}
690
691ObjCInterfaceDecl *ObjCInterfaceDecl::CreateEmpty(ASTContext &C) {
692 return new (C) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
Douglas Gregorfd002a72011-12-16 22:37:11 +0000693 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +0000694}
695
696ObjCInterfaceDecl::
697ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000698 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
699 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000700 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +0000701 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000702{
Douglas Gregorfd002a72011-12-16 22:37:11 +0000703 setPreviousDeclaration(PrevDecl);
704
705 // Copy the 'data' pointer over.
706 if (PrevDecl)
707 Data = PrevDecl->Data;
708
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +0000709 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000710}
711
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000712void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000713 assert(data().ExternallyCompleted && "Class is not externally completed");
714 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000715 getASTContext().getExternalSource()->CompleteType(
716 const_cast<ObjCInterfaceDecl *>(this));
717}
718
719void ObjCInterfaceDecl::setExternallyCompleted() {
720 assert(getASTContext().getExternalSource() &&
721 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000722 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000723 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000724 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000725}
726
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000727ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000728 if (const ObjCInterfaceDecl *Def = getDefinition()) {
729 if (data().ExternallyCompleted)
730 LoadExternalDefinition();
731
732 return getASTContext().getObjCImplementation(
733 const_cast<ObjCInterfaceDecl*>(Def));
734 }
735
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000736 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +0000737 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000738}
739
740void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000741 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000742}
743
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000744/// all_declared_ivar_begin - return first ivar declared in this class,
745/// its extensions and its implementation. Lazily build the list on first
746/// access.
747ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000748 // FIXME: Should make sure no callers ever do this.
749 if (!hasDefinition())
750 return 0;
751
752 if (data().IvarList)
753 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000754
755 ObjCIvarDecl *curIvar = 0;
756 if (!ivar_empty()) {
757 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000758 data().IvarList = (*I); ++I;
759 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000760 curIvar->setNextIvar(*I);
761 }
762
763 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
764 CDecl = CDecl->getNextClassExtension()) {
765 if (!CDecl->ivar_empty()) {
766 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
767 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000768 if (!data().IvarList) {
769 data().IvarList = (*I); ++I;
770 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000771 }
772 for ( ;I != E; curIvar = *I, ++I)
773 curIvar->setNextIvar(*I);
774 }
775 }
776
777 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
778 if (!ImplDecl->ivar_empty()) {
779 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
780 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000781 if (!data().IvarList) {
782 data().IvarList = (*I); ++I;
783 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000784 }
785 for ( ;I != E; curIvar = *I, ++I)
786 curIvar->setNextIvar(*I);
787 }
788 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000789 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000790}
Chris Lattnerab351632009-02-20 20:59:54 +0000791
792/// FindCategoryDeclaration - Finds category declaration in the list of
793/// categories for this class and returns it. Name of the category is passed
794/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000795///
Chris Lattnerab351632009-02-20 20:59:54 +0000796ObjCCategoryDecl *
797ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000798 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000799 LoadExternalDefinition();
800
Chris Lattnerab351632009-02-20 20:59:54 +0000801 for (ObjCCategoryDecl *Category = getCategoryList();
802 Category; Category = Category->getNextClassCategory())
803 if (Category->getIdentifier() == CategoryId)
804 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000805 return 0;
806}
807
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000808ObjCMethodDecl *
809ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
810 for (ObjCCategoryDecl *Category = getCategoryList();
811 Category; Category = Category->getNextClassCategory())
812 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
813 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
814 return MD;
815 return 0;
816}
817
818ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
819 for (ObjCCategoryDecl *Category = getCategoryList();
820 Category; Category = Category->getNextClassCategory())
821 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
822 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
823 return MD;
824 return 0;
825}
826
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000827/// ClassImplementsProtocol - Checks that 'lProto' protocol
828/// has been implemented in IDecl class, its super class or categories (if
829/// lookupCategory is true).
830bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
831 bool lookupCategory,
832 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000833 if (!hasDefinition())
834 return false;
835
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000836 ObjCInterfaceDecl *IDecl = this;
837 // 1st, look up the class.
838 const ObjCList<ObjCProtocolDecl> &Protocols =
839 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000841 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
842 E = Protocols.end(); PI != E; ++PI) {
843 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
844 return true;
845 // This is dubious and is added to be compatible with gcc. In gcc, it is
846 // also allowed assigning a protocol-qualified 'id' type to a LHS object
847 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
848 // object. This IMO, should be a bug.
849 // FIXME: Treat this as an extension, and flag this as an error when GCC
850 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000851 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000852 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
853 return true;
854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000856 // 2nd, look up the category.
857 if (lookupCategory)
858 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
859 CDecl = CDecl->getNextClassCategory()) {
860 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
861 E = CDecl->protocol_end(); PI != E; ++PI)
862 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
863 return true;
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000866 // 3rd, look up the super class(s)
867 if (IDecl->getSuperClass())
868 return
869 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
870 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000872 return false;
873}
874
Chris Lattnerab351632009-02-20 20:59:54 +0000875//===----------------------------------------------------------------------===//
876// ObjCIvarDecl
877//===----------------------------------------------------------------------===//
878
Daniel Dunbara0654922010-04-02 20:10:03 +0000879ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000880 SourceLocation StartLoc,
881 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000882 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000883 AccessControl ac, Expr *BW,
884 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000885 if (DC) {
886 // Ivar's can only appear in interfaces, implementations (via synthesized
887 // properties), and class extensions (via direct declaration, or synthesized
888 // properties).
889 //
890 // FIXME: This should really be asserting this:
891 // (isa<ObjCCategoryDecl>(DC) &&
892 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
893 // but unfortunately we sometimes place ivars into non-class extension
894 // categories on error. This breaks an AST invariant, and should not be
895 // fixed.
896 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
897 isa<ObjCCategoryDecl>(DC)) &&
898 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000899 // Once a new ivar is created in any of class/class-extension/implementation
900 // decl contexts, the previously built IvarList must be rebuilt.
901 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
902 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000903 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000904 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000905 if (BW)
906 IM->setHasSynthBitfield(true);
Chad Rosier30601782011-08-17 23:08:45 +0000907 } else {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000908 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
909 ID = CD->getClassInterface();
910 if (BW)
911 CD->setHasSynthBitfield(true);
912 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000913 }
914 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000915 }
916
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000917 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
918 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000919}
920
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000921const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
922 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000923
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000924 switch (DC->getKind()) {
925 default:
926 case ObjCCategoryImpl:
927 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000928 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000929
930 // Ivars can only appear in class extension categories.
931 case ObjCCategory: {
932 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
933 assert(CD->IsClassExtension() && "invalid container for ivar!");
934 return CD->getClassInterface();
935 }
936
937 case ObjCImplementation:
938 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
939
940 case ObjCInterface:
941 return cast<ObjCInterfaceDecl>(DC);
942 }
943}
Chris Lattnerab351632009-02-20 20:59:54 +0000944
945//===----------------------------------------------------------------------===//
946// ObjCAtDefsFieldDecl
947//===----------------------------------------------------------------------===//
948
949ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000950*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
951 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000952 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000953 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000954}
955
Chris Lattnerab351632009-02-20 20:59:54 +0000956//===----------------------------------------------------------------------===//
957// ObjCProtocolDecl
958//===----------------------------------------------------------------------===//
959
960ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000961 IdentifierInfo *Id,
962 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000963 SourceLocation atStartLoc,
964 bool isForwardDecl) {
965 return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, isForwardDecl);
Chris Lattnerab351632009-02-20 20:59:54 +0000966}
967
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000968ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
969 ObjCProtocolDecl *PDecl = this;
970
971 if (Name == getIdentifier())
972 return PDecl;
973
974 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
975 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
976 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000978 return NULL;
979}
980
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000981// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000982// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000983ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
984 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000985 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000987 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000988 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Chris Lattnerab351632009-02-20 20:59:54 +0000990 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000991 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000992 return MethodDecl;
993 return NULL;
994}
995
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000996void ObjCProtocolDecl::completedForwardDecl() {
997 assert(isForwardDecl() && "Only valid to call for forward refs");
998 isForwardProtoDecl = false;
999 if (ASTMutationListener *L = getASTContext().getASTMutationListener())
1000 L->CompletedObjCForwardRef(this);
1001}
1002
Chris Lattnerab351632009-02-20 20:59:54 +00001003//===----------------------------------------------------------------------===//
1004// ObjCClassDecl
1005//===----------------------------------------------------------------------===//
1006
Mike Stump1eb44332009-09-09 15:08:12 +00001007ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Douglas Gregoraf764722011-12-14 17:12:03 +00001008 ObjCInterfaceDecl *Interface,
1009 SourceLocation InterfaceLoc)
1010 : Decl(ObjCClass, DC, L), Interface(Interface), InterfaceLoc(InterfaceLoc)
1011{
Ted Kremenek321c22f2009-11-18 00:28:11 +00001012}
Chris Lattner38af2de2009-02-20 21:35:13 +00001013
Chris Lattnerab351632009-02-20 20:59:54 +00001014ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
1015 SourceLocation L,
Douglas Gregoraf764722011-12-14 17:12:03 +00001016 ObjCInterfaceDecl *Interface,
1017 SourceLocation InterfaceLoc) {
1018 return new (C) ObjCClassDecl(DC, L, Interface, InterfaceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001019}
1020
Ted Kremenek2dbdd622009-11-18 01:26:56 +00001021SourceRange ObjCClassDecl::getSourceRange() const {
Douglas Gregoraf764722011-12-14 17:12:03 +00001022 return SourceRange(getLocation(), InterfaceLoc);
Ted Kremenek2dbdd622009-11-18 01:26:56 +00001023}
1024
Chris Lattnerab351632009-02-20 20:59:54 +00001025//===----------------------------------------------------------------------===//
1026// ObjCForwardProtocolDecl
1027//===----------------------------------------------------------------------===//
1028
Chris Lattner38af2de2009-02-20 21:35:13 +00001029ObjCForwardProtocolDecl::
1030ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
1031 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +00001032 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +00001033: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +00001034 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +00001035}
1036
1037
Chris Lattnerab351632009-02-20 20:59:54 +00001038ObjCForwardProtocolDecl *
1039ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +00001040 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +00001041 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +00001042 unsigned NumElts,
1043 const SourceLocation *Locs) {
1044 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +00001045}
1046
Chris Lattnerab351632009-02-20 20:59:54 +00001047//===----------------------------------------------------------------------===//
1048// ObjCCategoryDecl
1049//===----------------------------------------------------------------------===//
1050
1051ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001052 SourceLocation AtLoc,
1053 SourceLocation ClassNameLoc,
1054 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001055 IdentifierInfo *Id,
1056 ObjCInterfaceDecl *IDecl) {
1057 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1058 CategoryNameLoc, Id,
1059 IDecl);
1060 if (IDecl) {
1061 // Link this category into its class's category list.
1062 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001063 if (IDecl->hasDefinition()) {
1064 IDecl->setCategoryList(CatDecl);
1065 if (ASTMutationListener *L = C.getASTMutationListener())
1066 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1067 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001068 }
1069
1070 return CatDecl;
1071}
1072
1073ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) {
1074 return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1075 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001076}
1077
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001078ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1079 return getASTContext().getObjCImplementation(
1080 const_cast<ObjCCategoryDecl*>(this));
1081}
1082
1083void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1084 getASTContext().setObjCImplementation(this, ImplD);
1085}
1086
1087
Chris Lattnerab351632009-02-20 20:59:54 +00001088//===----------------------------------------------------------------------===//
1089// ObjCCategoryImplDecl
1090//===----------------------------------------------------------------------===//
1091
1092ObjCCategoryImplDecl *
1093ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001094 IdentifierInfo *Id,
1095 ObjCInterfaceDecl *ClassInterface,
1096 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001097 SourceLocation atStartLoc,
1098 SourceLocation CategoryNameLoc) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001099 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001100 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001101}
1102
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001103ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001104 // The class interface might be NULL if we are working with invalid code.
1105 if (const ObjCInterfaceDecl *ID = getClassInterface())
1106 return ID->FindCategoryDeclaration(getIdentifier());
1107 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001108}
1109
Chris Lattnerab351632009-02-20 20:59:54 +00001110
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001111void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001112 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001113 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001114 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001115}
1116
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001117void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1118 ASTContext &Ctx = getASTContext();
1119
1120 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001121 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001122 if (IFace)
1123 Ctx.setObjCImplementation(IFace, ImplD);
1124
Duncan Sands98f2cca2009-07-21 07:56:29 +00001125 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001126 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1127 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1128 Ctx.setObjCImplementation(CD, ImplD);
1129 }
1130
1131 ClassInterface = IFace;
1132}
1133
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001134/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001135/// properties implemented in this category @implementation block and returns
1136/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001137///
Chris Lattner3aa18612009-02-28 18:42:10 +00001138ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001139FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1140 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001141 ObjCPropertyImplDecl *PID = *i;
1142 if (PID->getPropertyIvarDecl() &&
1143 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1144 return PID;
1145 }
1146 return 0;
1147}
1148
1149/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1150/// added to the list of those properties @synthesized/@dynamic in this
1151/// category @implementation block.
1152///
Chris Lattner3aa18612009-02-28 18:42:10 +00001153ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001154FindPropertyImplDecl(IdentifierInfo *Id) const {
1155 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001156 ObjCPropertyImplDecl *PID = *i;
1157 if (PID->getPropertyDecl()->getIdentifier() == Id)
1158 return PID;
1159 }
1160 return 0;
1161}
1162
Chris Lattner5f9e2722011-07-23 10:55:15 +00001163raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001164 const ObjCCategoryImplDecl *CID) {
1165 OS << CID->getName();
1166 return OS;
1167}
1168
Chris Lattnerab351632009-02-20 20:59:54 +00001169//===----------------------------------------------------------------------===//
1170// ObjCImplementationDecl
1171//===----------------------------------------------------------------------===//
1172
1173ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001174ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001175 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001176 ObjCInterfaceDecl *SuperDecl,
1177 SourceLocation nameLoc,
1178 SourceLocation atStartLoc) {
1179 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1180 nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001181}
1182
John McCallda6d9762011-07-22 04:15:06 +00001183void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1184 CXXCtorInitializer ** initializers,
1185 unsigned numInitializers) {
1186 if (numInitializers > 0) {
1187 NumIvarInitializers = numInitializers;
1188 CXXCtorInitializer **ivarInitializers =
1189 new (C) CXXCtorInitializer*[NumIvarInitializers];
1190 memcpy(ivarInitializers, initializers,
1191 numInitializers * sizeof(CXXCtorInitializer*));
1192 IvarInitializers = ivarInitializers;
1193 }
1194}
1195
Chris Lattner5f9e2722011-07-23 10:55:15 +00001196raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001197 const ObjCImplementationDecl *ID) {
1198 OS << ID->getName();
1199 return OS;
1200}
1201
Chris Lattnerab351632009-02-20 20:59:54 +00001202//===----------------------------------------------------------------------===//
1203// ObjCCompatibleAliasDecl
1204//===----------------------------------------------------------------------===//
1205
1206ObjCCompatibleAliasDecl *
1207ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1208 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001209 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001210 ObjCInterfaceDecl* AliasedClass) {
1211 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1212}
1213
1214//===----------------------------------------------------------------------===//
1215// ObjCPropertyDecl
1216//===----------------------------------------------------------------------===//
1217
1218ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1219 SourceLocation L,
1220 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001221 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +00001222 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001223 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001224 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001225}
1226
Chris Lattnerab351632009-02-20 20:59:54 +00001227//===----------------------------------------------------------------------===//
1228// ObjCPropertyImplDecl
1229//===----------------------------------------------------------------------===//
1230
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001231ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001232 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001233 SourceLocation atLoc,
1234 SourceLocation L,
1235 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001236 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001237 ObjCIvarDecl *ivar,
1238 SourceLocation ivarLoc) {
1239 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1240 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001241}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001242
Douglas Gregora4ffd852010-11-17 01:03:52 +00001243SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1244 SourceLocation EndLoc = getLocation();
1245 if (IvarLoc.isValid())
1246 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001247
Douglas Gregora4ffd852010-11-17 01:03:52 +00001248 return SourceRange(AtLoc, EndLoc);
1249}