blob: 817f7073af1318314b96d89cb483478a95315d84 [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"
Steve Naroff0de21fd2009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattner38af2de2009-02-20 21:35:13 +000024void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000025 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000026 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000027
28
Chris Lattner4ee413b2009-02-20 21:44:01 +000029 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000030 NumElts = Elts;
31 memcpy(List, InList, sizeof(void*)*Elts);
32}
33
Douglas Gregor18df52b2010-01-16 15:02:53 +000034void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
35 const SourceLocation *Locs, ASTContext &Ctx) {
36 if (Elts == 0)
37 return;
38
39 Locations = new (Ctx) SourceLocation[Elts];
40 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
41 set(InList, Elts, Ctx);
42}
43
Chris Lattner11e1e1a2009-02-20 21:16:26 +000044//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000045// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000046//===----------------------------------------------------------------------===//
47
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000048/// getIvarDecl - This method looks up an ivar in this ContextDecl.
49///
50ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000051ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000052 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000053 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000054 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
55 return ivar;
56 }
57 return 0;
58}
59
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000060// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000061ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000062ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000063 // Since instance & class methods can have the same name, the loop below
64 // ensures we get the correct method.
65 //
66 // @interface Whatever
67 // - (int) class_method;
68 // + (float) class_method;
69 // @end
70 //
71 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000072 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000073 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000074 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000075 return MD;
76 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000077 return 0;
78}
79
Ted Kremenek9f550ff2010-03-15 20:11:46 +000080ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000081ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000082 IdentifierInfo *propertyID) {
83
Ted Kremenekde09d0c2010-03-15 20:11:53 +000084 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000085 llvm::tie(I, E) = DC->lookup(propertyID);
86 for ( ; I != E; ++I)
87 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
88 return PD;
89
90 return 0;
91}
92
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000093/// FindPropertyDeclaration - Finds declaration of the property given its name
94/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000095ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000096ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenekde09d0c2010-03-15 20:11:53 +000098 if (ObjCPropertyDecl *PD =
99 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
100 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000102 switch (getKind()) {
103 default:
104 break;
105 case Decl::ObjCProtocol: {
106 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
107 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
108 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000109 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
110 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000111 break;
112 }
113 case Decl::ObjCInterface: {
114 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
115 // Look through categories.
116 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
117 Cat; Cat = Cat->getNextClassCategory())
118 if (!Cat->IsClassExtension())
119 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
120 return P;
121
122 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000123 for (ObjCInterfaceDecl::all_protocol_iterator
124 I = OID->all_referenced_protocol_begin(),
125 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000126 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
127 return P;
128
129 // Finally, check the super class.
130 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
131 return superClass->FindPropertyDeclaration(PropertyId);
132 break;
133 }
134 case Decl::ObjCCategory: {
135 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
136 // Look through protocols.
137 if (!OCD->IsClassExtension())
138 for (ObjCCategoryDecl::protocol_iterator
139 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
140 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
141 return P;
142
143 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000144 }
145 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000146 return 0;
147}
148
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000149/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
150/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000151/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000152///
153ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000154ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000155 IdentifierInfo *PropertyId) const {
Ted Kremenek37cafb02010-03-15 20:30:07 +0000156 if (ObjCPropertyDecl *PD =
157 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
158 return PD;
159
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000160 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000161 for (ObjCInterfaceDecl::all_protocol_iterator
162 I = all_referenced_protocol_begin(),
163 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000164 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
165 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000166
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000167 return 0;
168}
169
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000170void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
171 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
172 ASTContext &C)
173{
Ted Kremenek53b94412010-09-01 01:21:15 +0000174 if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) {
175 AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000176 return;
177 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000178
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000179 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000180 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000181 // class or its extension are very few.
182 llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
183 for (unsigned i = 0; i < ExtNum; i++) {
184 bool protocolExists = false;
185 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000186 for (all_protocol_iterator
187 p = all_referenced_protocol_begin(),
188 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000189 ObjCProtocolDecl *Proto = (*p);
190 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
191 protocolExists = true;
192 break;
193 }
194 }
195 // Do we want to warn on a protocol in extension class which
196 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000197 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000198 ProtocolRefs.push_back(ProtoInExtension);
199 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000200
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000201 if (ProtocolRefs.empty())
202 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000203
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000204 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000205 for (all_protocol_iterator p = all_referenced_protocol_begin(),
206 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000207 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000208 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000209
210 AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000211}
212
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000213/// getFirstClassExtension - Find first class extension of the given class.
214ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
215 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000216 CDecl = CDecl->getNextClassCategory())
217 if (CDecl->IsClassExtension())
218 return CDecl;
219 return 0;
220}
221
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000222/// getNextClassCategory - Find next class extension in list of categories.
223const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
224 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
225 CDecl = CDecl->getNextClassCategory())
226 if (CDecl->IsClassExtension())
227 return CDecl;
228 return 0;
229}
230
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000231ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
232 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000233 ObjCInterfaceDecl* ClassDecl = this;
234 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000235 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000236 clsDeclared = ClassDecl;
237 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000238 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000239 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
240 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000241 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
242 clsDeclared = ClassDecl;
243 return I;
244 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000245 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000246
Chris Lattner1e03a562008-03-16 00:19:01 +0000247 ClassDecl = ClassDecl->getSuperClass();
248 }
249 return NULL;
250}
251
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000252/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
253/// class whose name is passed as argument. If it is not one of the super classes
254/// the it returns NULL.
255ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
256 const IdentifierInfo*ICName) {
257 ObjCInterfaceDecl* ClassDecl = this;
258 while (ClassDecl != NULL) {
259 if (ClassDecl->getIdentifier() == ICName)
260 return ClassDecl;
261 ClassDecl = ClassDecl->getSuperClass();
262 }
263 return NULL;
264}
265
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000266/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000267/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000268ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
269 bool isInstance) const {
270 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000271 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner1e03a562008-03-16 00:19:01 +0000273 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000274 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000275 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner1e03a562008-03-16 00:19:01 +0000277 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000278 const ObjCList<ObjCProtocolDecl> &Protocols =
279 ClassDecl->getReferencedProtocols();
280 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
281 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000282 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000283 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattner1e03a562008-03-16 00:19:01 +0000285 // Didn't find one yet - now look through categories.
286 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
287 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000288 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000289 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Steve Naroffb79c01e2008-12-08 20:57:28 +0000291 // Didn't find one yet - look through protocols.
292 const ObjCList<ObjCProtocolDecl> &Protocols =
293 CatDecl->getReferencedProtocols();
294 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
295 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000296 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000297 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000298 CatDecl = CatDecl->getNextClassCategory();
299 }
300 ClassDecl = ClassDecl->getSuperClass();
301 }
302 return NULL;
303}
304
Steve Naroffd789d3d2009-10-01 23:46:04 +0000305ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod(
306 const Selector &Sel) {
307 ObjCMethodDecl *Method = 0;
308 if (ObjCImplementationDecl *ImpDecl = getImplementation())
309 Method = ImpDecl->getInstanceMethod(Sel);
310
311 if (!Method && getSuperClass())
312 return getSuperClass()->lookupPrivateInstanceMethod(Sel);
313 return Method;
314}
Chris Lattnerab351632009-02-20 20:59:54 +0000315
316//===----------------------------------------------------------------------===//
317// ObjCMethodDecl
318//===----------------------------------------------------------------------===//
319
320ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000321 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000322 SourceLocation endLoc,
323 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000324 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000325 DeclContext *contextDecl,
326 bool isInstance,
327 bool isVariadic,
328 bool isSynthesized,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000329 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000330 ImplementationControl impControl,
331 unsigned numSelectorArgs) {
Chris Lattnerab351632009-02-20 20:59:54 +0000332 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000333 SelInfo, T, ResultTInfo, contextDecl,
334 isInstance,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000335 isVariadic, isSynthesized, isDefined,
336 impControl,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000337 numSelectorArgs);
Chris Lattner1e03a562008-03-16 00:19:01 +0000338}
339
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000340/// \brief A definition will return its interface declaration.
341/// An interface declaration will return its definition.
342/// Otherwise it will return itself.
343ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
344 ASTContext &Ctx = getASTContext();
345 ObjCMethodDecl *Redecl = 0;
346 Decl *CtxD = cast<Decl>(getDeclContext());
347
348 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
349 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
350 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
351
352 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
353 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
354 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
355
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000356 } else if (ObjCImplementationDecl *ImplD =
357 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000358 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
359 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000360
361 } else if (ObjCCategoryImplDecl *CImplD =
362 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000363 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000364 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000365 }
366
367 return Redecl ? Redecl : this;
368}
369
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000370ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
371 Decl *CtxD = cast<Decl>(getDeclContext());
372
373 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
374 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
375 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
376 isInstanceMethod()))
377 return MD;
378
379 } else if (ObjCCategoryImplDecl *CImplD =
380 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000381 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000382 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
383 isInstanceMethod()))
384 return MD;
385 }
386
387 return this;
388}
389
Mike Stump1eb44332009-09-09 15:08:12 +0000390void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000391 const ObjCInterfaceDecl *OID) {
392 QualType selfTy;
393 if (isInstanceMethod()) {
394 // There may be no interface context due to error in declaration
395 // of the interface (which has been reported). Recover gracefully.
396 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000397 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000398 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000399 } else {
400 selfTy = Context.getObjCIdType();
401 }
402 } else // we have a factory method.
403 selfTy = Context.getObjCClassType();
404
Mike Stump1eb44332009-09-09 15:08:12 +0000405 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000406 &Context.Idents.get("self"), selfTy));
Chris Lattnerab351632009-02-20 20:59:54 +0000407
Mike Stump1eb44332009-09-09 15:08:12 +0000408 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
409 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000410 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000411}
412
Chris Lattnerab351632009-02-20 20:59:54 +0000413ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
414 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
415 return ID;
416 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
417 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000418 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000419 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000420
421 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerab351632009-02-20 20:59:54 +0000422 assert(false && "unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000423 return 0;
424}
425
Chris Lattnerab351632009-02-20 20:59:54 +0000426//===----------------------------------------------------------------------===//
427// ObjCInterfaceDecl
428//===----------------------------------------------------------------------===//
429
430ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
431 DeclContext *DC,
432 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000433 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000434 SourceLocation ClassLoc,
435 bool ForwardDecl, bool isInternal){
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000436 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
437 isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000438}
439
440ObjCInterfaceDecl::
441ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000442 SourceLocation CLoc, bool FD, bool isInternal)
Chris Lattnerab351632009-02-20 20:59:54 +0000443 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
444 TypeForDecl(0), SuperClass(0),
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000445 CategoryList(0), IvarList(0),
446 ForwardDecl(FD), InternalInterface(isInternal),
Chris Lattnerab351632009-02-20 20:59:54 +0000447 ClassLoc(CLoc) {
448}
449
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000450ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
451 return getASTContext().getObjCImplementation(
452 const_cast<ObjCInterfaceDecl*>(this));
453}
454
455void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
456 getASTContext().setObjCImplementation(this, ImplD);
457}
458
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000459/// all_declared_ivar_begin - return first ivar declared in this class,
460/// its extensions and its implementation. Lazily build the list on first
461/// access.
462ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
463 if (IvarList)
464 return IvarList;
465
466 ObjCIvarDecl *curIvar = 0;
467 if (!ivar_empty()) {
468 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
469 IvarList = (*I); ++I;
470 for (curIvar = IvarList; I != E; curIvar = *I, ++I)
471 curIvar->setNextIvar(*I);
472 }
473
474 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
475 CDecl = CDecl->getNextClassExtension()) {
476 if (!CDecl->ivar_empty()) {
477 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
478 E = CDecl->ivar_end();
479 if (!IvarList) {
480 IvarList = (*I); ++I;
481 curIvar = IvarList;
482 }
483 for ( ;I != E; curIvar = *I, ++I)
484 curIvar->setNextIvar(*I);
485 }
486 }
487
488 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
489 if (!ImplDecl->ivar_empty()) {
490 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
491 E = ImplDecl->ivar_end();
492 if (!IvarList) {
493 IvarList = (*I); ++I;
494 curIvar = IvarList;
495 }
496 for ( ;I != E; curIvar = *I, ++I)
497 curIvar->setNextIvar(*I);
498 }
499 }
500 return IvarList;
501}
Chris Lattnerab351632009-02-20 20:59:54 +0000502
503/// FindCategoryDeclaration - Finds category declaration in the list of
504/// categories for this class and returns it. Name of the category is passed
505/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000506///
Chris Lattnerab351632009-02-20 20:59:54 +0000507ObjCCategoryDecl *
508ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
509 for (ObjCCategoryDecl *Category = getCategoryList();
510 Category; Category = Category->getNextClassCategory())
511 if (Category->getIdentifier() == CategoryId)
512 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000513 return 0;
514}
515
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000516ObjCMethodDecl *
517ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
518 for (ObjCCategoryDecl *Category = getCategoryList();
519 Category; Category = Category->getNextClassCategory())
520 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
521 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
522 return MD;
523 return 0;
524}
525
526ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
527 for (ObjCCategoryDecl *Category = getCategoryList();
528 Category; Category = Category->getNextClassCategory())
529 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
530 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
531 return MD;
532 return 0;
533}
534
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000535/// ClassImplementsProtocol - Checks that 'lProto' protocol
536/// has been implemented in IDecl class, its super class or categories (if
537/// lookupCategory is true).
538bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
539 bool lookupCategory,
540 bool RHSIsQualifiedID) {
541 ObjCInterfaceDecl *IDecl = this;
542 // 1st, look up the class.
543 const ObjCList<ObjCProtocolDecl> &Protocols =
544 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000546 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
547 E = Protocols.end(); PI != E; ++PI) {
548 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
549 return true;
550 // This is dubious and is added to be compatible with gcc. In gcc, it is
551 // also allowed assigning a protocol-qualified 'id' type to a LHS object
552 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
553 // object. This IMO, should be a bug.
554 // FIXME: Treat this as an extension, and flag this as an error when GCC
555 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000556 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000557 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
558 return true;
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000561 // 2nd, look up the category.
562 if (lookupCategory)
563 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
564 CDecl = CDecl->getNextClassCategory()) {
565 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
566 E = CDecl->protocol_end(); PI != E; ++PI)
567 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
568 return true;
569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000571 // 3rd, look up the super class(s)
572 if (IDecl->getSuperClass())
573 return
574 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
575 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000577 return false;
578}
579
Chris Lattnerab351632009-02-20 20:59:54 +0000580//===----------------------------------------------------------------------===//
581// ObjCIvarDecl
582//===----------------------------------------------------------------------===//
583
Daniel Dunbara0654922010-04-02 20:10:03 +0000584ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Chris Lattnerab351632009-02-20 20:59:54 +0000585 SourceLocation L, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000586 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000587 AccessControl ac, Expr *BW,
588 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000589 if (DC) {
590 // Ivar's can only appear in interfaces, implementations (via synthesized
591 // properties), and class extensions (via direct declaration, or synthesized
592 // properties).
593 //
594 // FIXME: This should really be asserting this:
595 // (isa<ObjCCategoryDecl>(DC) &&
596 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
597 // but unfortunately we sometimes place ivars into non-class extension
598 // categories on error. This breaks an AST invariant, and should not be
599 // fixed.
600 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
601 isa<ObjCCategoryDecl>(DC)) &&
602 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000603 // Once a new ivar is created in any of class/class-extension/implementation
604 // decl contexts, the previously built IvarList must be rebuilt.
605 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
606 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000607 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000608 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000609 if (BW)
610 IM->setHasSynthBitfield(true);
611 }
612 else {
613 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
614 ID = CD->getClassInterface();
615 if (BW)
616 CD->setHasSynthBitfield(true);
617 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000618 }
619 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000620 }
621
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000622 return new (C) ObjCIvarDecl(DC, L, Id, T, TInfo, ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000623}
624
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000625const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
626 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000627
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000628 switch (DC->getKind()) {
629 default:
630 case ObjCCategoryImpl:
631 case ObjCProtocol:
632 assert(0 && "invalid ivar container!");
633 return 0;
634
635 // Ivars can only appear in class extension categories.
636 case ObjCCategory: {
637 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
638 assert(CD->IsClassExtension() && "invalid container for ivar!");
639 return CD->getClassInterface();
640 }
641
642 case ObjCImplementation:
643 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
644
645 case ObjCInterface:
646 return cast<ObjCInterfaceDecl>(DC);
647 }
648}
Chris Lattnerab351632009-02-20 20:59:54 +0000649
650//===----------------------------------------------------------------------===//
651// ObjCAtDefsFieldDecl
652//===----------------------------------------------------------------------===//
653
654ObjCAtDefsFieldDecl
655*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
656 IdentifierInfo *Id, QualType T, Expr *BW) {
657 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
658}
659
Chris Lattnerab351632009-02-20 20:59:54 +0000660//===----------------------------------------------------------------------===//
661// ObjCProtocolDecl
662//===----------------------------------------------------------------------===//
663
664ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000665 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000666 IdentifierInfo *Id) {
667 return new (C) ObjCProtocolDecl(DC, L, Id);
668}
669
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000670ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
671 ObjCProtocolDecl *PDecl = this;
672
673 if (Name == getIdentifier())
674 return PDecl;
675
676 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
677 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
678 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000680 return NULL;
681}
682
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000683// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000684// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000685ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
686 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000687 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000689 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000690 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattnerab351632009-02-20 20:59:54 +0000692 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000693 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000694 return MethodDecl;
695 return NULL;
696}
697
698//===----------------------------------------------------------------------===//
699// ObjCClassDecl
700//===----------------------------------------------------------------------===//
701
Mike Stump1eb44332009-09-09 15:08:12 +0000702ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek321c22f2009-11-18 00:28:11 +0000703 ObjCInterfaceDecl *const *Elts,
704 const SourceLocation *Locs,
705 unsigned nElts,
Chris Lattner38af2de2009-02-20 21:35:13 +0000706 ASTContext &C)
707 : Decl(ObjCClass, DC, L) {
Ted Kremenek321c22f2009-11-18 00:28:11 +0000708 setClassList(C, Elts, Locs, nElts);
Chris Lattner38af2de2009-02-20 21:35:13 +0000709}
710
Ted Kremenek321c22f2009-11-18 00:28:11 +0000711void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
712 const SourceLocation *Locs, unsigned Num) {
713 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
Chris Lattner32488542010-10-30 05:14:06 +0000714 llvm::alignOf<ObjCClassRef>());
Ted Kremenek321c22f2009-11-18 00:28:11 +0000715 for (unsigned i = 0; i < Num; ++i)
716 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
717
718 NumDecls = Num;
719}
Chris Lattner38af2de2009-02-20 21:35:13 +0000720
Chris Lattnerab351632009-02-20 20:59:54 +0000721ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
722 SourceLocation L,
723 ObjCInterfaceDecl *const *Elts,
Ted Kremenek321c22f2009-11-18 00:28:11 +0000724 const SourceLocation *Locs,
Chris Lattnerab351632009-02-20 20:59:54 +0000725 unsigned nElts) {
Ted Kremenek321c22f2009-11-18 00:28:11 +0000726 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000727}
728
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000729SourceRange ObjCClassDecl::getSourceRange() const {
730 // FIXME: We should include the semicolon
731 assert(NumDecls);
732 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
733}
734
Chris Lattnerab351632009-02-20 20:59:54 +0000735//===----------------------------------------------------------------------===//
736// ObjCForwardProtocolDecl
737//===----------------------------------------------------------------------===//
738
Chris Lattner38af2de2009-02-20 21:35:13 +0000739ObjCForwardProtocolDecl::
740ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
741 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000742 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +0000743: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +0000744 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +0000745}
746
747
Chris Lattnerab351632009-02-20 20:59:54 +0000748ObjCForwardProtocolDecl *
749ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000750 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000751 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000752 unsigned NumElts,
753 const SourceLocation *Locs) {
754 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000755}
756
Chris Lattnerab351632009-02-20 20:59:54 +0000757//===----------------------------------------------------------------------===//
758// ObjCCategoryDecl
759//===----------------------------------------------------------------------===//
760
761ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +0000762 SourceLocation AtLoc,
763 SourceLocation ClassNameLoc,
764 SourceLocation CategoryNameLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000765 IdentifierInfo *Id) {
Douglas Gregor3db211b2010-01-16 16:38:58 +0000766 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerab351632009-02-20 20:59:54 +0000767}
768
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000769ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
770 return getASTContext().getObjCImplementation(
771 const_cast<ObjCCategoryDecl*>(this));
772}
773
774void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
775 getASTContext().setObjCImplementation(this, ImplD);
776}
777
778
Chris Lattnerab351632009-02-20 20:59:54 +0000779//===----------------------------------------------------------------------===//
780// ObjCCategoryImplDecl
781//===----------------------------------------------------------------------===//
782
783ObjCCategoryImplDecl *
784ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
785 SourceLocation L,IdentifierInfo *Id,
786 ObjCInterfaceDecl *ClassInterface) {
787 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
788}
789
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000790ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000791 // The class interface might be NULL if we are working with invalid code.
792 if (const ObjCInterfaceDecl *ID = getClassInterface())
793 return ID->FindCategoryDeclaration(getIdentifier());
794 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000795}
796
Chris Lattnerab351632009-02-20 20:59:54 +0000797
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000798void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000799 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000800 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000801 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000802}
803
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000804void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
805 ASTContext &Ctx = getASTContext();
806
807 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +0000808 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000809 if (IFace)
810 Ctx.setObjCImplementation(IFace, ImplD);
811
Duncan Sands98f2cca2009-07-21 07:56:29 +0000812 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000813 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
814 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
815 Ctx.setObjCImplementation(CD, ImplD);
816 }
817
818 ClassInterface = IFace;
819}
820
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000821/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000822/// properties implemented in this category @implementation block and returns
823/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000824///
Chris Lattner3aa18612009-02-28 18:42:10 +0000825ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000826FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
827 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000828 ObjCPropertyImplDecl *PID = *i;
829 if (PID->getPropertyIvarDecl() &&
830 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
831 return PID;
832 }
833 return 0;
834}
835
836/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
837/// added to the list of those properties @synthesized/@dynamic in this
838/// category @implementation block.
839///
Chris Lattner3aa18612009-02-28 18:42:10 +0000840ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000841FindPropertyImplDecl(IdentifierInfo *Id) const {
842 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000843 ObjCPropertyImplDecl *PID = *i;
844 if (PID->getPropertyDecl()->getIdentifier() == Id)
845 return PID;
846 }
847 return 0;
848}
849
Benjamin Kramer900fc632010-04-17 09:33:03 +0000850llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
851 const ObjCCategoryImplDecl *CID) {
852 OS << CID->getName();
853 return OS;
854}
855
Chris Lattnerab351632009-02-20 20:59:54 +0000856//===----------------------------------------------------------------------===//
857// ObjCImplementationDecl
858//===----------------------------------------------------------------------===//
859
860ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000861ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +0000862 SourceLocation L,
863 ObjCInterfaceDecl *ClassInterface,
864 ObjCInterfaceDecl *SuperDecl) {
865 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
866}
867
Benjamin Kramer900fc632010-04-17 09:33:03 +0000868llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
869 const ObjCImplementationDecl *ID) {
870 OS << ID->getName();
871 return OS;
872}
873
Chris Lattnerab351632009-02-20 20:59:54 +0000874//===----------------------------------------------------------------------===//
875// ObjCCompatibleAliasDecl
876//===----------------------------------------------------------------------===//
877
878ObjCCompatibleAliasDecl *
879ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
880 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +0000881 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000882 ObjCInterfaceDecl* AliasedClass) {
883 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
884}
885
886//===----------------------------------------------------------------------===//
887// ObjCPropertyDecl
888//===----------------------------------------------------------------------===//
889
890ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
891 SourceLocation L,
892 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000893 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +0000894 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +0000895 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000896 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +0000897}
898
899
900//===----------------------------------------------------------------------===//
901// ObjCPropertyImplDecl
902//===----------------------------------------------------------------------===//
903
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000904ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +0000905 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000906 SourceLocation atLoc,
907 SourceLocation L,
908 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000909 Kind PK,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000910 ObjCIvarDecl *ivar) {
Steve Naroff3e970492009-01-27 21:25:57 +0000911 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000912}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000913
Chris Lattner0ed844b2008-04-04 06:12:32 +0000914