blob: 317dc95f540b21455bc509131ed58c6a510a20ab [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 Gregor0af55012011-12-16 03:12:41 +0000228 Data->Definition = this;
229}
230
231void ObjCInterfaceDecl::startDefinition() {
232 allocateDefinitionData();
233
Douglas Gregor53df7a12011-12-15 18:03:09 +0000234 // Update all of the declarations with a pointer to the definition.
235 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
236 RD != RDEnd; ++RD) {
237 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000238 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000239 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000240
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000241 if (ASTMutationListener *L = getASTContext().getASTMutationListener())
242 L->CompletedObjCForwardRef(this);
243}
244
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000245/// getFirstClassExtension - Find first class extension of the given class.
246ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
247 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000248 CDecl = CDecl->getNextClassCategory())
249 if (CDecl->IsClassExtension())
250 return CDecl;
251 return 0;
252}
253
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000254/// getNextClassCategory - Find next class extension in list of categories.
255const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
256 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
257 CDecl = CDecl->getNextClassCategory())
258 if (CDecl->IsClassExtension())
259 return CDecl;
260 return 0;
261}
262
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000263ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
264 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000265 // FIXME: Should make sure no callers ever do this.
266 if (!hasDefinition())
267 return 0;
268
269 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000270 LoadExternalDefinition();
271
Chris Lattner1e03a562008-03-16 00:19:01 +0000272 ObjCInterfaceDecl* ClassDecl = this;
273 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000274 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000275 clsDeclared = ClassDecl;
276 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000277 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000278 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
279 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000280 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
281 clsDeclared = ClassDecl;
282 return I;
283 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000284 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000285
Chris Lattner1e03a562008-03-16 00:19:01 +0000286 ClassDecl = ClassDecl->getSuperClass();
287 }
288 return NULL;
289}
290
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000291/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
292/// class whose name is passed as argument. If it is not one of the super classes
293/// the it returns NULL.
294ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
295 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000296 // FIXME: Should make sure no callers ever do this.
297 if (!hasDefinition())
298 return 0;
299
300 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000301 LoadExternalDefinition();
302
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000303 ObjCInterfaceDecl* ClassDecl = this;
304 while (ClassDecl != NULL) {
305 if (ClassDecl->getIdentifier() == ICName)
306 return ClassDecl;
307 ClassDecl = ClassDecl->getSuperClass();
308 }
309 return NULL;
310}
311
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000312/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000313/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000314ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
315 bool isInstance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000316 // FIXME: Should make sure no callers ever do this.
317 if (!hasDefinition())
318 return 0;
319
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000320 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000321 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000323 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000324 LoadExternalDefinition();
325
Chris Lattner1e03a562008-03-16 00:19:01 +0000326 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000327 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000328 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner1e03a562008-03-16 00:19:01 +0000330 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000331 const ObjCList<ObjCProtocolDecl> &Protocols =
332 ClassDecl->getReferencedProtocols();
333 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
334 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000335 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000336 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Chris Lattner1e03a562008-03-16 00:19:01 +0000338 // Didn't find one yet - now look through categories.
339 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
340 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000341 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000342 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Steve Naroffb79c01e2008-12-08 20:57:28 +0000344 // Didn't find one yet - look through protocols.
345 const ObjCList<ObjCProtocolDecl> &Protocols =
346 CatDecl->getReferencedProtocols();
347 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
348 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000349 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000350 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000351 CatDecl = CatDecl->getNextClassCategory();
352 }
353 ClassDecl = ClassDecl->getSuperClass();
354 }
355 return NULL;
356}
357
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000358ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
359 const Selector &Sel,
360 bool Instance) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000361 // FIXME: Should make sure no callers ever do this.
362 if (!hasDefinition())
363 return 0;
364
365 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000366 LoadExternalDefinition();
367
Steve Naroffd789d3d2009-10-01 23:46:04 +0000368 ObjCMethodDecl *Method = 0;
369 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000370 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
371 : ImpDecl->getClassMethod(Sel);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000372
373 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000374 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000375 return Method;
376}
Chris Lattnerab351632009-02-20 20:59:54 +0000377
378//===----------------------------------------------------------------------===//
379// ObjCMethodDecl
380//===----------------------------------------------------------------------===//
381
382ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000383 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000384 SourceLocation endLoc,
385 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000386 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000387 DeclContext *contextDecl,
388 bool isInstance,
389 bool isVariadic,
390 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000391 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000392 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000393 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000394 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000395 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000396 SelInfo, T, ResultTInfo, contextDecl,
397 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000398 isVariadic, isSynthesized, isImplicitlyDeclared,
399 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000400 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000401 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000402}
403
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000404void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
405 assert(PrevMethod);
406 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
407 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000408 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000409}
410
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000411void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
412 ArrayRef<ParmVarDecl*> Params,
413 ArrayRef<SourceLocation> SelLocs) {
414 ParamsAndSelLocs = 0;
415 NumParams = Params.size();
416 if (Params.empty() && SelLocs.empty())
417 return;
418
419 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
420 sizeof(SourceLocation) * SelLocs.size();
421 ParamsAndSelLocs = C.Allocate(Size);
422 std::copy(Params.begin(), Params.end(), getParams());
423 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
424}
425
426void ObjCMethodDecl::getSelectorLocs(
427 SmallVectorImpl<SourceLocation> &SelLocs) const {
428 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
429 SelLocs.push_back(getSelectorLoc(i));
430}
431
432void ObjCMethodDecl::setMethodParams(ASTContext &C,
433 ArrayRef<ParmVarDecl*> Params,
434 ArrayRef<SourceLocation> SelLocs) {
435 assert((!SelLocs.empty() || isImplicit()) &&
436 "No selector locs for non-implicit method");
437 if (isImplicit())
438 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
439
440 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
441 if (SelLocsKind != SelLoc_NonStandard)
442 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
443
444 setParamsAndSelLocs(C, Params, SelLocs);
445}
446
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000447/// \brief A definition will return its interface declaration.
448/// An interface declaration will return its definition.
449/// Otherwise it will return itself.
450ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
451 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000452 ObjCMethodDecl *Redecl = 0;
453 if (HasRedeclaration)
454 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000455 if (Redecl)
456 return Redecl;
457
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000458 Decl *CtxD = cast<Decl>(getDeclContext());
459
460 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
461 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
462 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
463
464 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
465 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
466 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
467
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000468 } else if (ObjCImplementationDecl *ImplD =
469 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000470 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
471 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000472
473 } else if (ObjCCategoryImplDecl *CImplD =
474 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000475 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000476 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000477 }
478
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000479 if (!Redecl && isRedeclaration()) {
480 // This is the last redeclaration, go back to the first method.
481 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
482 isInstanceMethod());
483 }
484
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000485 return Redecl ? Redecl : this;
486}
487
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000488ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
489 Decl *CtxD = cast<Decl>(getDeclContext());
490
491 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
492 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
493 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
494 isInstanceMethod()))
495 return MD;
496
497 } else if (ObjCCategoryImplDecl *CImplD =
498 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000499 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000500 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
501 isInstanceMethod()))
502 return MD;
503 }
504
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000505 if (isRedeclaration())
506 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
507 isInstanceMethod());
508
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000509 return this;
510}
511
John McCall85f3d762011-03-02 01:50:55 +0000512ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
513 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000514 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000515 return family;
516
John McCalld5313b02011-03-02 11:33:24 +0000517 // Check for an explicit attribute.
518 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
519 // The unfortunate necessity of mapping between enums here is due
520 // to the attributes framework.
521 switch (attr->getFamily()) {
522 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
523 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
524 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
525 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
526 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
527 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
528 }
529 Family = static_cast<unsigned>(family);
530 return family;
531 }
532
John McCall85f3d762011-03-02 01:50:55 +0000533 family = getSelector().getMethodFamily();
534 switch (family) {
535 case OMF_None: break;
536
537 // init only has a conventional meaning for an instance method, and
538 // it has to return an object.
539 case OMF_init:
540 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
541 family = OMF_None;
542 break;
543
544 // alloc/copy/new have a conventional meaning for both class and
545 // instance methods, but they require an object return.
546 case OMF_alloc:
547 case OMF_copy:
548 case OMF_mutableCopy:
549 case OMF_new:
550 if (!getResultType()->isObjCObjectPointerType())
551 family = OMF_None;
552 break;
553
554 // These selectors have a conventional meaning only for instance methods.
555 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000556 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000557 case OMF_retain:
558 case OMF_release:
559 case OMF_autorelease:
560 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000561 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000562 if (!isInstanceMethod())
563 family = OMF_None;
564 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000565
566 case OMF_performSelector:
567 if (!isInstanceMethod() ||
568 !getResultType()->isObjCIdType())
569 family = OMF_None;
570 else {
571 unsigned noParams = param_size();
572 if (noParams < 1 || noParams > 3)
573 family = OMF_None;
574 else {
575 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
576 QualType ArgT = (*it);
577 if (!ArgT->isObjCSelType()) {
578 family = OMF_None;
579 break;
580 }
581 while (--noParams) {
582 it++;
583 ArgT = (*it);
584 if (!ArgT->isObjCIdType()) {
585 family = OMF_None;
586 break;
587 }
588 }
589 }
590 }
591 break;
592
John McCall85f3d762011-03-02 01:50:55 +0000593 }
594
595 // Cache the result.
596 Family = static_cast<unsigned>(family);
597 return family;
598}
599
Mike Stump1eb44332009-09-09 15:08:12 +0000600void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000601 const ObjCInterfaceDecl *OID) {
602 QualType selfTy;
603 if (isInstanceMethod()) {
604 // There may be no interface context due to error in declaration
605 // of the interface (which has been reported). Recover gracefully.
606 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000607 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000608 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000609 } else {
610 selfTy = Context.getObjCIdType();
611 }
612 } else // we have a factory method.
613 selfTy = Context.getObjCClassType();
614
John McCall7acddac2011-06-17 06:42:21 +0000615 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000616 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000617
618 if (Context.getLangOptions().ObjCAutoRefCount) {
619 if (isInstanceMethod()) {
620 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000621
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000622 // 'self' is always __strong. It's actually pseudo-strong except
623 // in init methods (or methods labeled ns_consumes_self), though.
624 Qualifiers qs;
625 qs.setObjCLifetime(Qualifiers::OCL_Strong);
626 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000627
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000628 // In addition, 'self' is const unless this is an init method.
629 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
630 selfTy = selfTy.withConst();
631 selfIsPseudoStrong = true;
632 }
633 }
634 else {
635 assert(isClassMethod());
636 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000637 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000638 selfIsPseudoStrong = true;
639 }
John McCallf85e1932011-06-15 23:02:42 +0000640 }
641
642 ImplicitParamDecl *self
643 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
644 &Context.Idents.get("self"), selfTy);
645 setSelfDecl(self);
646
647 if (selfIsConsumed)
648 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000649
John McCall7acddac2011-06-17 06:42:21 +0000650 if (selfIsPseudoStrong)
651 self->setARCPseudoStrong(true);
652
Mike Stump1eb44332009-09-09 15:08:12 +0000653 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
654 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000655 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000656}
657
Chris Lattnerab351632009-02-20 20:59:54 +0000658ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
659 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
660 return ID;
661 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
662 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000663 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000664 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000665
666 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000667 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000668}
669
Chris Lattnerab351632009-02-20 20:59:54 +0000670//===----------------------------------------------------------------------===//
671// ObjCInterfaceDecl
672//===----------------------------------------------------------------------===//
673
674ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
675 DeclContext *DC,
676 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000677 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000678 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000679 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000680 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000681 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
682 isInternal);
683 C.getObjCInterfaceType(Result, PrevDecl);
684
685 if (PrevDecl) {
686 Result->Data = PrevDecl->Data;
687 Result->setPreviousDeclaration(PrevDecl);
688 }
689
690 return Result;
691}
692
693ObjCInterfaceDecl *ObjCInterfaceDecl::CreateEmpty(ASTContext &C) {
694 return new (C) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
695 false);
Chris Lattnerab351632009-02-20 20:59:54 +0000696}
697
698ObjCInterfaceDecl::
699ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000700 SourceLocation CLoc, bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000701 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +0000702 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000703{
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +0000704 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000705}
706
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000707void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000708 assert(data().ExternallyCompleted && "Class is not externally completed");
709 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000710 getASTContext().getExternalSource()->CompleteType(
711 const_cast<ObjCInterfaceDecl *>(this));
712}
713
714void ObjCInterfaceDecl::setExternallyCompleted() {
715 assert(getASTContext().getExternalSource() &&
716 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000717 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000718 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000719 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000720}
721
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000722ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000723 if (const ObjCInterfaceDecl *Def = getDefinition()) {
724 if (data().ExternallyCompleted)
725 LoadExternalDefinition();
726
727 return getASTContext().getObjCImplementation(
728 const_cast<ObjCInterfaceDecl*>(Def));
729 }
730
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000731 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +0000732 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000733}
734
735void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000736 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000737}
738
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000739/// all_declared_ivar_begin - return first ivar declared in this class,
740/// its extensions and its implementation. Lazily build the list on first
741/// access.
742ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000743 // FIXME: Should make sure no callers ever do this.
744 if (!hasDefinition())
745 return 0;
746
747 if (data().IvarList)
748 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000749
750 ObjCIvarDecl *curIvar = 0;
751 if (!ivar_empty()) {
752 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000753 data().IvarList = (*I); ++I;
754 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000755 curIvar->setNextIvar(*I);
756 }
757
758 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
759 CDecl = CDecl->getNextClassExtension()) {
760 if (!CDecl->ivar_empty()) {
761 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
762 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000763 if (!data().IvarList) {
764 data().IvarList = (*I); ++I;
765 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000766 }
767 for ( ;I != E; curIvar = *I, ++I)
768 curIvar->setNextIvar(*I);
769 }
770 }
771
772 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
773 if (!ImplDecl->ivar_empty()) {
774 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
775 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000776 if (!data().IvarList) {
777 data().IvarList = (*I); ++I;
778 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000779 }
780 for ( ;I != E; curIvar = *I, ++I)
781 curIvar->setNextIvar(*I);
782 }
783 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000784 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000785}
Chris Lattnerab351632009-02-20 20:59:54 +0000786
787/// FindCategoryDeclaration - Finds category declaration in the list of
788/// categories for this class and returns it. Name of the category is passed
789/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000790///
Chris Lattnerab351632009-02-20 20:59:54 +0000791ObjCCategoryDecl *
792ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000793 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000794 LoadExternalDefinition();
795
Chris Lattnerab351632009-02-20 20:59:54 +0000796 for (ObjCCategoryDecl *Category = getCategoryList();
797 Category; Category = Category->getNextClassCategory())
798 if (Category->getIdentifier() == CategoryId)
799 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000800 return 0;
801}
802
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000803ObjCMethodDecl *
804ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
805 for (ObjCCategoryDecl *Category = getCategoryList();
806 Category; Category = Category->getNextClassCategory())
807 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
808 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
809 return MD;
810 return 0;
811}
812
813ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
814 for (ObjCCategoryDecl *Category = getCategoryList();
815 Category; Category = Category->getNextClassCategory())
816 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
817 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
818 return MD;
819 return 0;
820}
821
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000822/// ClassImplementsProtocol - Checks that 'lProto' protocol
823/// has been implemented in IDecl class, its super class or categories (if
824/// lookupCategory is true).
825bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
826 bool lookupCategory,
827 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000828 if (!hasDefinition())
829 return false;
830
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000831 ObjCInterfaceDecl *IDecl = this;
832 // 1st, look up the class.
833 const ObjCList<ObjCProtocolDecl> &Protocols =
834 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000836 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
837 E = Protocols.end(); PI != E; ++PI) {
838 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
839 return true;
840 // This is dubious and is added to be compatible with gcc. In gcc, it is
841 // also allowed assigning a protocol-qualified 'id' type to a LHS object
842 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
843 // object. This IMO, should be a bug.
844 // FIXME: Treat this as an extension, and flag this as an error when GCC
845 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000846 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000847 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
848 return true;
849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000851 // 2nd, look up the category.
852 if (lookupCategory)
853 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
854 CDecl = CDecl->getNextClassCategory()) {
855 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
856 E = CDecl->protocol_end(); PI != E; ++PI)
857 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
858 return true;
859 }
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000861 // 3rd, look up the super class(s)
862 if (IDecl->getSuperClass())
863 return
864 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
865 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000867 return false;
868}
869
Chris Lattnerab351632009-02-20 20:59:54 +0000870//===----------------------------------------------------------------------===//
871// ObjCIvarDecl
872//===----------------------------------------------------------------------===//
873
Daniel Dunbara0654922010-04-02 20:10:03 +0000874ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000875 SourceLocation StartLoc,
876 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000877 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000878 AccessControl ac, Expr *BW,
879 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000880 if (DC) {
881 // Ivar's can only appear in interfaces, implementations (via synthesized
882 // properties), and class extensions (via direct declaration, or synthesized
883 // properties).
884 //
885 // FIXME: This should really be asserting this:
886 // (isa<ObjCCategoryDecl>(DC) &&
887 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
888 // but unfortunately we sometimes place ivars into non-class extension
889 // categories on error. This breaks an AST invariant, and should not be
890 // fixed.
891 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
892 isa<ObjCCategoryDecl>(DC)) &&
893 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000894 // Once a new ivar is created in any of class/class-extension/implementation
895 // decl contexts, the previously built IvarList must be rebuilt.
896 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
897 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000898 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000899 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000900 if (BW)
901 IM->setHasSynthBitfield(true);
Chad Rosier30601782011-08-17 23:08:45 +0000902 } else {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000903 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
904 ID = CD->getClassInterface();
905 if (BW)
906 CD->setHasSynthBitfield(true);
907 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000908 }
909 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000910 }
911
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000912 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
913 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000914}
915
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000916const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
917 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000918
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000919 switch (DC->getKind()) {
920 default:
921 case ObjCCategoryImpl:
922 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000923 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000924
925 // Ivars can only appear in class extension categories.
926 case ObjCCategory: {
927 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
928 assert(CD->IsClassExtension() && "invalid container for ivar!");
929 return CD->getClassInterface();
930 }
931
932 case ObjCImplementation:
933 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
934
935 case ObjCInterface:
936 return cast<ObjCInterfaceDecl>(DC);
937 }
938}
Chris Lattnerab351632009-02-20 20:59:54 +0000939
940//===----------------------------------------------------------------------===//
941// ObjCAtDefsFieldDecl
942//===----------------------------------------------------------------------===//
943
944ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000945*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
946 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000947 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000948 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000949}
950
Chris Lattnerab351632009-02-20 20:59:54 +0000951//===----------------------------------------------------------------------===//
952// ObjCProtocolDecl
953//===----------------------------------------------------------------------===//
954
955ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000956 IdentifierInfo *Id,
957 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000958 SourceLocation atStartLoc,
959 bool isForwardDecl) {
960 return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, isForwardDecl);
Chris Lattnerab351632009-02-20 20:59:54 +0000961}
962
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000963ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
964 ObjCProtocolDecl *PDecl = this;
965
966 if (Name == getIdentifier())
967 return PDecl;
968
969 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
970 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
971 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000973 return NULL;
974}
975
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000976// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000977// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000978ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
979 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000980 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000982 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000983 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattnerab351632009-02-20 20:59:54 +0000985 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000986 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000987 return MethodDecl;
988 return NULL;
989}
990
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000991void ObjCProtocolDecl::completedForwardDecl() {
992 assert(isForwardDecl() && "Only valid to call for forward refs");
993 isForwardProtoDecl = false;
994 if (ASTMutationListener *L = getASTContext().getASTMutationListener())
995 L->CompletedObjCForwardRef(this);
996}
997
Chris Lattnerab351632009-02-20 20:59:54 +0000998//===----------------------------------------------------------------------===//
999// ObjCClassDecl
1000//===----------------------------------------------------------------------===//
1001
Mike Stump1eb44332009-09-09 15:08:12 +00001002ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Douglas Gregoraf764722011-12-14 17:12:03 +00001003 ObjCInterfaceDecl *Interface,
1004 SourceLocation InterfaceLoc)
1005 : Decl(ObjCClass, DC, L), Interface(Interface), InterfaceLoc(InterfaceLoc)
1006{
Ted Kremenek321c22f2009-11-18 00:28:11 +00001007}
Chris Lattner38af2de2009-02-20 21:35:13 +00001008
Chris Lattnerab351632009-02-20 20:59:54 +00001009ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
1010 SourceLocation L,
Douglas Gregoraf764722011-12-14 17:12:03 +00001011 ObjCInterfaceDecl *Interface,
1012 SourceLocation InterfaceLoc) {
1013 return new (C) ObjCClassDecl(DC, L, Interface, InterfaceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001014}
1015
Ted Kremenek2dbdd622009-11-18 01:26:56 +00001016SourceRange ObjCClassDecl::getSourceRange() const {
Douglas Gregoraf764722011-12-14 17:12:03 +00001017 return SourceRange(getLocation(), InterfaceLoc);
Ted Kremenek2dbdd622009-11-18 01:26:56 +00001018}
1019
Chris Lattnerab351632009-02-20 20:59:54 +00001020//===----------------------------------------------------------------------===//
1021// ObjCForwardProtocolDecl
1022//===----------------------------------------------------------------------===//
1023
Chris Lattner38af2de2009-02-20 21:35:13 +00001024ObjCForwardProtocolDecl::
1025ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
1026 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +00001027 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +00001028: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +00001029 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +00001030}
1031
1032
Chris Lattnerab351632009-02-20 20:59:54 +00001033ObjCForwardProtocolDecl *
1034ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +00001035 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +00001036 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +00001037 unsigned NumElts,
1038 const SourceLocation *Locs) {
1039 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +00001040}
1041
Chris Lattnerab351632009-02-20 20:59:54 +00001042//===----------------------------------------------------------------------===//
1043// ObjCCategoryDecl
1044//===----------------------------------------------------------------------===//
1045
1046ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001047 SourceLocation AtLoc,
1048 SourceLocation ClassNameLoc,
1049 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001050 IdentifierInfo *Id,
1051 ObjCInterfaceDecl *IDecl) {
1052 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1053 CategoryNameLoc, Id,
1054 IDecl);
1055 if (IDecl) {
1056 // Link this category into its class's category list.
1057 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001058 if (IDecl->hasDefinition()) {
1059 IDecl->setCategoryList(CatDecl);
1060 if (ASTMutationListener *L = C.getASTMutationListener())
1061 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1062 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001063 }
1064
1065 return CatDecl;
1066}
1067
1068ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) {
1069 return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1070 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001071}
1072
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001073ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1074 return getASTContext().getObjCImplementation(
1075 const_cast<ObjCCategoryDecl*>(this));
1076}
1077
1078void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1079 getASTContext().setObjCImplementation(this, ImplD);
1080}
1081
1082
Chris Lattnerab351632009-02-20 20:59:54 +00001083//===----------------------------------------------------------------------===//
1084// ObjCCategoryImplDecl
1085//===----------------------------------------------------------------------===//
1086
1087ObjCCategoryImplDecl *
1088ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001089 IdentifierInfo *Id,
1090 ObjCInterfaceDecl *ClassInterface,
1091 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001092 SourceLocation atStartLoc,
1093 SourceLocation CategoryNameLoc) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001094 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001095 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001096}
1097
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001098ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001099 // The class interface might be NULL if we are working with invalid code.
1100 if (const ObjCInterfaceDecl *ID = getClassInterface())
1101 return ID->FindCategoryDeclaration(getIdentifier());
1102 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001103}
1104
Chris Lattnerab351632009-02-20 20:59:54 +00001105
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001106void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001107 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001108 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001109 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001110}
1111
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001112void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1113 ASTContext &Ctx = getASTContext();
1114
1115 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001116 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001117 if (IFace)
1118 Ctx.setObjCImplementation(IFace, ImplD);
1119
Duncan Sands98f2cca2009-07-21 07:56:29 +00001120 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001121 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1122 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1123 Ctx.setObjCImplementation(CD, ImplD);
1124 }
1125
1126 ClassInterface = IFace;
1127}
1128
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001129/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001130/// properties implemented in this category @implementation block and returns
1131/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001132///
Chris Lattner3aa18612009-02-28 18:42:10 +00001133ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001134FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1135 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001136 ObjCPropertyImplDecl *PID = *i;
1137 if (PID->getPropertyIvarDecl() &&
1138 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1139 return PID;
1140 }
1141 return 0;
1142}
1143
1144/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1145/// added to the list of those properties @synthesized/@dynamic in this
1146/// category @implementation block.
1147///
Chris Lattner3aa18612009-02-28 18:42:10 +00001148ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001149FindPropertyImplDecl(IdentifierInfo *Id) const {
1150 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001151 ObjCPropertyImplDecl *PID = *i;
1152 if (PID->getPropertyDecl()->getIdentifier() == Id)
1153 return PID;
1154 }
1155 return 0;
1156}
1157
Chris Lattner5f9e2722011-07-23 10:55:15 +00001158raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001159 const ObjCCategoryImplDecl *CID) {
1160 OS << CID->getName();
1161 return OS;
1162}
1163
Chris Lattnerab351632009-02-20 20:59:54 +00001164//===----------------------------------------------------------------------===//
1165// ObjCImplementationDecl
1166//===----------------------------------------------------------------------===//
1167
1168ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001169ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001170 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001171 ObjCInterfaceDecl *SuperDecl,
1172 SourceLocation nameLoc,
1173 SourceLocation atStartLoc) {
1174 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1175 nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001176}
1177
John McCallda6d9762011-07-22 04:15:06 +00001178void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1179 CXXCtorInitializer ** initializers,
1180 unsigned numInitializers) {
1181 if (numInitializers > 0) {
1182 NumIvarInitializers = numInitializers;
1183 CXXCtorInitializer **ivarInitializers =
1184 new (C) CXXCtorInitializer*[NumIvarInitializers];
1185 memcpy(ivarInitializers, initializers,
1186 numInitializers * sizeof(CXXCtorInitializer*));
1187 IvarInitializers = ivarInitializers;
1188 }
1189}
1190
Chris Lattner5f9e2722011-07-23 10:55:15 +00001191raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001192 const ObjCImplementationDecl *ID) {
1193 OS << ID->getName();
1194 return OS;
1195}
1196
Chris Lattnerab351632009-02-20 20:59:54 +00001197//===----------------------------------------------------------------------===//
1198// ObjCCompatibleAliasDecl
1199//===----------------------------------------------------------------------===//
1200
1201ObjCCompatibleAliasDecl *
1202ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1203 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001204 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001205 ObjCInterfaceDecl* AliasedClass) {
1206 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1207}
1208
1209//===----------------------------------------------------------------------===//
1210// ObjCPropertyDecl
1211//===----------------------------------------------------------------------===//
1212
1213ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1214 SourceLocation L,
1215 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001216 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +00001217 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001218 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001219 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001220}
1221
Chris Lattnerab351632009-02-20 20:59:54 +00001222//===----------------------------------------------------------------------===//
1223// ObjCPropertyImplDecl
1224//===----------------------------------------------------------------------===//
1225
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001226ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001227 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001228 SourceLocation atLoc,
1229 SourceLocation L,
1230 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001231 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001232 ObjCIvarDecl *ivar,
1233 SourceLocation ivarLoc) {
1234 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1235 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001236}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001237
Douglas Gregora4ffd852010-11-17 01:03:52 +00001238SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1239 SourceLocation EndLoc = getLocation();
1240 if (IvarLoc.isValid())
1241 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001242
Douglas Gregora4ffd852010-11-17 01:03:52 +00001243 return SourceRange(AtLoc, EndLoc);
1244}