blob: d420b2be49fbbd4b3fc73f90e4b87216a801c2e9 [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();
228 Data->Definition = this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000229
230 // Update all of the declarations with a pointer to the definition.
231 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
232 RD != RDEnd; ++RD) {
233 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000234 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000235 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000236}
237
238void ObjCInterfaceDecl::startDefinition() {
239 allocateDefinitionData();
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000240 if (ASTMutationListener *L = getASTContext().getASTMutationListener())
241 L->CompletedObjCForwardRef(this);
242}
243
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000244/// getFirstClassExtension - Find first class extension of the given class.
245ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
246 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000247 CDecl = CDecl->getNextClassCategory())
248 if (CDecl->IsClassExtension())
249 return CDecl;
250 return 0;
251}
252
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000253/// getNextClassCategory - Find next class extension in list of categories.
254const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
255 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
256 CDecl = CDecl->getNextClassCategory())
257 if (CDecl->IsClassExtension())
258 return CDecl;
259 return 0;
260}
261
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000262ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
263 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000264 // FIXME: Should make sure no callers ever do this.
265 if (!hasDefinition())
266 return 0;
267
268 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000269 LoadExternalDefinition();
270
Chris Lattner1e03a562008-03-16 00:19:01 +0000271 ObjCInterfaceDecl* ClassDecl = this;
272 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000273 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000274 clsDeclared = ClassDecl;
275 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000276 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000277 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
278 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000279 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
280 clsDeclared = ClassDecl;
281 return I;
282 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000283 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000284
Chris Lattner1e03a562008-03-16 00:19:01 +0000285 ClassDecl = ClassDecl->getSuperClass();
286 }
287 return NULL;
288}
289
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000290/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
291/// class whose name is passed as argument. If it is not one of the super classes
292/// the it returns NULL.
293ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
294 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000295 // FIXME: Should make sure no callers ever do this.
296 if (!hasDefinition())
297 return 0;
298
299 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000300 LoadExternalDefinition();
301
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000302 ObjCInterfaceDecl* ClassDecl = this;
303 while (ClassDecl != NULL) {
304 if (ClassDecl->getIdentifier() == ICName)
305 return ClassDecl;
306 ClassDecl = ClassDecl->getSuperClass();
307 }
308 return NULL;
309}
310
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000311/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000312/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000313ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
314 bool isInstance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000315 // FIXME: Should make sure no callers ever do this.
316 if (!hasDefinition())
317 return 0;
318
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000319 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000320 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000322 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000323 LoadExternalDefinition();
324
Chris Lattner1e03a562008-03-16 00:19:01 +0000325 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000326 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000327 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Chris Lattner1e03a562008-03-16 00:19:01 +0000329 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000330 const ObjCList<ObjCProtocolDecl> &Protocols =
331 ClassDecl->getReferencedProtocols();
332 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
333 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000334 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000335 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner1e03a562008-03-16 00:19:01 +0000337 // Didn't find one yet - now look through categories.
338 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
339 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000340 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000341 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Steve Naroffb79c01e2008-12-08 20:57:28 +0000343 // Didn't find one yet - look through protocols.
344 const ObjCList<ObjCProtocolDecl> &Protocols =
345 CatDecl->getReferencedProtocols();
346 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
347 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000348 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000349 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000350 CatDecl = CatDecl->getNextClassCategory();
351 }
352 ClassDecl = ClassDecl->getSuperClass();
353 }
354 return NULL;
355}
356
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000357ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
358 const Selector &Sel,
359 bool Instance) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000360 // FIXME: Should make sure no callers ever do this.
361 if (!hasDefinition())
362 return 0;
363
364 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000365 LoadExternalDefinition();
366
Steve Naroffd789d3d2009-10-01 23:46:04 +0000367 ObjCMethodDecl *Method = 0;
368 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000369 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
370 : ImpDecl->getClassMethod(Sel);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000371
372 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000373 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000374 return Method;
375}
Chris Lattnerab351632009-02-20 20:59:54 +0000376
377//===----------------------------------------------------------------------===//
378// ObjCMethodDecl
379//===----------------------------------------------------------------------===//
380
381ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000382 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000383 SourceLocation endLoc,
384 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000385 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000386 DeclContext *contextDecl,
387 bool isInstance,
388 bool isVariadic,
389 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000390 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000391 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000392 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000393 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000394 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000395 SelInfo, T, ResultTInfo, contextDecl,
396 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000397 isVariadic, isSynthesized, isImplicitlyDeclared,
398 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000399 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000400 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000401}
402
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000403void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
404 assert(PrevMethod);
405 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
406 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000407 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000408}
409
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000410void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
411 ArrayRef<ParmVarDecl*> Params,
412 ArrayRef<SourceLocation> SelLocs) {
413 ParamsAndSelLocs = 0;
414 NumParams = Params.size();
415 if (Params.empty() && SelLocs.empty())
416 return;
417
418 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
419 sizeof(SourceLocation) * SelLocs.size();
420 ParamsAndSelLocs = C.Allocate(Size);
421 std::copy(Params.begin(), Params.end(), getParams());
422 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
423}
424
425void ObjCMethodDecl::getSelectorLocs(
426 SmallVectorImpl<SourceLocation> &SelLocs) const {
427 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
428 SelLocs.push_back(getSelectorLoc(i));
429}
430
431void ObjCMethodDecl::setMethodParams(ASTContext &C,
432 ArrayRef<ParmVarDecl*> Params,
433 ArrayRef<SourceLocation> SelLocs) {
434 assert((!SelLocs.empty() || isImplicit()) &&
435 "No selector locs for non-implicit method");
436 if (isImplicit())
437 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
438
439 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
440 if (SelLocsKind != SelLoc_NonStandard)
441 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
442
443 setParamsAndSelLocs(C, Params, SelLocs);
444}
445
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000446/// \brief A definition will return its interface declaration.
447/// An interface declaration will return its definition.
448/// Otherwise it will return itself.
449ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
450 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000451 ObjCMethodDecl *Redecl = 0;
452 if (HasRedeclaration)
453 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000454 if (Redecl)
455 return Redecl;
456
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000457 Decl *CtxD = cast<Decl>(getDeclContext());
458
459 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
460 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
461 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
462
463 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
464 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
465 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
466
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000467 } else if (ObjCImplementationDecl *ImplD =
468 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000469 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
470 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000471
472 } else if (ObjCCategoryImplDecl *CImplD =
473 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000474 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000475 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000476 }
477
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000478 if (!Redecl && isRedeclaration()) {
479 // This is the last redeclaration, go back to the first method.
480 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
481 isInstanceMethod());
482 }
483
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000484 return Redecl ? Redecl : this;
485}
486
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000487ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
488 Decl *CtxD = cast<Decl>(getDeclContext());
489
490 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
491 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
492 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
493 isInstanceMethod()))
494 return MD;
495
496 } else if (ObjCCategoryImplDecl *CImplD =
497 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000498 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000499 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
500 isInstanceMethod()))
501 return MD;
502 }
503
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000504 if (isRedeclaration())
505 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
506 isInstanceMethod());
507
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000508 return this;
509}
510
John McCall85f3d762011-03-02 01:50:55 +0000511ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
512 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000513 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000514 return family;
515
John McCalld5313b02011-03-02 11:33:24 +0000516 // Check for an explicit attribute.
517 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
518 // The unfortunate necessity of mapping between enums here is due
519 // to the attributes framework.
520 switch (attr->getFamily()) {
521 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
522 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
523 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
524 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
525 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
526 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
527 }
528 Family = static_cast<unsigned>(family);
529 return family;
530 }
531
John McCall85f3d762011-03-02 01:50:55 +0000532 family = getSelector().getMethodFamily();
533 switch (family) {
534 case OMF_None: break;
535
536 // init only has a conventional meaning for an instance method, and
537 // it has to return an object.
538 case OMF_init:
539 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
540 family = OMF_None;
541 break;
542
543 // alloc/copy/new have a conventional meaning for both class and
544 // instance methods, but they require an object return.
545 case OMF_alloc:
546 case OMF_copy:
547 case OMF_mutableCopy:
548 case OMF_new:
549 if (!getResultType()->isObjCObjectPointerType())
550 family = OMF_None;
551 break;
552
553 // These selectors have a conventional meaning only for instance methods.
554 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000555 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000556 case OMF_retain:
557 case OMF_release:
558 case OMF_autorelease:
559 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000560 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000561 if (!isInstanceMethod())
562 family = OMF_None;
563 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000564
565 case OMF_performSelector:
566 if (!isInstanceMethod() ||
567 !getResultType()->isObjCIdType())
568 family = OMF_None;
569 else {
570 unsigned noParams = param_size();
571 if (noParams < 1 || noParams > 3)
572 family = OMF_None;
573 else {
574 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
575 QualType ArgT = (*it);
576 if (!ArgT->isObjCSelType()) {
577 family = OMF_None;
578 break;
579 }
580 while (--noParams) {
581 it++;
582 ArgT = (*it);
583 if (!ArgT->isObjCIdType()) {
584 family = OMF_None;
585 break;
586 }
587 }
588 }
589 }
590 break;
591
John McCall85f3d762011-03-02 01:50:55 +0000592 }
593
594 // Cache the result.
595 Family = static_cast<unsigned>(family);
596 return family;
597}
598
Mike Stump1eb44332009-09-09 15:08:12 +0000599void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000600 const ObjCInterfaceDecl *OID) {
601 QualType selfTy;
602 if (isInstanceMethod()) {
603 // There may be no interface context due to error in declaration
604 // of the interface (which has been reported). Recover gracefully.
605 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000606 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000607 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000608 } else {
609 selfTy = Context.getObjCIdType();
610 }
611 } else // we have a factory method.
612 selfTy = Context.getObjCClassType();
613
John McCall7acddac2011-06-17 06:42:21 +0000614 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000615 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000616
617 if (Context.getLangOptions().ObjCAutoRefCount) {
618 if (isInstanceMethod()) {
619 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000620
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000621 // 'self' is always __strong. It's actually pseudo-strong except
622 // in init methods (or methods labeled ns_consumes_self), though.
623 Qualifiers qs;
624 qs.setObjCLifetime(Qualifiers::OCL_Strong);
625 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000626
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000627 // In addition, 'self' is const unless this is an init method.
628 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
629 selfTy = selfTy.withConst();
630 selfIsPseudoStrong = true;
631 }
632 }
633 else {
634 assert(isClassMethod());
635 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000636 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000637 selfIsPseudoStrong = true;
638 }
John McCallf85e1932011-06-15 23:02:42 +0000639 }
640
641 ImplicitParamDecl *self
642 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
643 &Context.Idents.get("self"), selfTy);
644 setSelfDecl(self);
645
646 if (selfIsConsumed)
647 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000648
John McCall7acddac2011-06-17 06:42:21 +0000649 if (selfIsPseudoStrong)
650 self->setARCPseudoStrong(true);
651
Mike Stump1eb44332009-09-09 15:08:12 +0000652 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
653 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000654 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000655}
656
Chris Lattnerab351632009-02-20 20:59:54 +0000657ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
658 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
659 return ID;
660 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
661 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000662 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000663 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000664
665 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000666 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000667}
668
Chris Lattnerab351632009-02-20 20:59:54 +0000669//===----------------------------------------------------------------------===//
670// ObjCInterfaceDecl
671//===----------------------------------------------------------------------===//
672
673ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
674 DeclContext *DC,
675 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000676 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000677 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000678 bool isInternal){
679 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000680}
681
682ObjCInterfaceDecl::
683ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000684 SourceLocation CLoc, bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000685 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +0000686 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000687{
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +0000688 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000689}
690
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000691void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000692 assert(data().ExternallyCompleted && "Class is not externally completed");
693 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000694 getASTContext().getExternalSource()->CompleteType(
695 const_cast<ObjCInterfaceDecl *>(this));
696}
697
698void ObjCInterfaceDecl::setExternallyCompleted() {
699 assert(getASTContext().getExternalSource() &&
700 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000701 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000702 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000703 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000704}
705
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000706ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000707 if (const ObjCInterfaceDecl *Def = getDefinition()) {
708 if (data().ExternallyCompleted)
709 LoadExternalDefinition();
710
711 return getASTContext().getObjCImplementation(
712 const_cast<ObjCInterfaceDecl*>(Def));
713 }
714
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000715 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +0000716 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000717}
718
719void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000720 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000721}
722
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000723/// all_declared_ivar_begin - return first ivar declared in this class,
724/// its extensions and its implementation. Lazily build the list on first
725/// access.
726ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000727 // FIXME: Should make sure no callers ever do this.
728 if (!hasDefinition())
729 return 0;
730
731 if (data().IvarList)
732 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000733
734 ObjCIvarDecl *curIvar = 0;
735 if (!ivar_empty()) {
736 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000737 data().IvarList = (*I); ++I;
738 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000739 curIvar->setNextIvar(*I);
740 }
741
742 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
743 CDecl = CDecl->getNextClassExtension()) {
744 if (!CDecl->ivar_empty()) {
745 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
746 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000747 if (!data().IvarList) {
748 data().IvarList = (*I); ++I;
749 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000750 }
751 for ( ;I != E; curIvar = *I, ++I)
752 curIvar->setNextIvar(*I);
753 }
754 }
755
756 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
757 if (!ImplDecl->ivar_empty()) {
758 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
759 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000760 if (!data().IvarList) {
761 data().IvarList = (*I); ++I;
762 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000763 }
764 for ( ;I != E; curIvar = *I, ++I)
765 curIvar->setNextIvar(*I);
766 }
767 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000768 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000769}
Chris Lattnerab351632009-02-20 20:59:54 +0000770
771/// FindCategoryDeclaration - Finds category declaration in the list of
772/// categories for this class and returns it. Name of the category is passed
773/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000774///
Chris Lattnerab351632009-02-20 20:59:54 +0000775ObjCCategoryDecl *
776ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000777 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000778 LoadExternalDefinition();
779
Chris Lattnerab351632009-02-20 20:59:54 +0000780 for (ObjCCategoryDecl *Category = getCategoryList();
781 Category; Category = Category->getNextClassCategory())
782 if (Category->getIdentifier() == CategoryId)
783 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000784 return 0;
785}
786
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000787ObjCMethodDecl *
788ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
789 for (ObjCCategoryDecl *Category = getCategoryList();
790 Category; Category = Category->getNextClassCategory())
791 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
792 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
793 return MD;
794 return 0;
795}
796
797ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
798 for (ObjCCategoryDecl *Category = getCategoryList();
799 Category; Category = Category->getNextClassCategory())
800 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
801 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
802 return MD;
803 return 0;
804}
805
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000806/// ClassImplementsProtocol - Checks that 'lProto' protocol
807/// has been implemented in IDecl class, its super class or categories (if
808/// lookupCategory is true).
809bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
810 bool lookupCategory,
811 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000812 if (!hasDefinition())
813 return false;
814
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000815 ObjCInterfaceDecl *IDecl = this;
816 // 1st, look up the class.
817 const ObjCList<ObjCProtocolDecl> &Protocols =
818 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000820 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
821 E = Protocols.end(); PI != E; ++PI) {
822 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
823 return true;
824 // This is dubious and is added to be compatible with gcc. In gcc, it is
825 // also allowed assigning a protocol-qualified 'id' type to a LHS object
826 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
827 // object. This IMO, should be a bug.
828 // FIXME: Treat this as an extension, and flag this as an error when GCC
829 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000830 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000831 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
832 return true;
833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000835 // 2nd, look up the category.
836 if (lookupCategory)
837 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
838 CDecl = CDecl->getNextClassCategory()) {
839 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
840 E = CDecl->protocol_end(); PI != E; ++PI)
841 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
842 return true;
843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000845 // 3rd, look up the super class(s)
846 if (IDecl->getSuperClass())
847 return
848 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
849 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000851 return false;
852}
853
Douglas Gregor7723fec2011-12-15 20:29:51 +0000854void ObjCInterfaceDecl::setPreviousDeclaration(ObjCInterfaceDecl *PrevDecl) {
855 redeclarable_base::setPreviousDeclaration(PrevDecl);
856
857 // Inherit the 'Data' pointer from the previous declaration.
858 if (PrevDecl)
859 Data = PrevDecl->Data;
860}
861
Chris Lattnerab351632009-02-20 20:59:54 +0000862//===----------------------------------------------------------------------===//
863// ObjCIvarDecl
864//===----------------------------------------------------------------------===//
865
Daniel Dunbara0654922010-04-02 20:10:03 +0000866ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000867 SourceLocation StartLoc,
868 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000869 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000870 AccessControl ac, Expr *BW,
871 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000872 if (DC) {
873 // Ivar's can only appear in interfaces, implementations (via synthesized
874 // properties), and class extensions (via direct declaration, or synthesized
875 // properties).
876 //
877 // FIXME: This should really be asserting this:
878 // (isa<ObjCCategoryDecl>(DC) &&
879 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
880 // but unfortunately we sometimes place ivars into non-class extension
881 // categories on error. This breaks an AST invariant, and should not be
882 // fixed.
883 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
884 isa<ObjCCategoryDecl>(DC)) &&
885 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000886 // Once a new ivar is created in any of class/class-extension/implementation
887 // decl contexts, the previously built IvarList must be rebuilt.
888 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
889 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000890 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000891 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000892 if (BW)
893 IM->setHasSynthBitfield(true);
Chad Rosier30601782011-08-17 23:08:45 +0000894 } else {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000895 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
896 ID = CD->getClassInterface();
897 if (BW)
898 CD->setHasSynthBitfield(true);
899 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000900 }
901 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000902 }
903
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000904 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
905 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000906}
907
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000908const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
909 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000910
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000911 switch (DC->getKind()) {
912 default:
913 case ObjCCategoryImpl:
914 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000915 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000916
917 // Ivars can only appear in class extension categories.
918 case ObjCCategory: {
919 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
920 assert(CD->IsClassExtension() && "invalid container for ivar!");
921 return CD->getClassInterface();
922 }
923
924 case ObjCImplementation:
925 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
926
927 case ObjCInterface:
928 return cast<ObjCInterfaceDecl>(DC);
929 }
930}
Chris Lattnerab351632009-02-20 20:59:54 +0000931
932//===----------------------------------------------------------------------===//
933// ObjCAtDefsFieldDecl
934//===----------------------------------------------------------------------===//
935
936ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000937*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
938 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000939 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000940 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000941}
942
Chris Lattnerab351632009-02-20 20:59:54 +0000943//===----------------------------------------------------------------------===//
944// ObjCProtocolDecl
945//===----------------------------------------------------------------------===//
946
947ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000948 IdentifierInfo *Id,
949 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000950 SourceLocation atStartLoc,
951 bool isForwardDecl) {
952 return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, isForwardDecl);
Chris Lattnerab351632009-02-20 20:59:54 +0000953}
954
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000955ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
956 ObjCProtocolDecl *PDecl = this;
957
958 if (Name == getIdentifier())
959 return PDecl;
960
961 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
962 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
963 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000965 return NULL;
966}
967
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000968// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000969// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000970ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
971 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000972 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000974 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000975 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Chris Lattnerab351632009-02-20 20:59:54 +0000977 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000978 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000979 return MethodDecl;
980 return NULL;
981}
982
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000983void ObjCProtocolDecl::completedForwardDecl() {
984 assert(isForwardDecl() && "Only valid to call for forward refs");
985 isForwardProtoDecl = false;
986 if (ASTMutationListener *L = getASTContext().getASTMutationListener())
987 L->CompletedObjCForwardRef(this);
988}
989
Chris Lattnerab351632009-02-20 20:59:54 +0000990//===----------------------------------------------------------------------===//
991// ObjCClassDecl
992//===----------------------------------------------------------------------===//
993
Mike Stump1eb44332009-09-09 15:08:12 +0000994ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Douglas Gregoraf764722011-12-14 17:12:03 +0000995 ObjCInterfaceDecl *Interface,
996 SourceLocation InterfaceLoc)
997 : Decl(ObjCClass, DC, L), Interface(Interface), InterfaceLoc(InterfaceLoc)
998{
Ted Kremenek321c22f2009-11-18 00:28:11 +0000999}
Chris Lattner38af2de2009-02-20 21:35:13 +00001000
Chris Lattnerab351632009-02-20 20:59:54 +00001001ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
1002 SourceLocation L,
Douglas Gregoraf764722011-12-14 17:12:03 +00001003 ObjCInterfaceDecl *Interface,
1004 SourceLocation InterfaceLoc) {
1005 return new (C) ObjCClassDecl(DC, L, Interface, InterfaceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001006}
1007
Ted Kremenek2dbdd622009-11-18 01:26:56 +00001008SourceRange ObjCClassDecl::getSourceRange() const {
Douglas Gregoraf764722011-12-14 17:12:03 +00001009 return SourceRange(getLocation(), InterfaceLoc);
Ted Kremenek2dbdd622009-11-18 01:26:56 +00001010}
1011
Chris Lattnerab351632009-02-20 20:59:54 +00001012//===----------------------------------------------------------------------===//
1013// ObjCForwardProtocolDecl
1014//===----------------------------------------------------------------------===//
1015
Chris Lattner38af2de2009-02-20 21:35:13 +00001016ObjCForwardProtocolDecl::
1017ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
1018 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +00001019 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +00001020: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +00001021 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +00001022}
1023
1024
Chris Lattnerab351632009-02-20 20:59:54 +00001025ObjCForwardProtocolDecl *
1026ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +00001027 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +00001028 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +00001029 unsigned NumElts,
1030 const SourceLocation *Locs) {
1031 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +00001032}
1033
Chris Lattnerab351632009-02-20 20:59:54 +00001034//===----------------------------------------------------------------------===//
1035// ObjCCategoryDecl
1036//===----------------------------------------------------------------------===//
1037
1038ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001039 SourceLocation AtLoc,
1040 SourceLocation ClassNameLoc,
1041 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001042 IdentifierInfo *Id,
1043 ObjCInterfaceDecl *IDecl) {
1044 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1045 CategoryNameLoc, Id,
1046 IDecl);
1047 if (IDecl) {
1048 // Link this category into its class's category list.
1049 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001050 if (IDecl->hasDefinition()) {
1051 IDecl->setCategoryList(CatDecl);
1052 if (ASTMutationListener *L = C.getASTMutationListener())
1053 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1054 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001055 }
1056
1057 return CatDecl;
1058}
1059
1060ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) {
1061 return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1062 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001063}
1064
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001065ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1066 return getASTContext().getObjCImplementation(
1067 const_cast<ObjCCategoryDecl*>(this));
1068}
1069
1070void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1071 getASTContext().setObjCImplementation(this, ImplD);
1072}
1073
1074
Chris Lattnerab351632009-02-20 20:59:54 +00001075//===----------------------------------------------------------------------===//
1076// ObjCCategoryImplDecl
1077//===----------------------------------------------------------------------===//
1078
1079ObjCCategoryImplDecl *
1080ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001081 IdentifierInfo *Id,
1082 ObjCInterfaceDecl *ClassInterface,
1083 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001084 SourceLocation atStartLoc,
1085 SourceLocation CategoryNameLoc) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001086 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001087 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001088}
1089
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001090ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001091 // The class interface might be NULL if we are working with invalid code.
1092 if (const ObjCInterfaceDecl *ID = getClassInterface())
1093 return ID->FindCategoryDeclaration(getIdentifier());
1094 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001095}
1096
Chris Lattnerab351632009-02-20 20:59:54 +00001097
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001098void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001099 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001100 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001101 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001102}
1103
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001104void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1105 ASTContext &Ctx = getASTContext();
1106
1107 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001108 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001109 if (IFace)
1110 Ctx.setObjCImplementation(IFace, ImplD);
1111
Duncan Sands98f2cca2009-07-21 07:56:29 +00001112 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001113 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1114 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1115 Ctx.setObjCImplementation(CD, ImplD);
1116 }
1117
1118 ClassInterface = IFace;
1119}
1120
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001121/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001122/// properties implemented in this category @implementation block and returns
1123/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001124///
Chris Lattner3aa18612009-02-28 18:42:10 +00001125ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001126FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1127 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001128 ObjCPropertyImplDecl *PID = *i;
1129 if (PID->getPropertyIvarDecl() &&
1130 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1131 return PID;
1132 }
1133 return 0;
1134}
1135
1136/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1137/// added to the list of those properties @synthesized/@dynamic in this
1138/// category @implementation block.
1139///
Chris Lattner3aa18612009-02-28 18:42:10 +00001140ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001141FindPropertyImplDecl(IdentifierInfo *Id) const {
1142 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001143 ObjCPropertyImplDecl *PID = *i;
1144 if (PID->getPropertyDecl()->getIdentifier() == Id)
1145 return PID;
1146 }
1147 return 0;
1148}
1149
Chris Lattner5f9e2722011-07-23 10:55:15 +00001150raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001151 const ObjCCategoryImplDecl *CID) {
1152 OS << CID->getName();
1153 return OS;
1154}
1155
Chris Lattnerab351632009-02-20 20:59:54 +00001156//===----------------------------------------------------------------------===//
1157// ObjCImplementationDecl
1158//===----------------------------------------------------------------------===//
1159
1160ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001161ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001162 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001163 ObjCInterfaceDecl *SuperDecl,
1164 SourceLocation nameLoc,
1165 SourceLocation atStartLoc) {
1166 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1167 nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001168}
1169
John McCallda6d9762011-07-22 04:15:06 +00001170void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1171 CXXCtorInitializer ** initializers,
1172 unsigned numInitializers) {
1173 if (numInitializers > 0) {
1174 NumIvarInitializers = numInitializers;
1175 CXXCtorInitializer **ivarInitializers =
1176 new (C) CXXCtorInitializer*[NumIvarInitializers];
1177 memcpy(ivarInitializers, initializers,
1178 numInitializers * sizeof(CXXCtorInitializer*));
1179 IvarInitializers = ivarInitializers;
1180 }
1181}
1182
Chris Lattner5f9e2722011-07-23 10:55:15 +00001183raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001184 const ObjCImplementationDecl *ID) {
1185 OS << ID->getName();
1186 return OS;
1187}
1188
Chris Lattnerab351632009-02-20 20:59:54 +00001189//===----------------------------------------------------------------------===//
1190// ObjCCompatibleAliasDecl
1191//===----------------------------------------------------------------------===//
1192
1193ObjCCompatibleAliasDecl *
1194ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1195 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001196 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001197 ObjCInterfaceDecl* AliasedClass) {
1198 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1199}
1200
1201//===----------------------------------------------------------------------===//
1202// ObjCPropertyDecl
1203//===----------------------------------------------------------------------===//
1204
1205ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1206 SourceLocation L,
1207 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001208 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +00001209 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001210 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001211 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001212}
1213
Chris Lattnerab351632009-02-20 20:59:54 +00001214//===----------------------------------------------------------------------===//
1215// ObjCPropertyImplDecl
1216//===----------------------------------------------------------------------===//
1217
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001218ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001219 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001220 SourceLocation atLoc,
1221 SourceLocation L,
1222 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001223 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001224 ObjCIvarDecl *ivar,
1225 SourceLocation ivarLoc) {
1226 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1227 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001228}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001229
Douglas Gregora4ffd852010-11-17 01:03:52 +00001230SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1231 SourceLocation EndLoc = getLocation();
1232 if (IvarLoc.isValid())
1233 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001234
Douglas Gregora4ffd852010-11-17 01:03:52 +00001235 return SourceRange(AtLoc, EndLoc);
1236}