blob: 48fbbe0c2b41d456b5e1413e9c9bf6835979565f [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) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000240 ObjCInterfaceDecl* ClassDecl = this;
241 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000242 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000243 clsDeclared = ClassDecl;
244 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000245 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000246 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
247 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000248 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
249 clsDeclared = ClassDecl;
250 return I;
251 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000252 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000253
Chris Lattner1e03a562008-03-16 00:19:01 +0000254 ClassDecl = ClassDecl->getSuperClass();
255 }
256 return NULL;
257}
258
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000259/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
260/// class whose name is passed as argument. If it is not one of the super classes
261/// the it returns NULL.
262ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
263 const IdentifierInfo*ICName) {
264 ObjCInterfaceDecl* ClassDecl = this;
265 while (ClassDecl != NULL) {
266 if (ClassDecl->getIdentifier() == ICName)
267 return ClassDecl;
268 ClassDecl = ClassDecl->getSuperClass();
269 }
270 return NULL;
271}
272
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000273/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000274/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000275ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
276 bool isInstance) const {
277 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000278 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000280 if (ExternallyCompleted)
281 LoadExternalDefinition();
282
Chris Lattner1e03a562008-03-16 00:19:01 +0000283 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000284 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000285 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattner1e03a562008-03-16 00:19:01 +0000287 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000288 const ObjCList<ObjCProtocolDecl> &Protocols =
289 ClassDecl->getReferencedProtocols();
290 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
291 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000292 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000293 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner1e03a562008-03-16 00:19:01 +0000295 // Didn't find one yet - now look through categories.
296 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
297 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000298 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000299 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Steve Naroffb79c01e2008-12-08 20:57:28 +0000301 // Didn't find one yet - look through protocols.
302 const ObjCList<ObjCProtocolDecl> &Protocols =
303 CatDecl->getReferencedProtocols();
304 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
305 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000306 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000307 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000308 CatDecl = CatDecl->getNextClassCategory();
309 }
310 ClassDecl = ClassDecl->getSuperClass();
311 }
312 return NULL;
313}
314
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000315ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
316 const Selector &Sel,
317 bool Instance) {
Steve Naroffd789d3d2009-10-01 23:46:04 +0000318 ObjCMethodDecl *Method = 0;
319 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000320 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
321 : ImpDecl->getClassMethod(Sel);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000322
323 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000324 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000325 return Method;
326}
Chris Lattnerab351632009-02-20 20:59:54 +0000327
328//===----------------------------------------------------------------------===//
329// ObjCMethodDecl
330//===----------------------------------------------------------------------===//
331
332ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000333 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000334 SourceLocation endLoc,
335 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000336 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000337 DeclContext *contextDecl,
338 bool isInstance,
339 bool isVariadic,
340 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000341 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000342 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000343 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000344 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000345 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000346 SelInfo, T, ResultTInfo, contextDecl,
347 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000348 isVariadic, isSynthesized, isImplicitlyDeclared,
349 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000350 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000351 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000352}
353
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000354void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
355 ArrayRef<ParmVarDecl*> Params,
356 ArrayRef<SourceLocation> SelLocs) {
357 ParamsAndSelLocs = 0;
358 NumParams = Params.size();
359 if (Params.empty() && SelLocs.empty())
360 return;
361
362 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
363 sizeof(SourceLocation) * SelLocs.size();
364 ParamsAndSelLocs = C.Allocate(Size);
365 std::copy(Params.begin(), Params.end(), getParams());
366 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
367}
368
369void ObjCMethodDecl::getSelectorLocs(
370 SmallVectorImpl<SourceLocation> &SelLocs) const {
371 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
372 SelLocs.push_back(getSelectorLoc(i));
373}
374
375void ObjCMethodDecl::setMethodParams(ASTContext &C,
376 ArrayRef<ParmVarDecl*> Params,
377 ArrayRef<SourceLocation> SelLocs) {
378 assert((!SelLocs.empty() || isImplicit()) &&
379 "No selector locs for non-implicit method");
380 if (isImplicit())
381 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
382
383 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
384 if (SelLocsKind != SelLoc_NonStandard)
385 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
386
387 setParamsAndSelLocs(C, Params, SelLocs);
388}
389
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000390/// \brief A definition will return its interface declaration.
391/// An interface declaration will return its definition.
392/// Otherwise it will return itself.
393ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
394 ASTContext &Ctx = getASTContext();
395 ObjCMethodDecl *Redecl = 0;
396 Decl *CtxD = cast<Decl>(getDeclContext());
397
398 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
399 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
400 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
401
402 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
403 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
404 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
405
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000406 } else if (ObjCImplementationDecl *ImplD =
407 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000408 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
409 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000410
411 } else if (ObjCCategoryImplDecl *CImplD =
412 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000413 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000414 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000415 }
416
417 return Redecl ? Redecl : this;
418}
419
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000420ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
421 Decl *CtxD = cast<Decl>(getDeclContext());
422
423 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
424 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
425 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
426 isInstanceMethod()))
427 return MD;
428
429 } else if (ObjCCategoryImplDecl *CImplD =
430 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000431 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000432 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
433 isInstanceMethod()))
434 return MD;
435 }
436
437 return this;
438}
439
John McCall85f3d762011-03-02 01:50:55 +0000440ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
441 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000442 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000443 return family;
444
John McCalld5313b02011-03-02 11:33:24 +0000445 // Check for an explicit attribute.
446 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
447 // The unfortunate necessity of mapping between enums here is due
448 // to the attributes framework.
449 switch (attr->getFamily()) {
450 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
451 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
452 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
453 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
454 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
455 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
456 }
457 Family = static_cast<unsigned>(family);
458 return family;
459 }
460
John McCall85f3d762011-03-02 01:50:55 +0000461 family = getSelector().getMethodFamily();
462 switch (family) {
463 case OMF_None: break;
464
465 // init only has a conventional meaning for an instance method, and
466 // it has to return an object.
467 case OMF_init:
468 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
469 family = OMF_None;
470 break;
471
472 // alloc/copy/new have a conventional meaning for both class and
473 // instance methods, but they require an object return.
474 case OMF_alloc:
475 case OMF_copy:
476 case OMF_mutableCopy:
477 case OMF_new:
478 if (!getResultType()->isObjCObjectPointerType())
479 family = OMF_None;
480 break;
481
482 // These selectors have a conventional meaning only for instance methods.
483 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000484 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000485 case OMF_retain:
486 case OMF_release:
487 case OMF_autorelease:
488 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000489 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000490 if (!isInstanceMethod())
491 family = OMF_None;
492 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000493
494 case OMF_performSelector:
495 if (!isInstanceMethod() ||
496 !getResultType()->isObjCIdType())
497 family = OMF_None;
498 else {
499 unsigned noParams = param_size();
500 if (noParams < 1 || noParams > 3)
501 family = OMF_None;
502 else {
503 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
504 QualType ArgT = (*it);
505 if (!ArgT->isObjCSelType()) {
506 family = OMF_None;
507 break;
508 }
509 while (--noParams) {
510 it++;
511 ArgT = (*it);
512 if (!ArgT->isObjCIdType()) {
513 family = OMF_None;
514 break;
515 }
516 }
517 }
518 }
519 break;
520
John McCall85f3d762011-03-02 01:50:55 +0000521 }
522
523 // Cache the result.
524 Family = static_cast<unsigned>(family);
525 return family;
526}
527
Mike Stump1eb44332009-09-09 15:08:12 +0000528void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000529 const ObjCInterfaceDecl *OID) {
530 QualType selfTy;
531 if (isInstanceMethod()) {
532 // There may be no interface context due to error in declaration
533 // of the interface (which has been reported). Recover gracefully.
534 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000535 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000536 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000537 } else {
538 selfTy = Context.getObjCIdType();
539 }
540 } else // we have a factory method.
541 selfTy = Context.getObjCClassType();
542
John McCall7acddac2011-06-17 06:42:21 +0000543 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000544 bool selfIsConsumed = false;
545 if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) {
546 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
547
John McCall7acddac2011-06-17 06:42:21 +0000548 // 'self' is always __strong. It's actually pseudo-strong except
549 // in init methods, though.
John McCallf85e1932011-06-15 23:02:42 +0000550 Qualifiers qs;
551 qs.setObjCLifetime(Qualifiers::OCL_Strong);
552 selfTy = Context.getQualifiedType(selfTy, qs);
553
554 // In addition, 'self' is const unless this is an init method.
John McCall7acddac2011-06-17 06:42:21 +0000555 if (getMethodFamily() != OMF_init) {
John McCallf85e1932011-06-15 23:02:42 +0000556 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000557 selfIsPseudoStrong = true;
558 }
John McCallf85e1932011-06-15 23:02:42 +0000559 }
560
561 ImplicitParamDecl *self
562 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
563 &Context.Idents.get("self"), selfTy);
564 setSelfDecl(self);
565
566 if (selfIsConsumed)
567 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000568
John McCall7acddac2011-06-17 06:42:21 +0000569 if (selfIsPseudoStrong)
570 self->setARCPseudoStrong(true);
571
Mike Stump1eb44332009-09-09 15:08:12 +0000572 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
573 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000574 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000575}
576
Chris Lattnerab351632009-02-20 20:59:54 +0000577ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
578 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
579 return ID;
580 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
581 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000582 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000583 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000584
585 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000586 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000587}
588
Chris Lattnerab351632009-02-20 20:59:54 +0000589//===----------------------------------------------------------------------===//
590// ObjCInterfaceDecl
591//===----------------------------------------------------------------------===//
592
593ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
594 DeclContext *DC,
595 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000596 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000597 SourceLocation ClassLoc,
598 bool ForwardDecl, bool isInternal){
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000599 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
600 isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000601}
602
603ObjCInterfaceDecl::
604ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000605 SourceLocation CLoc, bool FD, bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000606 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Chris Lattnerab351632009-02-20 20:59:54 +0000607 TypeForDecl(0), SuperClass(0),
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000608 CategoryList(0), IvarList(0),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000609 ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false) {
Chris Lattnerab351632009-02-20 20:59:54 +0000610}
611
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000612void ObjCInterfaceDecl::LoadExternalDefinition() const {
613 assert(ExternallyCompleted && "Class is not externally completed");
614 ExternallyCompleted = false;
615 getASTContext().getExternalSource()->CompleteType(
616 const_cast<ObjCInterfaceDecl *>(this));
617}
618
619void ObjCInterfaceDecl::setExternallyCompleted() {
620 assert(getASTContext().getExternalSource() &&
621 "Class can't be externally completed without an external source");
622 assert(!ForwardDecl &&
623 "Forward declarations can't be externally completed");
624 ExternallyCompleted = true;
625}
626
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000627ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000628 if (ExternallyCompleted)
629 LoadExternalDefinition();
630
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000631 return getASTContext().getObjCImplementation(
632 const_cast<ObjCInterfaceDecl*>(this));
633}
634
635void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
636 getASTContext().setObjCImplementation(this, ImplD);
637}
638
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000639/// all_declared_ivar_begin - return first ivar declared in this class,
640/// its extensions and its implementation. Lazily build the list on first
641/// access.
642ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
643 if (IvarList)
644 return IvarList;
645
646 ObjCIvarDecl *curIvar = 0;
647 if (!ivar_empty()) {
648 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
649 IvarList = (*I); ++I;
650 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
651 curIvar->setNextIvar(*I);
652 }
653
654 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
655 CDecl = CDecl->getNextClassExtension()) {
656 if (!CDecl->ivar_empty()) {
657 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
658 E = CDecl->ivar_end();
659 if (!IvarList) {
660 IvarList = (*I); ++I;
661 curIvar = IvarList;
662 }
663 for ( ;I != E; curIvar = *I, ++I)
664 curIvar->setNextIvar(*I);
665 }
666 }
667
668 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
669 if (!ImplDecl->ivar_empty()) {
670 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
671 E = ImplDecl->ivar_end();
672 if (!IvarList) {
673 IvarList = (*I); ++I;
674 curIvar = IvarList;
675 }
676 for ( ;I != E; curIvar = *I, ++I)
677 curIvar->setNextIvar(*I);
678 }
679 }
680 return IvarList;
681}
Chris Lattnerab351632009-02-20 20:59:54 +0000682
683/// FindCategoryDeclaration - Finds category declaration in the list of
684/// categories for this class and returns it. Name of the category is passed
685/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000686///
Chris Lattnerab351632009-02-20 20:59:54 +0000687ObjCCategoryDecl *
688ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000689 if (ExternallyCompleted)
690 LoadExternalDefinition();
691
Chris Lattnerab351632009-02-20 20:59:54 +0000692 for (ObjCCategoryDecl *Category = getCategoryList();
693 Category; Category = Category->getNextClassCategory())
694 if (Category->getIdentifier() == CategoryId)
695 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000696 return 0;
697}
698
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000699ObjCMethodDecl *
700ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
701 for (ObjCCategoryDecl *Category = getCategoryList();
702 Category; Category = Category->getNextClassCategory())
703 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
704 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
705 return MD;
706 return 0;
707}
708
709ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
710 for (ObjCCategoryDecl *Category = getCategoryList();
711 Category; Category = Category->getNextClassCategory())
712 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
713 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
714 return MD;
715 return 0;
716}
717
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000718/// ClassImplementsProtocol - Checks that 'lProto' protocol
719/// has been implemented in IDecl class, its super class or categories (if
720/// lookupCategory is true).
721bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
722 bool lookupCategory,
723 bool RHSIsQualifiedID) {
724 ObjCInterfaceDecl *IDecl = this;
725 // 1st, look up the class.
726 const ObjCList<ObjCProtocolDecl> &Protocols =
727 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000729 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
730 E = Protocols.end(); PI != E; ++PI) {
731 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
732 return true;
733 // This is dubious and is added to be compatible with gcc. In gcc, it is
734 // also allowed assigning a protocol-qualified 'id' type to a LHS object
735 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
736 // object. This IMO, should be a bug.
737 // FIXME: Treat this as an extension, and flag this as an error when GCC
738 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000739 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000740 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
741 return true;
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000744 // 2nd, look up the category.
745 if (lookupCategory)
746 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
747 CDecl = CDecl->getNextClassCategory()) {
748 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
749 E = CDecl->protocol_end(); PI != E; ++PI)
750 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
751 return true;
752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000754 // 3rd, look up the super class(s)
755 if (IDecl->getSuperClass())
756 return
757 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
758 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000760 return false;
761}
762
Chris Lattnerab351632009-02-20 20:59:54 +0000763//===----------------------------------------------------------------------===//
764// ObjCIvarDecl
765//===----------------------------------------------------------------------===//
766
Daniel Dunbara0654922010-04-02 20:10:03 +0000767ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000768 SourceLocation StartLoc,
769 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000770 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000771 AccessControl ac, Expr *BW,
772 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000773 if (DC) {
774 // Ivar's can only appear in interfaces, implementations (via synthesized
775 // properties), and class extensions (via direct declaration, or synthesized
776 // properties).
777 //
778 // FIXME: This should really be asserting this:
779 // (isa<ObjCCategoryDecl>(DC) &&
780 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
781 // but unfortunately we sometimes place ivars into non-class extension
782 // categories on error. This breaks an AST invariant, and should not be
783 // fixed.
784 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
785 isa<ObjCCategoryDecl>(DC)) &&
786 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000787 // Once a new ivar is created in any of class/class-extension/implementation
788 // decl contexts, the previously built IvarList must be rebuilt.
789 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
790 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000791 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000792 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000793 if (BW)
794 IM->setHasSynthBitfield(true);
Chad Rosier30601782011-08-17 23:08:45 +0000795 } else {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000796 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
797 ID = CD->getClassInterface();
798 if (BW)
799 CD->setHasSynthBitfield(true);
800 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000801 }
802 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000803 }
804
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000805 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
806 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000807}
808
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000809const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
810 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000811
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000812 switch (DC->getKind()) {
813 default:
814 case ObjCCategoryImpl:
815 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000816 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000817
818 // Ivars can only appear in class extension categories.
819 case ObjCCategory: {
820 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
821 assert(CD->IsClassExtension() && "invalid container for ivar!");
822 return CD->getClassInterface();
823 }
824
825 case ObjCImplementation:
826 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
827
828 case ObjCInterface:
829 return cast<ObjCInterfaceDecl>(DC);
830 }
831}
Chris Lattnerab351632009-02-20 20:59:54 +0000832
833//===----------------------------------------------------------------------===//
834// ObjCAtDefsFieldDecl
835//===----------------------------------------------------------------------===//
836
837ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000838*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
839 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000840 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000841 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000842}
843
Chris Lattnerab351632009-02-20 20:59:54 +0000844//===----------------------------------------------------------------------===//
845// ObjCProtocolDecl
846//===----------------------------------------------------------------------===//
847
848ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000849 IdentifierInfo *Id,
850 SourceLocation nameLoc,
851 SourceLocation atStartLoc) {
852 return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +0000853}
854
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000855ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
856 ObjCProtocolDecl *PDecl = this;
857
858 if (Name == getIdentifier())
859 return PDecl;
860
861 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
862 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
863 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000865 return NULL;
866}
867
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000868// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000869// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000870ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
871 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000872 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000874 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000875 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Chris Lattnerab351632009-02-20 20:59:54 +0000877 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000878 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000879 return MethodDecl;
880 return NULL;
881}
882
883//===----------------------------------------------------------------------===//
884// ObjCClassDecl
885//===----------------------------------------------------------------------===//
886
Mike Stump1eb44332009-09-09 15:08:12 +0000887ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000888 ObjCInterfaceDecl *const Elt,
889 const SourceLocation Loc,
Chris Lattner38af2de2009-02-20 21:35:13 +0000890 ASTContext &C)
891 : Decl(ObjCClass, DC, L) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000892 setClass(C, Elt, Loc);
Ted Kremenek321c22f2009-11-18 00:28:11 +0000893}
Chris Lattner38af2de2009-02-20 21:35:13 +0000894
Chris Lattnerab351632009-02-20 20:59:54 +0000895ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
896 SourceLocation L,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000897 ObjCInterfaceDecl *const Elt,
898 const SourceLocation Loc) {
899 return new (C) ObjCClassDecl(DC, L, Elt, Loc, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000900}
901
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000902void ObjCClassDecl::setClass(ASTContext &C, ObjCInterfaceDecl*const Cls,
903 const SourceLocation Loc) {
904
905 ForwardDecl = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef),
906 llvm::alignOf<ObjCClassRef>());
907 new (ForwardDecl) ObjCClassRef(Cls, Loc);
908}
909
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000910SourceRange ObjCClassDecl::getSourceRange() const {
911 // FIXME: We should include the semicolon
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000912 return SourceRange(getLocation(), ForwardDecl->getLocation());
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000913}
914
Chris Lattnerab351632009-02-20 20:59:54 +0000915//===----------------------------------------------------------------------===//
916// ObjCForwardProtocolDecl
917//===----------------------------------------------------------------------===//
918
Chris Lattner38af2de2009-02-20 21:35:13 +0000919ObjCForwardProtocolDecl::
920ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
921 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000922 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +0000923: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +0000924 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +0000925}
926
927
Chris Lattnerab351632009-02-20 20:59:54 +0000928ObjCForwardProtocolDecl *
929ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000930 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000931 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000932 unsigned NumElts,
933 const SourceLocation *Locs) {
934 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000935}
936
Chris Lattnerab351632009-02-20 20:59:54 +0000937//===----------------------------------------------------------------------===//
938// ObjCCategoryDecl
939//===----------------------------------------------------------------------===//
940
941ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +0000942 SourceLocation AtLoc,
943 SourceLocation ClassNameLoc,
944 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000945 IdentifierInfo *Id,
946 ObjCInterfaceDecl *IDecl) {
947 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
948 CategoryNameLoc, Id,
949 IDecl);
950 if (IDecl) {
951 // Link this category into its class's category list.
952 CatDecl->NextClassCategory = IDecl->getCategoryList();
953 IDecl->setCategoryList(CatDecl);
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +0000954 if (ASTMutationListener *L = C.getASTMutationListener())
955 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000956 }
957
958 return CatDecl;
959}
960
961ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) {
962 return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
963 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +0000964}
965
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000966ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
967 return getASTContext().getObjCImplementation(
968 const_cast<ObjCCategoryDecl*>(this));
969}
970
971void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
972 getASTContext().setObjCImplementation(this, ImplD);
973}
974
975
Chris Lattnerab351632009-02-20 20:59:54 +0000976//===----------------------------------------------------------------------===//
977// ObjCCategoryImplDecl
978//===----------------------------------------------------------------------===//
979
980ObjCCategoryImplDecl *
981ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000982 IdentifierInfo *Id,
983 ObjCInterfaceDecl *ClassInterface,
984 SourceLocation nameLoc,
985 SourceLocation atStartLoc) {
986 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
987 nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +0000988}
989
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000990ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000991 // The class interface might be NULL if we are working with invalid code.
992 if (const ObjCInterfaceDecl *ID = getClassInterface())
993 return ID->FindCategoryDeclaration(getIdentifier());
994 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000995}
996
Chris Lattnerab351632009-02-20 20:59:54 +0000997
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000998void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000999 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001000 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001001 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001002}
1003
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001004void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1005 ASTContext &Ctx = getASTContext();
1006
1007 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001008 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001009 if (IFace)
1010 Ctx.setObjCImplementation(IFace, ImplD);
1011
Duncan Sands98f2cca2009-07-21 07:56:29 +00001012 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001013 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1014 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1015 Ctx.setObjCImplementation(CD, ImplD);
1016 }
1017
1018 ClassInterface = IFace;
1019}
1020
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001021/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001022/// properties implemented in this category @implementation block and returns
1023/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001024///
Chris Lattner3aa18612009-02-28 18:42:10 +00001025ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001026FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1027 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001028 ObjCPropertyImplDecl *PID = *i;
1029 if (PID->getPropertyIvarDecl() &&
1030 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1031 return PID;
1032 }
1033 return 0;
1034}
1035
1036/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1037/// added to the list of those properties @synthesized/@dynamic in this
1038/// category @implementation block.
1039///
Chris Lattner3aa18612009-02-28 18:42:10 +00001040ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041FindPropertyImplDecl(IdentifierInfo *Id) const {
1042 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001043 ObjCPropertyImplDecl *PID = *i;
1044 if (PID->getPropertyDecl()->getIdentifier() == Id)
1045 return PID;
1046 }
1047 return 0;
1048}
1049
Chris Lattner5f9e2722011-07-23 10:55:15 +00001050raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001051 const ObjCCategoryImplDecl *CID) {
1052 OS << CID->getName();
1053 return OS;
1054}
1055
Chris Lattnerab351632009-02-20 20:59:54 +00001056//===----------------------------------------------------------------------===//
1057// ObjCImplementationDecl
1058//===----------------------------------------------------------------------===//
1059
1060ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001061ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001062 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001063 ObjCInterfaceDecl *SuperDecl,
1064 SourceLocation nameLoc,
1065 SourceLocation atStartLoc) {
1066 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1067 nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001068}
1069
John McCallda6d9762011-07-22 04:15:06 +00001070void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1071 CXXCtorInitializer ** initializers,
1072 unsigned numInitializers) {
1073 if (numInitializers > 0) {
1074 NumIvarInitializers = numInitializers;
1075 CXXCtorInitializer **ivarInitializers =
1076 new (C) CXXCtorInitializer*[NumIvarInitializers];
1077 memcpy(ivarInitializers, initializers,
1078 numInitializers * sizeof(CXXCtorInitializer*));
1079 IvarInitializers = ivarInitializers;
1080 }
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 ObjCImplementationDecl *ID) {
1085 OS << ID->getName();
1086 return OS;
1087}
1088
Chris Lattnerab351632009-02-20 20:59:54 +00001089//===----------------------------------------------------------------------===//
1090// ObjCCompatibleAliasDecl
1091//===----------------------------------------------------------------------===//
1092
1093ObjCCompatibleAliasDecl *
1094ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1095 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001096 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001097 ObjCInterfaceDecl* AliasedClass) {
1098 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1099}
1100
1101//===----------------------------------------------------------------------===//
1102// ObjCPropertyDecl
1103//===----------------------------------------------------------------------===//
1104
1105ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1106 SourceLocation L,
1107 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001108 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +00001109 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001110 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001111 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001112}
1113
Chris Lattnerab351632009-02-20 20:59:54 +00001114//===----------------------------------------------------------------------===//
1115// ObjCPropertyImplDecl
1116//===----------------------------------------------------------------------===//
1117
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001118ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001119 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001120 SourceLocation atLoc,
1121 SourceLocation L,
1122 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001123 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001124 ObjCIvarDecl *ivar,
1125 SourceLocation ivarLoc) {
1126 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1127 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001128}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001129
Douglas Gregora4ffd852010-11-17 01:03:52 +00001130SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1131 SourceLocation EndLoc = getLocation();
1132 if (IvarLoc.isValid())
1133 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001134
Douglas Gregora4ffd852010-11-17 01:03:52 +00001135 return SourceRange(AtLoc, EndLoc);
1136}