blob: c682cf18bfa0d9bb4ef2135c6af0cbf911d0c19a [file] [log] [blame]
Chris Lattner89375192008-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 Dunbar221fa942008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner89375192008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner8d8829e2008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattner22298722009-02-20 21:35:13 +000024void ObjCListBase::Destroy(ASTContext &Ctx) {
Chris Lattner7c981a72009-02-20 21:44:01 +000025 Ctx.Deallocate(List);
Chris Lattner4d1eb762009-02-20 21:16:26 +000026 NumElts = 0;
27 List = 0;
28}
29
Chris Lattner22298722009-02-20 21:35:13 +000030void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Chris Lattner4d1eb762009-02-20 21:16:26 +000031 assert(List == 0 && "Elements already set!");
32 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000033
34
Chris Lattner7c981a72009-02-20 21:44:01 +000035 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000036 NumElts = Elts;
37 memcpy(List, InList, sizeof(void*)*Elts);
38}
39
Douglas Gregor002b6712010-01-16 15:02:53 +000040void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
41 const SourceLocation *Locs, ASTContext &Ctx) {
42 if (Elts == 0)
43 return;
44
45 Locations = new (Ctx) SourceLocation[Elts];
46 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
47 set(InList, Elts, Ctx);
48}
49
50void ObjCProtocolList::Destroy(ASTContext &Ctx) {
51 Ctx.Deallocate(Locations);
52 Locations = 0;
53 ObjCList<ObjCProtocolDecl>::Destroy(Ctx);
54}
Chris Lattner4d1eb762009-02-20 21:16:26 +000055
56//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000057// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000058//===----------------------------------------------------------------------===//
59
Fariborz Jahanian68453832009-06-05 18:16:35 +000060/// getIvarDecl - This method looks up an ivar in this ContextDecl.
61///
62ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000063ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian68453832009-06-05 18:16:35 +000064 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000065 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000066 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
67 return ivar;
68 }
69 return 0;
70}
71
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000072// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000073ObjCMethodDecl *
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000074ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroffc4173fa2009-02-22 19:35:57 +000075 // Since instance & class methods can have the same name, the loop below
76 // ensures we get the correct method.
77 //
78 // @interface Whatever
79 // - (int) class_method;
80 // + (float) class_method;
81 // @end
82 //
83 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000084 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000085 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000086 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000087 return MD;
88 }
Steve Naroff35c62ae2009-01-08 17:28:14 +000089 return 0;
90}
91
Ted Kremenek4fb821e2010-03-15 20:11:46 +000092ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +000093ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +000094 IdentifierInfo *propertyID) {
95
Ted Kremenekddcd1092010-03-15 20:11:53 +000096 DeclContext::lookup_const_iterator I, E;
Ted Kremenek4fb821e2010-03-15 20:11:46 +000097 llvm::tie(I, E) = DC->lookup(propertyID);
98 for ( ; I != E; ++I)
99 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
100 return PD;
101
102 return 0;
103}
104
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000105/// FindPropertyDeclaration - Finds declaration of the property given its name
106/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000107ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000108ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenekddcd1092010-03-15 20:11:53 +0000110 if (ObjCPropertyDecl *PD =
111 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
112 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000113
Ted Kremenekddcd1092010-03-15 20:11:53 +0000114 switch (getKind()) {
115 default:
116 break;
117 case Decl::ObjCProtocol: {
118 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
119 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
120 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000121 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
122 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000123 break;
124 }
125 case Decl::ObjCInterface: {
126 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
127 // Look through categories.
128 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
129 Cat; Cat = Cat->getNextClassCategory())
130 if (!Cat->IsClassExtension())
131 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
132 return P;
133
134 // Look through protocols.
135 for (ObjCInterfaceDecl::protocol_iterator
136 I = OID->protocol_begin(), E = OID->protocol_end(); I != E; ++I)
137 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
138 return P;
139
140 // Finally, check the super class.
141 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
142 return superClass->FindPropertyDeclaration(PropertyId);
143 break;
144 }
145 case Decl::ObjCCategory: {
146 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
147 // Look through protocols.
148 if (!OCD->IsClassExtension())
149 for (ObjCCategoryDecl::protocol_iterator
150 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
151 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
152 return P;
153
154 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000155 }
156 }
Steve Narofff9c65242008-06-05 13:55:23 +0000157 return 0;
158}
159
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000160/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
161/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000162/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000163///
164ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000165ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000166 IdentifierInfo *PropertyId) const {
Ted Kremenekd133a862010-03-15 20:30:07 +0000167 if (ObjCPropertyDecl *PD =
168 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
169 return PD;
170
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000171 // Look through protocols.
Ted Kremenekd133a862010-03-15 20:30:07 +0000172 for (ObjCInterfaceDecl::protocol_iterator
173 I = protocol_begin(), E = protocol_end(); I != E; ++I)
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000174 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
175 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000176
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000177 return 0;
178}
179
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000180void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
181 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
Douglas Gregor002b6712010-01-16 15:02:53 +0000182 const SourceLocation *Locs,
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000183 ASTContext &C)
184{
185 if (ReferencedProtocols.empty()) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000186 ReferencedProtocols.set(ExtList, ExtNum, Locs, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000187 return;
188 }
189 // Check for duplicate protocol in class's protocol list.
190 // This is (O)2. But it is extremely rare and number of protocols in
191 // class or its extension are very few.
192 llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Douglas Gregor002b6712010-01-16 15:02:53 +0000193 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000194 for (unsigned i = 0; i < ExtNum; i++) {
195 bool protocolExists = false;
196 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
197 for (protocol_iterator p = protocol_begin(), e = protocol_end();
198 p != e; p++) {
199 ObjCProtocolDecl *Proto = (*p);
200 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
201 protocolExists = true;
202 break;
203 }
204 }
205 // Do we want to warn on a protocol in extension class which
206 // already exist in the class? Probably not.
Douglas Gregor002b6712010-01-16 15:02:53 +0000207 if (!protocolExists) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000208 ProtocolRefs.push_back(ProtoInExtension);
Douglas Gregor002b6712010-01-16 15:02:53 +0000209 ProtocolLocs.push_back(Locs[i]);
210 }
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000211 }
212 if (ProtocolRefs.empty())
213 return;
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000214 // Merge ProtocolRefs into class's protocol list;
Douglas Gregor002b6712010-01-16 15:02:53 +0000215 protocol_loc_iterator pl = protocol_loc_begin();
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000216 for (protocol_iterator p = protocol_begin(), e = protocol_end();
Douglas Gregor002b6712010-01-16 15:02:53 +0000217 p != e; ++p, ++pl) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000218 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000219 ProtocolLocs.push_back(*pl);
220 }
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000221 ReferencedProtocols.Destroy(C);
222 unsigned NumProtoRefs = ProtocolRefs.size();
Douglas Gregor002b6712010-01-16 15:02:53 +0000223 setProtocolList(ProtocolRefs.data(), NumProtoRefs, ProtocolLocs.data(), C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000224}
225
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000226/// getFirstClassExtension - Find first class extension of the given class.
227ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
228 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000229 CDecl = CDecl->getNextClassCategory())
230 if (CDecl->IsClassExtension())
231 return CDecl;
232 return 0;
233}
234
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000235/// getNextClassCategory - Find next class extension in list of categories.
236const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
237 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
238 CDecl = CDecl->getNextClassCategory())
239 if (CDecl->IsClassExtension())
240 return CDecl;
241 return 0;
242}
243
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000244ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
245 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner89375192008-03-16 00:19:01 +0000246 ObjCInterfaceDecl* ClassDecl = this;
247 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000248 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000249 clsDeclared = ClassDecl;
250 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000251 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000252 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
253 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000254 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
255 clsDeclared = ClassDecl;
256 return I;
257 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000258 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000259
Chris Lattner89375192008-03-16 00:19:01 +0000260 ClassDecl = ClassDecl->getSuperClass();
261 }
262 return NULL;
263}
264
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000265/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
266/// class whose name is passed as argument. If it is not one of the super classes
267/// the it returns NULL.
268ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
269 const IdentifierInfo*ICName) {
270 ObjCInterfaceDecl* ClassDecl = this;
271 while (ClassDecl != NULL) {
272 if (ClassDecl->getIdentifier() == ICName)
273 return ClassDecl;
274 ClassDecl = ClassDecl->getSuperClass();
275 }
276 return NULL;
277}
278
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000279/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000280/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000281ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
282 bool isInstance) const {
283 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000284 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattner89375192008-03-16 00:19:01 +0000286 while (ClassDecl != NULL) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000287 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000288 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000289
Chris Lattner89375192008-03-16 00:19:01 +0000290 // Didn't find one yet - look through protocols.
Chris Lattnerd0045052008-07-21 18:19:38 +0000291 const ObjCList<ObjCProtocolDecl> &Protocols =
292 ClassDecl->getReferencedProtocols();
293 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
294 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000295 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000296 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattner89375192008-03-16 00:19:01 +0000298 // Didn't find one yet - now look through categories.
299 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
300 while (CatDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000301 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000302 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Steve Naroff5fd31b12008-12-08 20:57:28 +0000304 // Didn't find one yet - look through protocols.
305 const ObjCList<ObjCProtocolDecl> &Protocols =
306 CatDecl->getReferencedProtocols();
307 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
308 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000309 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroff8ec27842009-02-26 11:32:02 +0000310 return MethodDecl;
Chris Lattner89375192008-03-16 00:19:01 +0000311 CatDecl = CatDecl->getNextClassCategory();
312 }
313 ClassDecl = ClassDecl->getSuperClass();
314 }
315 return NULL;
316}
317
Steve Naroffbb69c942009-10-01 23:46:04 +0000318ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod(
319 const Selector &Sel) {
320 ObjCMethodDecl *Method = 0;
321 if (ObjCImplementationDecl *ImpDecl = getImplementation())
322 Method = ImpDecl->getInstanceMethod(Sel);
323
324 if (!Method && getSuperClass())
325 return getSuperClass()->lookupPrivateInstanceMethod(Sel);
326 return Method;
327}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000328
329//===----------------------------------------------------------------------===//
330// ObjCMethodDecl
331//===----------------------------------------------------------------------===//
332
333ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000334 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000335 SourceLocation endLoc,
336 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000337 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000338 DeclContext *contextDecl,
339 bool isInstance,
340 bool isVariadic,
341 bool isSynthesized,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000342 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000343 ImplementationControl impControl,
344 unsigned numSelectorArgs) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000345 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor12852d92010-03-08 14:59:44 +0000346 SelInfo, T, ResultTInfo, contextDecl,
347 isInstance,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000348 isVariadic, isSynthesized, isDefined,
349 impControl,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000350 numSelectorArgs);
Chris Lattner89375192008-03-16 00:19:01 +0000351}
352
Chris Lattner22298722009-02-20 21:35:13 +0000353void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000354 if (Body) Body->Destroy(C);
355 if (SelfDecl) SelfDecl->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000357 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
358 if (*I) (*I)->Destroy(C);
359
Chris Lattner22298722009-02-20 21:35:13 +0000360 ParamInfo.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000361
362 Decl::Destroy(C);
Chris Lattner89375192008-03-16 00:19:01 +0000363}
364
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000365/// \brief A definition will return its interface declaration.
366/// An interface declaration will return its definition.
367/// Otherwise it will return itself.
368ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
369 ASTContext &Ctx = getASTContext();
370 ObjCMethodDecl *Redecl = 0;
371 Decl *CtxD = cast<Decl>(getDeclContext());
372
373 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
374 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
375 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
376
377 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
378 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
379 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
380
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000381 } else if (ObjCImplementationDecl *ImplD =
382 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000383 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
384 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000385
386 } else if (ObjCCategoryImplDecl *CImplD =
387 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000388 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000389 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000390 }
391
392 return Redecl ? Redecl : this;
393}
394
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000395ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
396 Decl *CtxD = cast<Decl>(getDeclContext());
397
398 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
399 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
400 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
401 isInstanceMethod()))
402 return MD;
403
404 } else if (ObjCCategoryImplDecl *CImplD =
405 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000406 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000407 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
408 isInstanceMethod()))
409 return MD;
410 }
411
412 return this;
413}
414
Mike Stump11289f42009-09-09 15:08:12 +0000415void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000416 const ObjCInterfaceDecl *OID) {
417 QualType selfTy;
418 if (isInstanceMethod()) {
419 // There may be no interface context due to error in declaration
420 // of the interface (which has been reported). Recover gracefully.
421 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000422 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000423 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000424 } else {
425 selfTy = Context.getObjCIdType();
426 }
427 } else // we have a factory method.
428 selfTy = Context.getObjCClassType();
429
Mike Stump11289f42009-09-09 15:08:12 +0000430 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff04f2d142009-04-20 15:06:07 +0000431 &Context.Idents.get("self"), selfTy));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000432
Mike Stump11289f42009-09-09 15:08:12 +0000433 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
434 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000435 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000436}
437
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000438ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
439 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
440 return ID;
441 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
442 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000443 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000444 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000445
446 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000447 assert(false && "unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000448 return 0;
449}
450
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000451//===----------------------------------------------------------------------===//
452// ObjCInterfaceDecl
453//===----------------------------------------------------------------------===//
454
455ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
456 DeclContext *DC,
457 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000458 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000459 SourceLocation ClassLoc,
460 bool ForwardDecl, bool isInternal){
461 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
462 isInternal);
463}
464
465ObjCInterfaceDecl::
466ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
467 SourceLocation CLoc, bool FD, bool isInternal)
468 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
469 TypeForDecl(0), SuperClass(0),
470 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
471 ClassLoc(CLoc) {
472}
473
Mike Stump11289f42009-09-09 15:08:12 +0000474void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnerd8a47c42009-03-31 08:36:08 +0000475 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000476 if (*I) (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000477
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000478 // FIXME: CategoryList?
Mike Stump11289f42009-09-09 15:08:12 +0000479
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000480 // FIXME: Because there is no clear ownership
481 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
482 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
483 Decl::Destroy(C);
484}
485
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000486ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
487 return getASTContext().getObjCImplementation(
488 const_cast<ObjCInterfaceDecl*>(this));
489}
490
491void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
492 getASTContext().setObjCImplementation(this, ImplD);
493}
494
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000495
496/// FindCategoryDeclaration - Finds category declaration in the list of
497/// categories for this class and returns it. Name of the category is passed
498/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000499///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000500ObjCCategoryDecl *
501ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
502 for (ObjCCategoryDecl *Category = getCategoryList();
503 Category; Category = Category->getNextClassCategory())
504 if (Category->getIdentifier() == CategoryId)
505 return Category;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000506 return 0;
507}
508
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000509ObjCMethodDecl *
510ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
511 for (ObjCCategoryDecl *Category = getCategoryList();
512 Category; Category = Category->getNextClassCategory())
513 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
514 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
515 return MD;
516 return 0;
517}
518
519ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
520 for (ObjCCategoryDecl *Category = getCategoryList();
521 Category; Category = Category->getNextClassCategory())
522 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
523 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
524 return MD;
525 return 0;
526}
527
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000528/// ClassImplementsProtocol - Checks that 'lProto' protocol
529/// has been implemented in IDecl class, its super class or categories (if
530/// lookupCategory is true).
531bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
532 bool lookupCategory,
533 bool RHSIsQualifiedID) {
534 ObjCInterfaceDecl *IDecl = this;
535 // 1st, look up the class.
536 const ObjCList<ObjCProtocolDecl> &Protocols =
537 IDecl->getReferencedProtocols();
Mike Stump11289f42009-09-09 15:08:12 +0000538
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000539 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
540 E = Protocols.end(); PI != E; ++PI) {
541 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
542 return true;
543 // This is dubious and is added to be compatible with gcc. In gcc, it is
544 // also allowed assigning a protocol-qualified 'id' type to a LHS object
545 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
546 // object. This IMO, should be a bug.
547 // FIXME: Treat this as an extension, and flag this as an error when GCC
548 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000549 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000550 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
551 return true;
552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000554 // 2nd, look up the category.
555 if (lookupCategory)
556 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
557 CDecl = CDecl->getNextClassCategory()) {
558 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
559 E = CDecl->protocol_end(); PI != E; ++PI)
560 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
561 return true;
562 }
Mike Stump11289f42009-09-09 15:08:12 +0000563
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000564 // 3rd, look up the super class(s)
565 if (IDecl->getSuperClass())
566 return
567 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
568 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +0000569
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000570 return false;
571}
572
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000573//===----------------------------------------------------------------------===//
574// ObjCIvarDecl
575//===----------------------------------------------------------------------===//
576
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000577ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000578 SourceLocation L, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +0000579 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +0000580 AccessControl ac, Expr *BW,
581 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000582 if (DC) {
583 // Ivar's can only appear in interfaces, implementations (via synthesized
584 // properties), and class extensions (via direct declaration, or synthesized
585 // properties).
586 //
587 // FIXME: This should really be asserting this:
588 // (isa<ObjCCategoryDecl>(DC) &&
589 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
590 // but unfortunately we sometimes place ivars into non-class extension
591 // categories on error. This breaks an AST invariant, and should not be
592 // fixed.
593 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
594 isa<ObjCCategoryDecl>(DC)) &&
595 "Invalid ivar decl context!");
596 }
597
Fariborz Jahanian18722982010-07-17 00:59:30 +0000598 return new (C) ObjCIvarDecl(DC, L, Id, T, TInfo, ac, BW, synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000599}
600
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000601const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
602 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000603
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000604 switch (DC->getKind()) {
605 default:
606 case ObjCCategoryImpl:
607 case ObjCProtocol:
608 assert(0 && "invalid ivar container!");
609 return 0;
610
611 // Ivars can only appear in class extension categories.
612 case ObjCCategory: {
613 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
614 assert(CD->IsClassExtension() && "invalid container for ivar!");
615 return CD->getClassInterface();
616 }
617
618 case ObjCImplementation:
619 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
620
621 case ObjCInterface:
622 return cast<ObjCInterfaceDecl>(DC);
623 }
624}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000625
626//===----------------------------------------------------------------------===//
627// ObjCAtDefsFieldDecl
628//===----------------------------------------------------------------------===//
629
630ObjCAtDefsFieldDecl
631*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
632 IdentifierInfo *Id, QualType T, Expr *BW) {
633 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
634}
635
636void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
637 this->~ObjCAtDefsFieldDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000638 C.Deallocate((void *)this);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000639}
640
641//===----------------------------------------------------------------------===//
642// ObjCProtocolDecl
643//===----------------------------------------------------------------------===//
644
645ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000646 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000647 IdentifierInfo *Id) {
648 return new (C) ObjCProtocolDecl(DC, L, Id);
649}
650
651void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner22298722009-02-20 21:35:13 +0000652 ReferencedProtocols.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000653 ObjCContainerDecl::Destroy(C);
654}
655
Steve Naroff114aecb2009-03-01 16:12:44 +0000656ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
657 ObjCProtocolDecl *PDecl = this;
658
659 if (Name == getIdentifier())
660 return PDecl;
661
662 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
663 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
664 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Steve Naroff114aecb2009-03-01 16:12:44 +0000666 return NULL;
667}
668
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000669// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000670// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000671ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
672 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000673 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000675 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000676 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000678 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000679 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000680 return MethodDecl;
681 return NULL;
682}
683
684//===----------------------------------------------------------------------===//
685// ObjCClassDecl
686//===----------------------------------------------------------------------===//
687
Mike Stump11289f42009-09-09 15:08:12 +0000688ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000689 ObjCInterfaceDecl *const *Elts,
690 const SourceLocation *Locs,
691 unsigned nElts,
Chris Lattner22298722009-02-20 21:35:13 +0000692 ASTContext &C)
693 : Decl(ObjCClass, DC, L) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000694 setClassList(C, Elts, Locs, nElts);
Chris Lattner22298722009-02-20 21:35:13 +0000695}
696
Ted Kremenek9b124e12009-11-18 00:28:11 +0000697void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
698 const SourceLocation *Locs, unsigned Num) {
699 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
700 llvm::alignof<ObjCClassRef>());
701 for (unsigned i = 0; i < Num; ++i)
702 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
703
704 NumDecls = Num;
705}
Chris Lattner22298722009-02-20 21:35:13 +0000706
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000707ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
708 SourceLocation L,
709 ObjCInterfaceDecl *const *Elts,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000710 const SourceLocation *Locs,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000711 unsigned nElts) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000712 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000713}
714
715void ObjCClassDecl::Destroy(ASTContext &C) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000716 // ObjCInterfaceDecls registered with a DeclContext will get destroyed
717 // when the DeclContext is destroyed. For those created only by a forward
718 // declaration, the first @class that created the ObjCInterfaceDecl gets
719 // to destroy it.
720 // FIXME: Note that this ownership role is very brittle; a better
721 // polict is surely need in the future.
722 for (iterator I = begin(), E = end(); I !=E ; ++I) {
723 ObjCInterfaceDecl *ID = I->getInterface();
724 if (ID->isForwardDecl() && ID->getLocStart() == getLocStart())
725 ID->Destroy(C);
726 }
727
728 C.Deallocate(ForwardDecls);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000729 Decl::Destroy(C);
730}
731
Ted Kremenek49939c22009-11-18 01:26:56 +0000732SourceRange ObjCClassDecl::getSourceRange() const {
733 // FIXME: We should include the semicolon
734 assert(NumDecls);
735 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
736}
737
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000738//===----------------------------------------------------------------------===//
739// ObjCForwardProtocolDecl
740//===----------------------------------------------------------------------===//
741
Chris Lattner22298722009-02-20 21:35:13 +0000742ObjCForwardProtocolDecl::
743ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
744 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000745 const SourceLocation *Locs, ASTContext &C)
Mike Stump11289f42009-09-09 15:08:12 +0000746: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000747 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner22298722009-02-20 21:35:13 +0000748}
749
750
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000751ObjCForwardProtocolDecl *
752ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000753 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000754 ObjCProtocolDecl *const *Elts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000755 unsigned NumElts,
756 const SourceLocation *Locs) {
757 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000758}
759
760void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner22298722009-02-20 21:35:13 +0000761 ReferencedProtocols.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000762 Decl::Destroy(C);
763}
764
765//===----------------------------------------------------------------------===//
766// ObjCCategoryDecl
767//===----------------------------------------------------------------------===//
768
769ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor071676f2010-01-16 16:38:58 +0000770 SourceLocation AtLoc,
771 SourceLocation ClassNameLoc,
772 SourceLocation CategoryNameLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000773 IdentifierInfo *Id) {
Douglas Gregor071676f2010-01-16 16:38:58 +0000774 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000775}
776
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000777ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
778 return getASTContext().getObjCImplementation(
779 const_cast<ObjCCategoryDecl*>(this));
780}
781
782void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
783 getASTContext().setObjCImplementation(this, ImplD);
784}
785
786
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000787//===----------------------------------------------------------------------===//
788// ObjCCategoryImplDecl
789//===----------------------------------------------------------------------===//
790
791ObjCCategoryImplDecl *
792ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
793 SourceLocation L,IdentifierInfo *Id,
794 ObjCInterfaceDecl *ClassInterface) {
795 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
796}
797
Steve Narofff406f4d2009-10-29 21:11:04 +0000798ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000799 // The class interface might be NULL if we are working with invalid code.
800 if (const ObjCInterfaceDecl *ID = getClassInterface())
801 return ID->FindCategoryDeclaration(getIdentifier());
802 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000803}
804
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000805
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000806void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +0000807 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000808 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000809 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000810}
811
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000812void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
813 ASTContext &Ctx = getASTContext();
814
815 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +0000816 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000817 if (IFace)
818 Ctx.setObjCImplementation(IFace, ImplD);
819
Duncan Sands49c29ee2009-07-21 07:56:29 +0000820 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000821 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
822 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
823 Ctx.setObjCImplementation(CD, ImplD);
824 }
825
826 ClassInterface = IFace;
827}
828
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000829/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattneraab70d22009-02-16 19:24:31 +0000830/// properties implemented in this category @implementation block and returns
831/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000832///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000833ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000834FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
835 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000836 ObjCPropertyImplDecl *PID = *i;
837 if (PID->getPropertyIvarDecl() &&
838 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
839 return PID;
840 }
841 return 0;
842}
843
844/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
845/// added to the list of those properties @synthesized/@dynamic in this
846/// category @implementation block.
847///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000848ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000849FindPropertyImplDecl(IdentifierInfo *Id) const {
850 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000851 ObjCPropertyImplDecl *PID = *i;
852 if (PID->getPropertyDecl()->getIdentifier() == Id)
853 return PID;
854 }
855 return 0;
856}
857
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000858llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
859 const ObjCCategoryImplDecl *CID) {
860 OS << CID->getName();
861 return OS;
862}
863
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000864//===----------------------------------------------------------------------===//
865// ObjCImplementationDecl
866//===----------------------------------------------------------------------===//
867
868ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000869ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000870 SourceLocation L,
871 ObjCInterfaceDecl *ClassInterface,
872 ObjCInterfaceDecl *SuperDecl) {
873 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
874}
875
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000876llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
877 const ObjCImplementationDecl *ID) {
878 OS << ID->getName();
879 return OS;
880}
881
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000882//===----------------------------------------------------------------------===//
883// ObjCCompatibleAliasDecl
884//===----------------------------------------------------------------------===//
885
886ObjCCompatibleAliasDecl *
887ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
888 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +0000889 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000890 ObjCInterfaceDecl* AliasedClass) {
891 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
892}
893
894//===----------------------------------------------------------------------===//
895// ObjCPropertyDecl
896//===----------------------------------------------------------------------===//
897
898ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
899 SourceLocation L,
900 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000901 SourceLocation AtLoc,
John McCall339bb662010-06-04 20:50:08 +0000902 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000903 PropertyControl propControl) {
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000904 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000905}
906
907
908//===----------------------------------------------------------------------===//
909// ObjCPropertyImplDecl
910//===----------------------------------------------------------------------===//
911
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000912ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000913 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000914 SourceLocation atLoc,
915 SourceLocation L,
916 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +0000917 Kind PK,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000918 ObjCIvarDecl *ivar) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000919 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000920}
Chris Lattnered0e1642008-03-17 01:19:02 +0000921
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000922