blob: fce24dcf3849be6aaf74da9e5febdbef37e64bc6 [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 Jahaniand9235db2010-04-08 21:29:11 +0000342 ImplementationControl impControl,
343 unsigned numSelectorArgs) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000344 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor12852d92010-03-08 14:59:44 +0000345 SelInfo, T, ResultTInfo, contextDecl,
346 isInstance,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000347 isVariadic, isSynthesized, impControl,
348 numSelectorArgs);
Chris Lattner89375192008-03-16 00:19:01 +0000349}
350
Chris Lattner22298722009-02-20 21:35:13 +0000351void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000352 if (Body) Body->Destroy(C);
353 if (SelfDecl) SelfDecl->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000355 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
356 if (*I) (*I)->Destroy(C);
357
Chris Lattner22298722009-02-20 21:35:13 +0000358 ParamInfo.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000359
360 Decl::Destroy(C);
Chris Lattner89375192008-03-16 00:19:01 +0000361}
362
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000363/// \brief A definition will return its interface declaration.
364/// An interface declaration will return its definition.
365/// Otherwise it will return itself.
366ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
367 ASTContext &Ctx = getASTContext();
368 ObjCMethodDecl *Redecl = 0;
369 Decl *CtxD = cast<Decl>(getDeclContext());
370
371 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
372 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
373 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
374
375 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
376 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
377 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
378
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000379 } else if (ObjCImplementationDecl *ImplD =
380 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000381 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
382 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000383
384 } else if (ObjCCategoryImplDecl *CImplD =
385 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000386 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000387 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000388 }
389
390 return Redecl ? Redecl : this;
391}
392
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000393ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
394 Decl *CtxD = cast<Decl>(getDeclContext());
395
396 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
397 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
398 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
399 isInstanceMethod()))
400 return MD;
401
402 } else if (ObjCCategoryImplDecl *CImplD =
403 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000404 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000405 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
406 isInstanceMethod()))
407 return MD;
408 }
409
410 return this;
411}
412
Mike Stump11289f42009-09-09 15:08:12 +0000413void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000414 const ObjCInterfaceDecl *OID) {
415 QualType selfTy;
416 if (isInstanceMethod()) {
417 // There may be no interface context due to error in declaration
418 // of the interface (which has been reported). Recover gracefully.
419 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000420 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000421 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000422 } else {
423 selfTy = Context.getObjCIdType();
424 }
425 } else // we have a factory method.
426 selfTy = Context.getObjCClassType();
427
Mike Stump11289f42009-09-09 15:08:12 +0000428 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff04f2d142009-04-20 15:06:07 +0000429 &Context.Idents.get("self"), selfTy));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000430
Mike Stump11289f42009-09-09 15:08:12 +0000431 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
432 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000433 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000434}
435
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000436ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
437 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
438 return ID;
439 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
440 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000441 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000442 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000443
444 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000445 assert(false && "unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000446 return 0;
447}
448
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000449//===----------------------------------------------------------------------===//
450// ObjCInterfaceDecl
451//===----------------------------------------------------------------------===//
452
453ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
454 DeclContext *DC,
455 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000456 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000457 SourceLocation ClassLoc,
458 bool ForwardDecl, bool isInternal){
459 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
460 isInternal);
461}
462
463ObjCInterfaceDecl::
464ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
465 SourceLocation CLoc, bool FD, bool isInternal)
466 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
467 TypeForDecl(0), SuperClass(0),
468 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
469 ClassLoc(CLoc) {
470}
471
Mike Stump11289f42009-09-09 15:08:12 +0000472void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnerd8a47c42009-03-31 08:36:08 +0000473 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000474 if (*I) (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000475
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000476 // FIXME: CategoryList?
Mike Stump11289f42009-09-09 15:08:12 +0000477
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000478 // FIXME: Because there is no clear ownership
479 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
480 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
481 Decl::Destroy(C);
482}
483
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000484ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
485 return getASTContext().getObjCImplementation(
486 const_cast<ObjCInterfaceDecl*>(this));
487}
488
489void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
490 getASTContext().setObjCImplementation(this, ImplD);
491}
492
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000493
494/// FindCategoryDeclaration - Finds category declaration in the list of
495/// categories for this class and returns it. Name of the category is passed
496/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000497///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000498ObjCCategoryDecl *
499ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
500 for (ObjCCategoryDecl *Category = getCategoryList();
501 Category; Category = Category->getNextClassCategory())
502 if (Category->getIdentifier() == CategoryId)
503 return Category;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000504 return 0;
505}
506
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000507ObjCMethodDecl *
508ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
509 for (ObjCCategoryDecl *Category = getCategoryList();
510 Category; Category = Category->getNextClassCategory())
511 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
512 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
513 return MD;
514 return 0;
515}
516
517ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
518 for (ObjCCategoryDecl *Category = getCategoryList();
519 Category; Category = Category->getNextClassCategory())
520 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
521 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
522 return MD;
523 return 0;
524}
525
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000526/// ClassImplementsProtocol - Checks that 'lProto' protocol
527/// has been implemented in IDecl class, its super class or categories (if
528/// lookupCategory is true).
529bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
530 bool lookupCategory,
531 bool RHSIsQualifiedID) {
532 ObjCInterfaceDecl *IDecl = this;
533 // 1st, look up the class.
534 const ObjCList<ObjCProtocolDecl> &Protocols =
535 IDecl->getReferencedProtocols();
Mike Stump11289f42009-09-09 15:08:12 +0000536
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000537 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
538 E = Protocols.end(); PI != E; ++PI) {
539 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
540 return true;
541 // This is dubious and is added to be compatible with gcc. In gcc, it is
542 // also allowed assigning a protocol-qualified 'id' type to a LHS object
543 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
544 // object. This IMO, should be a bug.
545 // FIXME: Treat this as an extension, and flag this as an error when GCC
546 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000547 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000548 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
549 return true;
550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000552 // 2nd, look up the category.
553 if (lookupCategory)
554 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
555 CDecl = CDecl->getNextClassCategory()) {
556 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
557 E = CDecl->protocol_end(); PI != E; ++PI)
558 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
559 return true;
560 }
Mike Stump11289f42009-09-09 15:08:12 +0000561
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000562 // 3rd, look up the super class(s)
563 if (IDecl->getSuperClass())
564 return
565 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
566 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +0000567
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000568 return false;
569}
570
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000571//===----------------------------------------------------------------------===//
572// ObjCIvarDecl
573//===----------------------------------------------------------------------===//
574
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000575ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000576 SourceLocation L, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +0000577 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +0000578 AccessControl ac, Expr *BW,
579 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +0000580 if (DC) {
581 // Ivar's can only appear in interfaces, implementations (via synthesized
582 // properties), and class extensions (via direct declaration, or synthesized
583 // properties).
584 //
585 // FIXME: This should really be asserting this:
586 // (isa<ObjCCategoryDecl>(DC) &&
587 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
588 // but unfortunately we sometimes place ivars into non-class extension
589 // categories on error. This breaks an AST invariant, and should not be
590 // fixed.
591 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
592 isa<ObjCCategoryDecl>(DC)) &&
593 "Invalid ivar decl context!");
594 }
595
Fariborz Jahanian18722982010-07-17 00:59:30 +0000596 return new (C) ObjCIvarDecl(DC, L, Id, T, TInfo, ac, BW, synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000597}
598
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000599const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
600 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000601
Daniel Dunbar89947ea2010-04-02 21:13:59 +0000602 switch (DC->getKind()) {
603 default:
604 case ObjCCategoryImpl:
605 case ObjCProtocol:
606 assert(0 && "invalid ivar container!");
607 return 0;
608
609 // Ivars can only appear in class extension categories.
610 case ObjCCategory: {
611 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
612 assert(CD->IsClassExtension() && "invalid container for ivar!");
613 return CD->getClassInterface();
614 }
615
616 case ObjCImplementation:
617 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
618
619 case ObjCInterface:
620 return cast<ObjCInterfaceDecl>(DC);
621 }
622}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000623
624//===----------------------------------------------------------------------===//
625// ObjCAtDefsFieldDecl
626//===----------------------------------------------------------------------===//
627
628ObjCAtDefsFieldDecl
629*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
630 IdentifierInfo *Id, QualType T, Expr *BW) {
631 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
632}
633
634void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
635 this->~ObjCAtDefsFieldDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000636 C.Deallocate((void *)this);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000637}
638
639//===----------------------------------------------------------------------===//
640// ObjCProtocolDecl
641//===----------------------------------------------------------------------===//
642
643ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000644 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000645 IdentifierInfo *Id) {
646 return new (C) ObjCProtocolDecl(DC, L, Id);
647}
648
649void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner22298722009-02-20 21:35:13 +0000650 ReferencedProtocols.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000651 ObjCContainerDecl::Destroy(C);
652}
653
Steve Naroff114aecb2009-03-01 16:12:44 +0000654ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
655 ObjCProtocolDecl *PDecl = this;
656
657 if (Name == getIdentifier())
658 return PDecl;
659
660 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
661 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
662 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000663
Steve Naroff114aecb2009-03-01 16:12:44 +0000664 return NULL;
665}
666
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000667// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000668// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000669ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
670 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000671 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000673 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000674 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000675
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000676 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000677 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000678 return MethodDecl;
679 return NULL;
680}
681
682//===----------------------------------------------------------------------===//
683// ObjCClassDecl
684//===----------------------------------------------------------------------===//
685
Mike Stump11289f42009-09-09 15:08:12 +0000686ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000687 ObjCInterfaceDecl *const *Elts,
688 const SourceLocation *Locs,
689 unsigned nElts,
Chris Lattner22298722009-02-20 21:35:13 +0000690 ASTContext &C)
691 : Decl(ObjCClass, DC, L) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000692 setClassList(C, Elts, Locs, nElts);
Chris Lattner22298722009-02-20 21:35:13 +0000693}
694
Ted Kremenek9b124e12009-11-18 00:28:11 +0000695void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
696 const SourceLocation *Locs, unsigned Num) {
697 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
698 llvm::alignof<ObjCClassRef>());
699 for (unsigned i = 0; i < Num; ++i)
700 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
701
702 NumDecls = Num;
703}
Chris Lattner22298722009-02-20 21:35:13 +0000704
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000705ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
706 SourceLocation L,
707 ObjCInterfaceDecl *const *Elts,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000708 const SourceLocation *Locs,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000709 unsigned nElts) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000710 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000711}
712
713void ObjCClassDecl::Destroy(ASTContext &C) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000714 // ObjCInterfaceDecls registered with a DeclContext will get destroyed
715 // when the DeclContext is destroyed. For those created only by a forward
716 // declaration, the first @class that created the ObjCInterfaceDecl gets
717 // to destroy it.
718 // FIXME: Note that this ownership role is very brittle; a better
719 // polict is surely need in the future.
720 for (iterator I = begin(), E = end(); I !=E ; ++I) {
721 ObjCInterfaceDecl *ID = I->getInterface();
722 if (ID->isForwardDecl() && ID->getLocStart() == getLocStart())
723 ID->Destroy(C);
724 }
725
726 C.Deallocate(ForwardDecls);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000727 Decl::Destroy(C);
728}
729
Ted Kremenek49939c22009-11-18 01:26:56 +0000730SourceRange ObjCClassDecl::getSourceRange() const {
731 // FIXME: We should include the semicolon
732 assert(NumDecls);
733 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
734}
735
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000736//===----------------------------------------------------------------------===//
737// ObjCForwardProtocolDecl
738//===----------------------------------------------------------------------===//
739
Chris Lattner22298722009-02-20 21:35:13 +0000740ObjCForwardProtocolDecl::
741ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
742 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000743 const SourceLocation *Locs, ASTContext &C)
Mike Stump11289f42009-09-09 15:08:12 +0000744: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000745 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner22298722009-02-20 21:35:13 +0000746}
747
748
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000749ObjCForwardProtocolDecl *
750ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000751 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000752 ObjCProtocolDecl *const *Elts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000753 unsigned NumElts,
754 const SourceLocation *Locs) {
755 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000756}
757
758void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner22298722009-02-20 21:35:13 +0000759 ReferencedProtocols.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000760 Decl::Destroy(C);
761}
762
763//===----------------------------------------------------------------------===//
764// ObjCCategoryDecl
765//===----------------------------------------------------------------------===//
766
767ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor071676f2010-01-16 16:38:58 +0000768 SourceLocation AtLoc,
769 SourceLocation ClassNameLoc,
770 SourceLocation CategoryNameLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000771 IdentifierInfo *Id) {
Douglas Gregor071676f2010-01-16 16:38:58 +0000772 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000773}
774
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000775ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
776 return getASTContext().getObjCImplementation(
777 const_cast<ObjCCategoryDecl*>(this));
778}
779
780void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
781 getASTContext().setObjCImplementation(this, ImplD);
782}
783
784
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000785//===----------------------------------------------------------------------===//
786// ObjCCategoryImplDecl
787//===----------------------------------------------------------------------===//
788
789ObjCCategoryImplDecl *
790ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
791 SourceLocation L,IdentifierInfo *Id,
792 ObjCInterfaceDecl *ClassInterface) {
793 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
794}
795
Steve Narofff406f4d2009-10-29 21:11:04 +0000796ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000797 // The class interface might be NULL if we are working with invalid code.
798 if (const ObjCInterfaceDecl *ID = getClassInterface())
799 return ID->FindCategoryDeclaration(getIdentifier());
800 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000801}
802
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000803
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000804void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +0000805 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000806 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000807 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000808}
809
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000810void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
811 ASTContext &Ctx = getASTContext();
812
813 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +0000814 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000815 if (IFace)
816 Ctx.setObjCImplementation(IFace, ImplD);
817
Duncan Sands49c29ee2009-07-21 07:56:29 +0000818 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000819 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
820 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
821 Ctx.setObjCImplementation(CD, ImplD);
822 }
823
824 ClassInterface = IFace;
825}
826
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000827/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattneraab70d22009-02-16 19:24:31 +0000828/// properties implemented in this category @implementation block and returns
829/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000830///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000831ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000832FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
833 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000834 ObjCPropertyImplDecl *PID = *i;
835 if (PID->getPropertyIvarDecl() &&
836 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
837 return PID;
838 }
839 return 0;
840}
841
842/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
843/// added to the list of those properties @synthesized/@dynamic in this
844/// category @implementation block.
845///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000846ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000847FindPropertyImplDecl(IdentifierInfo *Id) const {
848 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000849 ObjCPropertyImplDecl *PID = *i;
850 if (PID->getPropertyDecl()->getIdentifier() == Id)
851 return PID;
852 }
853 return 0;
854}
855
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000856llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
857 const ObjCCategoryImplDecl *CID) {
858 OS << CID->getName();
859 return OS;
860}
861
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000862//===----------------------------------------------------------------------===//
863// ObjCImplementationDecl
864//===----------------------------------------------------------------------===//
865
866ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000867ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000868 SourceLocation L,
869 ObjCInterfaceDecl *ClassInterface,
870 ObjCInterfaceDecl *SuperDecl) {
871 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
872}
873
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000874llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
875 const ObjCImplementationDecl *ID) {
876 OS << ID->getName();
877 return OS;
878}
879
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000880//===----------------------------------------------------------------------===//
881// ObjCCompatibleAliasDecl
882//===----------------------------------------------------------------------===//
883
884ObjCCompatibleAliasDecl *
885ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
886 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +0000887 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000888 ObjCInterfaceDecl* AliasedClass) {
889 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
890}
891
892//===----------------------------------------------------------------------===//
893// ObjCPropertyDecl
894//===----------------------------------------------------------------------===//
895
896ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
897 SourceLocation L,
898 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000899 SourceLocation AtLoc,
John McCall339bb662010-06-04 20:50:08 +0000900 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000901 PropertyControl propControl) {
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000902 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000903}
904
905
906//===----------------------------------------------------------------------===//
907// ObjCPropertyImplDecl
908//===----------------------------------------------------------------------===//
909
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000910ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000911 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000912 SourceLocation atLoc,
913 SourceLocation L,
914 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +0000915 Kind PK,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000916 ObjCIvarDecl *ivar) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000917 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000918}
Chris Lattnered0e1642008-03-17 01:19:02 +0000919
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000920