blob: ab6b9e1f45ad7f7d255c598cfba60f03b2c89930 [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 Jahanianafe13862010-02-23 01:26:30 +0000226/// getClassExtension - Find class extension of the given class.
227// FIXME. can speed it up, if need be.
228ObjCCategoryDecl* ObjCInterfaceDecl::getClassExtension() const {
229 const ObjCInterfaceDecl* ClassDecl = this;
230 for (ObjCCategoryDecl *CDecl = ClassDecl->getCategoryList(); CDecl;
231 CDecl = CDecl->getNextClassCategory())
232 if (CDecl->IsClassExtension())
233 return CDecl;
234 return 0;
235}
236
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000237ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
238 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner89375192008-03-16 00:19:01 +0000239 ObjCInterfaceDecl* ClassDecl = this;
240 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000241 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000242 clsDeclared = ClassDecl;
243 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000244 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000245 if (const ObjCCategoryDecl *CDecl = ClassDecl->getClassExtension())
246 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
247 clsDeclared = ClassDecl;
248 return I;
249 }
250
Chris Lattner89375192008-03-16 00:19:01 +0000251 ClassDecl = ClassDecl->getSuperClass();
252 }
253 return NULL;
254}
255
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000256/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
257/// class whose name is passed as argument. If it is not one of the super classes
258/// the it returns NULL.
259ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
260 const IdentifierInfo*ICName) {
261 ObjCInterfaceDecl* ClassDecl = this;
262 while (ClassDecl != NULL) {
263 if (ClassDecl->getIdentifier() == ICName)
264 return ClassDecl;
265 ClassDecl = ClassDecl->getSuperClass();
266 }
267 return NULL;
268}
269
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000270/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000271/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000272ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
273 bool isInstance) const {
274 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000275 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattner89375192008-03-16 00:19:01 +0000277 while (ClassDecl != NULL) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000278 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000279 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattner89375192008-03-16 00:19:01 +0000281 // Didn't find one yet - look through protocols.
Chris Lattnerd0045052008-07-21 18:19:38 +0000282 const ObjCList<ObjCProtocolDecl> &Protocols =
283 ClassDecl->getReferencedProtocols();
284 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
285 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000286 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000287 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner89375192008-03-16 00:19:01 +0000289 // Didn't find one yet - now look through categories.
290 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
291 while (CatDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000292 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000293 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000294
Steve Naroff5fd31b12008-12-08 20:57:28 +0000295 // Didn't find one yet - look through protocols.
296 const ObjCList<ObjCProtocolDecl> &Protocols =
297 CatDecl->getReferencedProtocols();
298 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
299 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000300 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroff8ec27842009-02-26 11:32:02 +0000301 return MethodDecl;
Chris Lattner89375192008-03-16 00:19:01 +0000302 CatDecl = CatDecl->getNextClassCategory();
303 }
304 ClassDecl = ClassDecl->getSuperClass();
305 }
306 return NULL;
307}
308
Steve Naroffbb69c942009-10-01 23:46:04 +0000309ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod(
310 const Selector &Sel) {
311 ObjCMethodDecl *Method = 0;
312 if (ObjCImplementationDecl *ImpDecl = getImplementation())
313 Method = ImpDecl->getInstanceMethod(Sel);
314
315 if (!Method && getSuperClass())
316 return getSuperClass()->lookupPrivateInstanceMethod(Sel);
317 return Method;
318}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000319
320//===----------------------------------------------------------------------===//
321// ObjCMethodDecl
322//===----------------------------------------------------------------------===//
323
324ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000325 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000326 SourceLocation endLoc,
327 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000328 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000329 DeclContext *contextDecl,
330 bool isInstance,
331 bool isVariadic,
332 bool isSynthesized,
333 ImplementationControl impControl) {
334 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor12852d92010-03-08 14:59:44 +0000335 SelInfo, T, ResultTInfo, contextDecl,
336 isInstance,
337 isVariadic, isSynthesized, impControl);
Chris Lattner89375192008-03-16 00:19:01 +0000338}
339
Chris Lattner22298722009-02-20 21:35:13 +0000340void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000341 if (Body) Body->Destroy(C);
342 if (SelfDecl) SelfDecl->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000344 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
345 if (*I) (*I)->Destroy(C);
346
Chris Lattner22298722009-02-20 21:35:13 +0000347 ParamInfo.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000348
349 Decl::Destroy(C);
Chris Lattner89375192008-03-16 00:19:01 +0000350}
351
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000352/// \brief A definition will return its interface declaration.
353/// An interface declaration will return its definition.
354/// Otherwise it will return itself.
355ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
356 ASTContext &Ctx = getASTContext();
357 ObjCMethodDecl *Redecl = 0;
358 Decl *CtxD = cast<Decl>(getDeclContext());
359
360 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
361 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
362 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
363
364 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
365 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
366 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
367
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000368 } else if (ObjCImplementationDecl *ImplD =
369 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000370 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
371 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000372
373 } else if (ObjCCategoryImplDecl *CImplD =
374 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000375 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000376 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000377 }
378
379 return Redecl ? Redecl : this;
380}
381
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000382ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
383 Decl *CtxD = cast<Decl>(getDeclContext());
384
385 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
386 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
387 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
388 isInstanceMethod()))
389 return MD;
390
391 } else if (ObjCCategoryImplDecl *CImplD =
392 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000393 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000394 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
395 isInstanceMethod()))
396 return MD;
397 }
398
399 return this;
400}
401
Mike Stump11289f42009-09-09 15:08:12 +0000402void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000403 const ObjCInterfaceDecl *OID) {
404 QualType selfTy;
405 if (isInstanceMethod()) {
406 // There may be no interface context due to error in declaration
407 // of the interface (which has been reported). Recover gracefully.
408 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000409 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000410 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000411 } else {
412 selfTy = Context.getObjCIdType();
413 }
414 } else // we have a factory method.
415 selfTy = Context.getObjCClassType();
416
Mike Stump11289f42009-09-09 15:08:12 +0000417 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff04f2d142009-04-20 15:06:07 +0000418 &Context.Idents.get("self"), selfTy));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000419
Mike Stump11289f42009-09-09 15:08:12 +0000420 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
421 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000422 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000423}
424
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000425ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
426 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
427 return ID;
428 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
429 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000430 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000431 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000432
433 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000434 assert(false && "unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000435 return 0;
436}
437
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000438//===----------------------------------------------------------------------===//
439// ObjCInterfaceDecl
440//===----------------------------------------------------------------------===//
441
442ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
443 DeclContext *DC,
444 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000445 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000446 SourceLocation ClassLoc,
447 bool ForwardDecl, bool isInternal){
448 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
449 isInternal);
450}
451
452ObjCInterfaceDecl::
453ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
454 SourceLocation CLoc, bool FD, bool isInternal)
455 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
456 TypeForDecl(0), SuperClass(0),
457 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
458 ClassLoc(CLoc) {
459}
460
Mike Stump11289f42009-09-09 15:08:12 +0000461void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnerd8a47c42009-03-31 08:36:08 +0000462 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000463 if (*I) (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000464
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000465 // FIXME: CategoryList?
Mike Stump11289f42009-09-09 15:08:12 +0000466
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000467 // FIXME: Because there is no clear ownership
468 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
469 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
470 Decl::Destroy(C);
471}
472
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000473ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
474 return getASTContext().getObjCImplementation(
475 const_cast<ObjCInterfaceDecl*>(this));
476}
477
478void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
479 getASTContext().setObjCImplementation(this, ImplD);
480}
481
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000482
483/// FindCategoryDeclaration - Finds category declaration in the list of
484/// categories for this class and returns it. Name of the category is passed
485/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000486///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000487ObjCCategoryDecl *
488ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
489 for (ObjCCategoryDecl *Category = getCategoryList();
490 Category; Category = Category->getNextClassCategory())
491 if (Category->getIdentifier() == CategoryId)
492 return Category;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000493 return 0;
494}
495
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000496ObjCMethodDecl *
497ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
498 for (ObjCCategoryDecl *Category = getCategoryList();
499 Category; Category = Category->getNextClassCategory())
500 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
501 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
502 return MD;
503 return 0;
504}
505
506ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
507 for (ObjCCategoryDecl *Category = getCategoryList();
508 Category; Category = Category->getNextClassCategory())
509 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
510 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
511 return MD;
512 return 0;
513}
514
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000515/// ClassImplementsProtocol - Checks that 'lProto' protocol
516/// has been implemented in IDecl class, its super class or categories (if
517/// lookupCategory is true).
518bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
519 bool lookupCategory,
520 bool RHSIsQualifiedID) {
521 ObjCInterfaceDecl *IDecl = this;
522 // 1st, look up the class.
523 const ObjCList<ObjCProtocolDecl> &Protocols =
524 IDecl->getReferencedProtocols();
Mike Stump11289f42009-09-09 15:08:12 +0000525
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000526 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
527 E = Protocols.end(); PI != E; ++PI) {
528 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
529 return true;
530 // This is dubious and is added to be compatible with gcc. In gcc, it is
531 // also allowed assigning a protocol-qualified 'id' type to a LHS object
532 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
533 // object. This IMO, should be a bug.
534 // FIXME: Treat this as an extension, and flag this as an error when GCC
535 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000536 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000537 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
538 return true;
539 }
Mike Stump11289f42009-09-09 15:08:12 +0000540
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000541 // 2nd, look up the category.
542 if (lookupCategory)
543 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
544 CDecl = CDecl->getNextClassCategory()) {
545 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
546 E = CDecl->protocol_end(); PI != E; ++PI)
547 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
548 return true;
549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000551 // 3rd, look up the super class(s)
552 if (IDecl->getSuperClass())
553 return
554 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
555 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +0000556
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +0000557 return false;
558}
559
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000560//===----------------------------------------------------------------------===//
561// ObjCIvarDecl
562//===----------------------------------------------------------------------===//
563
564ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
565 SourceLocation L, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +0000566 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000567 AccessControl ac, Expr *BW) {
John McCallbcd03502009-12-07 02:54:59 +0000568 return new (C) ObjCIvarDecl(DC, L, Id, T, TInfo, ac, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000569}
570
571
572
573//===----------------------------------------------------------------------===//
574// ObjCAtDefsFieldDecl
575//===----------------------------------------------------------------------===//
576
577ObjCAtDefsFieldDecl
578*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
579 IdentifierInfo *Id, QualType T, Expr *BW) {
580 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
581}
582
583void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
584 this->~ObjCAtDefsFieldDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000585 C.Deallocate((void *)this);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000586}
587
588//===----------------------------------------------------------------------===//
589// ObjCProtocolDecl
590//===----------------------------------------------------------------------===//
591
592ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000593 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000594 IdentifierInfo *Id) {
595 return new (C) ObjCProtocolDecl(DC, L, Id);
596}
597
598void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner22298722009-02-20 21:35:13 +0000599 ReferencedProtocols.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000600 ObjCContainerDecl::Destroy(C);
601}
602
Steve Naroff114aecb2009-03-01 16:12:44 +0000603ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
604 ObjCProtocolDecl *PDecl = this;
605
606 if (Name == getIdentifier())
607 return PDecl;
608
609 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
610 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
611 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000612
Steve Naroff114aecb2009-03-01 16:12:44 +0000613 return NULL;
614}
615
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000616// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000617// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000618ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
619 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000620 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000621
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000622 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000623 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000624
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000625 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +0000626 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000627 return MethodDecl;
628 return NULL;
629}
630
631//===----------------------------------------------------------------------===//
632// ObjCClassDecl
633//===----------------------------------------------------------------------===//
634
Mike Stump11289f42009-09-09 15:08:12 +0000635ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000636 ObjCInterfaceDecl *const *Elts,
637 const SourceLocation *Locs,
638 unsigned nElts,
Chris Lattner22298722009-02-20 21:35:13 +0000639 ASTContext &C)
640 : Decl(ObjCClass, DC, L) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000641 setClassList(C, Elts, Locs, nElts);
Chris Lattner22298722009-02-20 21:35:13 +0000642}
643
Ted Kremenek9b124e12009-11-18 00:28:11 +0000644void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
645 const SourceLocation *Locs, unsigned Num) {
646 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
647 llvm::alignof<ObjCClassRef>());
648 for (unsigned i = 0; i < Num; ++i)
649 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
650
651 NumDecls = Num;
652}
Chris Lattner22298722009-02-20 21:35:13 +0000653
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000654ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
655 SourceLocation L,
656 ObjCInterfaceDecl *const *Elts,
Ted Kremenek9b124e12009-11-18 00:28:11 +0000657 const SourceLocation *Locs,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000658 unsigned nElts) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000659 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000660}
661
662void ObjCClassDecl::Destroy(ASTContext &C) {
Ted Kremenek9b124e12009-11-18 00:28:11 +0000663 // ObjCInterfaceDecls registered with a DeclContext will get destroyed
664 // when the DeclContext is destroyed. For those created only by a forward
665 // declaration, the first @class that created the ObjCInterfaceDecl gets
666 // to destroy it.
667 // FIXME: Note that this ownership role is very brittle; a better
668 // polict is surely need in the future.
669 for (iterator I = begin(), E = end(); I !=E ; ++I) {
670 ObjCInterfaceDecl *ID = I->getInterface();
671 if (ID->isForwardDecl() && ID->getLocStart() == getLocStart())
672 ID->Destroy(C);
673 }
674
675 C.Deallocate(ForwardDecls);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000676 Decl::Destroy(C);
677}
678
Ted Kremenek49939c22009-11-18 01:26:56 +0000679SourceRange ObjCClassDecl::getSourceRange() const {
680 // FIXME: We should include the semicolon
681 assert(NumDecls);
682 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
683}
684
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000685//===----------------------------------------------------------------------===//
686// ObjCForwardProtocolDecl
687//===----------------------------------------------------------------------===//
688
Chris Lattner22298722009-02-20 21:35:13 +0000689ObjCForwardProtocolDecl::
690ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
691 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000692 const SourceLocation *Locs, ASTContext &C)
Mike Stump11289f42009-09-09 15:08:12 +0000693: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor002b6712010-01-16 15:02:53 +0000694 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner22298722009-02-20 21:35:13 +0000695}
696
697
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000698ObjCForwardProtocolDecl *
699ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000700 SourceLocation L,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000701 ObjCProtocolDecl *const *Elts,
Douglas Gregor002b6712010-01-16 15:02:53 +0000702 unsigned NumElts,
703 const SourceLocation *Locs) {
704 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000705}
706
707void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner22298722009-02-20 21:35:13 +0000708 ReferencedProtocols.Destroy(C);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000709 Decl::Destroy(C);
710}
711
712//===----------------------------------------------------------------------===//
713// ObjCCategoryDecl
714//===----------------------------------------------------------------------===//
715
716ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor071676f2010-01-16 16:38:58 +0000717 SourceLocation AtLoc,
718 SourceLocation ClassNameLoc,
719 SourceLocation CategoryNameLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000720 IdentifierInfo *Id) {
Douglas Gregor071676f2010-01-16 16:38:58 +0000721 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000722}
723
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000724ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
725 return getASTContext().getObjCImplementation(
726 const_cast<ObjCCategoryDecl*>(this));
727}
728
729void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
730 getASTContext().setObjCImplementation(this, ImplD);
731}
732
733
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000734//===----------------------------------------------------------------------===//
735// ObjCCategoryImplDecl
736//===----------------------------------------------------------------------===//
737
738ObjCCategoryImplDecl *
739ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
740 SourceLocation L,IdentifierInfo *Id,
741 ObjCInterfaceDecl *ClassInterface) {
742 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
743}
744
Steve Narofff406f4d2009-10-29 21:11:04 +0000745ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000746 // The class interface might be NULL if we are working with invalid code.
747 if (const ObjCInterfaceDecl *ID = getClassInterface())
748 return ID->FindCategoryDeclaration(getIdentifier());
749 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000750}
751
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000752
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000753void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +0000754 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000755 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000756 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +0000757}
758
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000759void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
760 ASTContext &Ctx = getASTContext();
761
762 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +0000763 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000764 if (IFace)
765 Ctx.setObjCImplementation(IFace, ImplD);
766
Duncan Sands49c29ee2009-07-21 07:56:29 +0000767 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000768 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
769 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
770 Ctx.setObjCImplementation(CD, ImplD);
771 }
772
773 ClassInterface = IFace;
774}
775
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000776/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattneraab70d22009-02-16 19:24:31 +0000777/// properties implemented in this category @implementation block and returns
778/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000779///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000780ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000781FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
782 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000783 ObjCPropertyImplDecl *PID = *i;
784 if (PID->getPropertyIvarDecl() &&
785 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
786 return PID;
787 }
788 return 0;
789}
790
791/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
792/// added to the list of those properties @synthesized/@dynamic in this
793/// category @implementation block.
794///
Chris Lattnera9ca0522009-02-28 18:42:10 +0000795ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000796FindPropertyImplDecl(IdentifierInfo *Id) const {
797 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000798 ObjCPropertyImplDecl *PID = *i;
799 if (PID->getPropertyDecl()->getIdentifier() == Id)
800 return PID;
801 }
802 return 0;
803}
804
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000805//===----------------------------------------------------------------------===//
806// ObjCImplementationDecl
807//===----------------------------------------------------------------------===//
808
809ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000810ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000811 SourceLocation L,
812 ObjCInterfaceDecl *ClassInterface,
813 ObjCInterfaceDecl *SuperDecl) {
814 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
815}
816
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000817//===----------------------------------------------------------------------===//
818// ObjCCompatibleAliasDecl
819//===----------------------------------------------------------------------===//
820
821ObjCCompatibleAliasDecl *
822ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
823 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +0000824 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000825 ObjCInterfaceDecl* AliasedClass) {
826 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
827}
828
829//===----------------------------------------------------------------------===//
830// ObjCPropertyDecl
831//===----------------------------------------------------------------------===//
832
833ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
834 SourceLocation L,
835 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000836 SourceLocation AtLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000837 QualType T,
838 PropertyControl propControl) {
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000839 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000840}
841
842
843//===----------------------------------------------------------------------===//
844// ObjCPropertyImplDecl
845//===----------------------------------------------------------------------===//
846
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000847ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000848 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000849 SourceLocation atLoc,
850 SourceLocation L,
851 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +0000852 Kind PK,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000853 ObjCIvarDecl *ivar) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000854 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +0000855}
Chris Lattnered0e1642008-03-17 01:19:02 +0000856
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000857