blob: e8b6ef5cccc27216fc2e11317bd6cef11fe4919e [file] [log] [blame]
Chris Lattner10318b82008-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 Dunbarde300732008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Steve Naroffd85ba922009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner10318b82008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner114add62008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner217eb8c2009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattnerdf6133b2009-02-20 21:35:13 +000024void ObjCListBase::Destroy(ASTContext &Ctx) {
Chris Lattnercf6c8282009-02-20 21:44:01 +000025 Ctx.Deallocate(List);
Chris Lattner217eb8c2009-02-20 21:16:26 +000026 NumElts = 0;
27 List = 0;
28}
29
Chris Lattnerdf6133b2009-02-20 21:35:13 +000030void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Chris Lattner217eb8c2009-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.
33
Chris Lattnercf6c8282009-02-20 21:44:01 +000034
35 List = new (Ctx) void*[Elts];
Chris Lattner217eb8c2009-02-20 21:16:26 +000036 NumElts = Elts;
37 memcpy(List, InList, sizeof(void*)*Elts);
38}
39
40
41//===----------------------------------------------------------------------===//
Chris Lattner13f63602009-02-20 20:59:54 +000042// ObjCInterfaceDecl
Chris Lattner114add62008-03-16 00:49:28 +000043//===----------------------------------------------------------------------===//
44
Fariborz Jahanian074cf972009-06-05 18:16:35 +000045/// getIvarDecl - This method looks up an ivar in this ContextDecl.
46///
47ObjCIvarDecl *
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000048ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian074cf972009-06-05 18:16:35 +000049 lookup_const_iterator Ivar, IvarEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000050 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian074cf972009-06-05 18:16:35 +000051 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
52 return ivar;
53 }
54 return 0;
55}
56
Steve Naroffab63fd62009-01-08 17:28:14 +000057// Get the local instance method declared in this interface.
Douglas Gregorc55b0b02009-04-09 21:40:53 +000058ObjCMethodDecl *
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000059ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
Steve Naroffd85ba922009-02-22 19:35:57 +000060 // Since instance & class methods can have the same name, the loop below
61 // ensures we get the correct method.
62 //
63 // @interface Whatever
64 // - (int) class_method;
65 // + (float) class_method;
66 // @end
67 //
68 lookup_const_iterator Meth, MethEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000069 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroffd85ba922009-02-22 19:35:57 +000070 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
71 if (MD && MD->isInstanceMethod())
72 return MD;
73 }
Steve Naroffab63fd62009-01-08 17:28:14 +000074 return 0;
75}
76
77// Get the local class method declared in this interface.
Douglas Gregorc55b0b02009-04-09 21:40:53 +000078ObjCMethodDecl *
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000079ObjCContainerDecl::getClassMethod(Selector Sel) const {
Steve Naroffd85ba922009-02-22 19:35:57 +000080 // Since instance & class methods can have the same name, the loop below
81 // ensures we get the correct method.
82 //
83 // @interface Whatever
84 // - (int) class_method;
85 // + (float) class_method;
86 // @end
87 //
88 lookup_const_iterator Meth, MethEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000089 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroffd85ba922009-02-22 19:35:57 +000090 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
91 if (MD && MD->isClassMethod())
92 return MD;
93 }
Steve Naroffab63fd62009-01-08 17:28:14 +000094 return 0;
95}
96
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000097/// FindPropertyDeclaration - Finds declaration of the property given its name
98/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +000099/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000100///
101ObjCPropertyDecl *
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000102ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
103 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
Chris Lattner13f63602009-02-20 20:59:54 +0000104 if ((*I)->getIdentifier() == PropertyId)
105 return *I;
106
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000107 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
108 if (PID) {
Chris Lattner13f63602009-02-20 20:59:54 +0000109 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
110 E = PID->protocol_end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000111 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000112 return P;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000113 }
114
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000115 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +0000116 // Look through categories.
117 for (ObjCCategoryDecl *Category = OID->getCategoryList();
118 Category; Category = Category->getNextClassCategory()) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000119 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000120 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000121 }
122 // Look through protocols.
123 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
124 E = OID->protocol_end(); I != E; ++I) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000125 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000126 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000127 }
128 if (OID->getSuperClass())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000129 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattner13f63602009-02-20 20:59:54 +0000130 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000131 // Look through protocols.
132 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
133 E = OCD->protocol_end(); I != E; ++I) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000134 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000135 return P;
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000136 }
137 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000138 return 0;
139}
140
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000141ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
142 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner10318b82008-03-16 00:19:01 +0000143 ObjCInterfaceDecl* ClassDecl = this;
144 while (ClassDecl != NULL) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000145 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian074cf972009-06-05 18:16:35 +0000146 clsDeclared = ClassDecl;
147 return I;
Chris Lattner10318b82008-03-16 00:19:01 +0000148 }
149 ClassDecl = ClassDecl->getSuperClass();
150 }
151 return NULL;
152}
153
Fariborz Jahanianc0672e42009-05-22 17:12:32 +0000154/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
155/// class whose name is passed as argument. If it is not one of the super classes
156/// the it returns NULL.
157ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
158 const IdentifierInfo*ICName) {
159 ObjCInterfaceDecl* ClassDecl = this;
160 while (ClassDecl != NULL) {
161 if (ClassDecl->getIdentifier() == ICName)
162 return ClassDecl;
163 ClassDecl = ClassDecl->getSuperClass();
164 }
165 return NULL;
166}
167
Chris Lattner10318b82008-03-16 00:19:01 +0000168/// lookupInstanceMethod - This method returns an instance method by looking in
169/// the class, its categories, and its super classes (using a linear search).
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000170ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
Chris Lattner10318b82008-03-16 00:19:01 +0000171 ObjCInterfaceDecl* ClassDecl = this;
172 ObjCMethodDecl *MethodDecl = 0;
173
174 while (ClassDecl != NULL) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000175 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000176 return MethodDecl;
177
178 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000179 const ObjCList<ObjCProtocolDecl> &Protocols =
180 ClassDecl->getReferencedProtocols();
181 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
182 E = Protocols.end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000183 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000184 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000185
Chris Lattner10318b82008-03-16 00:19:01 +0000186 // Didn't find one yet - now look through categories.
187 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
188 while (CatDecl) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000189 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000190 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000191
192 // Didn't find one yet - look through protocols.
193 const ObjCList<ObjCProtocolDecl> &Protocols =
194 CatDecl->getReferencedProtocols();
195 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
196 E = Protocols.end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000197 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Steve Naroffaf151802008-12-08 20:57:28 +0000198 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000199 CatDecl = CatDecl->getNextClassCategory();
200 }
201 ClassDecl = ClassDecl->getSuperClass();
202 }
203 return NULL;
204}
205
206// lookupClassMethod - This method returns a class method by looking in the
207// class, its categories, and its super classes (using a linear search).
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000208ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
Chris Lattner10318b82008-03-16 00:19:01 +0000209 ObjCInterfaceDecl* ClassDecl = this;
210 ObjCMethodDecl *MethodDecl = 0;
211
212 while (ClassDecl != NULL) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000213 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000214 return MethodDecl;
215
216 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000217 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
218 E = ClassDecl->protocol_end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000219 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000220 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000221
Chris Lattner10318b82008-03-16 00:19:01 +0000222 // Didn't find one yet - now look through categories.
223 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
224 while (CatDecl) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000225 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000226 return MethodDecl;
Steve Naroff0ba24842009-02-26 11:32:02 +0000227
228 // Didn't find one yet - look through protocols.
229 const ObjCList<ObjCProtocolDecl> &Protocols =
230 CatDecl->getReferencedProtocols();
231 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
232 E = Protocols.end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000233 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Steve Naroff0ba24842009-02-26 11:32:02 +0000234 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000235 CatDecl = CatDecl->getNextClassCategory();
236 }
237 ClassDecl = ClassDecl->getSuperClass();
238 }
239 return NULL;
240}
241
Chris Lattner13f63602009-02-20 20:59:54 +0000242
243
244//===----------------------------------------------------------------------===//
245// ObjCMethodDecl
246//===----------------------------------------------------------------------===//
247
248ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
249 SourceLocation beginLoc,
250 SourceLocation endLoc,
251 Selector SelInfo, QualType T,
252 DeclContext *contextDecl,
253 bool isInstance,
254 bool isVariadic,
255 bool isSynthesized,
256 ImplementationControl impControl) {
257 return new (C) ObjCMethodDecl(beginLoc, endLoc,
258 SelInfo, T, contextDecl,
259 isInstance,
260 isVariadic, isSynthesized, impControl);
Chris Lattner10318b82008-03-16 00:19:01 +0000261}
262
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000263void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattner13f63602009-02-20 20:59:54 +0000264 if (Body) Body->Destroy(C);
265 if (SelfDecl) SelfDecl->Destroy(C);
266
267 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
268 if (*I) (*I)->Destroy(C);
269
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000270 ParamInfo.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000271
272 Decl::Destroy(C);
Chris Lattner10318b82008-03-16 00:19:01 +0000273}
274
Argiris Kirtzidis29c33872009-07-21 00:06:36 +0000275/// \brief A definition will return its interface declaration.
276/// An interface declaration will return its definition.
277/// Otherwise it will return itself.
278ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
279 ASTContext &Ctx = getASTContext();
280 ObjCMethodDecl *Redecl = 0;
281 Decl *CtxD = cast<Decl>(getDeclContext());
282
283 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
284 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
285 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
286
287 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
288 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
289 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
290
291 } else if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(CtxD)) {
292 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
293 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
294 }
295
296 return Redecl ? Redecl : this;
297}
298
Chris Lattner13f63602009-02-20 20:59:54 +0000299void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
300 const ObjCInterfaceDecl *OID) {
301 QualType selfTy;
302 if (isInstanceMethod()) {
303 // There may be no interface context due to error in declaration
304 // of the interface (which has been reported). Recover gracefully.
305 if (OID) {
Daniel Dunbarbe1ff272009-04-22 04:34:53 +0000306 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff329ec222009-07-10 23:34:53 +0000307 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattner13f63602009-02-20 20:59:54 +0000308 } else {
309 selfTy = Context.getObjCIdType();
310 }
311 } else // we have a factory method.
312 selfTy = Context.getObjCClassType();
313
Steve Naroff79ea0e02009-04-20 15:06:07 +0000314 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
315 &Context.Idents.get("self"), selfTy));
Chris Lattner13f63602009-02-20 20:59:54 +0000316
Steve Naroff79ea0e02009-04-20 15:06:07 +0000317 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
318 &Context.Idents.get("_cmd"),
319 Context.getObjCSelType()));
Chris Lattner13f63602009-02-20 20:59:54 +0000320}
321
322
323
324/// getSynthesizedMethodSize - Compute size of synthesized method name
325/// as done be the rewrite.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000326///
Chris Lattner13f63602009-02-20 20:59:54 +0000327unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
328 // syntesized method name is a concatenation of -/+[class-name selector]
329 // Get length of this name.
330 unsigned length = 3; // _I_ or _C_
331 length += getClassInterface()->getNameAsString().size()+1; // extra for _
332 if (const ObjCCategoryImplDecl *CID =
333 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
334 length += CID->getNameAsString().size()+1;
335 length += getSelector().getAsString().size(); // selector name
336 return length;
337}
338
339ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
340 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
341 return ID;
342 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
343 return CD->getClassInterface();
344 if (ObjCImplementationDecl *IMD =
345 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
346 return IMD->getClassInterface();
347 if (ObjCCategoryImplDecl *CID =
348 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
349 return CID->getClassInterface();
350 assert(false && "unknown method context");
Fariborz Jahanian68342282008-12-05 22:32:48 +0000351 return 0;
352}
353
Chris Lattner13f63602009-02-20 20:59:54 +0000354//===----------------------------------------------------------------------===//
355// ObjCInterfaceDecl
356//===----------------------------------------------------------------------===//
357
358ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
359 DeclContext *DC,
360 SourceLocation atLoc,
361 IdentifierInfo *Id,
362 SourceLocation ClassLoc,
363 bool ForwardDecl, bool isInternal){
364 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
365 isInternal);
366}
367
368ObjCInterfaceDecl::
369ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
370 SourceLocation CLoc, bool FD, bool isInternal)
371 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
372 TypeForDecl(0), SuperClass(0),
373 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
374 ClassLoc(CLoc) {
375}
376
377void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnereff36ed2009-03-31 08:36:08 +0000378 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattner13f63602009-02-20 20:59:54 +0000379 if (*I) (*I)->Destroy(C);
380
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000381 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000382 // FIXME: CategoryList?
383
384 // FIXME: Because there is no clear ownership
385 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
386 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
387 Decl::Destroy(C);
388}
389
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000390ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
391 return getASTContext().getObjCImplementation(
392 const_cast<ObjCInterfaceDecl*>(this));
393}
394
395void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
396 getASTContext().setObjCImplementation(this, ImplD);
397}
398
Chris Lattner13f63602009-02-20 20:59:54 +0000399
400/// FindCategoryDeclaration - Finds category declaration in the list of
401/// categories for this class and returns it. Name of the category is passed
402/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000403///
Chris Lattner13f63602009-02-20 20:59:54 +0000404ObjCCategoryDecl *
405ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
406 for (ObjCCategoryDecl *Category = getCategoryList();
407 Category; Category = Category->getNextClassCategory())
408 if (Category->getIdentifier() == CategoryId)
409 return Category;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000410 return 0;
411}
412
Argiris Kirtzidis20096862009-07-21 00:06:20 +0000413ObjCMethodDecl *
414ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
415 for (ObjCCategoryDecl *Category = getCategoryList();
416 Category; Category = Category->getNextClassCategory())
417 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
418 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
419 return MD;
420 return 0;
421}
422
423ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
424 for (ObjCCategoryDecl *Category = getCategoryList();
425 Category; Category = Category->getNextClassCategory())
426 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
427 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
428 return MD;
429 return 0;
430}
431
Chris Lattner13f63602009-02-20 20:59:54 +0000432//===----------------------------------------------------------------------===//
433// ObjCIvarDecl
434//===----------------------------------------------------------------------===//
435
436ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
437 SourceLocation L, IdentifierInfo *Id,
438 QualType T, AccessControl ac, Expr *BW) {
439 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
440}
441
442
443
444//===----------------------------------------------------------------------===//
445// ObjCAtDefsFieldDecl
446//===----------------------------------------------------------------------===//
447
448ObjCAtDefsFieldDecl
449*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
450 IdentifierInfo *Id, QualType T, Expr *BW) {
451 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
452}
453
454void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
455 this->~ObjCAtDefsFieldDecl();
456 C.Deallocate((void *)this);
457}
458
459//===----------------------------------------------------------------------===//
460// ObjCProtocolDecl
461//===----------------------------------------------------------------------===//
462
463ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
464 SourceLocation L,
465 IdentifierInfo *Id) {
466 return new (C) ObjCProtocolDecl(DC, L, Id);
467}
468
469void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000470 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000471 ObjCContainerDecl::Destroy(C);
472}
473
Steve Naroff98e71b82009-03-01 16:12:44 +0000474ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
475 ObjCProtocolDecl *PDecl = this;
476
477 if (Name == getIdentifier())
478 return PDecl;
479
480 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
481 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
482 return PDecl;
483
484 return NULL;
485}
486
Chris Lattner13f63602009-02-20 20:59:54 +0000487// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
488// it inherited.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000489ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
Chris Lattner13f63602009-02-20 20:59:54 +0000490 ObjCMethodDecl *MethodDecl = NULL;
491
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000492 if ((MethodDecl = getInstanceMethod(Sel)))
Chris Lattner13f63602009-02-20 20:59:54 +0000493 return MethodDecl;
494
495 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000496 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner13f63602009-02-20 20:59:54 +0000497 return MethodDecl;
498 return NULL;
499}
500
501// lookupInstanceMethod - Lookup a class method in the protocol and protocols
502// it inherited.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000503ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
Chris Lattner13f63602009-02-20 20:59:54 +0000504 ObjCMethodDecl *MethodDecl = NULL;
505
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000506 if ((MethodDecl = getClassMethod(Sel)))
Chris Lattner13f63602009-02-20 20:59:54 +0000507 return MethodDecl;
508
509 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000510 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner13f63602009-02-20 20:59:54 +0000511 return MethodDecl;
512 return NULL;
513}
514
515//===----------------------------------------------------------------------===//
516// ObjCClassDecl
517//===----------------------------------------------------------------------===//
518
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000519ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
520 ObjCInterfaceDecl *const *Elts, unsigned nElts,
521 ASTContext &C)
522 : Decl(ObjCClass, DC, L) {
523 ForwardDecls.set(Elts, nElts, C);
524}
525
526
Chris Lattner13f63602009-02-20 20:59:54 +0000527ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
528 SourceLocation L,
529 ObjCInterfaceDecl *const *Elts,
530 unsigned nElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000531 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000532}
533
534void ObjCClassDecl::Destroy(ASTContext &C) {
535
536 // FIXME: There is no clear ownership policy now for referenced
537 // ObjCInterfaceDecls. Some of them can be forward declarations that
538 // are never later defined (in which case the ObjCClassDecl owns them)
539 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
540 // we should have separate objects for forward declarations and definitions,
541 // obviating this problem. Because of this situation, referenced
542 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
543
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000544 ForwardDecls.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000545 Decl::Destroy(C);
546}
547
548//===----------------------------------------------------------------------===//
549// ObjCForwardProtocolDecl
550//===----------------------------------------------------------------------===//
551
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000552ObjCForwardProtocolDecl::
553ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
554 ObjCProtocolDecl *const *Elts, unsigned nElts,
555 ASTContext &C)
556: Decl(ObjCForwardProtocol, DC, L) {
557 ReferencedProtocols.set(Elts, nElts, C);
558}
559
560
Chris Lattner13f63602009-02-20 20:59:54 +0000561ObjCForwardProtocolDecl *
562ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
563 SourceLocation L,
564 ObjCProtocolDecl *const *Elts,
565 unsigned NumElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000566 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000567}
568
569void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000570 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000571 Decl::Destroy(C);
572}
573
574//===----------------------------------------------------------------------===//
575// ObjCCategoryDecl
576//===----------------------------------------------------------------------===//
577
578ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
579 SourceLocation L,
580 IdentifierInfo *Id) {
581 return new (C) ObjCCategoryDecl(DC, L, Id);
582}
583
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000584ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
585 return getASTContext().getObjCImplementation(
586 const_cast<ObjCCategoryDecl*>(this));
587}
588
589void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
590 getASTContext().setObjCImplementation(this, ImplD);
591}
592
593
Chris Lattner13f63602009-02-20 20:59:54 +0000594//===----------------------------------------------------------------------===//
595// ObjCCategoryImplDecl
596//===----------------------------------------------------------------------===//
597
598ObjCCategoryImplDecl *
599ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
600 SourceLocation L,IdentifierInfo *Id,
601 ObjCInterfaceDecl *ClassInterface) {
602 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
603}
604
605
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000606void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregorbd336c52009-04-23 02:42:49 +0000607 // FIXME: The context should be correct before we get here.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000608 property->setLexicalDeclContext(this);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000609 addDecl(property);
Douglas Gregorcd19b572009-04-23 01:02:12 +0000610}
611
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000612void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
613 ASTContext &Ctx = getASTContext();
614
615 if (ObjCImplementationDecl *ImplD
Duncan Sands57f4dfe2009-07-21 07:56:29 +0000616 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000617 if (IFace)
618 Ctx.setObjCImplementation(IFace, ImplD);
619
Duncan Sands57f4dfe2009-07-21 07:56:29 +0000620 } else if (ObjCCategoryImplDecl *ImplD =
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000621 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
622 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
623 Ctx.setObjCImplementation(CD, ImplD);
624 }
625
626 ClassInterface = IFace;
627}
628
Fariborz Jahanian68342282008-12-05 22:32:48 +0000629/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000630/// properties implemented in this category @implementation block and returns
631/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000632///
Chris Lattner649b3da2009-02-28 18:42:10 +0000633ObjCPropertyImplDecl *ObjCImplDecl::
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000634FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
635 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000636 ObjCPropertyImplDecl *PID = *i;
637 if (PID->getPropertyIvarDecl() &&
638 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
639 return PID;
640 }
641 return 0;
642}
643
644/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
645/// added to the list of those properties @synthesized/@dynamic in this
646/// category @implementation block.
647///
Chris Lattner649b3da2009-02-28 18:42:10 +0000648ObjCPropertyImplDecl *ObjCImplDecl::
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000649FindPropertyImplDecl(IdentifierInfo *Id) const {
650 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000651 ObjCPropertyImplDecl *PID = *i;
652 if (PID->getPropertyDecl()->getIdentifier() == Id)
653 return PID;
654 }
655 return 0;
656}
657
Chris Lattner649b3da2009-02-28 18:42:10 +0000658// getInstanceMethod - This method returns an instance method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000659// the class implementation. Unlike interfaces, we don't look outside the
660// implementation.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000661ObjCMethodDecl *ObjCImplDecl::getInstanceMethod(Selector Sel) const {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000662 // Since instance & class methods can have the same name, the loop below
663 // ensures we get the correct method.
664 //
665 // @interface Whatever
666 // - (int) class_method;
667 // + (float) class_method;
668 // @end
669 //
670 lookup_const_iterator Meth, MethEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000671 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
Douglas Gregorcd19b572009-04-23 01:02:12 +0000672 Meth != MethEnd; ++Meth) {
673 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
674 if (MD && MD->isInstanceMethod())
675 return MD;
676 }
677 return 0;
Chris Lattner10318b82008-03-16 00:19:01 +0000678}
679
Chris Lattner649b3da2009-02-28 18:42:10 +0000680// getClassMethod - This method returns an instance method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000681// the class implementation. Unlike interfaces, we don't look outside the
682// implementation.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000683ObjCMethodDecl *ObjCImplDecl::getClassMethod(Selector Sel) const {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000684 // Since instance & class methods can have the same name, the loop below
685 // ensures we get the correct method.
686 //
687 // @interface Whatever
688 // - (int) class_method;
689 // + (float) class_method;
690 // @end
691 //
692 lookup_const_iterator Meth, MethEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000693 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
Douglas Gregorcd19b572009-04-23 01:02:12 +0000694 Meth != MethEnd; ++Meth) {
695 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
696 if (MD && MD->isClassMethod())
697 return MD;
698 }
699 return 0;
Chris Lattner10318b82008-03-16 00:19:01 +0000700}
701
Chris Lattner13f63602009-02-20 20:59:54 +0000702//===----------------------------------------------------------------------===//
703// ObjCImplementationDecl
704//===----------------------------------------------------------------------===//
705
706ObjCImplementationDecl *
707ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
708 SourceLocation L,
709 ObjCInterfaceDecl *ClassInterface,
710 ObjCInterfaceDecl *SuperDecl) {
711 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
712}
713
Chris Lattner13f63602009-02-20 20:59:54 +0000714//===----------------------------------------------------------------------===//
715// ObjCCompatibleAliasDecl
716//===----------------------------------------------------------------------===//
717
718ObjCCompatibleAliasDecl *
719ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
720 SourceLocation L,
721 IdentifierInfo *Id,
722 ObjCInterfaceDecl* AliasedClass) {
723 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
724}
725
726//===----------------------------------------------------------------------===//
727// ObjCPropertyDecl
728//===----------------------------------------------------------------------===//
729
730ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
731 SourceLocation L,
732 IdentifierInfo *Id,
733 QualType T,
734 PropertyControl propControl) {
735 return new (C) ObjCPropertyDecl(DC, L, Id, T);
736}
737
738
739//===----------------------------------------------------------------------===//
740// ObjCPropertyImplDecl
741//===----------------------------------------------------------------------===//
742
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000743ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000744 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000745 SourceLocation atLoc,
746 SourceLocation L,
747 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000748 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000749 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000750 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000751}
Chris Lattner44859612008-03-17 01:19:02 +0000752
Chris Lattnereee57c02008-04-04 06:12:32 +0000753