blob: 2ae7ccc8165139ed7d950fb0be0fad566f501a87 [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//===----------------------------------------------------------------------===//
20// ObjC Decl Allocation/Deallocation Method Implementations
21//===----------------------------------------------------------------------===//
22
Chris Lattnereee57c02008-04-04 06:12:32 +000023ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
24 SourceLocation beginLoc,
Chris Lattner114add62008-03-16 00:49:28 +000025 SourceLocation endLoc,
26 Selector SelInfo, QualType T,
Steve Naroffab63fd62009-01-08 17:28:14 +000027 DeclContext *contextDecl,
Daniel Dunbard8bd6822008-08-20 18:02:42 +000028 bool isInstance,
Chris Lattner114add62008-03-16 00:49:28 +000029 bool isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +000030 bool isSynthesized,
Chris Lattnerf7355832008-03-16 00:58:16 +000031 ImplementationControl impControl) {
Steve Naroff5abb0282009-01-27 21:25:57 +000032 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Chris Lattnereee57c02008-04-04 06:12:32 +000033 SelInfo, T, contextDecl,
Daniel Dunbard8bd6822008-08-20 18:02:42 +000034 isInstance,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +000035 isVariadic, isSynthesized, impControl);
Chris Lattner0db541b2008-03-16 01:15:50 +000036}
Chris Lattner114add62008-03-16 00:49:28 +000037
Ted Kremenek8c945b12008-06-06 16:45:15 +000038void ObjCMethodDecl::Destroy(ASTContext& C) {
39 if (Body) Body->Destroy(C);
40 if (SelfDecl) SelfDecl->Destroy(C);
41
42 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
43 if (*I) (*I)->Destroy(C);
Chris Lattnerae538882009-02-20 05:54:35 +000044
Chris Lattner9408eb12009-02-20 06:23:21 +000045 ParamInfo.clear();
Chris Lattnerae538882009-02-20 05:54:35 +000046
Ted Kremenek8c945b12008-06-06 16:45:15 +000047 Decl::Destroy(C);
48}
49
Chris Lattner3e27e632009-02-20 06:03:09 +000050
Chris Lattnereee57c02008-04-04 06:12:32 +000051ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000052 DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000053 SourceLocation atLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000054 IdentifierInfo *Id,
55 SourceLocation ClassLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000056 bool ForwardDecl, bool isInternal){
Steve Naroff5abb0282009-01-27 21:25:57 +000057 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
Chris Lattner0db541b2008-03-16 01:15:50 +000058 isInternal);
59}
60
Chris Lattner3e27e632009-02-20 06:03:09 +000061ObjCInterfaceDecl::
62ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
63 SourceLocation CLoc, bool FD, bool isInternal)
64 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
Chris Lattner922132e2009-02-20 06:10:45 +000065 TypeForDecl(0), SuperClass(0),
Chris Lattner3e27e632009-02-20 06:03:09 +000066 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
67 ClassLoc(CLoc) {
Steve Naroffd9d4d502009-01-07 17:57:40 +000068}
69
Chris Lattner3e27e632009-02-20 06:03:09 +000070void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Ted Kremenek8c945b12008-06-06 16:45:15 +000071 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
72 if (*I) (*I)->Destroy(C);
73
Chris Lattner922132e2009-02-20 06:10:45 +000074 IVars.clear();
Chris Lattner3e27e632009-02-20 06:03:09 +000075 // FIXME: CategoryList?
76
Ted Kremenek22db1602008-06-06 17:21:42 +000077 // FIXME: Because there is no clear ownership
78 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
79 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
Ted Kremenek8c945b12008-06-06 16:45:15 +000080 Decl::Destroy(C);
81}
82
83
Argiris Kirtzidis96e79bf2009-02-17 20:20:37 +000084ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
85 SourceLocation L, IdentifierInfo *Id,
86 QualType T, AccessControl ac, Expr *BW) {
87 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
Chris Lattner114add62008-03-16 00:49:28 +000088}
89
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000090
91ObjCAtDefsFieldDecl
Douglas Gregor8acb7272008-12-11 16:49:14 +000092*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000093 IdentifierInfo *Id, QualType T, Expr *BW) {
Steve Naroff5abb0282009-01-27 21:25:57 +000094 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000095}
96
97void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
98 this->~ObjCAtDefsFieldDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000099 C.Deallocate((void *)this);
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000100}
101
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000102ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000103 SourceLocation L,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000104 IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000105 return new (C) ObjCProtocolDecl(DC, L, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +0000106}
107
Chris Lattnerae538882009-02-20 05:54:35 +0000108void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38d2ff22009-02-20 17:53:35 +0000109 ReferencedProtocols.clear();
Chris Lattnerae538882009-02-20 05:54:35 +0000110 ObjCContainerDecl::Destroy(C);
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000111}
112
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000113
Chris Lattnerae538882009-02-20 05:54:35 +0000114
115
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000116ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000117 SourceLocation L,
Chris Lattner6a028742009-02-20 18:04:31 +0000118 ObjCInterfaceDecl *const *Elts,
119 unsigned nElts) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000120 return new (C) ObjCClassDecl(DC, L, Elts, nElts);
Chris Lattnere29dc832008-03-16 20:34:23 +0000121}
122
Chris Lattner3e27e632009-02-20 06:03:09 +0000123void ObjCClassDecl::Destroy(ASTContext &C) {
Ted Kremenekff291622008-06-06 20:11:53 +0000124
125 // FIXME: There is no clear ownership policy now for referenced
126 // ObjCInterfaceDecls. Some of them can be forward declarations that
127 // are never later defined (in which case the ObjCClassDecl owns them)
128 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
129 // we should have separate objects for forward declarations and definitions,
130 // obviating this problem. Because of this situation, referenced
131 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
132
Chris Lattner6a028742009-02-20 18:04:31 +0000133 ForwardDecls.clear();
Ted Kremenekff291622008-06-06 20:11:53 +0000134 Decl::Destroy(C);
135}
136
Chris Lattnere29dc832008-03-16 20:34:23 +0000137ObjCForwardProtocolDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000138ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000139 SourceLocation L,
Chris Lattnerad446cf2009-02-20 18:10:37 +0000140 ObjCProtocolDecl *const *Elts,
141 unsigned NumElts) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000142 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts);
Chris Lattnere29dc832008-03-16 20:34:23 +0000143}
144
Chris Lattner3e27e632009-02-20 06:03:09 +0000145ObjCForwardProtocolDecl::
146ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
Chris Lattnerad446cf2009-02-20 18:10:37 +0000147 ObjCProtocolDecl *const *Elts, unsigned nElts)
Chris Lattner3e27e632009-02-20 06:03:09 +0000148 : Decl(ObjCForwardProtocol, DC, L) {
Chris Lattnerad446cf2009-02-20 18:10:37 +0000149 ReferencedProtocols.set(Elts, nElts);
Chris Lattner3e27e632009-02-20 06:03:09 +0000150}
151
152void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerad446cf2009-02-20 18:10:37 +0000153 ReferencedProtocols.clear();
154 Decl::Destroy(C);
Ted Kremenek1e5e0bf2008-06-06 21:05:33 +0000155}
156
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000157ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000158 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000159 IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000160 return new (C) ObjCCategoryDecl(DC, L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000161}
162
Chris Lattner1b6de332008-03-16 20:53:07 +0000163ObjCCategoryImplDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000164ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000165 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000166 ObjCInterfaceDecl *ClassInterface) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000167 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
Chris Lattner1b6de332008-03-16 20:53:07 +0000168}
169
170ObjCImplementationDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000171ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000172 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000173 ObjCInterfaceDecl *ClassInterface,
174 ObjCInterfaceDecl *SuperDecl) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000175 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
Chris Lattner1b6de332008-03-16 20:53:07 +0000176}
Chris Lattner10318b82008-03-16 00:19:01 +0000177
Chris Lattner2d1c4312008-03-16 21:17:37 +0000178ObjCCompatibleAliasDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000179ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000180 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000181 IdentifierInfo *Id,
182 ObjCInterfaceDecl* AliasedClass) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000183 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000184}
185
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000186ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000187 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000188 IdentifierInfo *Id,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000189 QualType T,
190 PropertyControl propControl) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000191 return new (C) ObjCPropertyDecl(DC, L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000192}
193
Chris Lattner10318b82008-03-16 00:19:01 +0000194//===----------------------------------------------------------------------===//
195// Objective-C Decl Implementation
196//===----------------------------------------------------------------------===//
197
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000198void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
199 const ObjCInterfaceDecl *OID) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000200 QualType selfTy;
Douglas Gregor5d764842009-01-09 17:18:27 +0000201 if (isInstanceMethod()) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000202 // There may be no interface context due to error in declaration
203 // of the interface (which has been reported). Recover gracefully.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000204 if (OID) {
205 selfTy = Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl *>(OID));
Daniel Dunbar88116372008-08-26 06:08:30 +0000206 selfTy = Context.getPointerType(selfTy);
207 } else {
208 selfTy = Context.getObjCIdType();
209 }
210 } else // we have a factory method.
211 selfTy = Context.getObjCClassType();
212
213 SelfDecl = ImplicitParamDecl::Create(Context, this,
214 SourceLocation(),
215 &Context.Idents.get("self"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000216 selfTy);
Daniel Dunbar88116372008-08-26 06:08:30 +0000217
218 CmdDecl = ImplicitParamDecl::Create(Context, this,
219 SourceLocation(),
220 &Context.Idents.get("_cmd"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000221 Context.getObjCSelType());
Daniel Dunbar88116372008-08-26 06:08:30 +0000222}
223
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000224/// FindCategoryDeclaration - Finds category declaration in the list of
225/// categories for this class and returns it. Name of the category is passed
226/// in 'CategoryId'. If category not found, return 0;
227///
228ObjCCategoryDecl *
229 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
230 for (ObjCCategoryDecl *Category = getCategoryList();
231 Category; Category = Category->getNextClassCategory())
232 if (Category->getIdentifier() == CategoryId)
233 return Category;
234 return 0;
235}
236
Fariborz Jahanian09772392008-12-13 22:20:28 +0000237/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
238/// storage which matches this 'ivar'.
239///
240FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000241 const ObjCIvarDecl *ivar) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000242 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000243 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
244 DeclarationName Member = ivar->getDeclName();
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000245 DeclContext::lookup_result Lookup = (const_cast< RecordDecl *>(RecordForDecl))
Steve Naroffab63fd62009-01-08 17:28:14 +0000246 ->lookup(Member);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000247 assert((Lookup.first != Lookup.second) && "field decl not found");
248 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
249 assert(MemberDecl && "field decl not found");
250 return MemberDecl;
251}
252
Chris Lattner10318b82008-03-16 00:19:01 +0000253/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
254/// Variables (Ivars) relative to what declared in @implementation;s class.
255/// Ivars into ObjCImplementationDecl's fields.
256///
257void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
258 ObjCIvarDecl **ivars, unsigned numIvars) {
259 NumIvars = numIvars;
260 if (numIvars) {
261 Ivars = new ObjCIvarDecl*[numIvars];
262 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
263 }
264}
265
Steve Naroffab63fd62009-01-08 17:28:14 +0000266// Get the local instance method declared in this interface.
267// FIXME: handle overloading, instance & class methods can have the same name.
268ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
269 lookup_const_result MethodResult = lookup(Sel);
270 if (MethodResult.first)
271 return const_cast<ObjCMethodDecl*>(
272 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
273 return 0;
274}
275
276// Get the local class method declared in this interface.
277ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
278 lookup_const_result MethodResult = lookup(Sel);
279 if (MethodResult.first)
280 return const_cast<ObjCMethodDecl*>(
281 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
282 return 0;
283}
284
285unsigned ObjCContainerDecl::getNumInstanceMethods() const {
286 unsigned sum = 0;
287 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I != E; ++I)
288 sum++;
289 return sum;
290}
291unsigned ObjCContainerDecl::getNumClassMethods() const {
292 unsigned sum = 0;
293 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I != E; ++I)
294 sum++;
295 return sum;
296}
Steve Naroff56af2002009-01-11 01:06:09 +0000297unsigned ObjCContainerDecl::getNumProperties() const {
298 unsigned sum = 0;
299 for (prop_iterator I=prop_begin(), E=prop_end(); I != E; ++I)
300 sum++;
301 return sum;
302}
Steve Naroffab63fd62009-01-08 17:28:14 +0000303
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000304/// FindPropertyDeclaration - Finds declaration of the property given its name
305/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +0000306/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000307///
308ObjCPropertyDecl *
Steve Naroff451f83c2009-01-09 15:36:25 +0000309ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
310 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I) {
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000311 ObjCPropertyDecl *property = *I;
312 if (property->getIdentifier() == PropertyId)
313 return property;
314 }
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000315 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
316 if (PID) {
317 for (ObjCProtocolDecl::protocol_iterator P = PID->protocol_begin(),
318 E = PID->protocol_end();
319 P != E; ++P)
320 if (ObjCPropertyDecl *property =
321 (*P)->FindPropertyDeclaration(PropertyId))
322 return property;
323 }
324
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000325 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +0000326 // Look through categories.
327 for (ObjCCategoryDecl *Category = OID->getCategoryList();
328 Category; Category = Category->getNextClassCategory()) {
329 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
330 if (property)
331 return property;
332 }
333 // Look through protocols.
334 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
335 E = OID->protocol_end(); I != E; ++I) {
336 ObjCProtocolDecl *Protocol = *I;
337 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
338 if (property)
339 return property;
340 }
341 if (OID->getSuperClass())
342 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000343 }
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000344 else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
345 // Look through protocols.
346 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
347 E = OCD->protocol_end(); I != E; ++I) {
348 ObjCProtocolDecl *Protocol = *I;
349 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
350 if (property)
351 return property;
352 }
353 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000354 return 0;
355}
356
Chris Lattner10318b82008-03-16 00:19:01 +0000357ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
358 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
359 ObjCInterfaceDecl* ClassDecl = this;
360 while (ClassDecl != NULL) {
361 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
362 I != E; ++I) {
363 if ((*I)->getIdentifier() == ID) {
364 clsDeclared = ClassDecl;
365 return *I;
366 }
367 }
368 ClassDecl = ClassDecl->getSuperClass();
369 }
370 return NULL;
371}
372
373/// lookupInstanceMethod - This method returns an instance method by looking in
374/// the class, its categories, and its super classes (using a linear search).
375ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
376 ObjCInterfaceDecl* ClassDecl = this;
377 ObjCMethodDecl *MethodDecl = 0;
378
379 while (ClassDecl != NULL) {
380 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
381 return MethodDecl;
382
383 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000384 const ObjCList<ObjCProtocolDecl> &Protocols =
385 ClassDecl->getReferencedProtocols();
386 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
387 E = Protocols.end(); I != E; ++I)
388 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000389 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000390
Chris Lattner10318b82008-03-16 00:19:01 +0000391 // Didn't find one yet - now look through categories.
392 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
393 while (CatDecl) {
394 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
395 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000396
397 // Didn't find one yet - look through protocols.
398 const ObjCList<ObjCProtocolDecl> &Protocols =
399 CatDecl->getReferencedProtocols();
400 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
401 E = Protocols.end(); I != E; ++I)
402 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
403 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000404 CatDecl = CatDecl->getNextClassCategory();
405 }
406 ClassDecl = ClassDecl->getSuperClass();
407 }
408 return NULL;
409}
410
411// lookupClassMethod - This method returns a class method by looking in the
412// class, its categories, and its super classes (using a linear search).
413ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
414 ObjCInterfaceDecl* ClassDecl = this;
415 ObjCMethodDecl *MethodDecl = 0;
416
417 while (ClassDecl != NULL) {
418 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
419 return MethodDecl;
420
421 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000422 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
423 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000424 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000425 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000426
Chris Lattner10318b82008-03-16 00:19:01 +0000427 // Didn't find one yet - now look through categories.
428 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
429 while (CatDecl) {
430 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
431 return MethodDecl;
432 CatDecl = CatDecl->getNextClassCategory();
433 }
434 ClassDecl = ClassDecl->getSuperClass();
435 }
436 return NULL;
437}
438
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000439/// getInstanceMethod - This method returns an instance method by
440/// looking in the class implementation. Unlike interfaces, we don't
441/// look outside the implementation.
442ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000443 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
444 if ((*I)->getSelector() == Sel)
445 return *I;
446 return NULL;
447}
448
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000449/// getClassMethod - This method returns a class method by looking in
450/// the class implementation. Unlike interfaces, we don't look outside
451/// the implementation.
452ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000453 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
454 I != E; ++I)
455 if ((*I)->getSelector() == Sel)
456 return *I;
457 return NULL;
458}
459
Fariborz Jahanian68342282008-12-05 22:32:48 +0000460/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
461/// added to the list of those properties @synthesized/@dynamic in this
462/// @implementation block.
463///
464ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
465 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
466 ObjCPropertyImplDecl *PID = *i;
467 if (PID->getPropertyDecl()->getIdentifier() == Id)
468 return PID;
469 }
470 return 0;
471}
472
473/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000474/// properties implemented in this @implementation block and returns the
475/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000476///
477ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
478 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
479 ObjCPropertyImplDecl *PID = *i;
480 if (PID->getPropertyIvarDecl() &&
481 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
482 return PID;
483 }
484 return 0;
485}
486
487/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000488/// properties implemented in this category @implementation block and returns
489/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000490///
Chris Lattner715e8482009-02-16 19:24:31 +0000491ObjCPropertyImplDecl *ObjCCategoryImplDecl::
492FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
493 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000494 ObjCPropertyImplDecl *PID = *i;
495 if (PID->getPropertyIvarDecl() &&
496 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
497 return PID;
498 }
499 return 0;
500}
501
502/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
503/// added to the list of those properties @synthesized/@dynamic in this
504/// category @implementation block.
505///
Chris Lattner715e8482009-02-16 19:24:31 +0000506ObjCPropertyImplDecl *ObjCCategoryImplDecl::
507FindPropertyImplDecl(IdentifierInfo *Id) const {
508 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000509 ObjCPropertyImplDecl *PID = *i;
510 if (PID->getPropertyDecl()->getIdentifier() == Id)
511 return PID;
512 }
513 return 0;
514}
515
Chris Lattner10318b82008-03-16 00:19:01 +0000516// lookupInstanceMethod - This method returns an instance method by looking in
517// the class implementation. Unlike interfaces, we don't look outside the
518// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000519ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000520 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
521 if ((*I)->getSelector() == Sel)
522 return *I;
523 return NULL;
524}
525
526// lookupClassMethod - This method returns an instance method by looking in
527// the class implementation. Unlike interfaces, we don't look outside the
528// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000529ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000530 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
531 I != E; ++I)
532 if ((*I)->getSelector() == Sel)
533 return *I;
534 return NULL;
535}
536
537// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
538// it inherited.
539ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
540 ObjCMethodDecl *MethodDecl = NULL;
541
542 if ((MethodDecl = getInstanceMethod(Sel)))
543 return MethodDecl;
544
Chris Lattner0be08822008-07-21 21:32:27 +0000545 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000546 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000547 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000548 return NULL;
549}
550
551// lookupInstanceMethod - Lookup a class method in the protocol and protocols
552// it inherited.
553ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
554 ObjCMethodDecl *MethodDecl = NULL;
555
556 if ((MethodDecl = getClassMethod(Sel)))
557 return MethodDecl;
558
Chris Lattner0be08822008-07-21 21:32:27 +0000559 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000560 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000561 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000562 return NULL;
563}
564
565/// getSynthesizedMethodSize - Compute size of synthesized method name
566/// as done be the rewrite.
567///
568unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
569 // syntesized method name is a concatenation of -/+[class-name selector]
570 // Get length of this name.
571 unsigned length = 3; // _I_ or _C_
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000572 length += getClassInterface()->getNameAsString().size()+1; // extra for _
Steve Naroff438be772009-01-08 19:41:02 +0000573 if (const ObjCCategoryImplDecl *CID =
574 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000575 length += CID->getNameAsString().size()+1;
Chris Lattner3a8f2942008-11-24 03:33:13 +0000576 length += getSelector().getAsString().size(); // selector name
Chris Lattner10318b82008-03-16 00:19:01 +0000577 return length;
578}
579
Chris Lattner052fbcd2008-04-06 05:25:03 +0000580ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Steve Naroff438be772009-01-08 19:41:02 +0000581 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000582 return ID;
Steve Naroff438be772009-01-08 19:41:02 +0000583 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000584 return CD->getClassInterface();
585 if (ObjCImplementationDecl *IMD =
Steve Naroff438be772009-01-08 19:41:02 +0000586 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000587 return IMD->getClassInterface();
Steve Naroff438be772009-01-08 19:41:02 +0000588 if (ObjCCategoryImplDecl *CID =
589 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000590 return CID->getClassInterface();
591 assert(false && "unknown method context");
592 return 0;
593}
Chris Lattner44859612008-03-17 01:19:02 +0000594
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000595ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000596 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000597 SourceLocation atLoc,
598 SourceLocation L,
599 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000600 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000601 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000602 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000603}
Chris Lattner44859612008-03-17 01:19:02 +0000604
Chris Lattnereee57c02008-04-04 06:12:32 +0000605