blob: 65a987836ff884e147ee4ff1e2a40901f56f16a0 [file] [log] [blame]
Chris Lattner1e03a562008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +000017#include "clang/AST/ASTMutationListener.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000018#include "llvm/ADT/STLExtras.h"
Anna Zaksad0ce532012-09-27 19:45:11 +000019#include "llvm/ADT/SmallString.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000020using namespace clang;
21
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000022//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000023// ObjCListBase
24//===----------------------------------------------------------------------===//
25
Chris Lattner38af2de2009-02-20 21:35:13 +000026void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000027 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000028 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000029
30
Chris Lattner4ee413b2009-02-20 21:44:01 +000031 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000032 NumElts = Elts;
33 memcpy(List, InList, sizeof(void*)*Elts);
34}
35
Douglas Gregor18df52b2010-01-16 15:02:53 +000036void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
37 const SourceLocation *Locs, ASTContext &Ctx) {
38 if (Elts == 0)
39 return;
40
41 Locations = new (Ctx) SourceLocation[Elts];
42 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
43 set(InList, Elts, Ctx);
44}
45
Chris Lattner11e1e1a2009-02-20 21:16:26 +000046//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000047// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000048//===----------------------------------------------------------------------===//
49
David Blaikie99ba9e32011-12-20 02:48:34 +000050void ObjCContainerDecl::anchor() { }
51
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000052/// getIvarDecl - This method looks up an ivar in this ContextDecl.
53///
54ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000055ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000056 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000057 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000058 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
59 return ivar;
60 }
61 return 0;
62}
63
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000064// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000065ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000066ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000067 // Since instance & class methods can have the same name, the loop below
68 // ensures we get the correct method.
69 //
70 // @interface Whatever
71 // - (int) class_method;
72 // + (float) class_method;
73 // @end
74 //
75 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000076 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000077 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000078 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000079 return MD;
80 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000081 return 0;
82}
83
Ted Kremenek9f550ff2010-03-15 20:11:46 +000084ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000085ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000086 IdentifierInfo *propertyID) {
87
Ted Kremenekde09d0c2010-03-15 20:11:53 +000088 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000089 llvm::tie(I, E) = DC->lookup(propertyID);
90 for ( ; I != E; ++I)
91 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
92 return PD;
93
94 return 0;
95}
96
Anna Zaksad0ce532012-09-27 19:45:11 +000097IdentifierInfo *
98ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
99 SmallString<128> ivarName;
100 {
101 llvm::raw_svector_ostream os(ivarName);
102 os << '_' << getIdentifier()->getName();
103 }
104 return &Ctx.Idents.get(ivarName.str());
105}
106
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000107/// FindPropertyDeclaration - Finds declaration of the property given its name
108/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000109ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000110ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000112 if (ObjCPropertyDecl *PD =
113 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
114 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000116 switch (getKind()) {
117 default:
118 break;
119 case Decl::ObjCProtocol: {
120 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
121 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
122 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000123 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
124 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000125 break;
126 }
127 case Decl::ObjCInterface: {
128 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
129 // Look through categories.
130 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
131 Cat; Cat = Cat->getNextClassCategory())
132 if (!Cat->IsClassExtension())
133 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
134 return P;
135
136 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000137 for (ObjCInterfaceDecl::all_protocol_iterator
138 I = OID->all_referenced_protocol_begin(),
139 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000140 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
141 return P;
142
143 // Finally, check the super class.
144 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
145 return superClass->FindPropertyDeclaration(PropertyId);
146 break;
147 }
148 case Decl::ObjCCategory: {
149 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
150 // Look through protocols.
151 if (!OCD->IsClassExtension())
152 for (ObjCCategoryDecl::protocol_iterator
153 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
154 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
155 return P;
156
157 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000158 }
159 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000160 return 0;
161}
162
David Blaikie99ba9e32011-12-20 02:48:34 +0000163void ObjCInterfaceDecl::anchor() { }
164
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000165/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
166/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000167/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000168///
169ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000170ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000171 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000172 // FIXME: Should make sure no callers ever do this.
173 if (!hasDefinition())
174 return 0;
175
176 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000177 LoadExternalDefinition();
178
Ted Kremenek37cafb02010-03-15 20:30:07 +0000179 if (ObjCPropertyDecl *PD =
180 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
181 return PD;
182
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000183 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000184 for (ObjCInterfaceDecl::all_protocol_iterator
185 I = all_referenced_protocol_begin(),
186 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000187 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
188 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000189
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000190 return 0;
191}
192
Anna Zakse63aedd2012-10-31 01:18:22 +0000193void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Anna Zaksb36ea372012-10-18 19:17:53 +0000194 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
195 E = prop_end(); P != E; ++P) {
196 ObjCPropertyDecl *Prop = *P;
197 PM[Prop->getIdentifier()] = Prop;
198 }
199 for (ObjCInterfaceDecl::all_protocol_iterator
200 PI = all_referenced_protocol_begin(),
201 E = all_referenced_protocol_end(); PI != E; ++PI)
202 (*PI)->collectPropertiesToImplement(PM);
Anna Zakse63aedd2012-10-31 01:18:22 +0000203 // Note, the properties declared only in class extensions are still copied
204 // into the main @interface's property list, and therefore we don't
205 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000206}
207
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000208void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
209 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
210 ASTContext &C)
211{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000212 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000213 LoadExternalDefinition();
214
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000215 if (data().AllReferencedProtocols.empty() &&
216 data().ReferencedProtocols.empty()) {
217 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000218 return;
219 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000220
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000221 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000222 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000223 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000225 for (unsigned i = 0; i < ExtNum; i++) {
226 bool protocolExists = false;
227 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000228 for (all_protocol_iterator
229 p = all_referenced_protocol_begin(),
230 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000231 ObjCProtocolDecl *Proto = (*p);
232 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
233 protocolExists = true;
234 break;
235 }
236 }
237 // Do we want to warn on a protocol in extension class which
238 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000239 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000240 ProtocolRefs.push_back(ProtoInExtension);
241 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000242
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000243 if (ProtocolRefs.empty())
244 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000245
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000246 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000247 for (all_protocol_iterator p = all_referenced_protocol_begin(),
248 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000249 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000250 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000251
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000252 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000253}
254
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000255void ObjCInterfaceDecl::allocateDefinitionData() {
256 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor26fec632011-12-15 18:17:27 +0000257 Data = new (getASTContext()) DefinitionData();
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000258 Data->Definition = this;
259
260 // Make the type point at the definition, now that we have one.
261 if (TypeForDecl)
262 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000263}
264
265void ObjCInterfaceDecl::startDefinition() {
266 allocateDefinitionData();
267
Douglas Gregor53df7a12011-12-15 18:03:09 +0000268 // Update all of the declarations with a pointer to the definition.
269 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
270 RD != RDEnd; ++RD) {
271 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000272 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000273 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000274}
275
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000276/// getFirstClassExtension - Find first class extension of the given class.
277ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
278 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000279 CDecl = CDecl->getNextClassCategory())
280 if (CDecl->IsClassExtension())
281 return CDecl;
282 return 0;
283}
284
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000285/// getNextClassCategory - Find next class extension in list of categories.
286const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
287 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
288 CDecl = CDecl->getNextClassCategory())
289 if (CDecl->IsClassExtension())
290 return CDecl;
291 return 0;
292}
293
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000294ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
295 ObjCInterfaceDecl *&clsDeclared) {
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
Chris Lattner1e03a562008-03-16 00:19:01 +0000303 ObjCInterfaceDecl* ClassDecl = this;
304 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000305 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000306 clsDeclared = ClassDecl;
307 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000308 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000309 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
310 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000311 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
312 clsDeclared = ClassDecl;
313 return I;
314 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000315 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000316
Chris Lattner1e03a562008-03-16 00:19:01 +0000317 ClassDecl = ClassDecl->getSuperClass();
318 }
319 return NULL;
320}
321
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000322/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
323/// class whose name is passed as argument. If it is not one of the super classes
324/// the it returns NULL.
325ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
326 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000327 // FIXME: Should make sure no callers ever do this.
328 if (!hasDefinition())
329 return 0;
330
331 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000332 LoadExternalDefinition();
333
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000334 ObjCInterfaceDecl* ClassDecl = this;
335 while (ClassDecl != NULL) {
336 if (ClassDecl->getIdentifier() == ICName)
337 return ClassDecl;
338 ClassDecl = ClassDecl->getSuperClass();
339 }
340 return NULL;
341}
342
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000343/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000344/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000345ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
346 bool isInstance,
347 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000348 // FIXME: Should make sure no callers ever do this.
349 if (!hasDefinition())
350 return 0;
351
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000352 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000353 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000355 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000356 LoadExternalDefinition();
357
Chris Lattner1e03a562008-03-16 00:19:01 +0000358 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000359 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000360 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattner1e03a562008-03-16 00:19:01 +0000362 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000363 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
364 E = ClassDecl->protocol_end();
365 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000366 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000367 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000368
369 // Didn't find one yet - now look through categories.
370 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
371 while (CatDecl) {
372 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
373 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000374
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000375 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000376 // Didn't find one yet - look through protocols.
377 const ObjCList<ObjCProtocolDecl> &Protocols =
378 CatDecl->getReferencedProtocols();
379 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
380 E = Protocols.end(); I != E; ++I)
381 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
382 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000383 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000384 CatDecl = CatDecl->getNextClassCategory();
Chris Lattner1e03a562008-03-16 00:19:01 +0000385 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000386
Chris Lattner1e03a562008-03-16 00:19:01 +0000387 ClassDecl = ClassDecl->getSuperClass();
388 }
389 return NULL;
390}
391
Anna Zakse61354b2012-07-27 19:07:44 +0000392// Will search "local" class/category implementations for a method decl.
393// If failed, then we search in class's root for an instance method.
394// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000395ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
396 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000397 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000398 // FIXME: Should make sure no callers ever do this.
399 if (!hasDefinition())
400 return 0;
401
402 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000403 LoadExternalDefinition();
404
Steve Naroffd789d3d2009-10-01 23:46:04 +0000405 ObjCMethodDecl *Method = 0;
406 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000407 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
408 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000409
410 // Look through local category implementations associated with the class.
411 if (!Method)
412 Method = Instance ? getCategoryInstanceMethod(Sel)
413 : getCategoryClassMethod(Sel);
414
415 // Before we give up, check if the selector is an instance method.
416 // But only in the root. This matches gcc's behavior and what the
417 // runtime expects.
418 if (!Instance && !Method && !getSuperClass()) {
419 Method = lookupInstanceMethod(Sel);
420 // Look through local category implementations associated
421 // with the root class.
422 if (!Method)
423 Method = lookupPrivateMethod(Sel, true);
424 }
425
Steve Naroffd789d3d2009-10-01 23:46:04 +0000426 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000427 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000428 return Method;
429}
Chris Lattnerab351632009-02-20 20:59:54 +0000430
431//===----------------------------------------------------------------------===//
432// ObjCMethodDecl
433//===----------------------------------------------------------------------===//
434
435ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000436 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000437 SourceLocation endLoc,
438 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000439 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000440 DeclContext *contextDecl,
441 bool isInstance,
442 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000443 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000444 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000445 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000446 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000447 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000448 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000449 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000450 isInstance, isVariadic, isPropertyAccessor,
451 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000452 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000453 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000454}
455
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000456ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
457 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
458 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
459 Selector(), QualType(), 0, 0);
460}
461
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000462Stmt *ObjCMethodDecl::getBody() const {
463 return Body.get(getASTContext().getExternalSource());
464}
465
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000466void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
467 assert(PrevMethod);
468 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
469 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000470 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000471}
472
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000473void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
474 ArrayRef<ParmVarDecl*> Params,
475 ArrayRef<SourceLocation> SelLocs) {
476 ParamsAndSelLocs = 0;
477 NumParams = Params.size();
478 if (Params.empty() && SelLocs.empty())
479 return;
480
481 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
482 sizeof(SourceLocation) * SelLocs.size();
483 ParamsAndSelLocs = C.Allocate(Size);
484 std::copy(Params.begin(), Params.end(), getParams());
485 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
486}
487
488void ObjCMethodDecl::getSelectorLocs(
489 SmallVectorImpl<SourceLocation> &SelLocs) const {
490 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
491 SelLocs.push_back(getSelectorLoc(i));
492}
493
494void ObjCMethodDecl::setMethodParams(ASTContext &C,
495 ArrayRef<ParmVarDecl*> Params,
496 ArrayRef<SourceLocation> SelLocs) {
497 assert((!SelLocs.empty() || isImplicit()) &&
498 "No selector locs for non-implicit method");
499 if (isImplicit())
500 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
501
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000502 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
503 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000504 if (SelLocsKind != SelLoc_NonStandard)
505 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
506
507 setParamsAndSelLocs(C, Params, SelLocs);
508}
509
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000510/// \brief A definition will return its interface declaration.
511/// An interface declaration will return its definition.
512/// Otherwise it will return itself.
513ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
514 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000515 ObjCMethodDecl *Redecl = 0;
516 if (HasRedeclaration)
517 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000518 if (Redecl)
519 return Redecl;
520
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000521 Decl *CtxD = cast<Decl>(getDeclContext());
522
523 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
524 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
525 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
526
527 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
528 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
529 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
530
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000531 } else if (ObjCImplementationDecl *ImplD =
532 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000533 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
534 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000535
536 } else if (ObjCCategoryImplDecl *CImplD =
537 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000538 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000539 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000540 }
541
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000542 if (!Redecl && isRedeclaration()) {
543 // This is the last redeclaration, go back to the first method.
544 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
545 isInstanceMethod());
546 }
547
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000548 return Redecl ? Redecl : this;
549}
550
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000551ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
552 Decl *CtxD = cast<Decl>(getDeclContext());
553
554 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
555 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
556 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
557 isInstanceMethod()))
558 return MD;
559
560 } else if (ObjCCategoryImplDecl *CImplD =
561 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000562 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000563 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
564 isInstanceMethod()))
565 return MD;
566 }
567
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000568 if (isRedeclaration())
569 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
570 isInstanceMethod());
571
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000572 return this;
573}
574
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000575SourceLocation ObjCMethodDecl::getLocEnd() const {
576 if (Stmt *Body = getBody())
577 return Body->getLocEnd();
578 return DeclEndLoc;
579}
580
John McCall85f3d762011-03-02 01:50:55 +0000581ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
582 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000583 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000584 return family;
585
John McCalld5313b02011-03-02 11:33:24 +0000586 // Check for an explicit attribute.
587 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
588 // The unfortunate necessity of mapping between enums here is due
589 // to the attributes framework.
590 switch (attr->getFamily()) {
591 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
592 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
593 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
594 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
595 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
596 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
597 }
598 Family = static_cast<unsigned>(family);
599 return family;
600 }
601
John McCall85f3d762011-03-02 01:50:55 +0000602 family = getSelector().getMethodFamily();
603 switch (family) {
604 case OMF_None: break;
605
606 // init only has a conventional meaning for an instance method, and
607 // it has to return an object.
608 case OMF_init:
609 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
610 family = OMF_None;
611 break;
612
613 // alloc/copy/new have a conventional meaning for both class and
614 // instance methods, but they require an object return.
615 case OMF_alloc:
616 case OMF_copy:
617 case OMF_mutableCopy:
618 case OMF_new:
619 if (!getResultType()->isObjCObjectPointerType())
620 family = OMF_None;
621 break;
622
623 // These selectors have a conventional meaning only for instance methods.
624 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000625 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000626 case OMF_retain:
627 case OMF_release:
628 case OMF_autorelease:
629 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000630 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000631 if (!isInstanceMethod())
632 family = OMF_None;
633 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000634
635 case OMF_performSelector:
636 if (!isInstanceMethod() ||
637 !getResultType()->isObjCIdType())
638 family = OMF_None;
639 else {
640 unsigned noParams = param_size();
641 if (noParams < 1 || noParams > 3)
642 family = OMF_None;
643 else {
644 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
645 QualType ArgT = (*it);
646 if (!ArgT->isObjCSelType()) {
647 family = OMF_None;
648 break;
649 }
650 while (--noParams) {
651 it++;
652 ArgT = (*it);
653 if (!ArgT->isObjCIdType()) {
654 family = OMF_None;
655 break;
656 }
657 }
658 }
659 }
660 break;
661
John McCall85f3d762011-03-02 01:50:55 +0000662 }
663
664 // Cache the result.
665 Family = static_cast<unsigned>(family);
666 return family;
667}
668
Mike Stump1eb44332009-09-09 15:08:12 +0000669void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000670 const ObjCInterfaceDecl *OID) {
671 QualType selfTy;
672 if (isInstanceMethod()) {
673 // There may be no interface context due to error in declaration
674 // of the interface (which has been reported). Recover gracefully.
675 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000676 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000677 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000678 } else {
679 selfTy = Context.getObjCIdType();
680 }
681 } else // we have a factory method.
682 selfTy = Context.getObjCClassType();
683
John McCall7acddac2011-06-17 06:42:21 +0000684 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000685 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000686
David Blaikie4e4d0842012-03-11 07:00:24 +0000687 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000688 if (isInstanceMethod()) {
689 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000690
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000691 // 'self' is always __strong. It's actually pseudo-strong except
692 // in init methods (or methods labeled ns_consumes_self), though.
693 Qualifiers qs;
694 qs.setObjCLifetime(Qualifiers::OCL_Strong);
695 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000696
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000697 // In addition, 'self' is const unless this is an init method.
698 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
699 selfTy = selfTy.withConst();
700 selfIsPseudoStrong = true;
701 }
702 }
703 else {
704 assert(isClassMethod());
705 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000706 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000707 selfIsPseudoStrong = true;
708 }
John McCallf85e1932011-06-15 23:02:42 +0000709 }
710
711 ImplicitParamDecl *self
712 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
713 &Context.Idents.get("self"), selfTy);
714 setSelfDecl(self);
715
716 if (selfIsConsumed)
717 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000718
John McCall7acddac2011-06-17 06:42:21 +0000719 if (selfIsPseudoStrong)
720 self->setARCPseudoStrong(true);
721
Mike Stump1eb44332009-09-09 15:08:12 +0000722 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
723 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000724 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000725}
726
Chris Lattnerab351632009-02-20 20:59:54 +0000727ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
728 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
729 return ID;
730 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
731 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000732 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000733 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000734
735 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000736 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000737}
738
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000739static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
740 const ObjCMethodDecl *Method,
741 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
742 bool MovedToSuper) {
743 if (!Container)
744 return;
745
746 // In categories look for overriden methods from protocols. A method from
747 // category is not "overriden" since it is considered as the "same" method
748 // (same USR) as the one from the interface.
749 if (const ObjCCategoryDecl *
750 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
751 // Check whether we have a matching method at this category but only if we
752 // are at the super class level.
753 if (MovedToSuper)
754 if (ObjCMethodDecl *
755 Overridden = Container->getMethod(Method->getSelector(),
756 Method->isInstanceMethod()))
757 if (Method != Overridden) {
758 // We found an override at this category; there is no need to look
759 // into its protocols.
760 Methods.push_back(Overridden);
761 return;
762 }
763
764 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
765 PEnd = Category->protocol_end();
766 P != PEnd; ++P)
767 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
768 return;
769 }
770
771 // Check whether we have a matching method at this level.
772 if (const ObjCMethodDecl *
773 Overridden = Container->getMethod(Method->getSelector(),
774 Method->isInstanceMethod()))
775 if (Method != Overridden) {
776 // We found an override at this level; there is no need to look
777 // into other protocols or categories.
778 Methods.push_back(Overridden);
779 return;
780 }
781
782 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
783 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
784 PEnd = Protocol->protocol_end();
785 P != PEnd; ++P)
786 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
787 }
788
789 if (const ObjCInterfaceDecl *
790 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
791 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
792 PEnd = Interface->protocol_end();
793 P != PEnd; ++P)
794 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
795
796 for (const ObjCCategoryDecl *Category = Interface->getCategoryList();
797 Category; Category = Category->getNextClassCategory())
798 CollectOverriddenMethodsRecurse(Category, Method, Methods,
799 MovedToSuper);
800
801 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
802 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
803 /*MovedToSuper=*/true);
804 }
805}
806
807static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
808 const ObjCMethodDecl *Method,
809 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
810 CollectOverriddenMethodsRecurse(Container, Method, Methods,
811 /*MovedToSuper=*/false);
812}
813
814static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
815 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
816 assert(Method->isOverriding());
817
818 if (const ObjCProtocolDecl *
819 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
820 CollectOverriddenMethods(ProtD, Method, overridden);
821
822 } else if (const ObjCImplDecl *
823 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
824 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
825 if (!ID)
826 return;
827 // Start searching for overridden methods using the method from the
828 // interface as starting point.
829 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
830 Method->isInstanceMethod()))
831 Method = IFaceMeth;
832 CollectOverriddenMethods(ID, Method, overridden);
833
834 } else if (const ObjCCategoryDecl *
835 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
836 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
837 if (!ID)
838 return;
839 // Start searching for overridden methods using the method from the
840 // interface as starting point.
841 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
842 Method->isInstanceMethod()))
843 Method = IFaceMeth;
844 CollectOverriddenMethods(ID, Method, overridden);
845
846 } else {
847 CollectOverriddenMethods(
848 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
849 Method, overridden);
850 }
851}
852
853static void collectOnCategoriesAfterLocation(SourceLocation Loc,
854 const ObjCInterfaceDecl *Class,
855 SourceManager &SM,
856 const ObjCMethodDecl *Method,
857 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
858 if (!Class)
859 return;
860
861 for (const ObjCCategoryDecl *Category = Class->getCategoryList();
862 Category; Category = Category->getNextClassCategory())
863 if (SM.isBeforeInTranslationUnit(Loc, Category->getLocation()))
864 CollectOverriddenMethodsRecurse(Category, Method, Methods, true);
865
866 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
867 Method, Methods);
868}
869
870/// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
871/// returns false.
872/// You'd think that in that case there are no overrides but categories can
873/// "introduce" new overridden methods that are missed by Sema because the
874/// overrides lookup that it does for methods, inside implementations, will
875/// stop at the interface level (if there is a method there) and not look
876/// further in super classes.
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000877/// Methods in an implementation can overide methods in super class's category
878/// but not in current class's category. But, such methods
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000879static void collectOverriddenMethodsFast(SourceManager &SM,
880 const ObjCMethodDecl *Method,
881 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
882 assert(!Method->isOverriding());
883
884 const ObjCContainerDecl *
885 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
886 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
887 return;
888 const ObjCInterfaceDecl *Class = Method->getClassInterface();
889 if (!Class)
890 return;
891
892 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
893 SM, Method, Methods);
894}
895
896void ObjCMethodDecl::getOverriddenMethods(
897 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
898 const ObjCMethodDecl *Method = this;
899
900 if (Method->isRedeclaration()) {
901 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
902 getMethod(Method->getSelector(), Method->isInstanceMethod());
903 }
904
905 if (!Method->isOverriding()) {
906 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
907 Method, Overridden);
908 } else {
909 collectOverriddenMethodsSlow(Method, Overridden);
910 assert(!Overridden.empty() &&
911 "ObjCMethodDecl's overriding bit is not as expected");
912 }
913}
914
Jordan Rose04bec392012-10-10 16:42:54 +0000915const ObjCPropertyDecl *
916ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
917 Selector Sel = getSelector();
918 unsigned NumArgs = Sel.getNumArgs();
919 if (NumArgs > 1)
920 return 0;
921
Jordan Rose50d2b262012-10-11 16:02:02 +0000922 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +0000923 return 0;
924
925 if (isPropertyAccessor()) {
926 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
927 bool IsGetter = (NumArgs == 0);
928
929 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
930 E = Container->prop_end();
931 I != E; ++I) {
932 Selector NextSel = IsGetter ? (*I)->getGetterName()
933 : (*I)->getSetterName();
934 if (NextSel == Sel)
935 return *I;
936 }
937
938 llvm_unreachable("Marked as a property accessor but no property found!");
939 }
940
941 if (!CheckOverrides)
942 return 0;
943
944 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
945 OverridesTy Overrides;
946 getOverriddenMethods(Overrides);
947 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
948 I != E; ++I) {
949 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
950 return Prop;
951 }
952
953 return 0;
954
955}
956
Chris Lattnerab351632009-02-20 20:59:54 +0000957//===----------------------------------------------------------------------===//
958// ObjCInterfaceDecl
959//===----------------------------------------------------------------------===//
960
Douglas Gregora6ea10e2012-01-17 18:09:05 +0000961ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +0000962 DeclContext *DC,
963 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000964 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000965 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000966 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000967 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000968 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000969 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000970 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000971 return Result;
972}
973
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000974ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
975 unsigned ID) {
976 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
977 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
978 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +0000979}
980
981ObjCInterfaceDecl::
982ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000983 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
984 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000985 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +0000986 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000987{
Douglas Gregorfd002a72011-12-16 22:37:11 +0000988 setPreviousDeclaration(PrevDecl);
989
990 // Copy the 'data' pointer over.
991 if (PrevDecl)
992 Data = PrevDecl->Data;
993
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +0000994 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000995}
996
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000997void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000998 assert(data().ExternallyCompleted && "Class is not externally completed");
999 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001000 getASTContext().getExternalSource()->CompleteType(
1001 const_cast<ObjCInterfaceDecl *>(this));
1002}
1003
1004void ObjCInterfaceDecl::setExternallyCompleted() {
1005 assert(getASTContext().getExternalSource() &&
1006 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001007 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001008 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001009 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001010}
1011
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001012ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001013 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1014 if (data().ExternallyCompleted)
1015 LoadExternalDefinition();
1016
1017 return getASTContext().getObjCImplementation(
1018 const_cast<ObjCInterfaceDecl*>(Def));
1019 }
1020
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001021 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001022 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001023}
1024
1025void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001026 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001027}
1028
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001029/// all_declared_ivar_begin - return first ivar declared in this class,
1030/// its extensions and its implementation. Lazily build the list on first
1031/// access.
1032ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001033 // FIXME: Should make sure no callers ever do this.
1034 if (!hasDefinition())
1035 return 0;
1036
1037 if (data().IvarList)
1038 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001039
1040 ObjCIvarDecl *curIvar = 0;
1041 if (!ivar_empty()) {
1042 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
David Blaikie581deb32012-06-06 20:45:41 +00001043 data().IvarList = *I; ++I;
1044 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1045 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001046 }
1047
1048 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
1049 CDecl = CDecl->getNextClassExtension()) {
1050 if (!CDecl->ivar_empty()) {
1051 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1052 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001053 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001054 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001055 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001056 }
David Blaikie581deb32012-06-06 20:45:41 +00001057 for ( ;I != E; curIvar = *I, ++I)
1058 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001059 }
1060 }
1061
1062 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1063 if (!ImplDecl->ivar_empty()) {
1064 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1065 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001066 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001067 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001068 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001069 }
David Blaikie581deb32012-06-06 20:45:41 +00001070 for ( ;I != E; curIvar = *I, ++I)
1071 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001072 }
1073 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001074 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001075}
Chris Lattnerab351632009-02-20 20:59:54 +00001076
1077/// FindCategoryDeclaration - Finds category declaration in the list of
1078/// categories for this class and returns it. Name of the category is passed
1079/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001080///
Chris Lattnerab351632009-02-20 20:59:54 +00001081ObjCCategoryDecl *
1082ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001083 // FIXME: Should make sure no callers ever do this.
1084 if (!hasDefinition())
1085 return 0;
1086
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001087 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001088 LoadExternalDefinition();
1089
Chris Lattnerab351632009-02-20 20:59:54 +00001090 for (ObjCCategoryDecl *Category = getCategoryList();
1091 Category; Category = Category->getNextClassCategory())
1092 if (Category->getIdentifier() == CategoryId)
1093 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001094 return 0;
1095}
1096
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001097ObjCMethodDecl *
1098ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1099 for (ObjCCategoryDecl *Category = getCategoryList();
1100 Category; Category = Category->getNextClassCategory())
1101 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1102 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1103 return MD;
1104 return 0;
1105}
1106
1107ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1108 for (ObjCCategoryDecl *Category = getCategoryList();
1109 Category; Category = Category->getNextClassCategory())
1110 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1111 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1112 return MD;
1113 return 0;
1114}
1115
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001116/// ClassImplementsProtocol - Checks that 'lProto' protocol
1117/// has been implemented in IDecl class, its super class or categories (if
1118/// lookupCategory is true).
1119bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1120 bool lookupCategory,
1121 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001122 if (!hasDefinition())
1123 return false;
1124
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001125 ObjCInterfaceDecl *IDecl = this;
1126 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001127 for (ObjCInterfaceDecl::protocol_iterator
1128 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001129 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1130 return true;
1131 // This is dubious and is added to be compatible with gcc. In gcc, it is
1132 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1133 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1134 // object. This IMO, should be a bug.
1135 // FIXME: Treat this as an extension, and flag this as an error when GCC
1136 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001137 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001138 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1139 return true;
1140 }
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001142 // 2nd, look up the category.
1143 if (lookupCategory)
1144 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
1145 CDecl = CDecl->getNextClassCategory()) {
1146 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
1147 E = CDecl->protocol_end(); PI != E; ++PI)
1148 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1149 return true;
1150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001152 // 3rd, look up the super class(s)
1153 if (IDecl->getSuperClass())
1154 return
1155 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1156 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001158 return false;
1159}
1160
Chris Lattnerab351632009-02-20 20:59:54 +00001161//===----------------------------------------------------------------------===//
1162// ObjCIvarDecl
1163//===----------------------------------------------------------------------===//
1164
David Blaikie99ba9e32011-12-20 02:48:34 +00001165void ObjCIvarDecl::anchor() { }
1166
Daniel Dunbara0654922010-04-02 20:10:03 +00001167ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001168 SourceLocation StartLoc,
1169 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001170 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001171 AccessControl ac, Expr *BW,
1172 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001173 if (DC) {
1174 // Ivar's can only appear in interfaces, implementations (via synthesized
1175 // properties), and class extensions (via direct declaration, or synthesized
1176 // properties).
1177 //
1178 // FIXME: This should really be asserting this:
1179 // (isa<ObjCCategoryDecl>(DC) &&
1180 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1181 // but unfortunately we sometimes place ivars into non-class extension
1182 // categories on error. This breaks an AST invariant, and should not be
1183 // fixed.
1184 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1185 isa<ObjCCategoryDecl>(DC)) &&
1186 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001187 // Once a new ivar is created in any of class/class-extension/implementation
1188 // decl contexts, the previously built IvarList must be rebuilt.
1189 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1190 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001191 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001192 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001193 else
1194 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001195 }
1196 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001197 }
1198
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001199 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1200 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001201}
1202
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001203ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1204 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1205 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1206 QualType(), 0, ObjCIvarDecl::None, 0, false);
1207}
1208
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001209const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1210 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001211
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001212 switch (DC->getKind()) {
1213 default:
1214 case ObjCCategoryImpl:
1215 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001216 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001217
1218 // Ivars can only appear in class extension categories.
1219 case ObjCCategory: {
1220 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1221 assert(CD->IsClassExtension() && "invalid container for ivar!");
1222 return CD->getClassInterface();
1223 }
1224
1225 case ObjCImplementation:
1226 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1227
1228 case ObjCInterface:
1229 return cast<ObjCInterfaceDecl>(DC);
1230 }
1231}
Chris Lattnerab351632009-02-20 20:59:54 +00001232
1233//===----------------------------------------------------------------------===//
1234// ObjCAtDefsFieldDecl
1235//===----------------------------------------------------------------------===//
1236
David Blaikie99ba9e32011-12-20 02:48:34 +00001237void ObjCAtDefsFieldDecl::anchor() { }
1238
Chris Lattnerab351632009-02-20 20:59:54 +00001239ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001240*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1241 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001242 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001243 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001244}
1245
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001246ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1247 unsigned ID) {
1248 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1249 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1250 0, QualType(), 0);
1251}
1252
Chris Lattnerab351632009-02-20 20:59:54 +00001253//===----------------------------------------------------------------------===//
1254// ObjCProtocolDecl
1255//===----------------------------------------------------------------------===//
1256
David Blaikie99ba9e32011-12-20 02:48:34 +00001257void ObjCProtocolDecl::anchor() { }
1258
Douglas Gregor27c6da22012-01-01 20:30:41 +00001259ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1260 SourceLocation nameLoc,
1261 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001262 ObjCProtocolDecl *PrevDecl)
1263 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001264{
1265 setPreviousDeclaration(PrevDecl);
1266 if (PrevDecl)
1267 Data = PrevDecl->Data;
1268}
1269
Chris Lattnerab351632009-02-20 20:59:54 +00001270ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001271 IdentifierInfo *Id,
1272 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001273 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001274 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001275 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001276 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001277
1278 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001279}
1280
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001281ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1282 unsigned ID) {
1283 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1284 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1285 0);
1286}
1287
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001288ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1289 ObjCProtocolDecl *PDecl = this;
1290
1291 if (Name == getIdentifier())
1292 return PDecl;
1293
1294 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1295 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1296 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001298 return NULL;
1299}
1300
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001301// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001302// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001303ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1304 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001305 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001307 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001308 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Chris Lattnerab351632009-02-20 20:59:54 +00001310 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001311 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001312 return MethodDecl;
1313 return NULL;
1314}
1315
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001316void ObjCProtocolDecl::allocateDefinitionData() {
1317 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001318 Data = new (getASTContext()) DefinitionData;
1319 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001320}
1321
1322void ObjCProtocolDecl::startDefinition() {
1323 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001324
1325 // Update all of the declarations with a pointer to the definition.
1326 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1327 RD != RDEnd; ++RD)
1328 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001329}
1330
Anna Zakse63aedd2012-10-31 01:18:22 +00001331void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Anna Zaksb36ea372012-10-18 19:17:53 +00001332 for (ObjCProtocolDecl::prop_iterator P = prop_begin(),
1333 E = prop_end(); P != E; ++P) {
1334 ObjCPropertyDecl *Prop = *P;
1335 // Insert into PM if not there already.
1336 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1337 }
1338 // Scan through protocol's protocols.
1339 for (ObjCProtocolDecl::protocol_iterator PI = protocol_begin(),
1340 E = protocol_end(); PI != E; ++PI)
1341 (*PI)->collectPropertiesToImplement(PM);
1342}
1343
1344
Chris Lattnerab351632009-02-20 20:59:54 +00001345//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001346// ObjCCategoryDecl
1347//===----------------------------------------------------------------------===//
1348
David Blaikie99ba9e32011-12-20 02:48:34 +00001349void ObjCCategoryDecl::anchor() { }
1350
Chris Lattnerab351632009-02-20 20:59:54 +00001351ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001352 SourceLocation AtLoc,
1353 SourceLocation ClassNameLoc,
1354 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001355 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001356 ObjCInterfaceDecl *IDecl,
1357 SourceLocation IvarLBraceLoc,
1358 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001359 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1360 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001361 IDecl,
1362 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001363 if (IDecl) {
1364 // Link this category into its class's category list.
1365 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001366 if (IDecl->hasDefinition()) {
1367 IDecl->setCategoryList(CatDecl);
1368 if (ASTMutationListener *L = C.getASTMutationListener())
1369 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1370 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001371 }
1372
1373 return CatDecl;
1374}
1375
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001376ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1377 unsigned ID) {
1378 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1379 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1380 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001381}
1382
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001383ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1384 return getASTContext().getObjCImplementation(
1385 const_cast<ObjCCategoryDecl*>(this));
1386}
1387
1388void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1389 getASTContext().setObjCImplementation(this, ImplD);
1390}
1391
1392
Chris Lattnerab351632009-02-20 20:59:54 +00001393//===----------------------------------------------------------------------===//
1394// ObjCCategoryImplDecl
1395//===----------------------------------------------------------------------===//
1396
David Blaikie99ba9e32011-12-20 02:48:34 +00001397void ObjCCategoryImplDecl::anchor() { }
1398
Chris Lattnerab351632009-02-20 20:59:54 +00001399ObjCCategoryImplDecl *
1400ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001401 IdentifierInfo *Id,
1402 ObjCInterfaceDecl *ClassInterface,
1403 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001404 SourceLocation atStartLoc,
1405 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001406 if (ClassInterface && ClassInterface->hasDefinition())
1407 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001408 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001409 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001410}
1411
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001412ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1413 unsigned ID) {
1414 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1415 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1416 SourceLocation(), SourceLocation());
1417}
1418
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001419ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001420 // The class interface might be NULL if we are working with invalid code.
1421 if (const ObjCInterfaceDecl *ID = getClassInterface())
1422 return ID->FindCategoryDeclaration(getIdentifier());
1423 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001424}
1425
Chris Lattnerab351632009-02-20 20:59:54 +00001426
David Blaikie99ba9e32011-12-20 02:48:34 +00001427void ObjCImplDecl::anchor() { }
1428
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001429void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001430 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001431 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001432 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001433}
1434
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001435void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1436 ASTContext &Ctx = getASTContext();
1437
1438 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001439 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001440 if (IFace)
1441 Ctx.setObjCImplementation(IFace, ImplD);
1442
Duncan Sands98f2cca2009-07-21 07:56:29 +00001443 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001444 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1445 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1446 Ctx.setObjCImplementation(CD, ImplD);
1447 }
1448
1449 ClassInterface = IFace;
1450}
1451
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001452/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
James Dennett1c3a46a2012-06-15 22:30:14 +00001453/// properties implemented in this category \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001454/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001455///
Chris Lattner3aa18612009-02-28 18:42:10 +00001456ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001457FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1458 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001459 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001460 if (PID->getPropertyIvarDecl() &&
1461 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1462 return PID;
1463 }
1464 return 0;
1465}
1466
1467/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001468/// added to the list of those properties \@synthesized/\@dynamic in this
1469/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001470///
Chris Lattner3aa18612009-02-28 18:42:10 +00001471ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001472FindPropertyImplDecl(IdentifierInfo *Id) const {
1473 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001474 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001475 if (PID->getPropertyDecl()->getIdentifier() == Id)
1476 return PID;
1477 }
1478 return 0;
1479}
1480
Chris Lattner5f9e2722011-07-23 10:55:15 +00001481raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001482 const ObjCCategoryImplDecl &CID) {
1483 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001484 return OS;
1485}
1486
Chris Lattnerab351632009-02-20 20:59:54 +00001487//===----------------------------------------------------------------------===//
1488// ObjCImplementationDecl
1489//===----------------------------------------------------------------------===//
1490
David Blaikie99ba9e32011-12-20 02:48:34 +00001491void ObjCImplementationDecl::anchor() { }
1492
Chris Lattnerab351632009-02-20 20:59:54 +00001493ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001494ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001495 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001496 ObjCInterfaceDecl *SuperDecl,
1497 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001498 SourceLocation atStartLoc,
1499 SourceLocation IvarLBraceLoc,
1500 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001501 if (ClassInterface && ClassInterface->hasDefinition())
1502 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001503 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001504 nameLoc, atStartLoc,
1505 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001506}
1507
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001508ObjCImplementationDecl *
1509ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1510 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1511 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1512 SourceLocation());
1513}
1514
John McCallda6d9762011-07-22 04:15:06 +00001515void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1516 CXXCtorInitializer ** initializers,
1517 unsigned numInitializers) {
1518 if (numInitializers > 0) {
1519 NumIvarInitializers = numInitializers;
1520 CXXCtorInitializer **ivarInitializers =
1521 new (C) CXXCtorInitializer*[NumIvarInitializers];
1522 memcpy(ivarInitializers, initializers,
1523 numInitializers * sizeof(CXXCtorInitializer*));
1524 IvarInitializers = ivarInitializers;
1525 }
1526}
1527
Chris Lattner5f9e2722011-07-23 10:55:15 +00001528raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001529 const ObjCImplementationDecl &ID) {
1530 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001531 return OS;
1532}
1533
Chris Lattnerab351632009-02-20 20:59:54 +00001534//===----------------------------------------------------------------------===//
1535// ObjCCompatibleAliasDecl
1536//===----------------------------------------------------------------------===//
1537
David Blaikie99ba9e32011-12-20 02:48:34 +00001538void ObjCCompatibleAliasDecl::anchor() { }
1539
Chris Lattnerab351632009-02-20 20:59:54 +00001540ObjCCompatibleAliasDecl *
1541ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1542 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001543 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001544 ObjCInterfaceDecl* AliasedClass) {
1545 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1546}
1547
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001548ObjCCompatibleAliasDecl *
1549ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1550 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1551 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1552}
1553
Chris Lattnerab351632009-02-20 20:59:54 +00001554//===----------------------------------------------------------------------===//
1555// ObjCPropertyDecl
1556//===----------------------------------------------------------------------===//
1557
David Blaikie99ba9e32011-12-20 02:48:34 +00001558void ObjCPropertyDecl::anchor() { }
1559
Chris Lattnerab351632009-02-20 20:59:54 +00001560ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1561 SourceLocation L,
1562 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001563 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001564 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001565 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001566 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001567 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001568}
1569
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001570ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1571 unsigned ID) {
1572 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1573 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001574 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001575 0);
1576}
1577
Chris Lattnerab351632009-02-20 20:59:54 +00001578//===----------------------------------------------------------------------===//
1579// ObjCPropertyImplDecl
1580//===----------------------------------------------------------------------===//
1581
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001582ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001583 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001584 SourceLocation atLoc,
1585 SourceLocation L,
1586 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001587 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001588 ObjCIvarDecl *ivar,
1589 SourceLocation ivarLoc) {
1590 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1591 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001592}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001593
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001594ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1595 unsigned ID) {
1596 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1597 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1598 0, Dynamic, 0, SourceLocation());
1599}
1600
Douglas Gregora4ffd852010-11-17 01:03:52 +00001601SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1602 SourceLocation EndLoc = getLocation();
1603 if (IvarLoc.isValid())
1604 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001605
Douglas Gregora4ffd852010-11-17 01:03:52 +00001606 return SourceRange(AtLoc, EndLoc);
1607}