blob: 4eee28b4682ce73ce0b4d26f1e20612ab36c7955 [file] [log] [blame]
Chris Lattner1e03a562008-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 Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattner38af2de2009-02-20 21:35:13 +000024void ObjCListBase::Destroy(ASTContext &Ctx) {
Chris Lattner4ee413b2009-02-20 21:44:01 +000025 Ctx.Deallocate(List);
Chris Lattner11e1e1a2009-02-20 21:16:26 +000026 NumElts = 0;
27 List = 0;
28}
29
Chris Lattner38af2de2009-02-20 21:35:13 +000030void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Chris Lattner11e1e1a2009-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 Lattner4ee413b2009-02-20 21:44:01 +000034
35 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000036 NumElts = Elts;
37 memcpy(List, InList, sizeof(void*)*Elts);
38}
39
40
41//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000042// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000043//===----------------------------------------------------------------------===//
44
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000045/// getIvarDecl - This method looks up an ivar in this ContextDecl.
46///
47ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000048ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000049 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000050 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000051 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
52 return ivar;
53 }
54 return 0;
55}
56
Steve Naroff0701bbb2009-01-08 17:28:14 +000057// Get the local instance method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000058ObjCMethodDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000059ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
Steve Naroff0de21fd2009-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;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000069 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000070 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
71 if (MD && MD->isInstanceMethod())
72 return MD;
73 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000074 return 0;
75}
76
77// Get the local class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000078ObjCMethodDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000079ObjCContainerDecl::getClassMethod(Selector Sel) const {
Steve Naroff0de21fd2009-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;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000089 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000090 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
91 if (MD && MD->isClassMethod())
92 return MD;
93 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000094 return 0;
95}
96
Fariborz Jahanian559c0c42008-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 Naroff93983f82009-01-11 12:47:58 +000099/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000100///
101ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000102ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
103 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
Chris Lattnerab351632009-02-20 20:59:54 +0000104 if ((*I)->getIdentifier() == PropertyId)
105 return *I;
106
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000107 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
108 if (PID) {
Chris Lattnerab351632009-02-20 20:59:54 +0000109 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
110 E = PID->protocol_end(); I != E; ++I)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000111 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000112 return P;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000113 }
114
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000115 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff09c47192009-01-09 15:36:25 +0000116 // Look through categories.
117 for (ObjCCategoryDecl *Category = OID->getCategoryList();
118 Category; Category = Category->getNextClassCategory()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000119 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000120 return P;
Steve Naroff09c47192009-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) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000125 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000126 return P;
Steve Naroff09c47192009-01-09 15:36:25 +0000127 }
128 if (OID->getSuperClass())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000129 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattnerab351632009-02-20 20:59:54 +0000130 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanianf034e9c2009-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) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000134 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000135 return P;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000136 }
137 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000138 return 0;
139}
140
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000141ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
142 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000143 ObjCInterfaceDecl* ClassDecl = this;
144 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000145 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000146 clsDeclared = ClassDecl;
147 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000148 }
149 ClassDecl = ClassDecl->getSuperClass();
150 }
151 return NULL;
152}
153
Fariborz Jahaniancd187622009-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 Lattner1e03a562008-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).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000170ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000171 ObjCInterfaceDecl* ClassDecl = this;
172 ObjCMethodDecl *MethodDecl = 0;
173
174 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000175 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000176 return MethodDecl;
177
178 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-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)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000183 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000184 return MethodDecl;
Chris Lattner3db6cae2008-07-21 18:19:38 +0000185
Chris Lattner1e03a562008-03-16 00:19:01 +0000186 // Didn't find one yet - now look through categories.
187 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
188 while (CatDecl) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000189 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000190 return MethodDecl;
Steve Naroffb79c01e2008-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)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000197 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Steve Naroffb79c01e2008-12-08 20:57:28 +0000198 return MethodDecl;
Chris Lattner1e03a562008-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).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000208ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000209 ObjCInterfaceDecl* ClassDecl = this;
210 ObjCMethodDecl *MethodDecl = 0;
211
212 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000213 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000214 return MethodDecl;
215
216 // Didn't find one yet - look through protocols.
Chris Lattner780f3292008-07-21 21:32:27 +0000217 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
218 E = ClassDecl->protocol_end(); I != E; ++I)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000219 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000220 return MethodDecl;
Chris Lattner780f3292008-07-21 21:32:27 +0000221
Chris Lattner1e03a562008-03-16 00:19:01 +0000222 // Didn't find one yet - now look through categories.
223 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
224 while (CatDecl) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000225 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000226 return MethodDecl;
Steve Naroffb5584222009-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)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000233 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Steve Naroffb5584222009-02-26 11:32:02 +0000234 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000235 CatDecl = CatDecl->getNextClassCategory();
236 }
237 ClassDecl = ClassDecl->getSuperClass();
238 }
239 return NULL;
240}
241
Chris Lattnerab351632009-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 Lattner1e03a562008-03-16 00:19:01 +0000261}
262
Chris Lattner38af2de2009-02-20 21:35:13 +0000263void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattnerab351632009-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 Lattner38af2de2009-02-20 21:35:13 +0000270 ParamInfo.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000271
272 Decl::Destroy(C);
Chris Lattner1e03a562008-03-16 00:19:01 +0000273}
274
Chris Lattnerab351632009-02-20 20:59:54 +0000275void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
276 const ObjCInterfaceDecl *OID) {
277 QualType selfTy;
278 if (isInstanceMethod()) {
279 // There may be no interface context due to error in declaration
280 // of the interface (which has been reported). Recover gracefully.
281 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000282 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000283 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000284 } else {
285 selfTy = Context.getObjCIdType();
286 }
287 } else // we have a factory method.
288 selfTy = Context.getObjCClassType();
289
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000290 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
291 &Context.Idents.get("self"), selfTy));
Chris Lattnerab351632009-02-20 20:59:54 +0000292
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000293 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
294 &Context.Idents.get("_cmd"),
295 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000296}
297
298
299
300/// getSynthesizedMethodSize - Compute size of synthesized method name
301/// as done be the rewrite.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000302///
Chris Lattnerab351632009-02-20 20:59:54 +0000303unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
304 // syntesized method name is a concatenation of -/+[class-name selector]
305 // Get length of this name.
306 unsigned length = 3; // _I_ or _C_
307 length += getClassInterface()->getNameAsString().size()+1; // extra for _
308 if (const ObjCCategoryImplDecl *CID =
309 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
310 length += CID->getNameAsString().size()+1;
311 length += getSelector().getAsString().size(); // selector name
312 return length;
313}
314
315ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
316 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
317 return ID;
318 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
319 return CD->getClassInterface();
320 if (ObjCImplementationDecl *IMD =
321 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
322 return IMD->getClassInterface();
323 if (ObjCCategoryImplDecl *CID =
324 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
325 return CID->getClassInterface();
326 assert(false && "unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000327 return 0;
328}
329
Chris Lattnerab351632009-02-20 20:59:54 +0000330//===----------------------------------------------------------------------===//
331// ObjCInterfaceDecl
332//===----------------------------------------------------------------------===//
333
334ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
335 DeclContext *DC,
336 SourceLocation atLoc,
337 IdentifierInfo *Id,
338 SourceLocation ClassLoc,
339 bool ForwardDecl, bool isInternal){
340 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
341 isInternal);
342}
343
344ObjCInterfaceDecl::
345ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
346 SourceLocation CLoc, bool FD, bool isInternal)
347 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
348 TypeForDecl(0), SuperClass(0),
349 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
350 ClassLoc(CLoc) {
351}
352
353void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnerd13d3022009-03-31 08:36:08 +0000354 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattnerab351632009-02-20 20:59:54 +0000355 if (*I) (*I)->Destroy(C);
356
Chris Lattner38af2de2009-02-20 21:35:13 +0000357 IVars.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000358 // FIXME: CategoryList?
359
360 // FIXME: Because there is no clear ownership
361 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
362 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
363 Decl::Destroy(C);
364}
365
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000366ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
367 return getASTContext().getObjCImplementation(
368 const_cast<ObjCInterfaceDecl*>(this));
369}
370
371void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
372 getASTContext().setObjCImplementation(this, ImplD);
373}
374
Chris Lattnerab351632009-02-20 20:59:54 +0000375
376/// FindCategoryDeclaration - Finds category declaration in the list of
377/// categories for this class and returns it. Name of the category is passed
378/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000379///
Chris Lattnerab351632009-02-20 20:59:54 +0000380ObjCCategoryDecl *
381ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
382 for (ObjCCategoryDecl *Category = getCategoryList();
383 Category; Category = Category->getNextClassCategory())
384 if (Category->getIdentifier() == CategoryId)
385 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000386 return 0;
387}
388
Chris Lattnerab351632009-02-20 20:59:54 +0000389//===----------------------------------------------------------------------===//
390// ObjCIvarDecl
391//===----------------------------------------------------------------------===//
392
393ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
394 SourceLocation L, IdentifierInfo *Id,
395 QualType T, AccessControl ac, Expr *BW) {
396 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
397}
398
399
400
401//===----------------------------------------------------------------------===//
402// ObjCAtDefsFieldDecl
403//===----------------------------------------------------------------------===//
404
405ObjCAtDefsFieldDecl
406*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
407 IdentifierInfo *Id, QualType T, Expr *BW) {
408 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
409}
410
411void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
412 this->~ObjCAtDefsFieldDecl();
413 C.Deallocate((void *)this);
414}
415
416//===----------------------------------------------------------------------===//
417// ObjCProtocolDecl
418//===----------------------------------------------------------------------===//
419
420ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
421 SourceLocation L,
422 IdentifierInfo *Id) {
423 return new (C) ObjCProtocolDecl(DC, L, Id);
424}
425
426void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000427 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000428 ObjCContainerDecl::Destroy(C);
429}
430
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000431ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
432 ObjCProtocolDecl *PDecl = this;
433
434 if (Name == getIdentifier())
435 return PDecl;
436
437 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
438 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
439 return PDecl;
440
441 return NULL;
442}
443
Chris Lattnerab351632009-02-20 20:59:54 +0000444// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
445// it inherited.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000446ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
Chris Lattnerab351632009-02-20 20:59:54 +0000447 ObjCMethodDecl *MethodDecl = NULL;
448
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000449 if ((MethodDecl = getInstanceMethod(Sel)))
Chris Lattnerab351632009-02-20 20:59:54 +0000450 return MethodDecl;
451
452 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000453 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattnerab351632009-02-20 20:59:54 +0000454 return MethodDecl;
455 return NULL;
456}
457
458// lookupInstanceMethod - Lookup a class method in the protocol and protocols
459// it inherited.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000460ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
Chris Lattnerab351632009-02-20 20:59:54 +0000461 ObjCMethodDecl *MethodDecl = NULL;
462
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000463 if ((MethodDecl = getClassMethod(Sel)))
Chris Lattnerab351632009-02-20 20:59:54 +0000464 return MethodDecl;
465
466 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000467 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattnerab351632009-02-20 20:59:54 +0000468 return MethodDecl;
469 return NULL;
470}
471
472//===----------------------------------------------------------------------===//
473// ObjCClassDecl
474//===----------------------------------------------------------------------===//
475
Chris Lattner38af2de2009-02-20 21:35:13 +0000476ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
477 ObjCInterfaceDecl *const *Elts, unsigned nElts,
478 ASTContext &C)
479 : Decl(ObjCClass, DC, L) {
480 ForwardDecls.set(Elts, nElts, C);
481}
482
483
Chris Lattnerab351632009-02-20 20:59:54 +0000484ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
485 SourceLocation L,
486 ObjCInterfaceDecl *const *Elts,
487 unsigned nElts) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000488 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000489}
490
491void ObjCClassDecl::Destroy(ASTContext &C) {
492
493 // FIXME: There is no clear ownership policy now for referenced
494 // ObjCInterfaceDecls. Some of them can be forward declarations that
495 // are never later defined (in which case the ObjCClassDecl owns them)
496 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
497 // we should have separate objects for forward declarations and definitions,
498 // obviating this problem. Because of this situation, referenced
499 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
500
Chris Lattner38af2de2009-02-20 21:35:13 +0000501 ForwardDecls.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000502 Decl::Destroy(C);
503}
504
505//===----------------------------------------------------------------------===//
506// ObjCForwardProtocolDecl
507//===----------------------------------------------------------------------===//
508
Chris Lattner38af2de2009-02-20 21:35:13 +0000509ObjCForwardProtocolDecl::
510ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
511 ObjCProtocolDecl *const *Elts, unsigned nElts,
512 ASTContext &C)
513: Decl(ObjCForwardProtocol, DC, L) {
514 ReferencedProtocols.set(Elts, nElts, C);
515}
516
517
Chris Lattnerab351632009-02-20 20:59:54 +0000518ObjCForwardProtocolDecl *
519ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
520 SourceLocation L,
521 ObjCProtocolDecl *const *Elts,
522 unsigned NumElts) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000523 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000524}
525
526void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000527 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000528 Decl::Destroy(C);
529}
530
531//===----------------------------------------------------------------------===//
532// ObjCCategoryDecl
533//===----------------------------------------------------------------------===//
534
535ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
536 SourceLocation L,
537 IdentifierInfo *Id) {
538 return new (C) ObjCCategoryDecl(DC, L, Id);
539}
540
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000541ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
542 return getASTContext().getObjCImplementation(
543 const_cast<ObjCCategoryDecl*>(this));
544}
545
546void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
547 getASTContext().setObjCImplementation(this, ImplD);
548}
549
550
Chris Lattnerab351632009-02-20 20:59:54 +0000551//===----------------------------------------------------------------------===//
552// ObjCCategoryImplDecl
553//===----------------------------------------------------------------------===//
554
555ObjCCategoryImplDecl *
556ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
557 SourceLocation L,IdentifierInfo *Id,
558 ObjCInterfaceDecl *ClassInterface) {
559 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
560}
561
562
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000563void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000564 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000565 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000566 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000567}
568
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000569void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
570 ASTContext &Ctx = getASTContext();
571
572 if (ObjCImplementationDecl *ImplD
573 = dyn_cast_or_null<ObjCImplementationDecl>(this))
574 if (IFace)
575 Ctx.setObjCImplementation(IFace, ImplD);
576
577 else if (ObjCCategoryImplDecl *ImplD =
578 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
579 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
580 Ctx.setObjCImplementation(CD, ImplD);
581 }
582
583 ClassInterface = IFace;
584}
585
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000586/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000587/// properties implemented in this category @implementation block and returns
588/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000589///
Chris Lattner3aa18612009-02-28 18:42:10 +0000590ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000591FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
592 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000593 ObjCPropertyImplDecl *PID = *i;
594 if (PID->getPropertyIvarDecl() &&
595 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
596 return PID;
597 }
598 return 0;
599}
600
601/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
602/// added to the list of those properties @synthesized/@dynamic in this
603/// category @implementation block.
604///
Chris Lattner3aa18612009-02-28 18:42:10 +0000605ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000606FindPropertyImplDecl(IdentifierInfo *Id) const {
607 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000608 ObjCPropertyImplDecl *PID = *i;
609 if (PID->getPropertyDecl()->getIdentifier() == Id)
610 return PID;
611 }
612 return 0;
613}
614
Chris Lattner3aa18612009-02-28 18:42:10 +0000615// getInstanceMethod - This method returns an instance method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000616// the class implementation. Unlike interfaces, we don't look outside the
617// implementation.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000618ObjCMethodDecl *ObjCImplDecl::getInstanceMethod(Selector Sel) const {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000619 // Since instance & class methods can have the same name, the loop below
620 // ensures we get the correct method.
621 //
622 // @interface Whatever
623 // - (int) class_method;
624 // + (float) class_method;
625 // @end
626 //
627 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000628 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000629 Meth != MethEnd; ++Meth) {
630 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
631 if (MD && MD->isInstanceMethod())
632 return MD;
633 }
634 return 0;
Chris Lattner1e03a562008-03-16 00:19:01 +0000635}
636
Chris Lattner3aa18612009-02-28 18:42:10 +0000637// getClassMethod - This method returns an instance method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000638// the class implementation. Unlike interfaces, we don't look outside the
639// implementation.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000640ObjCMethodDecl *ObjCImplDecl::getClassMethod(Selector Sel) const {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000641 // Since instance & class methods can have the same name, the loop below
642 // ensures we get the correct method.
643 //
644 // @interface Whatever
645 // - (int) class_method;
646 // + (float) class_method;
647 // @end
648 //
649 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000650 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000651 Meth != MethEnd; ++Meth) {
652 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
653 if (MD && MD->isClassMethod())
654 return MD;
655 }
656 return 0;
Chris Lattner1e03a562008-03-16 00:19:01 +0000657}
658
Chris Lattnerab351632009-02-20 20:59:54 +0000659//===----------------------------------------------------------------------===//
660// ObjCImplementationDecl
661//===----------------------------------------------------------------------===//
662
663ObjCImplementationDecl *
664ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
665 SourceLocation L,
666 ObjCInterfaceDecl *ClassInterface,
667 ObjCInterfaceDecl *SuperDecl) {
668 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
669}
670
Chris Lattnerab351632009-02-20 20:59:54 +0000671//===----------------------------------------------------------------------===//
672// ObjCCompatibleAliasDecl
673//===----------------------------------------------------------------------===//
674
675ObjCCompatibleAliasDecl *
676ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
677 SourceLocation L,
678 IdentifierInfo *Id,
679 ObjCInterfaceDecl* AliasedClass) {
680 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
681}
682
683//===----------------------------------------------------------------------===//
684// ObjCPropertyDecl
685//===----------------------------------------------------------------------===//
686
687ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
688 SourceLocation L,
689 IdentifierInfo *Id,
690 QualType T,
691 PropertyControl propControl) {
692 return new (C) ObjCPropertyDecl(DC, L, Id, T);
693}
694
695
696//===----------------------------------------------------------------------===//
697// ObjCPropertyImplDecl
698//===----------------------------------------------------------------------===//
699
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000700ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +0000701 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000702 SourceLocation atLoc,
703 SourceLocation L,
704 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000705 Kind PK,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000706 ObjCIvarDecl *ivar) {
Steve Naroff3e970492009-01-27 21:25:57 +0000707 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000708}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000709
Chris Lattner0ed844b2008-04-04 06:12:32 +0000710