blob: 461bb9697fe93fd2e5ddc37a7a4d170b8bd82396 [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"
Chris Lattner10318b82008-03-16 00:19:01 +000017using namespace clang;
18
Chris Lattner114add62008-03-16 00:49:28 +000019//===----------------------------------------------------------------------===//
Chris Lattner217eb8c2009-02-20 21:16:26 +000020// ObjCListBase
21//===----------------------------------------------------------------------===//
22
Chris Lattnerdf6133b2009-02-20 21:35:13 +000023void ObjCListBase::Destroy(ASTContext &Ctx) {
Chris Lattnercf6c8282009-02-20 21:44:01 +000024 Ctx.Deallocate(List);
Chris Lattner217eb8c2009-02-20 21:16:26 +000025 NumElts = 0;
26 List = 0;
27}
28
Chris Lattnerdf6133b2009-02-20 21:35:13 +000029void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Chris Lattner217eb8c2009-02-20 21:16:26 +000030 assert(List == 0 && "Elements already set!");
31 if (Elts == 0) return; // Setting to an empty list is a noop.
32
Chris Lattnercf6c8282009-02-20 21:44:01 +000033
34 List = new (Ctx) void*[Elts];
Chris Lattner217eb8c2009-02-20 21:16:26 +000035 NumElts = Elts;
36 memcpy(List, InList, sizeof(void*)*Elts);
37}
38
39
40//===----------------------------------------------------------------------===//
Chris Lattner13f63602009-02-20 20:59:54 +000041// ObjCInterfaceDecl
Chris Lattner114add62008-03-16 00:49:28 +000042//===----------------------------------------------------------------------===//
43
Steve Naroffab63fd62009-01-08 17:28:14 +000044// Get the local instance method declared in this interface.
45// FIXME: handle overloading, instance & class methods can have the same name.
46ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
47 lookup_const_result MethodResult = lookup(Sel);
48 if (MethodResult.first)
49 return const_cast<ObjCMethodDecl*>(
Chris Lattner13f63602009-02-20 20:59:54 +000050 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
Steve Naroffab63fd62009-01-08 17:28:14 +000051 return 0;
52}
53
54// Get the local class method declared in this interface.
55ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
56 lookup_const_result MethodResult = lookup(Sel);
57 if (MethodResult.first)
58 return const_cast<ObjCMethodDecl*>(
Chris Lattner13f63602009-02-20 20:59:54 +000059 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
Steve Naroffab63fd62009-01-08 17:28:14 +000060 return 0;
61}
62
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000063/// FindPropertyDeclaration - Finds declaration of the property given its name
64/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +000065/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000066///
67ObjCPropertyDecl *
Steve Naroff451f83c2009-01-09 15:36:25 +000068ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Chris Lattner13f63602009-02-20 20:59:54 +000069 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
70 if ((*I)->getIdentifier() == PropertyId)
71 return *I;
72
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000073 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
74 if (PID) {
Chris Lattner13f63602009-02-20 20:59:54 +000075 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
76 E = PID->protocol_end(); I != E; ++I)
77 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
78 return P;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000079 }
80
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000081 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +000082 // Look through categories.
83 for (ObjCCategoryDecl *Category = OID->getCategoryList();
84 Category; Category = Category->getNextClassCategory()) {
Chris Lattner13f63602009-02-20 20:59:54 +000085 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
86 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +000087 }
88 // Look through protocols.
89 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
90 E = OID->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +000091 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
92 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +000093 }
94 if (OID->getSuperClass())
95 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattner13f63602009-02-20 20:59:54 +000096 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000097 // Look through protocols.
98 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
99 E = OCD->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +0000100 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
101 return P;
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000102 }
103 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000104 return 0;
105}
106
Chris Lattner10318b82008-03-16 00:19:01 +0000107ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
108 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
109 ObjCInterfaceDecl* ClassDecl = this;
110 while (ClassDecl != NULL) {
111 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
112 I != E; ++I) {
113 if ((*I)->getIdentifier() == ID) {
114 clsDeclared = ClassDecl;
115 return *I;
116 }
117 }
118 ClassDecl = ClassDecl->getSuperClass();
119 }
120 return NULL;
121}
122
123/// lookupInstanceMethod - This method returns an instance method by looking in
124/// the class, its categories, and its super classes (using a linear search).
125ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
126 ObjCInterfaceDecl* ClassDecl = this;
127 ObjCMethodDecl *MethodDecl = 0;
128
129 while (ClassDecl != NULL) {
130 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
131 return MethodDecl;
132
133 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000134 const ObjCList<ObjCProtocolDecl> &Protocols =
135 ClassDecl->getReferencedProtocols();
136 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
137 E = Protocols.end(); I != E; ++I)
138 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000139 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000140
Chris Lattner10318b82008-03-16 00:19:01 +0000141 // Didn't find one yet - now look through categories.
142 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
143 while (CatDecl) {
144 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
145 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000146
147 // Didn't find one yet - look through protocols.
148 const ObjCList<ObjCProtocolDecl> &Protocols =
149 CatDecl->getReferencedProtocols();
150 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
151 E = Protocols.end(); I != E; ++I)
152 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
153 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000154 CatDecl = CatDecl->getNextClassCategory();
155 }
156 ClassDecl = ClassDecl->getSuperClass();
157 }
158 return NULL;
159}
160
161// lookupClassMethod - This method returns a class method by looking in the
162// class, its categories, and its super classes (using a linear search).
163ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
164 ObjCInterfaceDecl* ClassDecl = this;
165 ObjCMethodDecl *MethodDecl = 0;
166
167 while (ClassDecl != NULL) {
168 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
169 return MethodDecl;
170
171 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000172 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
173 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000174 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000175 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000176
Chris Lattner10318b82008-03-16 00:19:01 +0000177 // Didn't find one yet - now look through categories.
178 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
179 while (CatDecl) {
180 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
181 return MethodDecl;
182 CatDecl = CatDecl->getNextClassCategory();
183 }
184 ClassDecl = ClassDecl->getSuperClass();
185 }
186 return NULL;
187}
188
Chris Lattner13f63602009-02-20 20:59:54 +0000189
190
191//===----------------------------------------------------------------------===//
192// ObjCMethodDecl
193//===----------------------------------------------------------------------===//
194
195ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
196 SourceLocation beginLoc,
197 SourceLocation endLoc,
198 Selector SelInfo, QualType T,
199 DeclContext *contextDecl,
200 bool isInstance,
201 bool isVariadic,
202 bool isSynthesized,
203 ImplementationControl impControl) {
204 return new (C) ObjCMethodDecl(beginLoc, endLoc,
205 SelInfo, T, contextDecl,
206 isInstance,
207 isVariadic, isSynthesized, impControl);
Chris Lattner10318b82008-03-16 00:19:01 +0000208}
209
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000210void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattner13f63602009-02-20 20:59:54 +0000211 if (Body) Body->Destroy(C);
212 if (SelfDecl) SelfDecl->Destroy(C);
213
214 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
215 if (*I) (*I)->Destroy(C);
216
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000217 ParamInfo.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000218
219 Decl::Destroy(C);
Chris Lattner10318b82008-03-16 00:19:01 +0000220}
221
Chris Lattner13f63602009-02-20 20:59:54 +0000222void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
223 const ObjCInterfaceDecl *OID) {
224 QualType selfTy;
225 if (isInstanceMethod()) {
226 // There may be no interface context due to error in declaration
227 // of the interface (which has been reported). Recover gracefully.
228 if (OID) {
229 selfTy =Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID));
230 selfTy = Context.getPointerType(selfTy);
231 } else {
232 selfTy = Context.getObjCIdType();
233 }
234 } else // we have a factory method.
235 selfTy = Context.getObjCClassType();
236
237 SelfDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
238 &Context.Idents.get("self"), selfTy);
239
240 CmdDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
241 &Context.Idents.get("_cmd"),
242 Context.getObjCSelType());
243}
244
245
246
247/// getSynthesizedMethodSize - Compute size of synthesized method name
248/// as done be the rewrite.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000249///
Chris Lattner13f63602009-02-20 20:59:54 +0000250unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
251 // syntesized method name is a concatenation of -/+[class-name selector]
252 // Get length of this name.
253 unsigned length = 3; // _I_ or _C_
254 length += getClassInterface()->getNameAsString().size()+1; // extra for _
255 if (const ObjCCategoryImplDecl *CID =
256 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
257 length += CID->getNameAsString().size()+1;
258 length += getSelector().getAsString().size(); // selector name
259 return length;
260}
261
262ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
263 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
264 return ID;
265 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
266 return CD->getClassInterface();
267 if (ObjCImplementationDecl *IMD =
268 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
269 return IMD->getClassInterface();
270 if (ObjCCategoryImplDecl *CID =
271 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
272 return CID->getClassInterface();
273 assert(false && "unknown method context");
Fariborz Jahanian68342282008-12-05 22:32:48 +0000274 return 0;
275}
276
Chris Lattner13f63602009-02-20 20:59:54 +0000277//===----------------------------------------------------------------------===//
278// ObjCInterfaceDecl
279//===----------------------------------------------------------------------===//
280
281ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
282 DeclContext *DC,
283 SourceLocation atLoc,
284 IdentifierInfo *Id,
285 SourceLocation ClassLoc,
286 bool ForwardDecl, bool isInternal){
287 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
288 isInternal);
289}
290
291ObjCInterfaceDecl::
292ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
293 SourceLocation CLoc, bool FD, bool isInternal)
294 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
295 TypeForDecl(0), SuperClass(0),
296 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
297 ClassLoc(CLoc) {
298}
299
300void ObjCInterfaceDecl::Destroy(ASTContext &C) {
301 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
302 if (*I) (*I)->Destroy(C);
303
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000304 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000305 // FIXME: CategoryList?
306
307 // FIXME: Because there is no clear ownership
308 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
309 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
310 Decl::Destroy(C);
311}
312
313
314/// FindCategoryDeclaration - Finds category declaration in the list of
315/// categories for this class and returns it. Name of the category is passed
316/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000317///
Chris Lattner13f63602009-02-20 20:59:54 +0000318ObjCCategoryDecl *
319ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
320 for (ObjCCategoryDecl *Category = getCategoryList();
321 Category; Category = Category->getNextClassCategory())
322 if (Category->getIdentifier() == CategoryId)
323 return Category;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000324 return 0;
325}
326
Chris Lattner13f63602009-02-20 20:59:54 +0000327/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
328/// storage which matches this 'ivar'.
329///
330FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
331 const ObjCIvarDecl *IVar) {
332 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
333 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
334 DeclarationName Member = IVar->getDeclName();
335 DeclContext::lookup_result Lookup =
336 (const_cast< RecordDecl *>(RecordForDecl))->lookup(Member);
337 assert((Lookup.first != Lookup.second) && "field decl not found");
338 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
339 assert(MemberDecl && "field decl not found");
340 return MemberDecl;
341}
342
343//===----------------------------------------------------------------------===//
344// ObjCIvarDecl
345//===----------------------------------------------------------------------===//
346
347ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
348 SourceLocation L, IdentifierInfo *Id,
349 QualType T, AccessControl ac, Expr *BW) {
350 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
351}
352
353
354
355//===----------------------------------------------------------------------===//
356// ObjCAtDefsFieldDecl
357//===----------------------------------------------------------------------===//
358
359ObjCAtDefsFieldDecl
360*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
361 IdentifierInfo *Id, QualType T, Expr *BW) {
362 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
363}
364
365void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
366 this->~ObjCAtDefsFieldDecl();
367 C.Deallocate((void *)this);
368}
369
370//===----------------------------------------------------------------------===//
371// ObjCProtocolDecl
372//===----------------------------------------------------------------------===//
373
374ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
375 SourceLocation L,
376 IdentifierInfo *Id) {
377 return new (C) ObjCProtocolDecl(DC, L, Id);
378}
379
380void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000381 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000382 ObjCContainerDecl::Destroy(C);
383}
384
385// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
386// it inherited.
387ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
388 ObjCMethodDecl *MethodDecl = NULL;
389
390 if ((MethodDecl = getInstanceMethod(Sel)))
391 return MethodDecl;
392
393 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
394 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
395 return MethodDecl;
396 return NULL;
397}
398
399// lookupInstanceMethod - Lookup a class method in the protocol and protocols
400// it inherited.
401ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
402 ObjCMethodDecl *MethodDecl = NULL;
403
404 if ((MethodDecl = getClassMethod(Sel)))
405 return MethodDecl;
406
407 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
408 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
409 return MethodDecl;
410 return NULL;
411}
412
413//===----------------------------------------------------------------------===//
414// ObjCClassDecl
415//===----------------------------------------------------------------------===//
416
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000417ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
418 ObjCInterfaceDecl *const *Elts, unsigned nElts,
419 ASTContext &C)
420 : Decl(ObjCClass, DC, L) {
421 ForwardDecls.set(Elts, nElts, C);
422}
423
424
Chris Lattner13f63602009-02-20 20:59:54 +0000425ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
426 SourceLocation L,
427 ObjCInterfaceDecl *const *Elts,
428 unsigned nElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000429 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000430}
431
432void ObjCClassDecl::Destroy(ASTContext &C) {
433
434 // FIXME: There is no clear ownership policy now for referenced
435 // ObjCInterfaceDecls. Some of them can be forward declarations that
436 // are never later defined (in which case the ObjCClassDecl owns them)
437 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
438 // we should have separate objects for forward declarations and definitions,
439 // obviating this problem. Because of this situation, referenced
440 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
441
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000442 ForwardDecls.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000443 Decl::Destroy(C);
444}
445
446//===----------------------------------------------------------------------===//
447// ObjCForwardProtocolDecl
448//===----------------------------------------------------------------------===//
449
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000450ObjCForwardProtocolDecl::
451ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
452 ObjCProtocolDecl *const *Elts, unsigned nElts,
453 ASTContext &C)
454: Decl(ObjCForwardProtocol, DC, L) {
455 ReferencedProtocols.set(Elts, nElts, C);
456}
457
458
Chris Lattner13f63602009-02-20 20:59:54 +0000459ObjCForwardProtocolDecl *
460ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
461 SourceLocation L,
462 ObjCProtocolDecl *const *Elts,
463 unsigned NumElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000464 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000465}
466
467void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000468 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000469 Decl::Destroy(C);
470}
471
472//===----------------------------------------------------------------------===//
473// ObjCCategoryDecl
474//===----------------------------------------------------------------------===//
475
476ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
477 SourceLocation L,
478 IdentifierInfo *Id) {
479 return new (C) ObjCCategoryDecl(DC, L, Id);
480}
481
482//===----------------------------------------------------------------------===//
483// ObjCCategoryImplDecl
484//===----------------------------------------------------------------------===//
485
486ObjCCategoryImplDecl *
487ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
488 SourceLocation L,IdentifierInfo *Id,
489 ObjCInterfaceDecl *ClassInterface) {
490 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
491}
492
493
Fariborz Jahanian68342282008-12-05 22:32:48 +0000494/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000495/// properties implemented in this category @implementation block and returns
496/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000497///
Chris Lattner715e8482009-02-16 19:24:31 +0000498ObjCPropertyImplDecl *ObjCCategoryImplDecl::
499FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
500 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000501 ObjCPropertyImplDecl *PID = *i;
502 if (PID->getPropertyIvarDecl() &&
503 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
504 return PID;
505 }
506 return 0;
507}
508
509/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
510/// added to the list of those properties @synthesized/@dynamic in this
511/// category @implementation block.
512///
Chris Lattner715e8482009-02-16 19:24:31 +0000513ObjCPropertyImplDecl *ObjCCategoryImplDecl::
514FindPropertyImplDecl(IdentifierInfo *Id) const {
515 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000516 ObjCPropertyImplDecl *PID = *i;
517 if (PID->getPropertyDecl()->getIdentifier() == Id)
518 return PID;
519 }
520 return 0;
521}
522
Chris Lattner10318b82008-03-16 00:19:01 +0000523// lookupInstanceMethod - This method returns an instance method by looking in
524// the class implementation. Unlike interfaces, we don't look outside the
525// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000526ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000527 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
528 if ((*I)->getSelector() == Sel)
529 return *I;
530 return NULL;
531}
532
533// lookupClassMethod - This method returns an instance method by looking in
534// the class implementation. Unlike interfaces, we don't look outside the
535// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000536ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000537 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
538 I != E; ++I)
539 if ((*I)->getSelector() == Sel)
540 return *I;
541 return NULL;
542}
543
Chris Lattner13f63602009-02-20 20:59:54 +0000544//===----------------------------------------------------------------------===//
545// ObjCImplementationDecl
546//===----------------------------------------------------------------------===//
547
548ObjCImplementationDecl *
549ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
550 SourceLocation L,
551 ObjCInterfaceDecl *ClassInterface,
552 ObjCInterfaceDecl *SuperDecl) {
553 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
554}
555
556/// Destroy - Call destructors and release memory.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000557void ObjCImplementationDecl::Destroy(ASTContext &C) {
558 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000559 Decl::Destroy(C);
560}
561
562/// getInstanceMethod - This method returns an instance method by
563/// looking in the class implementation. Unlike interfaces, we don't
564/// look outside the implementation.
565ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
566 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
567 if ((*I)->getSelector() == Sel)
568 return *I;
Chris Lattner10318b82008-03-16 00:19:01 +0000569 return NULL;
570}
571
Chris Lattner13f63602009-02-20 20:59:54 +0000572/// getClassMethod - This method returns a class method by looking in
573/// the class implementation. Unlike interfaces, we don't look outside
574/// the implementation.
575ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
576 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
577 I != E; ++I)
578 if ((*I)->getSelector() == Sel)
579 return *I;
Chris Lattner10318b82008-03-16 00:19:01 +0000580 return NULL;
581}
582
Chris Lattner13f63602009-02-20 20:59:54 +0000583/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
584/// added to the list of those properties @synthesized/@dynamic in this
585/// @implementation block.
Chris Lattner10318b82008-03-16 00:19:01 +0000586///
Chris Lattner13f63602009-02-20 20:59:54 +0000587ObjCPropertyImplDecl *ObjCImplementationDecl::
588FindPropertyImplDecl(IdentifierInfo *Id) const {
589 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
590 ObjCPropertyImplDecl *PID = *i;
591 if (PID->getPropertyDecl()->getIdentifier() == Id)
592 return PID;
593 }
Chris Lattner10318b82008-03-16 00:19:01 +0000594 return 0;
595}
Chris Lattner44859612008-03-17 01:19:02 +0000596
Chris Lattner13f63602009-02-20 20:59:54 +0000597/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
598/// properties implemented in this @implementation block and returns the
599/// implemented property that uses it.
600///
601ObjCPropertyImplDecl *ObjCImplementationDecl::
602FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
603 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
604 ObjCPropertyImplDecl *PID = *i;
605 if (PID->getPropertyIvarDecl() &&
606 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
607 return PID;
608 }
609 return 0;
610}
611
612//===----------------------------------------------------------------------===//
613// ObjCCompatibleAliasDecl
614//===----------------------------------------------------------------------===//
615
616ObjCCompatibleAliasDecl *
617ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
618 SourceLocation L,
619 IdentifierInfo *Id,
620 ObjCInterfaceDecl* AliasedClass) {
621 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
622}
623
624//===----------------------------------------------------------------------===//
625// ObjCPropertyDecl
626//===----------------------------------------------------------------------===//
627
628ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
629 SourceLocation L,
630 IdentifierInfo *Id,
631 QualType T,
632 PropertyControl propControl) {
633 return new (C) ObjCPropertyDecl(DC, L, Id, T);
634}
635
636
637//===----------------------------------------------------------------------===//
638// ObjCPropertyImplDecl
639//===----------------------------------------------------------------------===//
640
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000641ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000642 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000643 SourceLocation atLoc,
644 SourceLocation L,
645 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000646 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000647 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000648 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000649}
Chris Lattner44859612008-03-17 01:19:02 +0000650
Chris Lattnereee57c02008-04-04 06:12:32 +0000651