blob: 855764fdbd31def9a2ada2413f07f426d98260dc [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
Steve Naroffab63fd62009-01-08 17:28:14 +000045// Get the local instance method declared in this interface.
Steve Naroffab63fd62009-01-08 17:28:14 +000046ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
Steve Naroffd85ba922009-02-22 19:35:57 +000047 // Since instance & class methods can have the same name, the loop below
48 // ensures we get the correct method.
49 //
50 // @interface Whatever
51 // - (int) class_method;
52 // + (float) class_method;
53 // @end
54 //
55 lookup_const_iterator Meth, MethEnd;
56 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
57 Meth != MethEnd; ++Meth) {
58 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
59 if (MD && MD->isInstanceMethod())
60 return MD;
61 }
Steve Naroffab63fd62009-01-08 17:28:14 +000062 return 0;
63}
64
65// Get the local class method declared in this interface.
66ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
Steve Naroffd85ba922009-02-22 19:35:57 +000067 // Since instance & class methods can have the same name, the loop below
68 // ensures we get the correct method.
69 //
70 // @interface Whatever
71 // - (int) class_method;
72 // + (float) class_method;
73 // @end
74 //
75 lookup_const_iterator Meth, MethEnd;
76 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
77 Meth != MethEnd; ++Meth) {
78 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
79 if (MD && MD->isClassMethod())
80 return MD;
81 }
Steve Naroffab63fd62009-01-08 17:28:14 +000082 return 0;
83}
84
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000085/// FindPropertyDeclaration - Finds declaration of the property given its name
86/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +000087/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000088///
89ObjCPropertyDecl *
Steve Naroff451f83c2009-01-09 15:36:25 +000090ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Chris Lattner13f63602009-02-20 20:59:54 +000091 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
92 if ((*I)->getIdentifier() == PropertyId)
93 return *I;
94
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000095 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
96 if (PID) {
Chris Lattner13f63602009-02-20 20:59:54 +000097 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
98 E = PID->protocol_end(); I != E; ++I)
99 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
100 return P;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000101 }
102
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000103 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +0000104 // Look through categories.
105 for (ObjCCategoryDecl *Category = OID->getCategoryList();
106 Category; Category = Category->getNextClassCategory()) {
Chris Lattner13f63602009-02-20 20:59:54 +0000107 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
108 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000109 }
110 // Look through protocols.
111 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
112 E = OID->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +0000113 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
114 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000115 }
116 if (OID->getSuperClass())
117 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattner13f63602009-02-20 20:59:54 +0000118 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000119 // Look through protocols.
120 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
121 E = OCD->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +0000122 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
123 return P;
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000124 }
125 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000126 return 0;
127}
128
Chris Lattner10318b82008-03-16 00:19:01 +0000129ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
130 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
131 ObjCInterfaceDecl* ClassDecl = this;
132 while (ClassDecl != NULL) {
133 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
134 I != E; ++I) {
135 if ((*I)->getIdentifier() == ID) {
136 clsDeclared = ClassDecl;
137 return *I;
138 }
139 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000140 // look into properties.
141 for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(),
142 E = ClassDecl->prop_end(); I != E; ++I) {
143 ObjCPropertyDecl *PDecl = (*I);
144 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
145 if (IV->getIdentifier() == ID) {
146 clsDeclared = ClassDecl;
147 return IV;
148 }
149 }
Chris Lattner10318b82008-03-16 00:19:01 +0000150 ClassDecl = ClassDecl->getSuperClass();
151 }
152 return NULL;
153}
154
155/// lookupInstanceMethod - This method returns an instance method by looking in
156/// the class, its categories, and its super classes (using a linear search).
157ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
158 ObjCInterfaceDecl* ClassDecl = this;
159 ObjCMethodDecl *MethodDecl = 0;
160
161 while (ClassDecl != NULL) {
162 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
163 return MethodDecl;
164
165 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000166 const ObjCList<ObjCProtocolDecl> &Protocols =
167 ClassDecl->getReferencedProtocols();
168 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
169 E = Protocols.end(); I != E; ++I)
Steve Naroff0ba24842009-02-26 11:32:02 +0000170 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000171 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000172
Chris Lattner10318b82008-03-16 00:19:01 +0000173 // Didn't find one yet - now look through categories.
174 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
175 while (CatDecl) {
176 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
177 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000178
179 // Didn't find one yet - look through protocols.
180 const ObjCList<ObjCProtocolDecl> &Protocols =
181 CatDecl->getReferencedProtocols();
182 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
183 E = Protocols.end(); I != E; ++I)
Steve Naroff0ba24842009-02-26 11:32:02 +0000184 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Steve Naroffaf151802008-12-08 20:57:28 +0000185 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000186 CatDecl = CatDecl->getNextClassCategory();
187 }
188 ClassDecl = ClassDecl->getSuperClass();
189 }
190 return NULL;
191}
192
193// lookupClassMethod - This method returns a class method by looking in the
194// class, its categories, and its super classes (using a linear search).
195ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
196 ObjCInterfaceDecl* ClassDecl = this;
197 ObjCMethodDecl *MethodDecl = 0;
198
199 while (ClassDecl != NULL) {
200 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
201 return MethodDecl;
202
203 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000204 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
205 E = ClassDecl->protocol_end(); I != E; ++I)
Steve Naroff0ba24842009-02-26 11:32:02 +0000206 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000207 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000208
Chris Lattner10318b82008-03-16 00:19:01 +0000209 // Didn't find one yet - now look through categories.
210 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
211 while (CatDecl) {
212 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
213 return MethodDecl;
Steve Naroff0ba24842009-02-26 11:32:02 +0000214
215 // Didn't find one yet - look through protocols.
216 const ObjCList<ObjCProtocolDecl> &Protocols =
217 CatDecl->getReferencedProtocols();
218 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
219 E = Protocols.end(); I != E; ++I)
220 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
221 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000222 CatDecl = CatDecl->getNextClassCategory();
223 }
224 ClassDecl = ClassDecl->getSuperClass();
225 }
226 return NULL;
227}
228
Chris Lattner13f63602009-02-20 20:59:54 +0000229
230
231//===----------------------------------------------------------------------===//
232// ObjCMethodDecl
233//===----------------------------------------------------------------------===//
234
235ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
236 SourceLocation beginLoc,
237 SourceLocation endLoc,
238 Selector SelInfo, QualType T,
239 DeclContext *contextDecl,
240 bool isInstance,
241 bool isVariadic,
242 bool isSynthesized,
243 ImplementationControl impControl) {
244 return new (C) ObjCMethodDecl(beginLoc, endLoc,
245 SelInfo, T, contextDecl,
246 isInstance,
247 isVariadic, isSynthesized, impControl);
Chris Lattner10318b82008-03-16 00:19:01 +0000248}
249
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000250void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattner13f63602009-02-20 20:59:54 +0000251 if (Body) Body->Destroy(C);
252 if (SelfDecl) SelfDecl->Destroy(C);
253
254 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
255 if (*I) (*I)->Destroy(C);
256
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000257 ParamInfo.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000258
259 Decl::Destroy(C);
Chris Lattner10318b82008-03-16 00:19:01 +0000260}
261
Chris Lattner13f63602009-02-20 20:59:54 +0000262void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
263 const ObjCInterfaceDecl *OID) {
264 QualType selfTy;
265 if (isInstanceMethod()) {
266 // There may be no interface context due to error in declaration
267 // of the interface (which has been reported). Recover gracefully.
268 if (OID) {
269 selfTy =Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID));
270 selfTy = Context.getPointerType(selfTy);
271 } else {
272 selfTy = Context.getObjCIdType();
273 }
274 } else // we have a factory method.
275 selfTy = Context.getObjCClassType();
276
277 SelfDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
278 &Context.Idents.get("self"), selfTy);
279
280 CmdDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
281 &Context.Idents.get("_cmd"),
282 Context.getObjCSelType());
283}
284
285
286
287/// getSynthesizedMethodSize - Compute size of synthesized method name
288/// as done be the rewrite.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000289///
Chris Lattner13f63602009-02-20 20:59:54 +0000290unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
291 // syntesized method name is a concatenation of -/+[class-name selector]
292 // Get length of this name.
293 unsigned length = 3; // _I_ or _C_
294 length += getClassInterface()->getNameAsString().size()+1; // extra for _
295 if (const ObjCCategoryImplDecl *CID =
296 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
297 length += CID->getNameAsString().size()+1;
298 length += getSelector().getAsString().size(); // selector name
299 return length;
300}
301
302ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
303 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
304 return ID;
305 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
306 return CD->getClassInterface();
307 if (ObjCImplementationDecl *IMD =
308 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
309 return IMD->getClassInterface();
310 if (ObjCCategoryImplDecl *CID =
311 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
312 return CID->getClassInterface();
313 assert(false && "unknown method context");
Fariborz Jahanian68342282008-12-05 22:32:48 +0000314 return 0;
315}
316
Chris Lattner13f63602009-02-20 20:59:54 +0000317//===----------------------------------------------------------------------===//
318// ObjCInterfaceDecl
319//===----------------------------------------------------------------------===//
320
321ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
322 DeclContext *DC,
323 SourceLocation atLoc,
324 IdentifierInfo *Id,
325 SourceLocation ClassLoc,
326 bool ForwardDecl, bool isInternal){
327 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
328 isInternal);
329}
330
331ObjCInterfaceDecl::
332ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
333 SourceLocation CLoc, bool FD, bool isInternal)
334 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
335 TypeForDecl(0), SuperClass(0),
336 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
337 ClassLoc(CLoc) {
338}
339
340void ObjCInterfaceDecl::Destroy(ASTContext &C) {
341 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
342 if (*I) (*I)->Destroy(C);
343
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000344 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000345 // FIXME: CategoryList?
346
347 // FIXME: Because there is no clear ownership
348 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
349 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
350 Decl::Destroy(C);
351}
352
353
354/// FindCategoryDeclaration - Finds category declaration in the list of
355/// categories for this class and returns it. Name of the category is passed
356/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000357///
Chris Lattner13f63602009-02-20 20:59:54 +0000358ObjCCategoryDecl *
359ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
360 for (ObjCCategoryDecl *Category = getCategoryList();
361 Category; Category = Category->getNextClassCategory())
362 if (Category->getIdentifier() == CategoryId)
363 return Category;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000364 return 0;
365}
366
Chris Lattner13f63602009-02-20 20:59:54 +0000367/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
368/// storage which matches this 'ivar'.
369///
370FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
371 const ObjCIvarDecl *IVar) {
372 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
373 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
374 DeclarationName Member = IVar->getDeclName();
375 DeclContext::lookup_result Lookup =
376 (const_cast< RecordDecl *>(RecordForDecl))->lookup(Member);
377 assert((Lookup.first != Lookup.second) && "field decl not found");
378 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
379 assert(MemberDecl && "field decl not found");
380 return MemberDecl;
381}
382
383//===----------------------------------------------------------------------===//
384// ObjCIvarDecl
385//===----------------------------------------------------------------------===//
386
387ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
388 SourceLocation L, IdentifierInfo *Id,
389 QualType T, AccessControl ac, Expr *BW) {
390 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
391}
392
393
394
395//===----------------------------------------------------------------------===//
396// ObjCAtDefsFieldDecl
397//===----------------------------------------------------------------------===//
398
399ObjCAtDefsFieldDecl
400*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
401 IdentifierInfo *Id, QualType T, Expr *BW) {
402 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
403}
404
405void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
406 this->~ObjCAtDefsFieldDecl();
407 C.Deallocate((void *)this);
408}
409
410//===----------------------------------------------------------------------===//
411// ObjCProtocolDecl
412//===----------------------------------------------------------------------===//
413
414ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
415 SourceLocation L,
416 IdentifierInfo *Id) {
417 return new (C) ObjCProtocolDecl(DC, L, Id);
418}
419
420void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000421 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000422 ObjCContainerDecl::Destroy(C);
423}
424
Steve Naroff98e71b82009-03-01 16:12:44 +0000425ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
426 ObjCProtocolDecl *PDecl = this;
427
428 if (Name == getIdentifier())
429 return PDecl;
430
431 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
432 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
433 return PDecl;
434
435 return NULL;
436}
437
Chris Lattner13f63602009-02-20 20:59:54 +0000438// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
439// it inherited.
440ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
441 ObjCMethodDecl *MethodDecl = NULL;
442
443 if ((MethodDecl = getInstanceMethod(Sel)))
444 return MethodDecl;
445
446 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
447 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
448 return MethodDecl;
449 return NULL;
450}
451
452// lookupInstanceMethod - Lookup a class method in the protocol and protocols
453// it inherited.
454ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
455 ObjCMethodDecl *MethodDecl = NULL;
456
457 if ((MethodDecl = getClassMethod(Sel)))
458 return MethodDecl;
459
460 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
461 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
462 return MethodDecl;
463 return NULL;
464}
465
466//===----------------------------------------------------------------------===//
467// ObjCClassDecl
468//===----------------------------------------------------------------------===//
469
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000470ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
471 ObjCInterfaceDecl *const *Elts, unsigned nElts,
472 ASTContext &C)
473 : Decl(ObjCClass, DC, L) {
474 ForwardDecls.set(Elts, nElts, C);
475}
476
477
Chris Lattner13f63602009-02-20 20:59:54 +0000478ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
479 SourceLocation L,
480 ObjCInterfaceDecl *const *Elts,
481 unsigned nElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000482 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000483}
484
485void ObjCClassDecl::Destroy(ASTContext &C) {
486
487 // FIXME: There is no clear ownership policy now for referenced
488 // ObjCInterfaceDecls. Some of them can be forward declarations that
489 // are never later defined (in which case the ObjCClassDecl owns them)
490 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
491 // we should have separate objects for forward declarations and definitions,
492 // obviating this problem. Because of this situation, referenced
493 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
494
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000495 ForwardDecls.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000496 Decl::Destroy(C);
497}
498
499//===----------------------------------------------------------------------===//
500// ObjCForwardProtocolDecl
501//===----------------------------------------------------------------------===//
502
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000503ObjCForwardProtocolDecl::
504ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
505 ObjCProtocolDecl *const *Elts, unsigned nElts,
506 ASTContext &C)
507: Decl(ObjCForwardProtocol, DC, L) {
508 ReferencedProtocols.set(Elts, nElts, C);
509}
510
511
Chris Lattner13f63602009-02-20 20:59:54 +0000512ObjCForwardProtocolDecl *
513ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
514 SourceLocation L,
515 ObjCProtocolDecl *const *Elts,
516 unsigned NumElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000517 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000518}
519
520void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000521 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000522 Decl::Destroy(C);
523}
524
525//===----------------------------------------------------------------------===//
526// ObjCCategoryDecl
527//===----------------------------------------------------------------------===//
528
529ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
530 SourceLocation L,
531 IdentifierInfo *Id) {
532 return new (C) ObjCCategoryDecl(DC, L, Id);
533}
534
535//===----------------------------------------------------------------------===//
536// ObjCCategoryImplDecl
537//===----------------------------------------------------------------------===//
538
539ObjCCategoryImplDecl *
540ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
541 SourceLocation L,IdentifierInfo *Id,
542 ObjCInterfaceDecl *ClassInterface) {
543 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
544}
545
546
Fariborz Jahanian68342282008-12-05 22:32:48 +0000547/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000548/// properties implemented in this category @implementation block and returns
549/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000550///
Chris Lattner649b3da2009-02-28 18:42:10 +0000551ObjCPropertyImplDecl *ObjCImplDecl::
Chris Lattner715e8482009-02-16 19:24:31 +0000552FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
553 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000554 ObjCPropertyImplDecl *PID = *i;
555 if (PID->getPropertyIvarDecl() &&
556 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
557 return PID;
558 }
559 return 0;
560}
561
562/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
563/// added to the list of those properties @synthesized/@dynamic in this
564/// category @implementation block.
565///
Chris Lattner649b3da2009-02-28 18:42:10 +0000566ObjCPropertyImplDecl *ObjCImplDecl::
Chris Lattner715e8482009-02-16 19:24:31 +0000567FindPropertyImplDecl(IdentifierInfo *Id) const {
568 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000569 ObjCPropertyImplDecl *PID = *i;
570 if (PID->getPropertyDecl()->getIdentifier() == Id)
571 return PID;
572 }
573 return 0;
574}
575
Chris Lattner649b3da2009-02-28 18:42:10 +0000576// getInstanceMethod - This method returns an instance method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000577// the class implementation. Unlike interfaces, we don't look outside the
578// implementation.
Chris Lattner649b3da2009-02-28 18:42:10 +0000579ObjCMethodDecl *ObjCImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000580 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
581 if ((*I)->getSelector() == Sel)
582 return *I;
583 return NULL;
584}
585
Chris Lattner649b3da2009-02-28 18:42:10 +0000586// getClassMethod - This method returns an instance method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000587// the class implementation. Unlike interfaces, we don't look outside the
588// implementation.
Chris Lattner649b3da2009-02-28 18:42:10 +0000589ObjCMethodDecl *ObjCImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000590 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
591 I != E; ++I)
592 if ((*I)->getSelector() == Sel)
593 return *I;
594 return NULL;
595}
596
Chris Lattner13f63602009-02-20 20:59:54 +0000597//===----------------------------------------------------------------------===//
598// ObjCImplementationDecl
599//===----------------------------------------------------------------------===//
600
601ObjCImplementationDecl *
602ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
603 SourceLocation L,
604 ObjCInterfaceDecl *ClassInterface,
605 ObjCInterfaceDecl *SuperDecl) {
606 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
607}
608
609/// Destroy - Call destructors and release memory.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000610void ObjCImplementationDecl::Destroy(ASTContext &C) {
611 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000612 Decl::Destroy(C);
613}
614
Chris Lattner13f63602009-02-20 20:59:54 +0000615//===----------------------------------------------------------------------===//
616// ObjCCompatibleAliasDecl
617//===----------------------------------------------------------------------===//
618
619ObjCCompatibleAliasDecl *
620ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
621 SourceLocation L,
622 IdentifierInfo *Id,
623 ObjCInterfaceDecl* AliasedClass) {
624 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
625}
626
627//===----------------------------------------------------------------------===//
628// ObjCPropertyDecl
629//===----------------------------------------------------------------------===//
630
631ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
632 SourceLocation L,
633 IdentifierInfo *Id,
634 QualType T,
635 PropertyControl propControl) {
636 return new (C) ObjCPropertyDecl(DC, L, Id, T);
637}
638
639
640//===----------------------------------------------------------------------===//
641// ObjCPropertyImplDecl
642//===----------------------------------------------------------------------===//
643
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000644ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000645 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000646 SourceLocation atLoc,
647 SourceLocation L,
648 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000649 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000650 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000651 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000652}
Chris Lattner44859612008-03-17 01:19:02 +0000653
Chris Lattnereee57c02008-04-04 06:12:32 +0000654