blob: 90b8807be995da0a107a900469f1864a9ba6bab4 [file] [log] [blame]
Chris Lattner1e03a562008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +000017#include "clang/AST/ASTMutationListener.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000018#include "llvm/ADT/STLExtras.h"
Anna Zaksad0ce532012-09-27 19:45:11 +000019#include "llvm/ADT/SmallString.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000020using namespace clang;
21
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000022//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000023// ObjCListBase
24//===----------------------------------------------------------------------===//
25
Chris Lattner38af2de2009-02-20 21:35:13 +000026void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000027 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000028 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000029
30
Chris Lattner4ee413b2009-02-20 21:44:01 +000031 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000032 NumElts = Elts;
33 memcpy(List, InList, sizeof(void*)*Elts);
34}
35
Douglas Gregor18df52b2010-01-16 15:02:53 +000036void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
37 const SourceLocation *Locs, ASTContext &Ctx) {
38 if (Elts == 0)
39 return;
40
41 Locations = new (Ctx) SourceLocation[Elts];
42 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
43 set(InList, Elts, Ctx);
44}
45
Chris Lattner11e1e1a2009-02-20 21:16:26 +000046//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000047// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000048//===----------------------------------------------------------------------===//
49
David Blaikie99ba9e32011-12-20 02:48:34 +000050void ObjCContainerDecl::anchor() { }
51
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000052/// getIvarDecl - This method looks up an ivar in this ContextDecl.
53///
54ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000055ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000056 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000057 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000058 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
59 return ivar;
60 }
61 return 0;
62}
63
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000064// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000065ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000066ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000067 // Since instance & class methods can have the same name, the loop below
68 // ensures we get the correct method.
69 //
70 // @interface Whatever
71 // - (int) class_method;
72 // + (float) class_method;
73 // @end
74 //
75 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000076 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000077 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000078 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000079 return MD;
80 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000081 return 0;
82}
83
Ted Kremenek9f550ff2010-03-15 20:11:46 +000084ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000085ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000086 IdentifierInfo *propertyID) {
87
Ted Kremenekde09d0c2010-03-15 20:11:53 +000088 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000089 llvm::tie(I, E) = DC->lookup(propertyID);
90 for ( ; I != E; ++I)
91 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
92 return PD;
93
94 return 0;
95}
96
Anna Zaksad0ce532012-09-27 19:45:11 +000097IdentifierInfo *
98ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
99 SmallString<128> ivarName;
100 {
101 llvm::raw_svector_ostream os(ivarName);
102 os << '_' << getIdentifier()->getName();
103 }
104 return &Ctx.Idents.get(ivarName.str());
105}
106
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000107/// FindPropertyDeclaration - Finds declaration of the property given its name
108/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000109ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000110ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000112 if (ObjCPropertyDecl *PD =
113 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
114 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000116 switch (getKind()) {
117 default:
118 break;
119 case Decl::ObjCProtocol: {
120 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
121 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
122 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000123 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
124 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000125 break;
126 }
127 case Decl::ObjCInterface: {
128 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
129 // Look through categories.
130 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
131 Cat; Cat = Cat->getNextClassCategory())
132 if (!Cat->IsClassExtension())
133 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
134 return P;
135
136 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000137 for (ObjCInterfaceDecl::all_protocol_iterator
138 I = OID->all_referenced_protocol_begin(),
139 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000140 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
141 return P;
142
143 // Finally, check the super class.
144 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
145 return superClass->FindPropertyDeclaration(PropertyId);
146 break;
147 }
148 case Decl::ObjCCategory: {
149 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
150 // Look through protocols.
151 if (!OCD->IsClassExtension())
152 for (ObjCCategoryDecl::protocol_iterator
153 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
154 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
155 return P;
156
157 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000158 }
159 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000160 return 0;
161}
162
David Blaikie99ba9e32011-12-20 02:48:34 +0000163void ObjCInterfaceDecl::anchor() { }
164
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000165/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
166/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000167/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000168///
169ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000170ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000171 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000172 // FIXME: Should make sure no callers ever do this.
173 if (!hasDefinition())
174 return 0;
175
176 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000177 LoadExternalDefinition();
178
Ted Kremenek37cafb02010-03-15 20:30:07 +0000179 if (ObjCPropertyDecl *PD =
180 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
181 return PD;
182
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000183 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000184 for (ObjCInterfaceDecl::all_protocol_iterator
185 I = all_referenced_protocol_begin(),
186 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000187 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
188 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000189
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000190 return 0;
191}
192
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000193void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
194 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
195 ASTContext &C)
196{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000197 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000198 LoadExternalDefinition();
199
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000200 if (data().AllReferencedProtocols.empty() &&
201 data().ReferencedProtocols.empty()) {
202 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000203 return;
204 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000205
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000206 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000207 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000208 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000210 for (unsigned i = 0; i < ExtNum; i++) {
211 bool protocolExists = false;
212 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000213 for (all_protocol_iterator
214 p = all_referenced_protocol_begin(),
215 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000216 ObjCProtocolDecl *Proto = (*p);
217 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
218 protocolExists = true;
219 break;
220 }
221 }
222 // Do we want to warn on a protocol in extension class which
223 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000224 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000225 ProtocolRefs.push_back(ProtoInExtension);
226 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000227
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000228 if (ProtocolRefs.empty())
229 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000230
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000231 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000232 for (all_protocol_iterator p = all_referenced_protocol_begin(),
233 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000234 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000235 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000236
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000237 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000238}
239
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000240void ObjCInterfaceDecl::allocateDefinitionData() {
241 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor26fec632011-12-15 18:17:27 +0000242 Data = new (getASTContext()) DefinitionData();
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000243 Data->Definition = this;
244
245 // Make the type point at the definition, now that we have one.
246 if (TypeForDecl)
247 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000248}
249
250void ObjCInterfaceDecl::startDefinition() {
251 allocateDefinitionData();
252
Douglas Gregor53df7a12011-12-15 18:03:09 +0000253 // Update all of the declarations with a pointer to the definition.
254 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
255 RD != RDEnd; ++RD) {
256 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000257 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000258 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000259}
260
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000261/// getFirstClassExtension - Find first class extension of the given class.
262ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
263 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000264 CDecl = CDecl->getNextClassCategory())
265 if (CDecl->IsClassExtension())
266 return CDecl;
267 return 0;
268}
269
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000270/// getNextClassCategory - Find next class extension in list of categories.
271const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
272 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
273 CDecl = CDecl->getNextClassCategory())
274 if (CDecl->IsClassExtension())
275 return CDecl;
276 return 0;
277}
278
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000279ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
280 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000281 // FIXME: Should make sure no callers ever do this.
282 if (!hasDefinition())
283 return 0;
284
285 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000286 LoadExternalDefinition();
287
Chris Lattner1e03a562008-03-16 00:19:01 +0000288 ObjCInterfaceDecl* ClassDecl = this;
289 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000290 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000291 clsDeclared = ClassDecl;
292 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000293 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000294 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
295 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000296 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
297 clsDeclared = ClassDecl;
298 return I;
299 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000300 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000301
Chris Lattner1e03a562008-03-16 00:19:01 +0000302 ClassDecl = ClassDecl->getSuperClass();
303 }
304 return NULL;
305}
306
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000307/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
308/// class whose name is passed as argument. If it is not one of the super classes
309/// the it returns NULL.
310ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
311 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000312 // FIXME: Should make sure no callers ever do this.
313 if (!hasDefinition())
314 return 0;
315
316 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000317 LoadExternalDefinition();
318
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000319 ObjCInterfaceDecl* ClassDecl = this;
320 while (ClassDecl != NULL) {
321 if (ClassDecl->getIdentifier() == ICName)
322 return ClassDecl;
323 ClassDecl = ClassDecl->getSuperClass();
324 }
325 return NULL;
326}
327
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000328/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000329/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000330ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
331 bool isInstance,
332 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000333 // FIXME: Should make sure no callers ever do this.
334 if (!hasDefinition())
335 return 0;
336
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000337 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000338 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000340 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000341 LoadExternalDefinition();
342
Chris Lattner1e03a562008-03-16 00:19:01 +0000343 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000344 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000345 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner1e03a562008-03-16 00:19:01 +0000347 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000348 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
349 E = ClassDecl->protocol_end();
350 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000351 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000352 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000353
354 // Didn't find one yet - now look through categories.
355 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
356 while (CatDecl) {
357 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
358 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000359
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000360 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000361 // Didn't find one yet - look through protocols.
362 const ObjCList<ObjCProtocolDecl> &Protocols =
363 CatDecl->getReferencedProtocols();
364 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
365 E = Protocols.end(); I != E; ++I)
366 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
367 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000368 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000369 CatDecl = CatDecl->getNextClassCategory();
Chris Lattner1e03a562008-03-16 00:19:01 +0000370 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000371
Chris Lattner1e03a562008-03-16 00:19:01 +0000372 ClassDecl = ClassDecl->getSuperClass();
373 }
374 return NULL;
375}
376
Anna Zakse61354b2012-07-27 19:07:44 +0000377// Will search "local" class/category implementations for a method decl.
378// If failed, then we search in class's root for an instance method.
379// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000380ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
381 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000382 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000383 // FIXME: Should make sure no callers ever do this.
384 if (!hasDefinition())
385 return 0;
386
387 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000388 LoadExternalDefinition();
389
Steve Naroffd789d3d2009-10-01 23:46:04 +0000390 ObjCMethodDecl *Method = 0;
391 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000392 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
393 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000394
395 // Look through local category implementations associated with the class.
396 if (!Method)
397 Method = Instance ? getCategoryInstanceMethod(Sel)
398 : getCategoryClassMethod(Sel);
399
400 // Before we give up, check if the selector is an instance method.
401 // But only in the root. This matches gcc's behavior and what the
402 // runtime expects.
403 if (!Instance && !Method && !getSuperClass()) {
404 Method = lookupInstanceMethod(Sel);
405 // Look through local category implementations associated
406 // with the root class.
407 if (!Method)
408 Method = lookupPrivateMethod(Sel, true);
409 }
410
Steve Naroffd789d3d2009-10-01 23:46:04 +0000411 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000412 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000413 return Method;
414}
Chris Lattnerab351632009-02-20 20:59:54 +0000415
416//===----------------------------------------------------------------------===//
417// ObjCMethodDecl
418//===----------------------------------------------------------------------===//
419
420ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000421 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000422 SourceLocation endLoc,
423 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000424 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000425 DeclContext *contextDecl,
426 bool isInstance,
427 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000428 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000429 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000430 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000431 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000432 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000433 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000434 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000435 isInstance, isVariadic, isPropertyAccessor,
436 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000437 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000438 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000439}
440
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000441ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
442 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
443 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
444 Selector(), QualType(), 0, 0);
445}
446
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000447Stmt *ObjCMethodDecl::getBody() const {
448 return Body.get(getASTContext().getExternalSource());
449}
450
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000451void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
452 assert(PrevMethod);
453 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
454 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000455 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000456}
457
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000458void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
459 ArrayRef<ParmVarDecl*> Params,
460 ArrayRef<SourceLocation> SelLocs) {
461 ParamsAndSelLocs = 0;
462 NumParams = Params.size();
463 if (Params.empty() && SelLocs.empty())
464 return;
465
466 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
467 sizeof(SourceLocation) * SelLocs.size();
468 ParamsAndSelLocs = C.Allocate(Size);
469 std::copy(Params.begin(), Params.end(), getParams());
470 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
471}
472
473void ObjCMethodDecl::getSelectorLocs(
474 SmallVectorImpl<SourceLocation> &SelLocs) const {
475 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
476 SelLocs.push_back(getSelectorLoc(i));
477}
478
479void ObjCMethodDecl::setMethodParams(ASTContext &C,
480 ArrayRef<ParmVarDecl*> Params,
481 ArrayRef<SourceLocation> SelLocs) {
482 assert((!SelLocs.empty() || isImplicit()) &&
483 "No selector locs for non-implicit method");
484 if (isImplicit())
485 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
486
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000487 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
488 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000489 if (SelLocsKind != SelLoc_NonStandard)
490 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
491
492 setParamsAndSelLocs(C, Params, SelLocs);
493}
494
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000495/// \brief A definition will return its interface declaration.
496/// An interface declaration will return its definition.
497/// Otherwise it will return itself.
498ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
499 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000500 ObjCMethodDecl *Redecl = 0;
501 if (HasRedeclaration)
502 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000503 if (Redecl)
504 return Redecl;
505
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000506 Decl *CtxD = cast<Decl>(getDeclContext());
507
508 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
509 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
510 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
511
512 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
513 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
514 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
515
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000516 } else if (ObjCImplementationDecl *ImplD =
517 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000518 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
519 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000520
521 } else if (ObjCCategoryImplDecl *CImplD =
522 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000523 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000524 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000525 }
526
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000527 if (!Redecl && isRedeclaration()) {
528 // This is the last redeclaration, go back to the first method.
529 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
530 isInstanceMethod());
531 }
532
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000533 return Redecl ? Redecl : this;
534}
535
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000536ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
537 Decl *CtxD = cast<Decl>(getDeclContext());
538
539 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
540 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
541 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
542 isInstanceMethod()))
543 return MD;
544
545 } else if (ObjCCategoryImplDecl *CImplD =
546 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000547 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000548 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
549 isInstanceMethod()))
550 return MD;
551 }
552
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000553 if (isRedeclaration())
554 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
555 isInstanceMethod());
556
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000557 return this;
558}
559
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000560SourceLocation ObjCMethodDecl::getLocEnd() const {
561 if (Stmt *Body = getBody())
562 return Body->getLocEnd();
563 return DeclEndLoc;
564}
565
John McCall85f3d762011-03-02 01:50:55 +0000566ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
567 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000568 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000569 return family;
570
John McCalld5313b02011-03-02 11:33:24 +0000571 // Check for an explicit attribute.
572 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
573 // The unfortunate necessity of mapping between enums here is due
574 // to the attributes framework.
575 switch (attr->getFamily()) {
576 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
577 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
578 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
579 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
580 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
581 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
582 }
583 Family = static_cast<unsigned>(family);
584 return family;
585 }
586
John McCall85f3d762011-03-02 01:50:55 +0000587 family = getSelector().getMethodFamily();
588 switch (family) {
589 case OMF_None: break;
590
591 // init only has a conventional meaning for an instance method, and
592 // it has to return an object.
593 case OMF_init:
594 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
595 family = OMF_None;
596 break;
597
598 // alloc/copy/new have a conventional meaning for both class and
599 // instance methods, but they require an object return.
600 case OMF_alloc:
601 case OMF_copy:
602 case OMF_mutableCopy:
603 case OMF_new:
604 if (!getResultType()->isObjCObjectPointerType())
605 family = OMF_None;
606 break;
607
608 // These selectors have a conventional meaning only for instance methods.
609 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000610 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000611 case OMF_retain:
612 case OMF_release:
613 case OMF_autorelease:
614 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000615 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000616 if (!isInstanceMethod())
617 family = OMF_None;
618 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000619
620 case OMF_performSelector:
621 if (!isInstanceMethod() ||
622 !getResultType()->isObjCIdType())
623 family = OMF_None;
624 else {
625 unsigned noParams = param_size();
626 if (noParams < 1 || noParams > 3)
627 family = OMF_None;
628 else {
629 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
630 QualType ArgT = (*it);
631 if (!ArgT->isObjCSelType()) {
632 family = OMF_None;
633 break;
634 }
635 while (--noParams) {
636 it++;
637 ArgT = (*it);
638 if (!ArgT->isObjCIdType()) {
639 family = OMF_None;
640 break;
641 }
642 }
643 }
644 }
645 break;
646
John McCall85f3d762011-03-02 01:50:55 +0000647 }
648
649 // Cache the result.
650 Family = static_cast<unsigned>(family);
651 return family;
652}
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000655 const ObjCInterfaceDecl *OID) {
656 QualType selfTy;
657 if (isInstanceMethod()) {
658 // There may be no interface context due to error in declaration
659 // of the interface (which has been reported). Recover gracefully.
660 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000661 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000662 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000663 } else {
664 selfTy = Context.getObjCIdType();
665 }
666 } else // we have a factory method.
667 selfTy = Context.getObjCClassType();
668
John McCall7acddac2011-06-17 06:42:21 +0000669 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000670 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000671
David Blaikie4e4d0842012-03-11 07:00:24 +0000672 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000673 if (isInstanceMethod()) {
674 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000675
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000676 // 'self' is always __strong. It's actually pseudo-strong except
677 // in init methods (or methods labeled ns_consumes_self), though.
678 Qualifiers qs;
679 qs.setObjCLifetime(Qualifiers::OCL_Strong);
680 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000681
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000682 // In addition, 'self' is const unless this is an init method.
683 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
684 selfTy = selfTy.withConst();
685 selfIsPseudoStrong = true;
686 }
687 }
688 else {
689 assert(isClassMethod());
690 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000691 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000692 selfIsPseudoStrong = true;
693 }
John McCallf85e1932011-06-15 23:02:42 +0000694 }
695
696 ImplicitParamDecl *self
697 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
698 &Context.Idents.get("self"), selfTy);
699 setSelfDecl(self);
700
701 if (selfIsConsumed)
702 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000703
John McCall7acddac2011-06-17 06:42:21 +0000704 if (selfIsPseudoStrong)
705 self->setARCPseudoStrong(true);
706
Mike Stump1eb44332009-09-09 15:08:12 +0000707 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
708 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000709 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000710}
711
Chris Lattnerab351632009-02-20 20:59:54 +0000712ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
713 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
714 return ID;
715 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
716 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000717 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000718 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000719
720 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000721 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000722}
723
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000724static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
725 const ObjCMethodDecl *Method,
726 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
727 bool MovedToSuper) {
728 if (!Container)
729 return;
730
731 // In categories look for overriden methods from protocols. A method from
732 // category is not "overriden" since it is considered as the "same" method
733 // (same USR) as the one from the interface.
734 if (const ObjCCategoryDecl *
735 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
736 // Check whether we have a matching method at this category but only if we
737 // are at the super class level.
738 if (MovedToSuper)
739 if (ObjCMethodDecl *
740 Overridden = Container->getMethod(Method->getSelector(),
741 Method->isInstanceMethod()))
742 if (Method != Overridden) {
743 // We found an override at this category; there is no need to look
744 // into its protocols.
745 Methods.push_back(Overridden);
746 return;
747 }
748
749 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
750 PEnd = Category->protocol_end();
751 P != PEnd; ++P)
752 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
753 return;
754 }
755
756 // Check whether we have a matching method at this level.
757 if (const ObjCMethodDecl *
758 Overridden = Container->getMethod(Method->getSelector(),
759 Method->isInstanceMethod()))
760 if (Method != Overridden) {
761 // We found an override at this level; there is no need to look
762 // into other protocols or categories.
763 Methods.push_back(Overridden);
764 return;
765 }
766
767 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
768 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
769 PEnd = Protocol->protocol_end();
770 P != PEnd; ++P)
771 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
772 }
773
774 if (const ObjCInterfaceDecl *
775 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
776 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
777 PEnd = Interface->protocol_end();
778 P != PEnd; ++P)
779 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
780
781 for (const ObjCCategoryDecl *Category = Interface->getCategoryList();
782 Category; Category = Category->getNextClassCategory())
783 CollectOverriddenMethodsRecurse(Category, Method, Methods,
784 MovedToSuper);
785
786 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
787 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
788 /*MovedToSuper=*/true);
789 }
790}
791
792static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
793 const ObjCMethodDecl *Method,
794 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
795 CollectOverriddenMethodsRecurse(Container, Method, Methods,
796 /*MovedToSuper=*/false);
797}
798
799static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
800 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
801 assert(Method->isOverriding());
802
803 if (const ObjCProtocolDecl *
804 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
805 CollectOverriddenMethods(ProtD, Method, overridden);
806
807 } else if (const ObjCImplDecl *
808 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
809 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
810 if (!ID)
811 return;
812 // Start searching for overridden methods using the method from the
813 // interface as starting point.
814 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
815 Method->isInstanceMethod()))
816 Method = IFaceMeth;
817 CollectOverriddenMethods(ID, Method, overridden);
818
819 } else if (const ObjCCategoryDecl *
820 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
821 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
822 if (!ID)
823 return;
824 // Start searching for overridden methods using the method from the
825 // interface as starting point.
826 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
827 Method->isInstanceMethod()))
828 Method = IFaceMeth;
829 CollectOverriddenMethods(ID, Method, overridden);
830
831 } else {
832 CollectOverriddenMethods(
833 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
834 Method, overridden);
835 }
836}
837
838static void collectOnCategoriesAfterLocation(SourceLocation Loc,
839 const ObjCInterfaceDecl *Class,
840 SourceManager &SM,
841 const ObjCMethodDecl *Method,
842 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
843 if (!Class)
844 return;
845
846 for (const ObjCCategoryDecl *Category = Class->getCategoryList();
847 Category; Category = Category->getNextClassCategory())
848 if (SM.isBeforeInTranslationUnit(Loc, Category->getLocation()))
849 CollectOverriddenMethodsRecurse(Category, Method, Methods, true);
850
851 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
852 Method, Methods);
853}
854
855/// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
856/// returns false.
857/// You'd think that in that case there are no overrides but categories can
858/// "introduce" new overridden methods that are missed by Sema because the
859/// overrides lookup that it does for methods, inside implementations, will
860/// stop at the interface level (if there is a method there) and not look
861/// further in super classes.
862static void collectOverriddenMethodsFast(SourceManager &SM,
863 const ObjCMethodDecl *Method,
864 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
865 assert(!Method->isOverriding());
866
867 const ObjCContainerDecl *
868 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
869 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
870 return;
871 const ObjCInterfaceDecl *Class = Method->getClassInterface();
872 if (!Class)
873 return;
874
875 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
876 SM, Method, Methods);
877}
878
879void ObjCMethodDecl::getOverriddenMethods(
880 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
881 const ObjCMethodDecl *Method = this;
882
883 if (Method->isRedeclaration()) {
884 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
885 getMethod(Method->getSelector(), Method->isInstanceMethod());
886 }
887
888 if (!Method->isOverriding()) {
889 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
890 Method, Overridden);
891 } else {
892 collectOverriddenMethodsSlow(Method, Overridden);
893 assert(!Overridden.empty() &&
894 "ObjCMethodDecl's overriding bit is not as expected");
895 }
896}
897
Jordan Rose04bec392012-10-10 16:42:54 +0000898const ObjCPropertyDecl *
899ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
900 Selector Sel = getSelector();
901 unsigned NumArgs = Sel.getNumArgs();
902 if (NumArgs > 1)
903 return 0;
904
905 if (getMethodFamily() != OMF_None)
906 return 0;
907
908 if (isPropertyAccessor()) {
909 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
910 bool IsGetter = (NumArgs == 0);
911
912 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
913 E = Container->prop_end();
914 I != E; ++I) {
915 Selector NextSel = IsGetter ? (*I)->getGetterName()
916 : (*I)->getSetterName();
917 if (NextSel == Sel)
918 return *I;
919 }
920
921 llvm_unreachable("Marked as a property accessor but no property found!");
922 }
923
924 if (!CheckOverrides)
925 return 0;
926
927 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
928 OverridesTy Overrides;
929 getOverriddenMethods(Overrides);
930 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
931 I != E; ++I) {
932 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
933 return Prop;
934 }
935
936 return 0;
937
938}
939
Chris Lattnerab351632009-02-20 20:59:54 +0000940//===----------------------------------------------------------------------===//
941// ObjCInterfaceDecl
942//===----------------------------------------------------------------------===//
943
Douglas Gregora6ea10e2012-01-17 18:09:05 +0000944ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +0000945 DeclContext *DC,
946 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000947 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000948 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000949 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000950 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000951 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000952 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000953 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000954 return Result;
955}
956
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000957ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
958 unsigned ID) {
959 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
960 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
961 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +0000962}
963
964ObjCInterfaceDecl::
965ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000966 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
967 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000968 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +0000969 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000970{
Douglas Gregorfd002a72011-12-16 22:37:11 +0000971 setPreviousDeclaration(PrevDecl);
972
973 // Copy the 'data' pointer over.
974 if (PrevDecl)
975 Data = PrevDecl->Data;
976
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +0000977 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000978}
979
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000980void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000981 assert(data().ExternallyCompleted && "Class is not externally completed");
982 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000983 getASTContext().getExternalSource()->CompleteType(
984 const_cast<ObjCInterfaceDecl *>(this));
985}
986
987void ObjCInterfaceDecl::setExternallyCompleted() {
988 assert(getASTContext().getExternalSource() &&
989 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000990 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000991 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000992 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000993}
994
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000995ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000996 if (const ObjCInterfaceDecl *Def = getDefinition()) {
997 if (data().ExternallyCompleted)
998 LoadExternalDefinition();
999
1000 return getASTContext().getObjCImplementation(
1001 const_cast<ObjCInterfaceDecl*>(Def));
1002 }
1003
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001004 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001005 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001006}
1007
1008void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001009 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001010}
1011
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001012/// all_declared_ivar_begin - return first ivar declared in this class,
1013/// its extensions and its implementation. Lazily build the list on first
1014/// access.
1015ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001016 // FIXME: Should make sure no callers ever do this.
1017 if (!hasDefinition())
1018 return 0;
1019
1020 if (data().IvarList)
1021 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001022
1023 ObjCIvarDecl *curIvar = 0;
1024 if (!ivar_empty()) {
1025 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
David Blaikie581deb32012-06-06 20:45:41 +00001026 data().IvarList = *I; ++I;
1027 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1028 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001029 }
1030
1031 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
1032 CDecl = CDecl->getNextClassExtension()) {
1033 if (!CDecl->ivar_empty()) {
1034 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1035 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001036 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001037 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001038 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001039 }
David Blaikie581deb32012-06-06 20:45:41 +00001040 for ( ;I != E; curIvar = *I, ++I)
1041 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001042 }
1043 }
1044
1045 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1046 if (!ImplDecl->ivar_empty()) {
1047 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1048 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001049 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001050 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001051 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001052 }
David Blaikie581deb32012-06-06 20:45:41 +00001053 for ( ;I != E; curIvar = *I, ++I)
1054 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001055 }
1056 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001057 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001058}
Chris Lattnerab351632009-02-20 20:59:54 +00001059
1060/// FindCategoryDeclaration - Finds category declaration in the list of
1061/// categories for this class and returns it. Name of the category is passed
1062/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001063///
Chris Lattnerab351632009-02-20 20:59:54 +00001064ObjCCategoryDecl *
1065ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001066 // FIXME: Should make sure no callers ever do this.
1067 if (!hasDefinition())
1068 return 0;
1069
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001070 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001071 LoadExternalDefinition();
1072
Chris Lattnerab351632009-02-20 20:59:54 +00001073 for (ObjCCategoryDecl *Category = getCategoryList();
1074 Category; Category = Category->getNextClassCategory())
1075 if (Category->getIdentifier() == CategoryId)
1076 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001077 return 0;
1078}
1079
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001080ObjCMethodDecl *
1081ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1082 for (ObjCCategoryDecl *Category = getCategoryList();
1083 Category; Category = Category->getNextClassCategory())
1084 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1085 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1086 return MD;
1087 return 0;
1088}
1089
1090ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1091 for (ObjCCategoryDecl *Category = getCategoryList();
1092 Category; Category = Category->getNextClassCategory())
1093 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1094 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1095 return MD;
1096 return 0;
1097}
1098
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001099/// ClassImplementsProtocol - Checks that 'lProto' protocol
1100/// has been implemented in IDecl class, its super class or categories (if
1101/// lookupCategory is true).
1102bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1103 bool lookupCategory,
1104 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001105 if (!hasDefinition())
1106 return false;
1107
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001108 ObjCInterfaceDecl *IDecl = this;
1109 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001110 for (ObjCInterfaceDecl::protocol_iterator
1111 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001112 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1113 return true;
1114 // This is dubious and is added to be compatible with gcc. In gcc, it is
1115 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1116 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1117 // object. This IMO, should be a bug.
1118 // FIXME: Treat this as an extension, and flag this as an error when GCC
1119 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001120 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001121 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1122 return true;
1123 }
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001125 // 2nd, look up the category.
1126 if (lookupCategory)
1127 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
1128 CDecl = CDecl->getNextClassCategory()) {
1129 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
1130 E = CDecl->protocol_end(); PI != E; ++PI)
1131 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1132 return true;
1133 }
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001135 // 3rd, look up the super class(s)
1136 if (IDecl->getSuperClass())
1137 return
1138 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1139 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001141 return false;
1142}
1143
Chris Lattnerab351632009-02-20 20:59:54 +00001144//===----------------------------------------------------------------------===//
1145// ObjCIvarDecl
1146//===----------------------------------------------------------------------===//
1147
David Blaikie99ba9e32011-12-20 02:48:34 +00001148void ObjCIvarDecl::anchor() { }
1149
Daniel Dunbara0654922010-04-02 20:10:03 +00001150ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001151 SourceLocation StartLoc,
1152 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001153 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001154 AccessControl ac, Expr *BW,
1155 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001156 if (DC) {
1157 // Ivar's can only appear in interfaces, implementations (via synthesized
1158 // properties), and class extensions (via direct declaration, or synthesized
1159 // properties).
1160 //
1161 // FIXME: This should really be asserting this:
1162 // (isa<ObjCCategoryDecl>(DC) &&
1163 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1164 // but unfortunately we sometimes place ivars into non-class extension
1165 // categories on error. This breaks an AST invariant, and should not be
1166 // fixed.
1167 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1168 isa<ObjCCategoryDecl>(DC)) &&
1169 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001170 // Once a new ivar is created in any of class/class-extension/implementation
1171 // decl contexts, the previously built IvarList must be rebuilt.
1172 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1173 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001174 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001175 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001176 else
1177 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001178 }
1179 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001180 }
1181
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001182 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1183 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001184}
1185
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001186ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1187 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1188 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1189 QualType(), 0, ObjCIvarDecl::None, 0, false);
1190}
1191
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001192const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1193 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001194
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001195 switch (DC->getKind()) {
1196 default:
1197 case ObjCCategoryImpl:
1198 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001199 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001200
1201 // Ivars can only appear in class extension categories.
1202 case ObjCCategory: {
1203 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1204 assert(CD->IsClassExtension() && "invalid container for ivar!");
1205 return CD->getClassInterface();
1206 }
1207
1208 case ObjCImplementation:
1209 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1210
1211 case ObjCInterface:
1212 return cast<ObjCInterfaceDecl>(DC);
1213 }
1214}
Chris Lattnerab351632009-02-20 20:59:54 +00001215
1216//===----------------------------------------------------------------------===//
1217// ObjCAtDefsFieldDecl
1218//===----------------------------------------------------------------------===//
1219
David Blaikie99ba9e32011-12-20 02:48:34 +00001220void ObjCAtDefsFieldDecl::anchor() { }
1221
Chris Lattnerab351632009-02-20 20:59:54 +00001222ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001223*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1224 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001225 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001226 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001227}
1228
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001229ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1230 unsigned ID) {
1231 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1232 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1233 0, QualType(), 0);
1234}
1235
Chris Lattnerab351632009-02-20 20:59:54 +00001236//===----------------------------------------------------------------------===//
1237// ObjCProtocolDecl
1238//===----------------------------------------------------------------------===//
1239
David Blaikie99ba9e32011-12-20 02:48:34 +00001240void ObjCProtocolDecl::anchor() { }
1241
Douglas Gregor27c6da22012-01-01 20:30:41 +00001242ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1243 SourceLocation nameLoc,
1244 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001245 ObjCProtocolDecl *PrevDecl)
1246 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001247{
1248 setPreviousDeclaration(PrevDecl);
1249 if (PrevDecl)
1250 Data = PrevDecl->Data;
1251}
1252
Chris Lattnerab351632009-02-20 20:59:54 +00001253ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001254 IdentifierInfo *Id,
1255 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001256 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001257 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001258 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001259 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001260
1261 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001262}
1263
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001264ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1265 unsigned ID) {
1266 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1267 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1268 0);
1269}
1270
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001271ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1272 ObjCProtocolDecl *PDecl = this;
1273
1274 if (Name == getIdentifier())
1275 return PDecl;
1276
1277 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1278 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1279 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001281 return NULL;
1282}
1283
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001284// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001285// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001286ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1287 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001288 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001290 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001291 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Chris Lattnerab351632009-02-20 20:59:54 +00001293 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001294 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001295 return MethodDecl;
1296 return NULL;
1297}
1298
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001299void ObjCProtocolDecl::allocateDefinitionData() {
1300 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001301 Data = new (getASTContext()) DefinitionData;
1302 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001303}
1304
1305void ObjCProtocolDecl::startDefinition() {
1306 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001307
1308 // Update all of the declarations with a pointer to the definition.
1309 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1310 RD != RDEnd; ++RD)
1311 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001312}
1313
Chris Lattnerab351632009-02-20 20:59:54 +00001314//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001315// ObjCCategoryDecl
1316//===----------------------------------------------------------------------===//
1317
David Blaikie99ba9e32011-12-20 02:48:34 +00001318void ObjCCategoryDecl::anchor() { }
1319
Chris Lattnerab351632009-02-20 20:59:54 +00001320ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001321 SourceLocation AtLoc,
1322 SourceLocation ClassNameLoc,
1323 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001324 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001325 ObjCInterfaceDecl *IDecl,
1326 SourceLocation IvarLBraceLoc,
1327 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001328 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1329 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001330 IDecl,
1331 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001332 if (IDecl) {
1333 // Link this category into its class's category list.
1334 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001335 if (IDecl->hasDefinition()) {
1336 IDecl->setCategoryList(CatDecl);
1337 if (ASTMutationListener *L = C.getASTMutationListener())
1338 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1339 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001340 }
1341
1342 return CatDecl;
1343}
1344
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001345ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1346 unsigned ID) {
1347 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1348 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1349 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001350}
1351
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001352ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1353 return getASTContext().getObjCImplementation(
1354 const_cast<ObjCCategoryDecl*>(this));
1355}
1356
1357void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1358 getASTContext().setObjCImplementation(this, ImplD);
1359}
1360
1361
Chris Lattnerab351632009-02-20 20:59:54 +00001362//===----------------------------------------------------------------------===//
1363// ObjCCategoryImplDecl
1364//===----------------------------------------------------------------------===//
1365
David Blaikie99ba9e32011-12-20 02:48:34 +00001366void ObjCCategoryImplDecl::anchor() { }
1367
Chris Lattnerab351632009-02-20 20:59:54 +00001368ObjCCategoryImplDecl *
1369ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001370 IdentifierInfo *Id,
1371 ObjCInterfaceDecl *ClassInterface,
1372 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001373 SourceLocation atStartLoc,
1374 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001375 if (ClassInterface && ClassInterface->hasDefinition())
1376 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001377 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001378 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001379}
1380
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001381ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1382 unsigned ID) {
1383 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1384 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1385 SourceLocation(), SourceLocation());
1386}
1387
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001388ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001389 // The class interface might be NULL if we are working with invalid code.
1390 if (const ObjCInterfaceDecl *ID = getClassInterface())
1391 return ID->FindCategoryDeclaration(getIdentifier());
1392 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001393}
1394
Chris Lattnerab351632009-02-20 20:59:54 +00001395
David Blaikie99ba9e32011-12-20 02:48:34 +00001396void ObjCImplDecl::anchor() { }
1397
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001398void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001399 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001400 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001401 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001402}
1403
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001404void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1405 ASTContext &Ctx = getASTContext();
1406
1407 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001408 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001409 if (IFace)
1410 Ctx.setObjCImplementation(IFace, ImplD);
1411
Duncan Sands98f2cca2009-07-21 07:56:29 +00001412 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001413 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1414 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1415 Ctx.setObjCImplementation(CD, ImplD);
1416 }
1417
1418 ClassInterface = IFace;
1419}
1420
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001421/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
James Dennett1c3a46a2012-06-15 22:30:14 +00001422/// properties implemented in this category \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001423/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001424///
Chris Lattner3aa18612009-02-28 18:42:10 +00001425ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001426FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1427 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001428 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001429 if (PID->getPropertyIvarDecl() &&
1430 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1431 return PID;
1432 }
1433 return 0;
1434}
1435
1436/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001437/// added to the list of those properties \@synthesized/\@dynamic in this
1438/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001439///
Chris Lattner3aa18612009-02-28 18:42:10 +00001440ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001441FindPropertyImplDecl(IdentifierInfo *Id) const {
1442 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001443 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001444 if (PID->getPropertyDecl()->getIdentifier() == Id)
1445 return PID;
1446 }
1447 return 0;
1448}
1449
Chris Lattner5f9e2722011-07-23 10:55:15 +00001450raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001451 const ObjCCategoryImplDecl &CID) {
1452 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001453 return OS;
1454}
1455
Chris Lattnerab351632009-02-20 20:59:54 +00001456//===----------------------------------------------------------------------===//
1457// ObjCImplementationDecl
1458//===----------------------------------------------------------------------===//
1459
David Blaikie99ba9e32011-12-20 02:48:34 +00001460void ObjCImplementationDecl::anchor() { }
1461
Chris Lattnerab351632009-02-20 20:59:54 +00001462ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001463ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001464 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001465 ObjCInterfaceDecl *SuperDecl,
1466 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001467 SourceLocation atStartLoc,
1468 SourceLocation IvarLBraceLoc,
1469 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001470 if (ClassInterface && ClassInterface->hasDefinition())
1471 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001472 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001473 nameLoc, atStartLoc,
1474 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001475}
1476
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001477ObjCImplementationDecl *
1478ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1479 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1480 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1481 SourceLocation());
1482}
1483
John McCallda6d9762011-07-22 04:15:06 +00001484void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1485 CXXCtorInitializer ** initializers,
1486 unsigned numInitializers) {
1487 if (numInitializers > 0) {
1488 NumIvarInitializers = numInitializers;
1489 CXXCtorInitializer **ivarInitializers =
1490 new (C) CXXCtorInitializer*[NumIvarInitializers];
1491 memcpy(ivarInitializers, initializers,
1492 numInitializers * sizeof(CXXCtorInitializer*));
1493 IvarInitializers = ivarInitializers;
1494 }
1495}
1496
Chris Lattner5f9e2722011-07-23 10:55:15 +00001497raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001498 const ObjCImplementationDecl &ID) {
1499 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001500 return OS;
1501}
1502
Chris Lattnerab351632009-02-20 20:59:54 +00001503//===----------------------------------------------------------------------===//
1504// ObjCCompatibleAliasDecl
1505//===----------------------------------------------------------------------===//
1506
David Blaikie99ba9e32011-12-20 02:48:34 +00001507void ObjCCompatibleAliasDecl::anchor() { }
1508
Chris Lattnerab351632009-02-20 20:59:54 +00001509ObjCCompatibleAliasDecl *
1510ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1511 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001512 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001513 ObjCInterfaceDecl* AliasedClass) {
1514 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1515}
1516
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001517ObjCCompatibleAliasDecl *
1518ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1519 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1520 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1521}
1522
Chris Lattnerab351632009-02-20 20:59:54 +00001523//===----------------------------------------------------------------------===//
1524// ObjCPropertyDecl
1525//===----------------------------------------------------------------------===//
1526
David Blaikie99ba9e32011-12-20 02:48:34 +00001527void ObjCPropertyDecl::anchor() { }
1528
Chris Lattnerab351632009-02-20 20:59:54 +00001529ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1530 SourceLocation L,
1531 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001532 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001533 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001534 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001535 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001536 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001537}
1538
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001539ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1540 unsigned ID) {
1541 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1542 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001543 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001544 0);
1545}
1546
Chris Lattnerab351632009-02-20 20:59:54 +00001547//===----------------------------------------------------------------------===//
1548// ObjCPropertyImplDecl
1549//===----------------------------------------------------------------------===//
1550
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001551ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001552 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001553 SourceLocation atLoc,
1554 SourceLocation L,
1555 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001556 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001557 ObjCIvarDecl *ivar,
1558 SourceLocation ivarLoc) {
1559 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1560 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001561}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001562
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001563ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1564 unsigned ID) {
1565 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1566 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1567 0, Dynamic, 0, SourceLocation());
1568}
1569
Douglas Gregora4ffd852010-11-17 01:03:52 +00001570SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1571 SourceLocation EndLoc = getLocation();
1572 if (IvarLoc.isValid())
1573 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001574
Douglas Gregora4ffd852010-11-17 01:03:52 +00001575 return SourceRange(AtLoc, EndLoc);
1576}