blob: 7964f25b30c096f89ea5c9e6faf823742e38a1cb [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
Argiris Kirtzidis7a7757b2009-07-25 22:15:22 +000057// Get the local instance/class method declared in this interface.
Douglas Gregorc55b0b02009-04-09 21:40:53 +000058ObjCMethodDecl *
Argiris Kirtzidis7a7757b2009-07-25 22:15:22 +000059ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) 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);
Argiris Kirtzidis7a7757b2009-07-25 22:15:22 +000071 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffd85ba922009-02-22 19:35:57 +000072 return MD;
73 }
Steve Naroffab63fd62009-01-08 17:28:14 +000074 return 0;
75}
76
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000077/// FindPropertyDeclaration - Finds declaration of the property given its name
78/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +000079/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000080///
81ObjCPropertyDecl *
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000082ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
83 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
Chris Lattner13f63602009-02-20 20:59:54 +000084 if ((*I)->getIdentifier() == PropertyId)
85 return *I;
86
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000087 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
88 if (PID) {
Chris Lattner13f63602009-02-20 20:59:54 +000089 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
90 E = PID->protocol_end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000091 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +000092 return P;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000093 }
94
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000095 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +000096 // Look through categories.
97 for (ObjCCategoryDecl *Category = OID->getCategoryList();
98 Category; Category = Category->getNextClassCategory()) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000099 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000100 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000101 }
102 // Look through protocols.
103 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
104 E = OID->protocol_end(); I != E; ++I) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000105 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000106 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000107 }
108 if (OID->getSuperClass())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000109 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattner13f63602009-02-20 20:59:54 +0000110 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000111 // Look through protocols.
112 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
113 E = OCD->protocol_end(); I != E; ++I) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000114 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattner13f63602009-02-20 20:59:54 +0000115 return P;
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000116 }
117 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000118 return 0;
119}
120
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000121ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
122 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner10318b82008-03-16 00:19:01 +0000123 ObjCInterfaceDecl* ClassDecl = this;
124 while (ClassDecl != NULL) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000125 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian074cf972009-06-05 18:16:35 +0000126 clsDeclared = ClassDecl;
127 return I;
Chris Lattner10318b82008-03-16 00:19:01 +0000128 }
129 ClassDecl = ClassDecl->getSuperClass();
130 }
131 return NULL;
132}
133
Fariborz Jahanianc0672e42009-05-22 17:12:32 +0000134/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
135/// class whose name is passed as argument. If it is not one of the super classes
136/// the it returns NULL.
137ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
138 const IdentifierInfo*ICName) {
139 ObjCInterfaceDecl* ClassDecl = this;
140 while (ClassDecl != NULL) {
141 if (ClassDecl->getIdentifier() == ICName)
142 return ClassDecl;
143 ClassDecl = ClassDecl->getSuperClass();
144 }
145 return NULL;
146}
147
Chris Lattner10318b82008-03-16 00:19:01 +0000148/// lookupInstanceMethod - This method returns an instance method by looking in
149/// the class, its categories, and its super classes (using a linear search).
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000150ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
Chris Lattner10318b82008-03-16 00:19:01 +0000151 ObjCInterfaceDecl* ClassDecl = this;
152 ObjCMethodDecl *MethodDecl = 0;
153
154 while (ClassDecl != NULL) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000155 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000156 return MethodDecl;
157
158 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000159 const ObjCList<ObjCProtocolDecl> &Protocols =
160 ClassDecl->getReferencedProtocols();
161 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
162 E = Protocols.end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000163 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000164 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000165
Chris Lattner10318b82008-03-16 00:19:01 +0000166 // Didn't find one yet - now look through categories.
167 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
168 while (CatDecl) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000169 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000170 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000171
172 // Didn't find one yet - look through protocols.
173 const ObjCList<ObjCProtocolDecl> &Protocols =
174 CatDecl->getReferencedProtocols();
175 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
176 E = Protocols.end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000177 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Steve Naroffaf151802008-12-08 20:57:28 +0000178 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000179 CatDecl = CatDecl->getNextClassCategory();
180 }
181 ClassDecl = ClassDecl->getSuperClass();
182 }
183 return NULL;
184}
185
186// lookupClassMethod - This method returns a class method by looking in the
187// class, its categories, and its super classes (using a linear search).
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000188ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
Chris Lattner10318b82008-03-16 00:19:01 +0000189 ObjCInterfaceDecl* ClassDecl = this;
190 ObjCMethodDecl *MethodDecl = 0;
191
192 while (ClassDecl != NULL) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000193 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000194 return MethodDecl;
195
196 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000197 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
198 E = ClassDecl->protocol_end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000199 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000200 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000201
Chris Lattner10318b82008-03-16 00:19:01 +0000202 // Didn't find one yet - now look through categories.
203 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
204 while (CatDecl) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000205 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000206 return MethodDecl;
Steve Naroff0ba24842009-02-26 11:32:02 +0000207
208 // Didn't find one yet - look through protocols.
209 const ObjCList<ObjCProtocolDecl> &Protocols =
210 CatDecl->getReferencedProtocols();
211 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
212 E = Protocols.end(); I != E; ++I)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000213 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Steve Naroff0ba24842009-02-26 11:32:02 +0000214 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000215 CatDecl = CatDecl->getNextClassCategory();
216 }
217 ClassDecl = ClassDecl->getSuperClass();
218 }
219 return NULL;
220}
221
Chris Lattner13f63602009-02-20 20:59:54 +0000222
223
224//===----------------------------------------------------------------------===//
225// ObjCMethodDecl
226//===----------------------------------------------------------------------===//
227
228ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
229 SourceLocation beginLoc,
230 SourceLocation endLoc,
231 Selector SelInfo, QualType T,
232 DeclContext *contextDecl,
233 bool isInstance,
234 bool isVariadic,
235 bool isSynthesized,
236 ImplementationControl impControl) {
237 return new (C) ObjCMethodDecl(beginLoc, endLoc,
238 SelInfo, T, contextDecl,
239 isInstance,
240 isVariadic, isSynthesized, impControl);
Chris Lattner10318b82008-03-16 00:19:01 +0000241}
242
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000243void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattner13f63602009-02-20 20:59:54 +0000244 if (Body) Body->Destroy(C);
245 if (SelfDecl) SelfDecl->Destroy(C);
246
247 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
248 if (*I) (*I)->Destroy(C);
249
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000250 ParamInfo.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000251
252 Decl::Destroy(C);
Chris Lattner10318b82008-03-16 00:19:01 +0000253}
254
Argiris Kirtzidis29c33872009-07-21 00:06:36 +0000255/// \brief A definition will return its interface declaration.
256/// An interface declaration will return its definition.
257/// Otherwise it will return itself.
258ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
259 ASTContext &Ctx = getASTContext();
260 ObjCMethodDecl *Redecl = 0;
261 Decl *CtxD = cast<Decl>(getDeclContext());
262
263 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
264 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
265 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
266
267 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
268 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
269 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
270
271 } else if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(CtxD)) {
272 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
273 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
274 }
275
276 return Redecl ? Redecl : this;
277}
278
Chris Lattner13f63602009-02-20 20:59:54 +0000279void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
280 const ObjCInterfaceDecl *OID) {
281 QualType selfTy;
282 if (isInstanceMethod()) {
283 // There may be no interface context due to error in declaration
284 // of the interface (which has been reported). Recover gracefully.
285 if (OID) {
Daniel Dunbarbe1ff272009-04-22 04:34:53 +0000286 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff329ec222009-07-10 23:34:53 +0000287 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattner13f63602009-02-20 20:59:54 +0000288 } else {
289 selfTy = Context.getObjCIdType();
290 }
291 } else // we have a factory method.
292 selfTy = Context.getObjCClassType();
293
Steve Naroff79ea0e02009-04-20 15:06:07 +0000294 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
295 &Context.Idents.get("self"), selfTy));
Chris Lattner13f63602009-02-20 20:59:54 +0000296
Steve Naroff79ea0e02009-04-20 15:06:07 +0000297 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
298 &Context.Idents.get("_cmd"),
299 Context.getObjCSelType()));
Chris Lattner13f63602009-02-20 20:59:54 +0000300}
301
302
303
304/// getSynthesizedMethodSize - Compute size of synthesized method name
305/// as done be the rewrite.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000306///
Chris Lattner13f63602009-02-20 20:59:54 +0000307unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
308 // syntesized method name is a concatenation of -/+[class-name selector]
309 // Get length of this name.
310 unsigned length = 3; // _I_ or _C_
311 length += getClassInterface()->getNameAsString().size()+1; // extra for _
312 if (const ObjCCategoryImplDecl *CID =
313 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
314 length += CID->getNameAsString().size()+1;
315 length += getSelector().getAsString().size(); // selector name
316 return length;
317}
318
319ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
320 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
321 return ID;
322 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
323 return CD->getClassInterface();
324 if (ObjCImplementationDecl *IMD =
325 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
326 return IMD->getClassInterface();
327 if (ObjCCategoryImplDecl *CID =
328 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
329 return CID->getClassInterface();
330 assert(false && "unknown method context");
Fariborz Jahanian68342282008-12-05 22:32:48 +0000331 return 0;
332}
333
Chris Lattner13f63602009-02-20 20:59:54 +0000334//===----------------------------------------------------------------------===//
335// ObjCInterfaceDecl
336//===----------------------------------------------------------------------===//
337
338ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
339 DeclContext *DC,
340 SourceLocation atLoc,
341 IdentifierInfo *Id,
342 SourceLocation ClassLoc,
343 bool ForwardDecl, bool isInternal){
344 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
345 isInternal);
346}
347
348ObjCInterfaceDecl::
349ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
350 SourceLocation CLoc, bool FD, bool isInternal)
351 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
352 TypeForDecl(0), SuperClass(0),
353 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
354 ClassLoc(CLoc) {
355}
356
357void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnereff36ed2009-03-31 08:36:08 +0000358 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattner13f63602009-02-20 20:59:54 +0000359 if (*I) (*I)->Destroy(C);
360
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000361 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000362 // FIXME: CategoryList?
363
364 // FIXME: Because there is no clear ownership
365 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
366 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
367 Decl::Destroy(C);
368}
369
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000370ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
371 return getASTContext().getObjCImplementation(
372 const_cast<ObjCInterfaceDecl*>(this));
373}
374
375void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
376 getASTContext().setObjCImplementation(this, ImplD);
377}
378
Chris Lattner13f63602009-02-20 20:59:54 +0000379
380/// FindCategoryDeclaration - Finds category declaration in the list of
381/// categories for this class and returns it. Name of the category is passed
382/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000383///
Chris Lattner13f63602009-02-20 20:59:54 +0000384ObjCCategoryDecl *
385ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
386 for (ObjCCategoryDecl *Category = getCategoryList();
387 Category; Category = Category->getNextClassCategory())
388 if (Category->getIdentifier() == CategoryId)
389 return Category;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000390 return 0;
391}
392
Argiris Kirtzidis20096862009-07-21 00:06:20 +0000393ObjCMethodDecl *
394ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
395 for (ObjCCategoryDecl *Category = getCategoryList();
396 Category; Category = Category->getNextClassCategory())
397 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
398 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
399 return MD;
400 return 0;
401}
402
403ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
404 for (ObjCCategoryDecl *Category = getCategoryList();
405 Category; Category = Category->getNextClassCategory())
406 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
407 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
408 return MD;
409 return 0;
410}
411
Chris Lattner13f63602009-02-20 20:59:54 +0000412//===----------------------------------------------------------------------===//
413// ObjCIvarDecl
414//===----------------------------------------------------------------------===//
415
416ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
417 SourceLocation L, IdentifierInfo *Id,
418 QualType T, AccessControl ac, Expr *BW) {
419 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
420}
421
422
423
424//===----------------------------------------------------------------------===//
425// ObjCAtDefsFieldDecl
426//===----------------------------------------------------------------------===//
427
428ObjCAtDefsFieldDecl
429*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
430 IdentifierInfo *Id, QualType T, Expr *BW) {
431 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
432}
433
434void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
435 this->~ObjCAtDefsFieldDecl();
436 C.Deallocate((void *)this);
437}
438
439//===----------------------------------------------------------------------===//
440// ObjCProtocolDecl
441//===----------------------------------------------------------------------===//
442
443ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
444 SourceLocation L,
445 IdentifierInfo *Id) {
446 return new (C) ObjCProtocolDecl(DC, L, Id);
447}
448
449void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000450 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000451 ObjCContainerDecl::Destroy(C);
452}
453
Steve Naroff98e71b82009-03-01 16:12:44 +0000454ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
455 ObjCProtocolDecl *PDecl = this;
456
457 if (Name == getIdentifier())
458 return PDecl;
459
460 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
461 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
462 return PDecl;
463
464 return NULL;
465}
466
Argiris Kirtzidisaba400c2009-07-25 22:15:38 +0000467// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattner13f63602009-02-20 20:59:54 +0000468// it inherited.
Argiris Kirtzidisaba400c2009-07-25 22:15:38 +0000469ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
470 bool isInstance) const {
Chris Lattner13f63602009-02-20 20:59:54 +0000471 ObjCMethodDecl *MethodDecl = NULL;
472
Argiris Kirtzidisaba400c2009-07-25 22:15:38 +0000473 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattner13f63602009-02-20 20:59:54 +0000474 return MethodDecl;
475
476 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argiris Kirtzidisaba400c2009-07-25 22:15:38 +0000477 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner13f63602009-02-20 20:59:54 +0000478 return MethodDecl;
479 return NULL;
480}
481
482//===----------------------------------------------------------------------===//
483// ObjCClassDecl
484//===----------------------------------------------------------------------===//
485
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000486ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
487 ObjCInterfaceDecl *const *Elts, unsigned nElts,
488 ASTContext &C)
489 : Decl(ObjCClass, DC, L) {
490 ForwardDecls.set(Elts, nElts, C);
491}
492
493
Chris Lattner13f63602009-02-20 20:59:54 +0000494ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
495 SourceLocation L,
496 ObjCInterfaceDecl *const *Elts,
497 unsigned nElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000498 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000499}
500
501void ObjCClassDecl::Destroy(ASTContext &C) {
502
503 // FIXME: There is no clear ownership policy now for referenced
504 // ObjCInterfaceDecls. Some of them can be forward declarations that
505 // are never later defined (in which case the ObjCClassDecl owns them)
506 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
507 // we should have separate objects for forward declarations and definitions,
508 // obviating this problem. Because of this situation, referenced
509 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
510
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000511 ForwardDecls.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000512 Decl::Destroy(C);
513}
514
515//===----------------------------------------------------------------------===//
516// ObjCForwardProtocolDecl
517//===----------------------------------------------------------------------===//
518
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000519ObjCForwardProtocolDecl::
520ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
521 ObjCProtocolDecl *const *Elts, unsigned nElts,
522 ASTContext &C)
523: Decl(ObjCForwardProtocol, DC, L) {
524 ReferencedProtocols.set(Elts, nElts, C);
525}
526
527
Chris Lattner13f63602009-02-20 20:59:54 +0000528ObjCForwardProtocolDecl *
529ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
530 SourceLocation L,
531 ObjCProtocolDecl *const *Elts,
532 unsigned NumElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000533 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000534}
535
536void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000537 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000538 Decl::Destroy(C);
539}
540
541//===----------------------------------------------------------------------===//
542// ObjCCategoryDecl
543//===----------------------------------------------------------------------===//
544
545ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
546 SourceLocation L,
547 IdentifierInfo *Id) {
548 return new (C) ObjCCategoryDecl(DC, L, Id);
549}
550
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000551ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
552 return getASTContext().getObjCImplementation(
553 const_cast<ObjCCategoryDecl*>(this));
554}
555
556void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
557 getASTContext().setObjCImplementation(this, ImplD);
558}
559
560
Chris Lattner13f63602009-02-20 20:59:54 +0000561//===----------------------------------------------------------------------===//
562// ObjCCategoryImplDecl
563//===----------------------------------------------------------------------===//
564
565ObjCCategoryImplDecl *
566ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
567 SourceLocation L,IdentifierInfo *Id,
568 ObjCInterfaceDecl *ClassInterface) {
569 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
570}
571
572
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000573void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregorbd336c52009-04-23 02:42:49 +0000574 // FIXME: The context should be correct before we get here.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000575 property->setLexicalDeclContext(this);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000576 addDecl(property);
Douglas Gregorcd19b572009-04-23 01:02:12 +0000577}
578
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000579void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
580 ASTContext &Ctx = getASTContext();
581
582 if (ObjCImplementationDecl *ImplD
Duncan Sands57f4dfe2009-07-21 07:56:29 +0000583 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000584 if (IFace)
585 Ctx.setObjCImplementation(IFace, ImplD);
586
Duncan Sands57f4dfe2009-07-21 07:56:29 +0000587 } else if (ObjCCategoryImplDecl *ImplD =
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000588 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
589 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
590 Ctx.setObjCImplementation(CD, ImplD);
591 }
592
593 ClassInterface = IFace;
594}
595
Fariborz Jahanian68342282008-12-05 22:32:48 +0000596/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000597/// properties implemented in this category @implementation block and returns
598/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000599///
Chris Lattner649b3da2009-02-28 18:42:10 +0000600ObjCPropertyImplDecl *ObjCImplDecl::
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000601FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
602 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000603 ObjCPropertyImplDecl *PID = *i;
604 if (PID->getPropertyIvarDecl() &&
605 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
606 return PID;
607 }
608 return 0;
609}
610
611/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
612/// added to the list of those properties @synthesized/@dynamic in this
613/// category @implementation block.
614///
Chris Lattner649b3da2009-02-28 18:42:10 +0000615ObjCPropertyImplDecl *ObjCImplDecl::
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000616FindPropertyImplDecl(IdentifierInfo *Id) const {
617 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000618 ObjCPropertyImplDecl *PID = *i;
619 if (PID->getPropertyDecl()->getIdentifier() == Id)
620 return PID;
621 }
622 return 0;
623}
624
Chris Lattner649b3da2009-02-28 18:42:10 +0000625// getInstanceMethod - This method returns an instance method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000626// the class implementation. Unlike interfaces, we don't look outside the
627// implementation.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000628ObjCMethodDecl *ObjCImplDecl::getInstanceMethod(Selector Sel) const {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000629 // Since instance & class methods can have the same name, the loop below
630 // ensures we get the correct method.
631 //
632 // @interface Whatever
633 // - (int) class_method;
634 // + (float) class_method;
635 // @end
636 //
637 lookup_const_iterator Meth, MethEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000638 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
Douglas Gregorcd19b572009-04-23 01:02:12 +0000639 Meth != MethEnd; ++Meth) {
640 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
641 if (MD && MD->isInstanceMethod())
642 return MD;
643 }
644 return 0;
Chris Lattner10318b82008-03-16 00:19:01 +0000645}
646
Chris Lattner649b3da2009-02-28 18:42:10 +0000647// getClassMethod - This method returns an instance method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000648// the class implementation. Unlike interfaces, we don't look outside the
649// implementation.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000650ObjCMethodDecl *ObjCImplDecl::getClassMethod(Selector Sel) const {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000651 // Since instance & class methods can have the same name, the loop below
652 // ensures we get the correct method.
653 //
654 // @interface Whatever
655 // - (int) class_method;
656 // + (float) class_method;
657 // @end
658 //
659 lookup_const_iterator Meth, MethEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000660 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
Douglas Gregorcd19b572009-04-23 01:02:12 +0000661 Meth != MethEnd; ++Meth) {
662 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
663 if (MD && MD->isClassMethod())
664 return MD;
665 }
666 return 0;
Chris Lattner10318b82008-03-16 00:19:01 +0000667}
668
Chris Lattner13f63602009-02-20 20:59:54 +0000669//===----------------------------------------------------------------------===//
670// ObjCImplementationDecl
671//===----------------------------------------------------------------------===//
672
673ObjCImplementationDecl *
674ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
675 SourceLocation L,
676 ObjCInterfaceDecl *ClassInterface,
677 ObjCInterfaceDecl *SuperDecl) {
678 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
679}
680
Chris Lattner13f63602009-02-20 20:59:54 +0000681//===----------------------------------------------------------------------===//
682// ObjCCompatibleAliasDecl
683//===----------------------------------------------------------------------===//
684
685ObjCCompatibleAliasDecl *
686ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
687 SourceLocation L,
688 IdentifierInfo *Id,
689 ObjCInterfaceDecl* AliasedClass) {
690 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
691}
692
693//===----------------------------------------------------------------------===//
694// ObjCPropertyDecl
695//===----------------------------------------------------------------------===//
696
697ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
698 SourceLocation L,
699 IdentifierInfo *Id,
700 QualType T,
701 PropertyControl propControl) {
702 return new (C) ObjCPropertyDecl(DC, L, Id, T);
703}
704
705
706//===----------------------------------------------------------------------===//
707// ObjCPropertyImplDecl
708//===----------------------------------------------------------------------===//
709
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000710ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000711 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000712 SourceLocation atLoc,
713 SourceLocation L,
714 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000715 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000716 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000717 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000718}
Chris Lattner44859612008-03-17 01:19:02 +0000719
Chris Lattnereee57c02008-04-04 06:12:32 +0000720