blob: 7eb2688134bef92f52715330478736657fd5780b [file] [log] [blame]
Chris Lattner1e03a562008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +000017#include "clang/AST/ASTMutationListener.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000019using namespace clang;
20
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000021//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000022// ObjCListBase
23//===----------------------------------------------------------------------===//
24
Chris Lattner38af2de2009-02-20 21:35:13 +000025void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000026 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000027 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000028
29
Chris Lattner4ee413b2009-02-20 21:44:01 +000030 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000031 NumElts = Elts;
32 memcpy(List, InList, sizeof(void*)*Elts);
33}
34
Douglas Gregor18df52b2010-01-16 15:02:53 +000035void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
36 const SourceLocation *Locs, ASTContext &Ctx) {
37 if (Elts == 0)
38 return;
39
40 Locations = new (Ctx) SourceLocation[Elts];
41 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
42 set(InList, Elts, Ctx);
43}
44
Chris Lattner11e1e1a2009-02-20 21:16:26 +000045//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000046// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000047//===----------------------------------------------------------------------===//
48
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000049/// getIvarDecl - This method looks up an ivar in this ContextDecl.
50///
51ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000052ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000053 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000054 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000055 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
56 return ivar;
57 }
58 return 0;
59}
60
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000061// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000062ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000063ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000064 // Since instance & class methods can have the same name, the loop below
65 // ensures we get the correct method.
66 //
67 // @interface Whatever
68 // - (int) class_method;
69 // + (float) class_method;
70 // @end
71 //
72 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000073 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000074 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000075 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000076 return MD;
77 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000078 return 0;
79}
80
Ted Kremenek9f550ff2010-03-15 20:11:46 +000081ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000082ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000083 IdentifierInfo *propertyID) {
84
Ted Kremenekde09d0c2010-03-15 20:11:53 +000085 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000086 llvm::tie(I, E) = DC->lookup(propertyID);
87 for ( ; I != E; ++I)
88 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
89 return PD;
90
91 return 0;
92}
93
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000094/// FindPropertyDeclaration - Finds declaration of the property given its name
95/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000096ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000097ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenekde09d0c2010-03-15 20:11:53 +000099 if (ObjCPropertyDecl *PD =
100 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
101 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000103 switch (getKind()) {
104 default:
105 break;
106 case Decl::ObjCProtocol: {
107 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
108 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
109 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000110 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
111 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000112 break;
113 }
114 case Decl::ObjCInterface: {
115 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
116 // Look through categories.
117 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
118 Cat; Cat = Cat->getNextClassCategory())
119 if (!Cat->IsClassExtension())
120 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
121 return P;
122
123 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000124 for (ObjCInterfaceDecl::all_protocol_iterator
125 I = OID->all_referenced_protocol_begin(),
126 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000127 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
128 return P;
129
130 // Finally, check the super class.
131 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
132 return superClass->FindPropertyDeclaration(PropertyId);
133 break;
134 }
135 case Decl::ObjCCategory: {
136 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
137 // Look through protocols.
138 if (!OCD->IsClassExtension())
139 for (ObjCCategoryDecl::protocol_iterator
140 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
141 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
142 return P;
143
144 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000145 }
146 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000147 return 0;
148}
149
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000150/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
151/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000152/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000153///
154ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000155ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000156 IdentifierInfo *PropertyId) const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000157 if (ExternallyCompleted)
158 LoadExternalDefinition();
159
Ted Kremenek37cafb02010-03-15 20:30:07 +0000160 if (ObjCPropertyDecl *PD =
161 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
162 return PD;
163
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000164 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000165 for (ObjCInterfaceDecl::all_protocol_iterator
166 I = all_referenced_protocol_begin(),
167 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000168 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
169 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000170
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000171 return 0;
172}
173
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000174void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
175 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
176 ASTContext &C)
177{
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000178 if (ExternallyCompleted)
179 LoadExternalDefinition();
180
Ted Kremenek53b94412010-09-01 01:21:15 +0000181 if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) {
182 AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000183 return;
184 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000185
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000186 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000187 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000188 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000189 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000190 for (unsigned i = 0; i < ExtNum; i++) {
191 bool protocolExists = false;
192 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000193 for (all_protocol_iterator
194 p = all_referenced_protocol_begin(),
195 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000196 ObjCProtocolDecl *Proto = (*p);
197 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
198 protocolExists = true;
199 break;
200 }
201 }
202 // Do we want to warn on a protocol in extension class which
203 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000204 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000205 ProtocolRefs.push_back(ProtoInExtension);
206 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000207
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000208 if (ProtocolRefs.empty())
209 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000210
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000211 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000212 for (all_protocol_iterator p = all_referenced_protocol_begin(),
213 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000214 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000215 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000216
217 AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000218}
219
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000220/// getFirstClassExtension - Find first class extension of the given class.
221ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
222 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000223 CDecl = CDecl->getNextClassCategory())
224 if (CDecl->IsClassExtension())
225 return CDecl;
226 return 0;
227}
228
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000229/// getNextClassCategory - Find next class extension in list of categories.
230const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
231 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
232 CDecl = CDecl->getNextClassCategory())
233 if (CDecl->IsClassExtension())
234 return CDecl;
235 return 0;
236}
237
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000238ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
239 ObjCInterfaceDecl *&clsDeclared) {
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000240 if (ExternallyCompleted)
241 LoadExternalDefinition();
242
Chris Lattner1e03a562008-03-16 00:19:01 +0000243 ObjCInterfaceDecl* ClassDecl = this;
244 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000245 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000246 clsDeclared = ClassDecl;
247 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000248 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000249 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
250 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000251 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
252 clsDeclared = ClassDecl;
253 return I;
254 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000255 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000256
Chris Lattner1e03a562008-03-16 00:19:01 +0000257 ClassDecl = ClassDecl->getSuperClass();
258 }
259 return NULL;
260}
261
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000262/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
263/// class whose name is passed as argument. If it is not one of the super classes
264/// the it returns NULL.
265ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
266 const IdentifierInfo*ICName) {
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000267 if (ExternallyCompleted)
268 LoadExternalDefinition();
269
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000270 ObjCInterfaceDecl* ClassDecl = this;
271 while (ClassDecl != NULL) {
272 if (ClassDecl->getIdentifier() == ICName)
273 return ClassDecl;
274 ClassDecl = ClassDecl->getSuperClass();
275 }
276 return NULL;
277}
278
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000279/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000280/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000281ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
282 bool isInstance) const {
283 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000284 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000286 if (ExternallyCompleted)
287 LoadExternalDefinition();
288
Chris Lattner1e03a562008-03-16 00:19:01 +0000289 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000290 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000291 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner1e03a562008-03-16 00:19:01 +0000293 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000294 const ObjCList<ObjCProtocolDecl> &Protocols =
295 ClassDecl->getReferencedProtocols();
296 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
297 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000298 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000299 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattner1e03a562008-03-16 00:19:01 +0000301 // Didn't find one yet - now look through categories.
302 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
303 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000304 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000305 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Steve Naroffb79c01e2008-12-08 20:57:28 +0000307 // Didn't find one yet - look through protocols.
308 const ObjCList<ObjCProtocolDecl> &Protocols =
309 CatDecl->getReferencedProtocols();
310 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
311 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000312 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000313 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000314 CatDecl = CatDecl->getNextClassCategory();
315 }
316 ClassDecl = ClassDecl->getSuperClass();
317 }
318 return NULL;
319}
320
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000321ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
322 const Selector &Sel,
323 bool Instance) {
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000324 if (ExternallyCompleted)
325 LoadExternalDefinition();
326
Steve Naroffd789d3d2009-10-01 23:46:04 +0000327 ObjCMethodDecl *Method = 0;
328 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000329 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
330 : ImpDecl->getClassMethod(Sel);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000331
332 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000333 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000334 return Method;
335}
Chris Lattnerab351632009-02-20 20:59:54 +0000336
337//===----------------------------------------------------------------------===//
338// ObjCMethodDecl
339//===----------------------------------------------------------------------===//
340
341ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000342 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000343 SourceLocation endLoc,
344 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000345 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000346 DeclContext *contextDecl,
347 bool isInstance,
348 bool isVariadic,
349 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000350 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000351 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000352 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000353 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000354 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000355 SelInfo, T, ResultTInfo, contextDecl,
356 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000357 isVariadic, isSynthesized, isImplicitlyDeclared,
358 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000359 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000360 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000361}
362
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000363void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
364 assert(PrevMethod);
365 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
366 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000367 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000368}
369
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000370void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
371 ArrayRef<ParmVarDecl*> Params,
372 ArrayRef<SourceLocation> SelLocs) {
373 ParamsAndSelLocs = 0;
374 NumParams = Params.size();
375 if (Params.empty() && SelLocs.empty())
376 return;
377
378 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
379 sizeof(SourceLocation) * SelLocs.size();
380 ParamsAndSelLocs = C.Allocate(Size);
381 std::copy(Params.begin(), Params.end(), getParams());
382 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
383}
384
385void ObjCMethodDecl::getSelectorLocs(
386 SmallVectorImpl<SourceLocation> &SelLocs) const {
387 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
388 SelLocs.push_back(getSelectorLoc(i));
389}
390
391void ObjCMethodDecl::setMethodParams(ASTContext &C,
392 ArrayRef<ParmVarDecl*> Params,
393 ArrayRef<SourceLocation> SelLocs) {
394 assert((!SelLocs.empty() || isImplicit()) &&
395 "No selector locs for non-implicit method");
396 if (isImplicit())
397 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
398
399 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
400 if (SelLocsKind != SelLoc_NonStandard)
401 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
402
403 setParamsAndSelLocs(C, Params, SelLocs);
404}
405
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000406/// \brief A definition will return its interface declaration.
407/// An interface declaration will return its definition.
408/// Otherwise it will return itself.
409ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
410 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000411 ObjCMethodDecl *Redecl = 0;
412 if (HasRedeclaration)
413 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000414 if (Redecl)
415 return Redecl;
416
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000417 Decl *CtxD = cast<Decl>(getDeclContext());
418
419 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
420 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
421 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
422
423 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
424 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
425 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
426
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000427 } else if (ObjCImplementationDecl *ImplD =
428 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000429 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
430 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000431
432 } else if (ObjCCategoryImplDecl *CImplD =
433 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000434 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000435 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000436 }
437
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000438 if (!Redecl && isRedeclaration()) {
439 // This is the last redeclaration, go back to the first method.
440 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
441 isInstanceMethod());
442 }
443
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000444 return Redecl ? Redecl : this;
445}
446
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000447ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
448 Decl *CtxD = cast<Decl>(getDeclContext());
449
450 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
451 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
452 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
453 isInstanceMethod()))
454 return MD;
455
456 } else if (ObjCCategoryImplDecl *CImplD =
457 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000458 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000459 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
460 isInstanceMethod()))
461 return MD;
462 }
463
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000464 if (isRedeclaration())
465 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
466 isInstanceMethod());
467
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000468 return this;
469}
470
John McCall85f3d762011-03-02 01:50:55 +0000471ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
472 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000473 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000474 return family;
475
John McCalld5313b02011-03-02 11:33:24 +0000476 // Check for an explicit attribute.
477 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
478 // The unfortunate necessity of mapping between enums here is due
479 // to the attributes framework.
480 switch (attr->getFamily()) {
481 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
482 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
483 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
484 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
485 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
486 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
487 }
488 Family = static_cast<unsigned>(family);
489 return family;
490 }
491
John McCall85f3d762011-03-02 01:50:55 +0000492 family = getSelector().getMethodFamily();
493 switch (family) {
494 case OMF_None: break;
495
496 // init only has a conventional meaning for an instance method, and
497 // it has to return an object.
498 case OMF_init:
499 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
500 family = OMF_None;
501 break;
502
503 // alloc/copy/new have a conventional meaning for both class and
504 // instance methods, but they require an object return.
505 case OMF_alloc:
506 case OMF_copy:
507 case OMF_mutableCopy:
508 case OMF_new:
509 if (!getResultType()->isObjCObjectPointerType())
510 family = OMF_None;
511 break;
512
513 // These selectors have a conventional meaning only for instance methods.
514 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000515 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000516 case OMF_retain:
517 case OMF_release:
518 case OMF_autorelease:
519 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000520 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000521 if (!isInstanceMethod())
522 family = OMF_None;
523 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000524
525 case OMF_performSelector:
526 if (!isInstanceMethod() ||
527 !getResultType()->isObjCIdType())
528 family = OMF_None;
529 else {
530 unsigned noParams = param_size();
531 if (noParams < 1 || noParams > 3)
532 family = OMF_None;
533 else {
534 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
535 QualType ArgT = (*it);
536 if (!ArgT->isObjCSelType()) {
537 family = OMF_None;
538 break;
539 }
540 while (--noParams) {
541 it++;
542 ArgT = (*it);
543 if (!ArgT->isObjCIdType()) {
544 family = OMF_None;
545 break;
546 }
547 }
548 }
549 }
550 break;
551
John McCall85f3d762011-03-02 01:50:55 +0000552 }
553
554 // Cache the result.
555 Family = static_cast<unsigned>(family);
556 return family;
557}
558
Mike Stump1eb44332009-09-09 15:08:12 +0000559void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000560 const ObjCInterfaceDecl *OID) {
561 QualType selfTy;
562 if (isInstanceMethod()) {
563 // There may be no interface context due to error in declaration
564 // of the interface (which has been reported). Recover gracefully.
565 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000566 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000567 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000568 } else {
569 selfTy = Context.getObjCIdType();
570 }
571 } else // we have a factory method.
572 selfTy = Context.getObjCClassType();
573
John McCall7acddac2011-06-17 06:42:21 +0000574 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000575 bool selfIsConsumed = false;
576 if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) {
577 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
578
John McCall7acddac2011-06-17 06:42:21 +0000579 // 'self' is always __strong. It's actually pseudo-strong except
580 // in init methods, though.
John McCallf85e1932011-06-15 23:02:42 +0000581 Qualifiers qs;
582 qs.setObjCLifetime(Qualifiers::OCL_Strong);
583 selfTy = Context.getQualifiedType(selfTy, qs);
584
585 // In addition, 'self' is const unless this is an init method.
John McCall7acddac2011-06-17 06:42:21 +0000586 if (getMethodFamily() != OMF_init) {
John McCallf85e1932011-06-15 23:02:42 +0000587 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000588 selfIsPseudoStrong = true;
589 }
John McCallf85e1932011-06-15 23:02:42 +0000590 }
591
592 ImplicitParamDecl *self
593 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
594 &Context.Idents.get("self"), selfTy);
595 setSelfDecl(self);
596
597 if (selfIsConsumed)
598 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000599
John McCall7acddac2011-06-17 06:42:21 +0000600 if (selfIsPseudoStrong)
601 self->setARCPseudoStrong(true);
602
Mike Stump1eb44332009-09-09 15:08:12 +0000603 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
604 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000605 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000606}
607
Chris Lattnerab351632009-02-20 20:59:54 +0000608ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
609 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
610 return ID;
611 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
612 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000613 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000614 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000615
616 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000617 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000618}
619
Chris Lattnerab351632009-02-20 20:59:54 +0000620//===----------------------------------------------------------------------===//
621// ObjCInterfaceDecl
622//===----------------------------------------------------------------------===//
623
624ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
625 DeclContext *DC,
626 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000627 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000628 SourceLocation ClassLoc,
629 bool ForwardDecl, bool isInternal){
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000630 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
631 isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000632}
633
634ObjCInterfaceDecl::
635ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000636 SourceLocation CLoc, bool FD, bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000637 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Chris Lattnerab351632009-02-20 20:59:54 +0000638 TypeForDecl(0), SuperClass(0),
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000639 CategoryList(0), IvarList(0),
640 InitiallyForwardDecl(FD), ForwardDecl(FD),
641 InternalInterface(isInternal), ExternallyCompleted(false) {
Chris Lattnerab351632009-02-20 20:59:54 +0000642}
643
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000644void ObjCInterfaceDecl::LoadExternalDefinition() const {
645 assert(ExternallyCompleted && "Class is not externally completed");
646 ExternallyCompleted = false;
647 getASTContext().getExternalSource()->CompleteType(
648 const_cast<ObjCInterfaceDecl *>(this));
649}
650
651void ObjCInterfaceDecl::setExternallyCompleted() {
652 assert(getASTContext().getExternalSource() &&
653 "Class can't be externally completed without an external source");
654 assert(!ForwardDecl &&
655 "Forward declarations can't be externally completed");
656 ExternallyCompleted = true;
657}
658
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000659ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000660 if (ExternallyCompleted)
661 LoadExternalDefinition();
662
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000663 return getASTContext().getObjCImplementation(
664 const_cast<ObjCInterfaceDecl*>(this));
665}
666
667void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
668 getASTContext().setObjCImplementation(this, ImplD);
669}
670
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000671/// all_declared_ivar_begin - return first ivar declared in this class,
672/// its extensions and its implementation. Lazily build the list on first
673/// access.
674ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
675 if (IvarList)
676 return IvarList;
677
678 ObjCIvarDecl *curIvar = 0;
679 if (!ivar_empty()) {
680 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
681 IvarList = (*I); ++I;
682 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
683 curIvar->setNextIvar(*I);
684 }
685
686 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
687 CDecl = CDecl->getNextClassExtension()) {
688 if (!CDecl->ivar_empty()) {
689 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
690 E = CDecl->ivar_end();
691 if (!IvarList) {
692 IvarList = (*I); ++I;
693 curIvar = IvarList;
694 }
695 for ( ;I != E; curIvar = *I, ++I)
696 curIvar->setNextIvar(*I);
697 }
698 }
699
700 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
701 if (!ImplDecl->ivar_empty()) {
702 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
703 E = ImplDecl->ivar_end();
704 if (!IvarList) {
705 IvarList = (*I); ++I;
706 curIvar = IvarList;
707 }
708 for ( ;I != E; curIvar = *I, ++I)
709 curIvar->setNextIvar(*I);
710 }
711 }
712 return IvarList;
713}
Chris Lattnerab351632009-02-20 20:59:54 +0000714
715/// FindCategoryDeclaration - Finds category declaration in the list of
716/// categories for this class and returns it. Name of the category is passed
717/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000718///
Chris Lattnerab351632009-02-20 20:59:54 +0000719ObjCCategoryDecl *
720ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000721 if (ExternallyCompleted)
722 LoadExternalDefinition();
723
Chris Lattnerab351632009-02-20 20:59:54 +0000724 for (ObjCCategoryDecl *Category = getCategoryList();
725 Category; Category = Category->getNextClassCategory())
726 if (Category->getIdentifier() == CategoryId)
727 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000728 return 0;
729}
730
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000731ObjCMethodDecl *
732ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
733 for (ObjCCategoryDecl *Category = getCategoryList();
734 Category; Category = Category->getNextClassCategory())
735 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
736 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
737 return MD;
738 return 0;
739}
740
741ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
742 for (ObjCCategoryDecl *Category = getCategoryList();
743 Category; Category = Category->getNextClassCategory())
744 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
745 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
746 return MD;
747 return 0;
748}
749
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000750/// ClassImplementsProtocol - Checks that 'lProto' protocol
751/// has been implemented in IDecl class, its super class or categories (if
752/// lookupCategory is true).
753bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
754 bool lookupCategory,
755 bool RHSIsQualifiedID) {
756 ObjCInterfaceDecl *IDecl = this;
757 // 1st, look up the class.
758 const ObjCList<ObjCProtocolDecl> &Protocols =
759 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000761 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
762 E = Protocols.end(); PI != E; ++PI) {
763 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
764 return true;
765 // This is dubious and is added to be compatible with gcc. In gcc, it is
766 // also allowed assigning a protocol-qualified 'id' type to a LHS object
767 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
768 // object. This IMO, should be a bug.
769 // FIXME: Treat this as an extension, and flag this as an error when GCC
770 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000771 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000772 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
773 return true;
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000776 // 2nd, look up the category.
777 if (lookupCategory)
778 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
779 CDecl = CDecl->getNextClassCategory()) {
780 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
781 E = CDecl->protocol_end(); PI != E; ++PI)
782 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
783 return true;
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000786 // 3rd, look up the super class(s)
787 if (IDecl->getSuperClass())
788 return
789 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
790 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000792 return false;
793}
794
Chris Lattnerab351632009-02-20 20:59:54 +0000795//===----------------------------------------------------------------------===//
796// ObjCIvarDecl
797//===----------------------------------------------------------------------===//
798
Daniel Dunbara0654922010-04-02 20:10:03 +0000799ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000800 SourceLocation StartLoc,
801 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000802 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000803 AccessControl ac, Expr *BW,
804 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000805 if (DC) {
806 // Ivar's can only appear in interfaces, implementations (via synthesized
807 // properties), and class extensions (via direct declaration, or synthesized
808 // properties).
809 //
810 // FIXME: This should really be asserting this:
811 // (isa<ObjCCategoryDecl>(DC) &&
812 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
813 // but unfortunately we sometimes place ivars into non-class extension
814 // categories on error. This breaks an AST invariant, and should not be
815 // fixed.
816 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
817 isa<ObjCCategoryDecl>(DC)) &&
818 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000819 // Once a new ivar is created in any of class/class-extension/implementation
820 // decl contexts, the previously built IvarList must be rebuilt.
821 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
822 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000823 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000824 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000825 if (BW)
826 IM->setHasSynthBitfield(true);
Chad Rosier30601782011-08-17 23:08:45 +0000827 } else {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000828 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
829 ID = CD->getClassInterface();
830 if (BW)
831 CD->setHasSynthBitfield(true);
832 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000833 }
834 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000835 }
836
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000837 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
838 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000839}
840
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000841const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
842 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000843
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000844 switch (DC->getKind()) {
845 default:
846 case ObjCCategoryImpl:
847 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000848 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000849
850 // Ivars can only appear in class extension categories.
851 case ObjCCategory: {
852 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
853 assert(CD->IsClassExtension() && "invalid container for ivar!");
854 return CD->getClassInterface();
855 }
856
857 case ObjCImplementation:
858 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
859
860 case ObjCInterface:
861 return cast<ObjCInterfaceDecl>(DC);
862 }
863}
Chris Lattnerab351632009-02-20 20:59:54 +0000864
865//===----------------------------------------------------------------------===//
866// ObjCAtDefsFieldDecl
867//===----------------------------------------------------------------------===//
868
869ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000870*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
871 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000872 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000873 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000874}
875
Chris Lattnerab351632009-02-20 20:59:54 +0000876//===----------------------------------------------------------------------===//
877// ObjCProtocolDecl
878//===----------------------------------------------------------------------===//
879
880ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000881 IdentifierInfo *Id,
882 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000883 SourceLocation atStartLoc,
884 bool isForwardDecl) {
885 return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, isForwardDecl);
Chris Lattnerab351632009-02-20 20:59:54 +0000886}
887
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000888ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
889 ObjCProtocolDecl *PDecl = this;
890
891 if (Name == getIdentifier())
892 return PDecl;
893
894 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
895 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
896 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000898 return NULL;
899}
900
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000901// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000902// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000903ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
904 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000905 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000907 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000908 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Chris Lattnerab351632009-02-20 20:59:54 +0000910 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000911 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000912 return MethodDecl;
913 return NULL;
914}
915
916//===----------------------------------------------------------------------===//
917// ObjCClassDecl
918//===----------------------------------------------------------------------===//
919
Mike Stump1eb44332009-09-09 15:08:12 +0000920ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000921 ObjCInterfaceDecl *const Elt,
922 const SourceLocation Loc,
Chris Lattner38af2de2009-02-20 21:35:13 +0000923 ASTContext &C)
924 : Decl(ObjCClass, DC, L) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000925 setClass(C, Elt, Loc);
Ted Kremenek321c22f2009-11-18 00:28:11 +0000926}
Chris Lattner38af2de2009-02-20 21:35:13 +0000927
Chris Lattnerab351632009-02-20 20:59:54 +0000928ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
929 SourceLocation L,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000930 ObjCInterfaceDecl *const Elt,
931 const SourceLocation Loc) {
932 return new (C) ObjCClassDecl(DC, L, Elt, Loc, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000933}
934
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000935void ObjCClassDecl::setClass(ASTContext &C, ObjCInterfaceDecl*const Cls,
936 const SourceLocation Loc) {
937
938 ForwardDecl = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef),
939 llvm::alignOf<ObjCClassRef>());
940 new (ForwardDecl) ObjCClassRef(Cls, Loc);
941}
942
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000943SourceRange ObjCClassDecl::getSourceRange() const {
944 // FIXME: We should include the semicolon
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000945 return SourceRange(getLocation(), ForwardDecl->getLocation());
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000946}
947
Chris Lattnerab351632009-02-20 20:59:54 +0000948//===----------------------------------------------------------------------===//
949// ObjCForwardProtocolDecl
950//===----------------------------------------------------------------------===//
951
Chris Lattner38af2de2009-02-20 21:35:13 +0000952ObjCForwardProtocolDecl::
953ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
954 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000955 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +0000956: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +0000957 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +0000958}
959
960
Chris Lattnerab351632009-02-20 20:59:54 +0000961ObjCForwardProtocolDecl *
962ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000963 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000964 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000965 unsigned NumElts,
966 const SourceLocation *Locs) {
967 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000968}
969
Chris Lattnerab351632009-02-20 20:59:54 +0000970//===----------------------------------------------------------------------===//
971// ObjCCategoryDecl
972//===----------------------------------------------------------------------===//
973
974ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +0000975 SourceLocation AtLoc,
976 SourceLocation ClassNameLoc,
977 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000978 IdentifierInfo *Id,
979 ObjCInterfaceDecl *IDecl) {
980 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
981 CategoryNameLoc, Id,
982 IDecl);
983 if (IDecl) {
984 // Link this category into its class's category list.
985 CatDecl->NextClassCategory = IDecl->getCategoryList();
986 IDecl->setCategoryList(CatDecl);
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +0000987 if (ASTMutationListener *L = C.getASTMutationListener())
988 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000989 }
990
991 return CatDecl;
992}
993
994ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) {
995 return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
996 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +0000997}
998
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000999ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1000 return getASTContext().getObjCImplementation(
1001 const_cast<ObjCCategoryDecl*>(this));
1002}
1003
1004void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1005 getASTContext().setObjCImplementation(this, ImplD);
1006}
1007
1008
Chris Lattnerab351632009-02-20 20:59:54 +00001009//===----------------------------------------------------------------------===//
1010// ObjCCategoryImplDecl
1011//===----------------------------------------------------------------------===//
1012
1013ObjCCategoryImplDecl *
1014ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001015 IdentifierInfo *Id,
1016 ObjCInterfaceDecl *ClassInterface,
1017 SourceLocation nameLoc,
1018 SourceLocation atStartLoc) {
1019 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1020 nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001021}
1022
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001023ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001024 // The class interface might be NULL if we are working with invalid code.
1025 if (const ObjCInterfaceDecl *ID = getClassInterface())
1026 return ID->FindCategoryDeclaration(getIdentifier());
1027 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001028}
1029
Chris Lattnerab351632009-02-20 20:59:54 +00001030
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001031void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001032 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001033 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001035}
1036
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001037void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1038 ASTContext &Ctx = getASTContext();
1039
1040 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001041 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001042 if (IFace)
1043 Ctx.setObjCImplementation(IFace, ImplD);
1044
Duncan Sands98f2cca2009-07-21 07:56:29 +00001045 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001046 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1047 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1048 Ctx.setObjCImplementation(CD, ImplD);
1049 }
1050
1051 ClassInterface = IFace;
1052}
1053
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001054/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001055/// properties implemented in this category @implementation block and returns
1056/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001057///
Chris Lattner3aa18612009-02-28 18:42:10 +00001058ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001059FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1060 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001061 ObjCPropertyImplDecl *PID = *i;
1062 if (PID->getPropertyIvarDecl() &&
1063 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1064 return PID;
1065 }
1066 return 0;
1067}
1068
1069/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1070/// added to the list of those properties @synthesized/@dynamic in this
1071/// category @implementation block.
1072///
Chris Lattner3aa18612009-02-28 18:42:10 +00001073ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001074FindPropertyImplDecl(IdentifierInfo *Id) const {
1075 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001076 ObjCPropertyImplDecl *PID = *i;
1077 if (PID->getPropertyDecl()->getIdentifier() == Id)
1078 return PID;
1079 }
1080 return 0;
1081}
1082
Chris Lattner5f9e2722011-07-23 10:55:15 +00001083raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001084 const ObjCCategoryImplDecl *CID) {
1085 OS << CID->getName();
1086 return OS;
1087}
1088
Chris Lattnerab351632009-02-20 20:59:54 +00001089//===----------------------------------------------------------------------===//
1090// ObjCImplementationDecl
1091//===----------------------------------------------------------------------===//
1092
1093ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001094ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001095 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001096 ObjCInterfaceDecl *SuperDecl,
1097 SourceLocation nameLoc,
1098 SourceLocation atStartLoc) {
1099 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1100 nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001101}
1102
John McCallda6d9762011-07-22 04:15:06 +00001103void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1104 CXXCtorInitializer ** initializers,
1105 unsigned numInitializers) {
1106 if (numInitializers > 0) {
1107 NumIvarInitializers = numInitializers;
1108 CXXCtorInitializer **ivarInitializers =
1109 new (C) CXXCtorInitializer*[NumIvarInitializers];
1110 memcpy(ivarInitializers, initializers,
1111 numInitializers * sizeof(CXXCtorInitializer*));
1112 IvarInitializers = ivarInitializers;
1113 }
1114}
1115
Chris Lattner5f9e2722011-07-23 10:55:15 +00001116raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001117 const ObjCImplementationDecl *ID) {
1118 OS << ID->getName();
1119 return OS;
1120}
1121
Chris Lattnerab351632009-02-20 20:59:54 +00001122//===----------------------------------------------------------------------===//
1123// ObjCCompatibleAliasDecl
1124//===----------------------------------------------------------------------===//
1125
1126ObjCCompatibleAliasDecl *
1127ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1128 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001129 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001130 ObjCInterfaceDecl* AliasedClass) {
1131 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1132}
1133
1134//===----------------------------------------------------------------------===//
1135// ObjCPropertyDecl
1136//===----------------------------------------------------------------------===//
1137
1138ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1139 SourceLocation L,
1140 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001141 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +00001142 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001143 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001144 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001145}
1146
Chris Lattnerab351632009-02-20 20:59:54 +00001147//===----------------------------------------------------------------------===//
1148// ObjCPropertyImplDecl
1149//===----------------------------------------------------------------------===//
1150
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001151ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001152 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001153 SourceLocation atLoc,
1154 SourceLocation L,
1155 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001156 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001157 ObjCIvarDecl *ivar,
1158 SourceLocation ivarLoc) {
1159 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1160 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001161}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001162
Douglas Gregora4ffd852010-11-17 01:03:52 +00001163SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1164 SourceLocation EndLoc = getLocation();
1165 if (IvarLoc.isValid())
1166 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001167
Douglas Gregora4ffd852010-11-17 01:03:52 +00001168 return SourceRange(AtLoc, EndLoc);
1169}