blob: 5b59af9d9539b045275e7f16787fffaa4ab713a8 [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 Lattnere29dc832008-03-16 20:34:23 +0000118 ObjCInterfaceDecl **Elts, unsigned nElts) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000119 return new (C) ObjCClassDecl(DC, L, Elts, nElts);
Chris Lattnere29dc832008-03-16 20:34:23 +0000120}
121
Chris Lattner3e27e632009-02-20 06:03:09 +0000122ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
123 ObjCInterfaceDecl **Elts, unsigned nElts)
124 : Decl(ObjCClass, DC, L) {
125 if (nElts) {
126 ForwardDecls = new ObjCInterfaceDecl*[nElts];
127 memcpy(ForwardDecls, Elts, nElts*sizeof(ObjCInterfaceDecl*));
128 } else {
129 ForwardDecls = 0;
130 }
131 NumForwardDecls = nElts;
Ted Kremenekff291622008-06-06 20:11:53 +0000132}
133
Chris Lattner3e27e632009-02-20 06:03:09 +0000134void ObjCClassDecl::Destroy(ASTContext &C) {
Ted Kremenekff291622008-06-06 20:11:53 +0000135
136 // FIXME: There is no clear ownership policy now for referenced
137 // ObjCInterfaceDecls. Some of them can be forward declarations that
138 // are never later defined (in which case the ObjCClassDecl owns them)
139 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
140 // we should have separate objects for forward declarations and definitions,
141 // obviating this problem. Because of this situation, referenced
142 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
143
Chris Lattner3e27e632009-02-20 06:03:09 +0000144 delete [] ForwardDecls;
145 ForwardDecls = 0;
146
Ted Kremenekff291622008-06-06 20:11:53 +0000147 Decl::Destroy(C);
148}
149
Chris Lattnere29dc832008-03-16 20:34:23 +0000150ObjCForwardProtocolDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000151ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000152 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000153 ObjCProtocolDecl **Elts, unsigned NumElts) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000154 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts);
Chris Lattnere29dc832008-03-16 20:34:23 +0000155}
156
Chris Lattner3e27e632009-02-20 06:03:09 +0000157ObjCForwardProtocolDecl::
158ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
159 ObjCProtocolDecl **Elts, unsigned nElts)
160 : Decl(ObjCForwardProtocol, DC, L) {
161 NumReferencedProtocols = nElts;
162 if (nElts) {
163 ReferencedProtocols = new ObjCProtocolDecl*[nElts];
164 memcpy(ReferencedProtocols, Elts, nElts*sizeof(ObjCProtocolDecl*));
165 } else {
166 ReferencedProtocols = 0;
167 }
168}
169
170void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Ted Kremenek1e5e0bf2008-06-06 21:05:33 +0000171 delete [] ReferencedProtocols;
Chris Lattner3e27e632009-02-20 06:03:09 +0000172 ReferencedProtocols = 0;
Ted Kremenek1e5e0bf2008-06-06 21:05:33 +0000173}
174
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000175ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000176 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000177 IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000178 return new (C) ObjCCategoryDecl(DC, L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000179}
180
Chris Lattner1b6de332008-03-16 20:53:07 +0000181ObjCCategoryImplDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000182ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000183 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000184 ObjCInterfaceDecl *ClassInterface) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000185 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
Chris Lattner1b6de332008-03-16 20:53:07 +0000186}
187
188ObjCImplementationDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000189ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000190 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000191 ObjCInterfaceDecl *ClassInterface,
192 ObjCInterfaceDecl *SuperDecl) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000193 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
Chris Lattner1b6de332008-03-16 20:53:07 +0000194}
Chris Lattner10318b82008-03-16 00:19:01 +0000195
Chris Lattner2d1c4312008-03-16 21:17:37 +0000196ObjCCompatibleAliasDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000197ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000198 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000199 IdentifierInfo *Id,
200 ObjCInterfaceDecl* AliasedClass) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000201 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000202}
203
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000204ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000205 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000206 IdentifierInfo *Id,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000207 QualType T,
208 PropertyControl propControl) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000209 return new (C) ObjCPropertyDecl(DC, L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000210}
211
Chris Lattner10318b82008-03-16 00:19:01 +0000212//===----------------------------------------------------------------------===//
213// Objective-C Decl Implementation
214//===----------------------------------------------------------------------===//
215
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000216void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
217 const ObjCInterfaceDecl *OID) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000218 QualType selfTy;
Douglas Gregor5d764842009-01-09 17:18:27 +0000219 if (isInstanceMethod()) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000220 // There may be no interface context due to error in declaration
221 // of the interface (which has been reported). Recover gracefully.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000222 if (OID) {
223 selfTy = Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl *>(OID));
Daniel Dunbar88116372008-08-26 06:08:30 +0000224 selfTy = Context.getPointerType(selfTy);
225 } else {
226 selfTy = Context.getObjCIdType();
227 }
228 } else // we have a factory method.
229 selfTy = Context.getObjCClassType();
230
231 SelfDecl = ImplicitParamDecl::Create(Context, this,
232 SourceLocation(),
233 &Context.Idents.get("self"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000234 selfTy);
Daniel Dunbar88116372008-08-26 06:08:30 +0000235
236 CmdDecl = ImplicitParamDecl::Create(Context, this,
237 SourceLocation(),
238 &Context.Idents.get("_cmd"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000239 Context.getObjCSelType());
Daniel Dunbar88116372008-08-26 06:08:30 +0000240}
241
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000242/// FindCategoryDeclaration - Finds category declaration in the list of
243/// categories for this class and returns it. Name of the category is passed
244/// in 'CategoryId'. If category not found, return 0;
245///
246ObjCCategoryDecl *
247 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
248 for (ObjCCategoryDecl *Category = getCategoryList();
249 Category; Category = Category->getNextClassCategory())
250 if (Category->getIdentifier() == CategoryId)
251 return Category;
252 return 0;
253}
254
Fariborz Jahanian09772392008-12-13 22:20:28 +0000255/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
256/// storage which matches this 'ivar'.
257///
258FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000259 const ObjCIvarDecl *ivar) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000260 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000261 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
262 DeclarationName Member = ivar->getDeclName();
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000263 DeclContext::lookup_result Lookup = (const_cast< RecordDecl *>(RecordForDecl))
Steve Naroffab63fd62009-01-08 17:28:14 +0000264 ->lookup(Member);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000265 assert((Lookup.first != Lookup.second) && "field decl not found");
266 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
267 assert(MemberDecl && "field decl not found");
268 return MemberDecl;
269}
270
Chris Lattner10318b82008-03-16 00:19:01 +0000271/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
272/// Variables (Ivars) relative to what declared in @implementation;s class.
273/// Ivars into ObjCImplementationDecl's fields.
274///
275void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
276 ObjCIvarDecl **ivars, unsigned numIvars) {
277 NumIvars = numIvars;
278 if (numIvars) {
279 Ivars = new ObjCIvarDecl*[numIvars];
280 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
281 }
282}
283
Steve Naroffab63fd62009-01-08 17:28:14 +0000284// Get the local instance method declared in this interface.
285// FIXME: handle overloading, instance & class methods can have the same name.
286ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
287 lookup_const_result MethodResult = lookup(Sel);
288 if (MethodResult.first)
289 return const_cast<ObjCMethodDecl*>(
290 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
291 return 0;
292}
293
294// Get the local class method declared in this interface.
295ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
296 lookup_const_result MethodResult = lookup(Sel);
297 if (MethodResult.first)
298 return const_cast<ObjCMethodDecl*>(
299 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
300 return 0;
301}
302
303unsigned ObjCContainerDecl::getNumInstanceMethods() const {
304 unsigned sum = 0;
305 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I != E; ++I)
306 sum++;
307 return sum;
308}
309unsigned ObjCContainerDecl::getNumClassMethods() const {
310 unsigned sum = 0;
311 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I != E; ++I)
312 sum++;
313 return sum;
314}
Steve Naroff56af2002009-01-11 01:06:09 +0000315unsigned ObjCContainerDecl::getNumProperties() const {
316 unsigned sum = 0;
317 for (prop_iterator I=prop_begin(), E=prop_end(); I != E; ++I)
318 sum++;
319 return sum;
320}
Steve Naroffab63fd62009-01-08 17:28:14 +0000321
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000322/// FindPropertyDeclaration - Finds declaration of the property given its name
323/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +0000324/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000325///
326ObjCPropertyDecl *
Steve Naroff451f83c2009-01-09 15:36:25 +0000327ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
328 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I) {
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000329 ObjCPropertyDecl *property = *I;
330 if (property->getIdentifier() == PropertyId)
331 return property;
332 }
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000333 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
334 if (PID) {
335 for (ObjCProtocolDecl::protocol_iterator P = PID->protocol_begin(),
336 E = PID->protocol_end();
337 P != E; ++P)
338 if (ObjCPropertyDecl *property =
339 (*P)->FindPropertyDeclaration(PropertyId))
340 return property;
341 }
342
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000343 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +0000344 // Look through categories.
345 for (ObjCCategoryDecl *Category = OID->getCategoryList();
346 Category; Category = Category->getNextClassCategory()) {
347 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
348 if (property)
349 return property;
350 }
351 // Look through protocols.
352 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
353 E = OID->protocol_end(); I != E; ++I) {
354 ObjCProtocolDecl *Protocol = *I;
355 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
356 if (property)
357 return property;
358 }
359 if (OID->getSuperClass())
360 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000361 }
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000362 else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
363 // Look through protocols.
364 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
365 E = OCD->protocol_end(); I != E; ++I) {
366 ObjCProtocolDecl *Protocol = *I;
367 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
368 if (property)
369 return property;
370 }
371 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000372 return 0;
373}
374
Chris Lattner10318b82008-03-16 00:19:01 +0000375ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
376 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
377 ObjCInterfaceDecl* ClassDecl = this;
378 while (ClassDecl != NULL) {
379 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
380 I != E; ++I) {
381 if ((*I)->getIdentifier() == ID) {
382 clsDeclared = ClassDecl;
383 return *I;
384 }
385 }
386 ClassDecl = ClassDecl->getSuperClass();
387 }
388 return NULL;
389}
390
391/// lookupInstanceMethod - This method returns an instance method by looking in
392/// the class, its categories, and its super classes (using a linear search).
393ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
394 ObjCInterfaceDecl* ClassDecl = this;
395 ObjCMethodDecl *MethodDecl = 0;
396
397 while (ClassDecl != NULL) {
398 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
399 return MethodDecl;
400
401 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000402 const ObjCList<ObjCProtocolDecl> &Protocols =
403 ClassDecl->getReferencedProtocols();
404 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
405 E = Protocols.end(); I != E; ++I)
406 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000407 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000408
Chris Lattner10318b82008-03-16 00:19:01 +0000409 // Didn't find one yet - now look through categories.
410 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
411 while (CatDecl) {
412 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
413 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000414
415 // Didn't find one yet - look through protocols.
416 const ObjCList<ObjCProtocolDecl> &Protocols =
417 CatDecl->getReferencedProtocols();
418 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
419 E = Protocols.end(); I != E; ++I)
420 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
421 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000422 CatDecl = CatDecl->getNextClassCategory();
423 }
424 ClassDecl = ClassDecl->getSuperClass();
425 }
426 return NULL;
427}
428
429// lookupClassMethod - This method returns a class method by looking in the
430// class, its categories, and its super classes (using a linear search).
431ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
432 ObjCInterfaceDecl* ClassDecl = this;
433 ObjCMethodDecl *MethodDecl = 0;
434
435 while (ClassDecl != NULL) {
436 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
437 return MethodDecl;
438
439 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000440 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
441 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000442 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000443 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000444
Chris Lattner10318b82008-03-16 00:19:01 +0000445 // Didn't find one yet - now look through categories.
446 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
447 while (CatDecl) {
448 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
449 return MethodDecl;
450 CatDecl = CatDecl->getNextClassCategory();
451 }
452 ClassDecl = ClassDecl->getSuperClass();
453 }
454 return NULL;
455}
456
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000457/// getInstanceMethod - This method returns an instance method by
458/// looking in the class implementation. Unlike interfaces, we don't
459/// look outside the implementation.
460ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000461 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
462 if ((*I)->getSelector() == Sel)
463 return *I;
464 return NULL;
465}
466
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000467/// getClassMethod - This method returns a class method by looking in
468/// the class implementation. Unlike interfaces, we don't look outside
469/// the implementation.
470ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000471 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
472 I != E; ++I)
473 if ((*I)->getSelector() == Sel)
474 return *I;
475 return NULL;
476}
477
Fariborz Jahanian68342282008-12-05 22:32:48 +0000478/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
479/// added to the list of those properties @synthesized/@dynamic in this
480/// @implementation block.
481///
482ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
483 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
484 ObjCPropertyImplDecl *PID = *i;
485 if (PID->getPropertyDecl()->getIdentifier() == Id)
486 return PID;
487 }
488 return 0;
489}
490
491/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000492/// properties implemented in this @implementation block and returns the
493/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000494///
495ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
496 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
497 ObjCPropertyImplDecl *PID = *i;
498 if (PID->getPropertyIvarDecl() &&
499 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
500 return PID;
501 }
502 return 0;
503}
504
505/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000506/// properties implemented in this category @implementation block and returns
507/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000508///
Chris Lattner715e8482009-02-16 19:24:31 +0000509ObjCPropertyImplDecl *ObjCCategoryImplDecl::
510FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
511 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000512 ObjCPropertyImplDecl *PID = *i;
513 if (PID->getPropertyIvarDecl() &&
514 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
515 return PID;
516 }
517 return 0;
518}
519
520/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
521/// added to the list of those properties @synthesized/@dynamic in this
522/// category @implementation block.
523///
Chris Lattner715e8482009-02-16 19:24:31 +0000524ObjCPropertyImplDecl *ObjCCategoryImplDecl::
525FindPropertyImplDecl(IdentifierInfo *Id) const {
526 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000527 ObjCPropertyImplDecl *PID = *i;
528 if (PID->getPropertyDecl()->getIdentifier() == Id)
529 return PID;
530 }
531 return 0;
532}
533
Chris Lattner10318b82008-03-16 00:19:01 +0000534// lookupInstanceMethod - This method returns an instance method by looking in
535// the class implementation. Unlike interfaces, we don't look outside the
536// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000537ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000538 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
539 if ((*I)->getSelector() == Sel)
540 return *I;
541 return NULL;
542}
543
544// lookupClassMethod - This method returns an instance method by looking in
545// the class implementation. Unlike interfaces, we don't look outside the
546// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000547ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000548 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
549 I != E; ++I)
550 if ((*I)->getSelector() == Sel)
551 return *I;
552 return NULL;
553}
554
555// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
556// it inherited.
557ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
558 ObjCMethodDecl *MethodDecl = NULL;
559
560 if ((MethodDecl = getInstanceMethod(Sel)))
561 return MethodDecl;
562
Chris Lattner0be08822008-07-21 21:32:27 +0000563 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000564 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000565 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000566 return NULL;
567}
568
569// lookupInstanceMethod - Lookup a class method in the protocol and protocols
570// it inherited.
571ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
572 ObjCMethodDecl *MethodDecl = NULL;
573
574 if ((MethodDecl = getClassMethod(Sel)))
575 return MethodDecl;
576
Chris Lattner0be08822008-07-21 21:32:27 +0000577 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000578 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000579 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000580 return NULL;
581}
582
583/// getSynthesizedMethodSize - Compute size of synthesized method name
584/// as done be the rewrite.
585///
586unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
587 // syntesized method name is a concatenation of -/+[class-name selector]
588 // Get length of this name.
589 unsigned length = 3; // _I_ or _C_
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000590 length += getClassInterface()->getNameAsString().size()+1; // extra for _
Steve Naroff438be772009-01-08 19:41:02 +0000591 if (const ObjCCategoryImplDecl *CID =
592 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000593 length += CID->getNameAsString().size()+1;
Chris Lattner3a8f2942008-11-24 03:33:13 +0000594 length += getSelector().getAsString().size(); // selector name
Chris Lattner10318b82008-03-16 00:19:01 +0000595 return length;
596}
597
Chris Lattner052fbcd2008-04-06 05:25:03 +0000598ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Steve Naroff438be772009-01-08 19:41:02 +0000599 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000600 return ID;
Steve Naroff438be772009-01-08 19:41:02 +0000601 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000602 return CD->getClassInterface();
603 if (ObjCImplementationDecl *IMD =
Steve Naroff438be772009-01-08 19:41:02 +0000604 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000605 return IMD->getClassInterface();
Steve Naroff438be772009-01-08 19:41:02 +0000606 if (ObjCCategoryImplDecl *CID =
607 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000608 return CID->getClassInterface();
609 assert(false && "unknown method context");
610 return 0;
611}
Chris Lattner44859612008-03-17 01:19:02 +0000612
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000613ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000614 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000615 SourceLocation atLoc,
616 SourceLocation L,
617 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000618 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000619 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000620 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000621}
Chris Lattner44859612008-03-17 01:19:02 +0000622
Chris Lattnereee57c02008-04-04 06:12:32 +0000623