blob: 506d3e4c3877e7b63399ab098230c531d13f6122 [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 +000038ObjCMethodDecl::~ObjCMethodDecl() {
39 delete [] ParamInfo;
Ted Kremenek8c945b12008-06-06 16:45:15 +000040}
41
42void ObjCMethodDecl::Destroy(ASTContext& C) {
43 if (Body) Body->Destroy(C);
44 if (SelfDecl) SelfDecl->Destroy(C);
45
46 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
47 if (*I) (*I)->Destroy(C);
48
49 Decl::Destroy(C);
50}
51
Chris Lattnereee57c02008-04-04 06:12:32 +000052ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000053 DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000054 SourceLocation atLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000055 IdentifierInfo *Id,
56 SourceLocation ClassLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000057 bool ForwardDecl, bool isInternal){
Steve Naroff5abb0282009-01-27 21:25:57 +000058 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
Chris Lattner0db541b2008-03-16 01:15:50 +000059 isInternal);
60}
61
Steve Naroffd9d4d502009-01-07 17:57:40 +000062ObjCContainerDecl::~ObjCContainerDecl() {
Steve Naroffd9d4d502009-01-07 17:57:40 +000063}
64
65ObjCInterfaceDecl::~ObjCInterfaceDecl() {
66 delete [] Ivars;
Ted Kremenek8c945b12008-06-06 16:45:15 +000067 // FIXME: CategoryList?
68}
69
70void ObjCInterfaceDecl::Destroy(ASTContext& C) {
71 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
72 if (*I) (*I)->Destroy(C);
73
Ted Kremenek22db1602008-06-06 17:21:42 +000074 // FIXME: Because there is no clear ownership
75 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
76 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
Ted Kremenek8c945b12008-06-06 16:45:15 +000077 Decl::Destroy(C);
78}
79
80
Argiris Kirtzidis96e79bf2009-02-17 20:20:37 +000081ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
82 SourceLocation L, IdentifierInfo *Id,
83 QualType T, AccessControl ac, Expr *BW) {
84 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
Chris Lattner114add62008-03-16 00:49:28 +000085}
86
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000087
88ObjCAtDefsFieldDecl
Douglas Gregor8acb7272008-12-11 16:49:14 +000089*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000090 IdentifierInfo *Id, QualType T, Expr *BW) {
Steve Naroff5abb0282009-01-27 21:25:57 +000091 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000092}
93
94void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
95 this->~ObjCAtDefsFieldDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000096 C.Deallocate((void *)this);
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000097}
98
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000099ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000100 SourceLocation L,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000101 IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000102 return new (C) ObjCProtocolDecl(DC, L, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +0000103}
104
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000105ObjCProtocolDecl::~ObjCProtocolDecl() {
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000106 delete [] PropertyDecl;
107}
108
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000109
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000110ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000111 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000112 ObjCInterfaceDecl **Elts, unsigned nElts) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000113 return new (C) ObjCClassDecl(DC, L, Elts, nElts);
Chris Lattnere29dc832008-03-16 20:34:23 +0000114}
115
Ted Kremenekff291622008-06-06 20:11:53 +0000116ObjCClassDecl::~ObjCClassDecl() {
117 delete [] ForwardDecls;
118}
119
120void ObjCClassDecl::Destroy(ASTContext& C) {
121
122 // FIXME: There is no clear ownership policy now for referenced
123 // ObjCInterfaceDecls. Some of them can be forward declarations that
124 // are never later defined (in which case the ObjCClassDecl owns them)
125 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
126 // we should have separate objects for forward declarations and definitions,
127 // obviating this problem. Because of this situation, referenced
128 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
129
130 Decl::Destroy(C);
131}
132
Chris Lattnere29dc832008-03-16 20:34:23 +0000133ObjCForwardProtocolDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000134ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000135 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000136 ObjCProtocolDecl **Elts, unsigned NumElts) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000137 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts);
Chris Lattnere29dc832008-03-16 20:34:23 +0000138}
139
Ted Kremenek1e5e0bf2008-06-06 21:05:33 +0000140ObjCForwardProtocolDecl::~ObjCForwardProtocolDecl() {
141 delete [] ReferencedProtocols;
142}
143
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000144ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000145 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000146 IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000147 return new (C) ObjCCategoryDecl(DC, L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000148}
149
Chris Lattner1b6de332008-03-16 20:53:07 +0000150ObjCCategoryImplDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000151ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000152 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000153 ObjCInterfaceDecl *ClassInterface) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000154 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
Chris Lattner1b6de332008-03-16 20:53:07 +0000155}
156
157ObjCImplementationDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000158ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000159 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000160 ObjCInterfaceDecl *ClassInterface,
161 ObjCInterfaceDecl *SuperDecl) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000162 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
Chris Lattner1b6de332008-03-16 20:53:07 +0000163}
Chris Lattner10318b82008-03-16 00:19:01 +0000164
Chris Lattner2d1c4312008-03-16 21:17:37 +0000165ObjCCompatibleAliasDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000166ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000167 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000168 IdentifierInfo *Id,
169 ObjCInterfaceDecl* AliasedClass) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000170 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000171}
172
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000173ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000174 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000175 IdentifierInfo *Id,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000176 QualType T,
177 PropertyControl propControl) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000178 return new (C) ObjCPropertyDecl(DC, L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000179}
180
Chris Lattner10318b82008-03-16 00:19:01 +0000181//===----------------------------------------------------------------------===//
182// Objective-C Decl Implementation
183//===----------------------------------------------------------------------===//
184
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000185void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
186 const ObjCInterfaceDecl *OID) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000187 QualType selfTy;
Douglas Gregor5d764842009-01-09 17:18:27 +0000188 if (isInstanceMethod()) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000189 // There may be no interface context due to error in declaration
190 // of the interface (which has been reported). Recover gracefully.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000191 if (OID) {
192 selfTy = Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl *>(OID));
Daniel Dunbar88116372008-08-26 06:08:30 +0000193 selfTy = Context.getPointerType(selfTy);
194 } else {
195 selfTy = Context.getObjCIdType();
196 }
197 } else // we have a factory method.
198 selfTy = Context.getObjCClassType();
199
200 SelfDecl = ImplicitParamDecl::Create(Context, this,
201 SourceLocation(),
202 &Context.Idents.get("self"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000203 selfTy);
Daniel Dunbar88116372008-08-26 06:08:30 +0000204
205 CmdDecl = ImplicitParamDecl::Create(Context, this,
206 SourceLocation(),
207 &Context.Idents.get("_cmd"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000208 Context.getObjCSelType());
Daniel Dunbar88116372008-08-26 06:08:30 +0000209}
210
Chris Lattner10318b82008-03-16 00:19:01 +0000211void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
212 unsigned NumParams) {
213 assert(ParamInfo == 0 && "Already has param info!");
214
215 // Zero params -> null pointer.
216 if (NumParams) {
217 ParamInfo = new ParmVarDecl*[NumParams];
218 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
219 NumMethodParams = NumParams;
220 }
221}
222
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000223/// FindCategoryDeclaration - Finds category declaration in the list of
224/// categories for this class and returns it. Name of the category is passed
225/// in 'CategoryId'. If category not found, return 0;
226///
227ObjCCategoryDecl *
228 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
229 for (ObjCCategoryDecl *Category = getCategoryList();
230 Category; Category = Category->getNextClassCategory())
231 if (Category->getIdentifier() == CategoryId)
232 return Category;
233 return 0;
234}
235
Chris Lattner10318b82008-03-16 00:19:01 +0000236/// ObjCAddInstanceVariablesToClass - Inserts instance variables
237/// into ObjCInterfaceDecl's fields.
238///
239void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
240 unsigned numIvars,
241 SourceLocation RBrac) {
242 NumIvars = numIvars;
243 if (numIvars) {
244 Ivars = new ObjCIvarDecl*[numIvars];
245 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
246 }
247 setLocEnd(RBrac);
248}
249
Fariborz Jahanian09772392008-12-13 22:20:28 +0000250/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
251/// storage which matches this 'ivar'.
252///
253FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000254 const ObjCIvarDecl *ivar) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000255 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000256 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
257 DeclarationName Member = ivar->getDeclName();
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000258 DeclContext::lookup_result Lookup = (const_cast< RecordDecl *>(RecordForDecl))
Steve Naroffab63fd62009-01-08 17:28:14 +0000259 ->lookup(Member);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000260 assert((Lookup.first != Lookup.second) && "field decl not found");
261 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
262 assert(MemberDecl && "field decl not found");
263 return MemberDecl;
264}
265
Chris Lattner10318b82008-03-16 00:19:01 +0000266/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
267/// Variables (Ivars) relative to what declared in @implementation;s class.
268/// Ivars into ObjCImplementationDecl's fields.
269///
270void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
271 ObjCIvarDecl **ivars, unsigned numIvars) {
272 NumIvars = numIvars;
273 if (numIvars) {
274 Ivars = new ObjCIvarDecl*[numIvars];
275 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
276 }
277}
278
Steve Naroffab63fd62009-01-08 17:28:14 +0000279// Get the local instance method declared in this interface.
280// FIXME: handle overloading, instance & class methods can have the same name.
281ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
282 lookup_const_result MethodResult = lookup(Sel);
283 if (MethodResult.first)
284 return const_cast<ObjCMethodDecl*>(
285 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
286 return 0;
287}
288
289// Get the local class method declared in this interface.
290ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
291 lookup_const_result MethodResult = lookup(Sel);
292 if (MethodResult.first)
293 return const_cast<ObjCMethodDecl*>(
294 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
295 return 0;
296}
297
298unsigned ObjCContainerDecl::getNumInstanceMethods() const {
299 unsigned sum = 0;
300 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I != E; ++I)
301 sum++;
302 return sum;
303}
304unsigned ObjCContainerDecl::getNumClassMethods() const {
305 unsigned sum = 0;
306 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I != E; ++I)
307 sum++;
308 return sum;
309}
Steve Naroff56af2002009-01-11 01:06:09 +0000310unsigned ObjCContainerDecl::getNumProperties() const {
311 unsigned sum = 0;
312 for (prop_iterator I=prop_begin(), E=prop_end(); I != E; ++I)
313 sum++;
314 return sum;
315}
Steve Naroffab63fd62009-01-08 17:28:14 +0000316
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000317/// FindPropertyDeclaration - Finds declaration of the property given its name
318/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +0000319/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000320///
321ObjCPropertyDecl *
Steve Naroff451f83c2009-01-09 15:36:25 +0000322ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
323 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I) {
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000324 ObjCPropertyDecl *property = *I;
325 if (property->getIdentifier() == PropertyId)
326 return property;
327 }
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000328 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
329 if (PID) {
330 for (ObjCProtocolDecl::protocol_iterator P = PID->protocol_begin(),
331 E = PID->protocol_end();
332 P != E; ++P)
333 if (ObjCPropertyDecl *property =
334 (*P)->FindPropertyDeclaration(PropertyId))
335 return property;
336 }
337
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000338 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +0000339 // Look through categories.
340 for (ObjCCategoryDecl *Category = OID->getCategoryList();
341 Category; Category = Category->getNextClassCategory()) {
342 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
343 if (property)
344 return property;
345 }
346 // Look through protocols.
347 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
348 E = OID->protocol_end(); I != E; ++I) {
349 ObjCProtocolDecl *Protocol = *I;
350 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
351 if (property)
352 return property;
353 }
354 if (OID->getSuperClass())
355 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000356 }
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000357 else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
358 // Look through protocols.
359 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
360 E = OCD->protocol_end(); I != E; ++I) {
361 ObjCProtocolDecl *Protocol = *I;
362 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
363 if (property)
364 return property;
365 }
366 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000367 return 0;
368}
369
Chris Lattner10318b82008-03-16 00:19:01 +0000370ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
371 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
372 ObjCInterfaceDecl* ClassDecl = this;
373 while (ClassDecl != NULL) {
374 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
375 I != E; ++I) {
376 if ((*I)->getIdentifier() == ID) {
377 clsDeclared = ClassDecl;
378 return *I;
379 }
380 }
381 ClassDecl = ClassDecl->getSuperClass();
382 }
383 return NULL;
384}
385
386/// lookupInstanceMethod - This method returns an instance method by looking in
387/// the class, its categories, and its super classes (using a linear search).
388ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
389 ObjCInterfaceDecl* ClassDecl = this;
390 ObjCMethodDecl *MethodDecl = 0;
391
392 while (ClassDecl != NULL) {
393 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
394 return MethodDecl;
395
396 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000397 const ObjCList<ObjCProtocolDecl> &Protocols =
398 ClassDecl->getReferencedProtocols();
399 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
400 E = Protocols.end(); I != E; ++I)
401 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000402 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000403
Chris Lattner10318b82008-03-16 00:19:01 +0000404 // Didn't find one yet - now look through categories.
405 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
406 while (CatDecl) {
407 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
408 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000409
410 // Didn't find one yet - look through protocols.
411 const ObjCList<ObjCProtocolDecl> &Protocols =
412 CatDecl->getReferencedProtocols();
413 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
414 E = Protocols.end(); I != E; ++I)
415 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
416 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000417 CatDecl = CatDecl->getNextClassCategory();
418 }
419 ClassDecl = ClassDecl->getSuperClass();
420 }
421 return NULL;
422}
423
424// lookupClassMethod - This method returns a class method by looking in the
425// class, its categories, and its super classes (using a linear search).
426ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
427 ObjCInterfaceDecl* ClassDecl = this;
428 ObjCMethodDecl *MethodDecl = 0;
429
430 while (ClassDecl != NULL) {
431 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
432 return MethodDecl;
433
434 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000435 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
436 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000437 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000438 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000439
Chris Lattner10318b82008-03-16 00:19:01 +0000440 // Didn't find one yet - now look through categories.
441 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
442 while (CatDecl) {
443 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
444 return MethodDecl;
445 CatDecl = CatDecl->getNextClassCategory();
446 }
447 ClassDecl = ClassDecl->getSuperClass();
448 }
449 return NULL;
450}
451
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000452/// getInstanceMethod - This method returns an instance method by
453/// looking in the class implementation. Unlike interfaces, we don't
454/// look outside the implementation.
455ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000456 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
457 if ((*I)->getSelector() == Sel)
458 return *I;
459 return NULL;
460}
461
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000462/// getClassMethod - This method returns a class method by looking in
463/// the class implementation. Unlike interfaces, we don't look outside
464/// the implementation.
465ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000466 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
467 I != E; ++I)
468 if ((*I)->getSelector() == Sel)
469 return *I;
470 return NULL;
471}
472
Fariborz Jahanian68342282008-12-05 22:32:48 +0000473/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
474/// added to the list of those properties @synthesized/@dynamic in this
475/// @implementation block.
476///
477ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
478 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
479 ObjCPropertyImplDecl *PID = *i;
480 if (PID->getPropertyDecl()->getIdentifier() == Id)
481 return PID;
482 }
483 return 0;
484}
485
486/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000487/// properties implemented in this @implementation block and returns the
488/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000489///
490ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
491 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
492 ObjCPropertyImplDecl *PID = *i;
493 if (PID->getPropertyIvarDecl() &&
494 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
495 return PID;
496 }
497 return 0;
498}
499
500/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000501/// properties implemented in this category @implementation block and returns
502/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000503///
Chris Lattner715e8482009-02-16 19:24:31 +0000504ObjCPropertyImplDecl *ObjCCategoryImplDecl::
505FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
506 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000507 ObjCPropertyImplDecl *PID = *i;
508 if (PID->getPropertyIvarDecl() &&
509 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
510 return PID;
511 }
512 return 0;
513}
514
515/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
516/// added to the list of those properties @synthesized/@dynamic in this
517/// category @implementation block.
518///
Chris Lattner715e8482009-02-16 19:24:31 +0000519ObjCPropertyImplDecl *ObjCCategoryImplDecl::
520FindPropertyImplDecl(IdentifierInfo *Id) const {
521 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000522 ObjCPropertyImplDecl *PID = *i;
523 if (PID->getPropertyDecl()->getIdentifier() == Id)
524 return PID;
525 }
526 return 0;
527}
528
Chris Lattner10318b82008-03-16 00:19:01 +0000529// lookupInstanceMethod - This method returns an instance method by looking in
530// the class implementation. Unlike interfaces, we don't look outside the
531// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000532ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000533 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
534 if ((*I)->getSelector() == Sel)
535 return *I;
536 return NULL;
537}
538
539// lookupClassMethod - This method returns an instance method by looking in
540// the class implementation. Unlike interfaces, we don't look outside the
541// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000542ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000543 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
544 I != E; ++I)
545 if ((*I)->getSelector() == Sel)
546 return *I;
547 return NULL;
548}
549
550// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
551// it inherited.
552ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
553 ObjCMethodDecl *MethodDecl = NULL;
554
555 if ((MethodDecl = getInstanceMethod(Sel)))
556 return MethodDecl;
557
Chris Lattner0be08822008-07-21 21:32:27 +0000558 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000559 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000560 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000561 return NULL;
562}
563
564// lookupInstanceMethod - Lookup a class method in the protocol and protocols
565// it inherited.
566ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
567 ObjCMethodDecl *MethodDecl = NULL;
568
569 if ((MethodDecl = getClassMethod(Sel)))
570 return MethodDecl;
571
Chris Lattner0be08822008-07-21 21:32:27 +0000572 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000573 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000574 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000575 return NULL;
576}
577
578/// getSynthesizedMethodSize - Compute size of synthesized method name
579/// as done be the rewrite.
580///
581unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
582 // syntesized method name is a concatenation of -/+[class-name selector]
583 // Get length of this name.
584 unsigned length = 3; // _I_ or _C_
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000585 length += getClassInterface()->getNameAsString().size()+1; // extra for _
Steve Naroff438be772009-01-08 19:41:02 +0000586 if (const ObjCCategoryImplDecl *CID =
587 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000588 length += CID->getNameAsString().size()+1;
Chris Lattner3a8f2942008-11-24 03:33:13 +0000589 length += getSelector().getAsString().size(); // selector name
Chris Lattner10318b82008-03-16 00:19:01 +0000590 return length;
591}
592
Chris Lattner052fbcd2008-04-06 05:25:03 +0000593ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Steve Naroff438be772009-01-08 19:41:02 +0000594 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000595 return ID;
Steve Naroff438be772009-01-08 19:41:02 +0000596 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000597 return CD->getClassInterface();
598 if (ObjCImplementationDecl *IMD =
Steve Naroff438be772009-01-08 19:41:02 +0000599 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000600 return IMD->getClassInterface();
Steve Naroff438be772009-01-08 19:41:02 +0000601 if (ObjCCategoryImplDecl *CID =
602 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000603 return CID->getClassInterface();
604 assert(false && "unknown method context");
605 return 0;
606}
Chris Lattner44859612008-03-17 01:19:02 +0000607
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000608ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000609 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000610 SourceLocation atLoc,
611 SourceLocation L,
612 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000613 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000614 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000615 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000616}
Chris Lattner44859612008-03-17 01:19:02 +0000617
Chris Lattnereee57c02008-04-04 06:12:32 +0000618