blob: 0e1e833d2fd9ff61661cb4be2229335c08a97cb2 [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,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +0000335 ArrayRef<SourceLocation> SelLocs,
Chris Lattnerab351632009-02-20 20:59:54 +0000336 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000337 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000338 DeclContext *contextDecl,
339 bool isInstance,
340 bool isVariadic,
341 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000342 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000343 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000344 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000345 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000346 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000347 SelInfo, T, ResultTInfo, contextDecl,
348 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000349 isVariadic, isSynthesized, isImplicitlyDeclared,
350 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000351 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000352 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000353}
354
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000355/// \brief A definition will return its interface declaration.
356/// An interface declaration will return its definition.
357/// Otherwise it will return itself.
358ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
359 ASTContext &Ctx = getASTContext();
360 ObjCMethodDecl *Redecl = 0;
361 Decl *CtxD = cast<Decl>(getDeclContext());
362
363 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
364 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
365 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
366
367 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
368 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
369 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
370
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000371 } else if (ObjCImplementationDecl *ImplD =
372 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000373 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
374 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000375
376 } else if (ObjCCategoryImplDecl *CImplD =
377 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000378 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000379 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000380 }
381
382 return Redecl ? Redecl : this;
383}
384
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000385ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
386 Decl *CtxD = cast<Decl>(getDeclContext());
387
388 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
389 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
390 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
391 isInstanceMethod()))
392 return MD;
393
394 } else if (ObjCCategoryImplDecl *CImplD =
395 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000396 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000397 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
398 isInstanceMethod()))
399 return MD;
400 }
401
402 return this;
403}
404
John McCall85f3d762011-03-02 01:50:55 +0000405ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
406 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000407 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000408 return family;
409
John McCalld5313b02011-03-02 11:33:24 +0000410 // Check for an explicit attribute.
411 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
412 // The unfortunate necessity of mapping between enums here is due
413 // to the attributes framework.
414 switch (attr->getFamily()) {
415 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
416 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
417 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
418 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
419 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
420 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
421 }
422 Family = static_cast<unsigned>(family);
423 return family;
424 }
425
John McCall85f3d762011-03-02 01:50:55 +0000426 family = getSelector().getMethodFamily();
427 switch (family) {
428 case OMF_None: break;
429
430 // init only has a conventional meaning for an instance method, and
431 // it has to return an object.
432 case OMF_init:
433 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
434 family = OMF_None;
435 break;
436
437 // alloc/copy/new have a conventional meaning for both class and
438 // instance methods, but they require an object return.
439 case OMF_alloc:
440 case OMF_copy:
441 case OMF_mutableCopy:
442 case OMF_new:
443 if (!getResultType()->isObjCObjectPointerType())
444 family = OMF_None;
445 break;
446
447 // These selectors have a conventional meaning only for instance methods.
448 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000449 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000450 case OMF_retain:
451 case OMF_release:
452 case OMF_autorelease:
453 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000454 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000455 if (!isInstanceMethod())
456 family = OMF_None;
457 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000458
459 case OMF_performSelector:
460 if (!isInstanceMethod() ||
461 !getResultType()->isObjCIdType())
462 family = OMF_None;
463 else {
464 unsigned noParams = param_size();
465 if (noParams < 1 || noParams > 3)
466 family = OMF_None;
467 else {
468 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
469 QualType ArgT = (*it);
470 if (!ArgT->isObjCSelType()) {
471 family = OMF_None;
472 break;
473 }
474 while (--noParams) {
475 it++;
476 ArgT = (*it);
477 if (!ArgT->isObjCIdType()) {
478 family = OMF_None;
479 break;
480 }
481 }
482 }
483 }
484 break;
485
John McCall85f3d762011-03-02 01:50:55 +0000486 }
487
488 // Cache the result.
489 Family = static_cast<unsigned>(family);
490 return family;
491}
492
Mike Stump1eb44332009-09-09 15:08:12 +0000493void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000494 const ObjCInterfaceDecl *OID) {
495 QualType selfTy;
496 if (isInstanceMethod()) {
497 // There may be no interface context due to error in declaration
498 // of the interface (which has been reported). Recover gracefully.
499 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000500 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000501 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000502 } else {
503 selfTy = Context.getObjCIdType();
504 }
505 } else // we have a factory method.
506 selfTy = Context.getObjCClassType();
507
John McCall7acddac2011-06-17 06:42:21 +0000508 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000509 bool selfIsConsumed = false;
510 if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) {
511 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
512
John McCall7acddac2011-06-17 06:42:21 +0000513 // 'self' is always __strong. It's actually pseudo-strong except
514 // in init methods, though.
John McCallf85e1932011-06-15 23:02:42 +0000515 Qualifiers qs;
516 qs.setObjCLifetime(Qualifiers::OCL_Strong);
517 selfTy = Context.getQualifiedType(selfTy, qs);
518
519 // In addition, 'self' is const unless this is an init method.
John McCall7acddac2011-06-17 06:42:21 +0000520 if (getMethodFamily() != OMF_init) {
John McCallf85e1932011-06-15 23:02:42 +0000521 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000522 selfIsPseudoStrong = true;
523 }
John McCallf85e1932011-06-15 23:02:42 +0000524 }
525
526 ImplicitParamDecl *self
527 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
528 &Context.Idents.get("self"), selfTy);
529 setSelfDecl(self);
530
531 if (selfIsConsumed)
532 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000533
John McCall7acddac2011-06-17 06:42:21 +0000534 if (selfIsPseudoStrong)
535 self->setARCPseudoStrong(true);
536
Mike Stump1eb44332009-09-09 15:08:12 +0000537 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
538 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000539 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000540}
541
Chris Lattnerab351632009-02-20 20:59:54 +0000542ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
543 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
544 return ID;
545 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
546 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000547 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000548 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000549
550 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000551 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000552}
553
Chris Lattnerab351632009-02-20 20:59:54 +0000554//===----------------------------------------------------------------------===//
555// ObjCInterfaceDecl
556//===----------------------------------------------------------------------===//
557
558ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
559 DeclContext *DC,
560 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000561 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000562 SourceLocation ClassLoc,
563 bool ForwardDecl, bool isInternal){
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000564 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
565 isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000566}
567
568ObjCInterfaceDecl::
569ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000570 SourceLocation CLoc, bool FD, bool isInternal)
Chris Lattnerab351632009-02-20 20:59:54 +0000571 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
572 TypeForDecl(0), SuperClass(0),
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000573 CategoryList(0), IvarList(0),
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000574 ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false),
Chris Lattnerab351632009-02-20 20:59:54 +0000575 ClassLoc(CLoc) {
576}
577
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000578void ObjCInterfaceDecl::LoadExternalDefinition() const {
579 assert(ExternallyCompleted && "Class is not externally completed");
580 ExternallyCompleted = false;
581 getASTContext().getExternalSource()->CompleteType(
582 const_cast<ObjCInterfaceDecl *>(this));
583}
584
585void ObjCInterfaceDecl::setExternallyCompleted() {
586 assert(getASTContext().getExternalSource() &&
587 "Class can't be externally completed without an external source");
588 assert(!ForwardDecl &&
589 "Forward declarations can't be externally completed");
590 ExternallyCompleted = true;
591}
592
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000593ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000594 if (ExternallyCompleted)
595 LoadExternalDefinition();
596
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000597 return getASTContext().getObjCImplementation(
598 const_cast<ObjCInterfaceDecl*>(this));
599}
600
601void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
602 getASTContext().setObjCImplementation(this, ImplD);
603}
604
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000605/// all_declared_ivar_begin - return first ivar declared in this class,
606/// its extensions and its implementation. Lazily build the list on first
607/// access.
608ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
609 if (IvarList)
610 return IvarList;
611
612 ObjCIvarDecl *curIvar = 0;
613 if (!ivar_empty()) {
614 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
615 IvarList = (*I); ++I;
616 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
617 curIvar->setNextIvar(*I);
618 }
619
620 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
621 CDecl = CDecl->getNextClassExtension()) {
622 if (!CDecl->ivar_empty()) {
623 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
624 E = CDecl->ivar_end();
625 if (!IvarList) {
626 IvarList = (*I); ++I;
627 curIvar = IvarList;
628 }
629 for ( ;I != E; curIvar = *I, ++I)
630 curIvar->setNextIvar(*I);
631 }
632 }
633
634 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
635 if (!ImplDecl->ivar_empty()) {
636 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
637 E = ImplDecl->ivar_end();
638 if (!IvarList) {
639 IvarList = (*I); ++I;
640 curIvar = IvarList;
641 }
642 for ( ;I != E; curIvar = *I, ++I)
643 curIvar->setNextIvar(*I);
644 }
645 }
646 return IvarList;
647}
Chris Lattnerab351632009-02-20 20:59:54 +0000648
649/// FindCategoryDeclaration - Finds category declaration in the list of
650/// categories for this class and returns it. Name of the category is passed
651/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000652///
Chris Lattnerab351632009-02-20 20:59:54 +0000653ObjCCategoryDecl *
654ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000655 if (ExternallyCompleted)
656 LoadExternalDefinition();
657
Chris Lattnerab351632009-02-20 20:59:54 +0000658 for (ObjCCategoryDecl *Category = getCategoryList();
659 Category; Category = Category->getNextClassCategory())
660 if (Category->getIdentifier() == CategoryId)
661 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000662 return 0;
663}
664
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000665ObjCMethodDecl *
666ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
667 for (ObjCCategoryDecl *Category = getCategoryList();
668 Category; Category = Category->getNextClassCategory())
669 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
670 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
671 return MD;
672 return 0;
673}
674
675ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
676 for (ObjCCategoryDecl *Category = getCategoryList();
677 Category; Category = Category->getNextClassCategory())
678 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
679 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
680 return MD;
681 return 0;
682}
683
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000684/// ClassImplementsProtocol - Checks that 'lProto' protocol
685/// has been implemented in IDecl class, its super class or categories (if
686/// lookupCategory is true).
687bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
688 bool lookupCategory,
689 bool RHSIsQualifiedID) {
690 ObjCInterfaceDecl *IDecl = this;
691 // 1st, look up the class.
692 const ObjCList<ObjCProtocolDecl> &Protocols =
693 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000695 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
696 E = Protocols.end(); PI != E; ++PI) {
697 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
698 return true;
699 // This is dubious and is added to be compatible with gcc. In gcc, it is
700 // also allowed assigning a protocol-qualified 'id' type to a LHS object
701 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
702 // object. This IMO, should be a bug.
703 // FIXME: Treat this as an extension, and flag this as an error when GCC
704 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000705 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000706 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
707 return true;
708 }
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000710 // 2nd, look up the category.
711 if (lookupCategory)
712 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
713 CDecl = CDecl->getNextClassCategory()) {
714 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
715 E = CDecl->protocol_end(); PI != E; ++PI)
716 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
717 return true;
718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000720 // 3rd, look up the super class(s)
721 if (IDecl->getSuperClass())
722 return
723 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
724 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000726 return false;
727}
728
Chris Lattnerab351632009-02-20 20:59:54 +0000729//===----------------------------------------------------------------------===//
730// ObjCIvarDecl
731//===----------------------------------------------------------------------===//
732
Daniel Dunbara0654922010-04-02 20:10:03 +0000733ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000734 SourceLocation StartLoc,
735 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000736 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000737 AccessControl ac, Expr *BW,
738 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000739 if (DC) {
740 // Ivar's can only appear in interfaces, implementations (via synthesized
741 // properties), and class extensions (via direct declaration, or synthesized
742 // properties).
743 //
744 // FIXME: This should really be asserting this:
745 // (isa<ObjCCategoryDecl>(DC) &&
746 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
747 // but unfortunately we sometimes place ivars into non-class extension
748 // categories on error. This breaks an AST invariant, and should not be
749 // fixed.
750 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
751 isa<ObjCCategoryDecl>(DC)) &&
752 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000753 // Once a new ivar is created in any of class/class-extension/implementation
754 // decl contexts, the previously built IvarList must be rebuilt.
755 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
756 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000757 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000758 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000759 if (BW)
760 IM->setHasSynthBitfield(true);
Chad Rosier30601782011-08-17 23:08:45 +0000761 } else {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000762 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
763 ID = CD->getClassInterface();
764 if (BW)
765 CD->setHasSynthBitfield(true);
766 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000767 }
768 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000769 }
770
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000771 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
772 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000773}
774
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000775const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
776 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000777
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000778 switch (DC->getKind()) {
779 default:
780 case ObjCCategoryImpl:
781 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000782 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000783
784 // Ivars can only appear in class extension categories.
785 case ObjCCategory: {
786 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
787 assert(CD->IsClassExtension() && "invalid container for ivar!");
788 return CD->getClassInterface();
789 }
790
791 case ObjCImplementation:
792 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
793
794 case ObjCInterface:
795 return cast<ObjCInterfaceDecl>(DC);
796 }
797}
Chris Lattnerab351632009-02-20 20:59:54 +0000798
799//===----------------------------------------------------------------------===//
800// ObjCAtDefsFieldDecl
801//===----------------------------------------------------------------------===//
802
803ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000804*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
805 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000806 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000807 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000808}
809
Chris Lattnerab351632009-02-20 20:59:54 +0000810//===----------------------------------------------------------------------===//
811// ObjCProtocolDecl
812//===----------------------------------------------------------------------===//
813
814ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000815 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000816 IdentifierInfo *Id) {
817 return new (C) ObjCProtocolDecl(DC, L, Id);
818}
819
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000820ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
821 ObjCProtocolDecl *PDecl = this;
822
823 if (Name == getIdentifier())
824 return PDecl;
825
826 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
827 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
828 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000830 return NULL;
831}
832
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000833// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000834// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000835ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
836 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000837 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000839 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000840 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Chris Lattnerab351632009-02-20 20:59:54 +0000842 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000843 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000844 return MethodDecl;
845 return NULL;
846}
847
848//===----------------------------------------------------------------------===//
849// ObjCClassDecl
850//===----------------------------------------------------------------------===//
851
Mike Stump1eb44332009-09-09 15:08:12 +0000852ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000853 ObjCInterfaceDecl *const Elt,
854 const SourceLocation Loc,
Chris Lattner38af2de2009-02-20 21:35:13 +0000855 ASTContext &C)
856 : Decl(ObjCClass, DC, L) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000857 setClass(C, Elt, Loc);
Ted Kremenek321c22f2009-11-18 00:28:11 +0000858}
Chris Lattner38af2de2009-02-20 21:35:13 +0000859
Chris Lattnerab351632009-02-20 20:59:54 +0000860ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
861 SourceLocation L,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000862 ObjCInterfaceDecl *const Elt,
863 const SourceLocation Loc) {
864 return new (C) ObjCClassDecl(DC, L, Elt, Loc, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000865}
866
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000867void ObjCClassDecl::setClass(ASTContext &C, ObjCInterfaceDecl*const Cls,
868 const SourceLocation Loc) {
869
870 ForwardDecl = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef),
871 llvm::alignOf<ObjCClassRef>());
872 new (ForwardDecl) ObjCClassRef(Cls, Loc);
873}
874
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000875SourceRange ObjCClassDecl::getSourceRange() const {
876 // FIXME: We should include the semicolon
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000877 return SourceRange(getLocation(), ForwardDecl->getLocation());
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000878}
879
Chris Lattnerab351632009-02-20 20:59:54 +0000880//===----------------------------------------------------------------------===//
881// ObjCForwardProtocolDecl
882//===----------------------------------------------------------------------===//
883
Chris Lattner38af2de2009-02-20 21:35:13 +0000884ObjCForwardProtocolDecl::
885ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
886 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000887 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +0000888: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +0000889 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +0000890}
891
892
Chris Lattnerab351632009-02-20 20:59:54 +0000893ObjCForwardProtocolDecl *
894ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000895 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000896 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000897 unsigned NumElts,
898 const SourceLocation *Locs) {
899 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000900}
901
Chris Lattnerab351632009-02-20 20:59:54 +0000902//===----------------------------------------------------------------------===//
903// ObjCCategoryDecl
904//===----------------------------------------------------------------------===//
905
906ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +0000907 SourceLocation AtLoc,
908 SourceLocation ClassNameLoc,
909 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000910 IdentifierInfo *Id,
911 ObjCInterfaceDecl *IDecl) {
912 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
913 CategoryNameLoc, Id,
914 IDecl);
915 if (IDecl) {
916 // Link this category into its class's category list.
917 CatDecl->NextClassCategory = IDecl->getCategoryList();
918 IDecl->setCategoryList(CatDecl);
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +0000919 if (ASTMutationListener *L = C.getASTMutationListener())
920 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000921 }
922
923 return CatDecl;
924}
925
926ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) {
927 return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
928 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +0000929}
930
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000931ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
932 return getASTContext().getObjCImplementation(
933 const_cast<ObjCCategoryDecl*>(this));
934}
935
936void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
937 getASTContext().setObjCImplementation(this, ImplD);
938}
939
940
Chris Lattnerab351632009-02-20 20:59:54 +0000941//===----------------------------------------------------------------------===//
942// ObjCCategoryImplDecl
943//===----------------------------------------------------------------------===//
944
945ObjCCategoryImplDecl *
946ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
947 SourceLocation L,IdentifierInfo *Id,
948 ObjCInterfaceDecl *ClassInterface) {
949 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
950}
951
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000952ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000953 // The class interface might be NULL if we are working with invalid code.
954 if (const ObjCInterfaceDecl *ID = getClassInterface())
955 return ID->FindCategoryDeclaration(getIdentifier());
956 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000957}
958
Chris Lattnerab351632009-02-20 20:59:54 +0000959
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000960void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000961 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000962 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000963 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000964}
965
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000966void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
967 ASTContext &Ctx = getASTContext();
968
969 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +0000970 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000971 if (IFace)
972 Ctx.setObjCImplementation(IFace, ImplD);
973
Duncan Sands98f2cca2009-07-21 07:56:29 +0000974 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000975 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
976 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
977 Ctx.setObjCImplementation(CD, ImplD);
978 }
979
980 ClassInterface = IFace;
981}
982
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000983/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000984/// properties implemented in this category @implementation block and returns
985/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000986///
Chris Lattner3aa18612009-02-28 18:42:10 +0000987ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000988FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
989 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000990 ObjCPropertyImplDecl *PID = *i;
991 if (PID->getPropertyIvarDecl() &&
992 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
993 return PID;
994 }
995 return 0;
996}
997
998/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
999/// added to the list of those properties @synthesized/@dynamic in this
1000/// category @implementation block.
1001///
Chris Lattner3aa18612009-02-28 18:42:10 +00001002ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001003FindPropertyImplDecl(IdentifierInfo *Id) const {
1004 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001005 ObjCPropertyImplDecl *PID = *i;
1006 if (PID->getPropertyDecl()->getIdentifier() == Id)
1007 return PID;
1008 }
1009 return 0;
1010}
1011
Chris Lattner5f9e2722011-07-23 10:55:15 +00001012raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001013 const ObjCCategoryImplDecl *CID) {
1014 OS << CID->getName();
1015 return OS;
1016}
1017
Chris Lattnerab351632009-02-20 20:59:54 +00001018//===----------------------------------------------------------------------===//
1019// ObjCImplementationDecl
1020//===----------------------------------------------------------------------===//
1021
1022ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001023ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001024 SourceLocation L,
1025 ObjCInterfaceDecl *ClassInterface,
1026 ObjCInterfaceDecl *SuperDecl) {
1027 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
1028}
1029
John McCallda6d9762011-07-22 04:15:06 +00001030void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1031 CXXCtorInitializer ** initializers,
1032 unsigned numInitializers) {
1033 if (numInitializers > 0) {
1034 NumIvarInitializers = numInitializers;
1035 CXXCtorInitializer **ivarInitializers =
1036 new (C) CXXCtorInitializer*[NumIvarInitializers];
1037 memcpy(ivarInitializers, initializers,
1038 numInitializers * sizeof(CXXCtorInitializer*));
1039 IvarInitializers = ivarInitializers;
1040 }
1041}
1042
Chris Lattner5f9e2722011-07-23 10:55:15 +00001043raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001044 const ObjCImplementationDecl *ID) {
1045 OS << ID->getName();
1046 return OS;
1047}
1048
Chris Lattnerab351632009-02-20 20:59:54 +00001049//===----------------------------------------------------------------------===//
1050// ObjCCompatibleAliasDecl
1051//===----------------------------------------------------------------------===//
1052
1053ObjCCompatibleAliasDecl *
1054ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1055 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001056 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001057 ObjCInterfaceDecl* AliasedClass) {
1058 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1059}
1060
1061//===----------------------------------------------------------------------===//
1062// ObjCPropertyDecl
1063//===----------------------------------------------------------------------===//
1064
1065ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1066 SourceLocation L,
1067 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001068 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +00001069 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001070 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001071 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001072}
1073
Chris Lattnerab351632009-02-20 20:59:54 +00001074//===----------------------------------------------------------------------===//
1075// ObjCPropertyImplDecl
1076//===----------------------------------------------------------------------===//
1077
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001078ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001079 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001080 SourceLocation atLoc,
1081 SourceLocation L,
1082 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001083 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001084 ObjCIvarDecl *ivar,
1085 SourceLocation ivarLoc) {
1086 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1087 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001088}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001089
Douglas Gregora4ffd852010-11-17 01:03:52 +00001090SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1091 SourceLocation EndLoc = getLocation();
1092 if (IvarLoc.isValid())
1093 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001094
Douglas Gregora4ffd852010-11-17 01:03:52 +00001095 return SourceRange(AtLoc, EndLoc);
1096}