blob: 2263af7d906f952bbac5ed470a225854680f802e [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) {
Chris Lattner114add62008-03-16 00:49:28 +000032 void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000033 return new (Mem) ObjCMethodDecl(beginLoc, endLoc,
34 SelInfo, T, contextDecl,
Daniel Dunbard8bd6822008-08-20 18:02:42 +000035 isInstance,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +000036 isVariadic, isSynthesized, impControl);
Chris Lattner0db541b2008-03-16 01:15:50 +000037}
Chris Lattner114add62008-03-16 00:49:28 +000038
Ted Kremenek8c945b12008-06-06 16:45:15 +000039ObjCMethodDecl::~ObjCMethodDecl() {
40 delete [] ParamInfo;
Ted Kremenek8c945b12008-06-06 16:45:15 +000041}
42
43void ObjCMethodDecl::Destroy(ASTContext& C) {
44 if (Body) Body->Destroy(C);
45 if (SelfDecl) SelfDecl->Destroy(C);
46
47 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
48 if (*I) (*I)->Destroy(C);
49
50 Decl::Destroy(C);
51}
52
Chris Lattnereee57c02008-04-04 06:12:32 +000053ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
54 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){
58 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Chris Lattner5cece462008-07-21 07:06:49 +000059 return new (Mem) ObjCInterfaceDecl(atLoc, Id, ClassLoc, ForwardDecl,
Chris Lattner0db541b2008-03-16 01:15:50 +000060 isInternal);
61}
62
Steve Naroffd9d4d502009-01-07 17:57:40 +000063ObjCContainerDecl::~ObjCContainerDecl() {
Steve Naroffd9d4d502009-01-07 17:57:40 +000064}
65
66ObjCInterfaceDecl::~ObjCInterfaceDecl() {
67 delete [] Ivars;
Ted Kremenek8c945b12008-06-06 16:45:15 +000068 delete [] PropertyDecl;
69 // FIXME: CategoryList?
70}
71
72void ObjCInterfaceDecl::Destroy(ASTContext& C) {
73 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
74 if (*I) (*I)->Destroy(C);
75
76 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
77 if (*I) (*I)->Destroy(C);
78
79 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
80 if (*I) (*I)->Destroy(C);
Ted Kremenek22db1602008-06-06 17:21:42 +000081
82 // FIXME: Because there is no clear ownership
83 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
84 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
85
Ted Kremenek8c945b12008-06-06 16:45:15 +000086 Decl::Destroy(C);
87}
88
89
Chris Lattnerf3874bc2008-04-06 04:47:34 +000090ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Ted Kremenek173dd312008-07-23 18:04:17 +000091 IdentifierInfo *Id, QualType T,
92 AccessControl ac, Expr *BW) {
Chris Lattner0db541b2008-03-16 01:15:50 +000093 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Ted Kremenek173dd312008-07-23 18:04:17 +000094 return new (Mem) ObjCIvarDecl(L, Id, T, ac, BW);
Chris Lattner114add62008-03-16 00:49:28 +000095}
96
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000097
98ObjCAtDefsFieldDecl
Douglas Gregor8acb7272008-12-11 16:49:14 +000099*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000100 IdentifierInfo *Id, QualType T, Expr *BW) {
101 void *Mem = C.getAllocator().Allocate<ObjCAtDefsFieldDecl>();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000102 return new (Mem) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000103}
104
105void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
106 this->~ObjCAtDefsFieldDecl();
107 C.getAllocator().Deallocate((void *)this);
108}
109
Chris Lattnereee57c02008-04-04 06:12:32 +0000110ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
111 SourceLocation L,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000112 IdentifierInfo *Id) {
Chris Lattner180f7e22008-03-16 01:23:04 +0000113 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner0be08822008-07-21 21:32:27 +0000114 return new (Mem) ObjCProtocolDecl(L, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +0000115}
116
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000117ObjCProtocolDecl::~ObjCProtocolDecl() {
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000118 delete [] PropertyDecl;
119}
120
121void ObjCProtocolDecl::Destroy(ASTContext& C) {
122
123 // Referenced Protocols are not owned, so don't Destroy them.
124
125 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
126 if (*I) (*I)->Destroy(C);
127
128 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
129 if (*I) (*I)->Destroy(C);
130
131 // FIXME: Because there is no clear ownership
132 // role between ObjCProtocolDecls and the ObjCPropertyDecls that they
133 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
134
135 Decl::Destroy(C);
136}
137
138
Chris Lattnereee57c02008-04-04 06:12:32 +0000139ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
140 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000141 ObjCInterfaceDecl **Elts, unsigned nElts) {
142 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
143 return new (Mem) ObjCClassDecl(L, Elts, nElts);
144}
145
Ted Kremenekff291622008-06-06 20:11:53 +0000146ObjCClassDecl::~ObjCClassDecl() {
147 delete [] ForwardDecls;
148}
149
150void ObjCClassDecl::Destroy(ASTContext& C) {
151
152 // FIXME: There is no clear ownership policy now for referenced
153 // ObjCInterfaceDecls. Some of them can be forward declarations that
154 // are never later defined (in which case the ObjCClassDecl owns them)
155 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
156 // we should have separate objects for forward declarations and definitions,
157 // obviating this problem. Because of this situation, referenced
158 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
159
160 Decl::Destroy(C);
161}
162
Chris Lattnere29dc832008-03-16 20:34:23 +0000163ObjCForwardProtocolDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000164ObjCForwardProtocolDecl::Create(ASTContext &C,
165 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000166 ObjCProtocolDecl **Elts, unsigned NumElts) {
167 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
168 return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
169}
170
Ted Kremenek1e5e0bf2008-06-06 21:05:33 +0000171ObjCForwardProtocolDecl::~ObjCForwardProtocolDecl() {
172 delete [] ReferencedProtocols;
173}
174
Chris Lattnereee57c02008-04-04 06:12:32 +0000175ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
176 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000177 IdentifierInfo *Id) {
178 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner321b5d12008-03-16 20:47:45 +0000179 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000180}
181
Chris Lattner1b6de332008-03-16 20:53:07 +0000182ObjCCategoryImplDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000183ObjCCategoryImplDecl::Create(ASTContext &C,
184 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000185 ObjCInterfaceDecl *ClassInterface) {
186 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
187 return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
188}
189
190ObjCImplementationDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000191ObjCImplementationDecl::Create(ASTContext &C,
192 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000193 IdentifierInfo *Id,
194 ObjCInterfaceDecl *ClassInterface,
195 ObjCInterfaceDecl *SuperDecl) {
196 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
197 return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
198}
Chris Lattner10318b82008-03-16 00:19:01 +0000199
Chris Lattner2d1c4312008-03-16 21:17:37 +0000200ObjCCompatibleAliasDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000201ObjCCompatibleAliasDecl::Create(ASTContext &C,
202 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000203 IdentifierInfo *Id,
204 ObjCInterfaceDecl* AliasedClass) {
205 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
206 return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
207}
208
Chris Lattnereee57c02008-04-04 06:12:32 +0000209ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000210 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000211 IdentifierInfo *Id,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000212 QualType T,
213 PropertyControl propControl) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000214 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000215 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000216}
217
Chris Lattner10318b82008-03-16 00:19:01 +0000218//===----------------------------------------------------------------------===//
219// Objective-C Decl Implementation
220//===----------------------------------------------------------------------===//
221
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000222void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
223 const ObjCInterfaceDecl *OID) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000224 QualType selfTy;
225 if (isInstance()) {
226 // There may be no interface context due to error in declaration
227 // of the interface (which has been reported). Recover gracefully.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000228 if (OID) {
229 selfTy = Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl *>(OID));
Daniel Dunbar88116372008-08-26 06:08:30 +0000230 selfTy = Context.getPointerType(selfTy);
231 } else {
232 selfTy = Context.getObjCIdType();
233 }
234 } else // we have a factory method.
235 selfTy = Context.getObjCClassType();
236
237 SelfDecl = ImplicitParamDecl::Create(Context, this,
238 SourceLocation(),
239 &Context.Idents.get("self"),
240 selfTy, 0);
241
242 CmdDecl = ImplicitParamDecl::Create(Context, this,
243 SourceLocation(),
244 &Context.Idents.get("_cmd"),
245 Context.getObjCSelType(), 0);
246}
247
Chris Lattner10318b82008-03-16 00:19:01 +0000248void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
249 unsigned NumParams) {
250 assert(ParamInfo == 0 && "Already has param info!");
251
252 // Zero params -> null pointer.
253 if (NumParams) {
254 ParamInfo = new ParmVarDecl*[NumParams];
255 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
256 NumMethodParams = NumParams;
257 }
258}
259
Fariborz Jahanian4739da12008-11-25 21:48:26 +0000260/// isPropertyReadonly - Return true if property is a readonly, by seaching
261/// for the property in the class and in its categories.
262///
263bool ObjCInterfaceDecl::isPropertyReadonly(ObjCPropertyDecl *PDecl) const
264{
265 if (PDecl->isReadOnly()) {
266 // Main class has the property as 'readyonly'. Must search
267 // through the category list to see if the property's
268 // attribute has been over-ridden to 'readwrite'.
269 for (ObjCCategoryDecl *Category = getCategoryList();
270 Category; Category = Category->getNextClassCategory()) {
271 PDecl= Category->FindPropertyDeclaration(PDecl->getIdentifier());
272 if (PDecl && !PDecl->isReadOnly())
273 return false;
274 }
275 return true;
276 }
277 return false;
278}
279
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000280/// FindPropertyDeclaration - Finds declaration of the property given its name
281/// in 'PropertyId' and returns it. It returns 0, if not found.
282///
283ObjCPropertyDecl *
284 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
285 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
286 E = classprop_end(); I != E; ++I) {
287 ObjCPropertyDecl *property = *I;
288 if (property->getIdentifier() == PropertyId)
289 return property;
290 }
Steve Naroff30faf472008-06-04 04:46:04 +0000291 // Look through categories.
292 for (ObjCCategoryDecl *Category = getCategoryList();
293 Category; Category = Category->getNextClassCategory()) {
294 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
295 if (property)
296 return property;
297 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000298 // Look through protocols.
299 for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(),
300 E = protocol_end(); I != E; ++I) {
301 ObjCProtocolDecl *Protocol = *I;
302 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
303 if (property)
304 return property;
305 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000306 if (getSuperClass())
307 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000308 return 0;
309}
310
311/// FindCategoryDeclaration - Finds category declaration in the list of
312/// categories for this class and returns it. Name of the category is passed
313/// in 'CategoryId'. If category not found, return 0;
314///
315ObjCCategoryDecl *
316 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
317 for (ObjCCategoryDecl *Category = getCategoryList();
318 Category; Category = Category->getNextClassCategory())
319 if (Category->getIdentifier() == CategoryId)
320 return Category;
321 return 0;
322}
323
324/// FindIvarDeclaration - Find an Ivar declaration in this class given its
325/// name in 'IvarId'. On failure to find, return 0;
326///
327ObjCIvarDecl *
328 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
329 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
330 IVE = ivar_end(); IVI != IVE; ++IVI) {
331 ObjCIvarDecl* Ivar = (*IVI);
332 if (Ivar->getIdentifier() == IvarId)
333 return Ivar;
334 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000335 if (getSuperClass())
336 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000337 return 0;
338}
339
Chris Lattner10318b82008-03-16 00:19:01 +0000340/// ObjCAddInstanceVariablesToClass - Inserts instance variables
341/// into ObjCInterfaceDecl's fields.
342///
343void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
344 unsigned numIvars,
345 SourceLocation RBrac) {
346 NumIvars = numIvars;
347 if (numIvars) {
348 Ivars = new ObjCIvarDecl*[numIvars];
349 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
350 }
351 setLocEnd(RBrac);
352}
353
Fariborz Jahanian09772392008-12-13 22:20:28 +0000354/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
355/// storage which matches this 'ivar'.
356///
357FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000358 const ObjCIvarDecl *ivar) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000359 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000360 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
361 DeclarationName Member = ivar->getDeclName();
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000362 DeclContext::lookup_result Lookup = (const_cast< RecordDecl *>(RecordForDecl))
Steve Naroffab63fd62009-01-08 17:28:14 +0000363 ->lookup(Member);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000364 assert((Lookup.first != Lookup.second) && "field decl not found");
365 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
366 assert(MemberDecl && "field decl not found");
367 return MemberDecl;
368}
369
Chris Lattner10318b82008-03-16 00:19:01 +0000370/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
371/// Variables (Ivars) relative to what declared in @implementation;s class.
372/// Ivars into ObjCImplementationDecl's fields.
373///
374void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
375 ObjCIvarDecl **ivars, unsigned numIvars) {
376 NumIvars = numIvars;
377 if (numIvars) {
378 Ivars = new ObjCIvarDecl*[numIvars];
379 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
380 }
381}
382
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000383/// addProperties - Insert property declaration AST nodes into
384/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-03-16 21:23:50 +0000385///
386void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
387 unsigned NumProperties) {
388 if (NumProperties == 0) return;
389
390 NumPropertyDecl = NumProperties;
391 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
392 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
393}
394
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000395/// mergeProperties - Adds properties to the end of list of current properties
396/// for this class.
397
398void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
399 unsigned NumNewProperties) {
400 if (NumNewProperties == 0) return;
401
402 if (PropertyDecl) {
403 ObjCPropertyDecl **newPropertyDecl =
404 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
405 ObjCPropertyDecl **buf = newPropertyDecl;
406 // put back original properties in buffer.
407 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
408 // Add new properties to this buffer.
409 memcpy(buf+NumPropertyDecl, Properties,
410 NumNewProperties*sizeof(ObjCPropertyDecl*));
Nuno Lopes1ee78042008-05-10 10:31:54 +0000411 delete[] PropertyDecl;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000412 PropertyDecl = newPropertyDecl;
413 NumPropertyDecl += NumNewProperties;
414 }
415 else {
Nuno Lopes1ee78042008-05-10 10:31:54 +0000416 addProperties(Properties, NumNewProperties);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000417 }
418}
419
Steve Naroffab63fd62009-01-08 17:28:14 +0000420// Get the local instance method declared in this interface.
421// FIXME: handle overloading, instance & class methods can have the same name.
422ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
423 lookup_const_result MethodResult = lookup(Sel);
424 if (MethodResult.first)
425 return const_cast<ObjCMethodDecl*>(
426 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
427 return 0;
428}
429
430// Get the local class method declared in this interface.
431ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
432 lookup_const_result MethodResult = lookup(Sel);
433 if (MethodResult.first)
434 return const_cast<ObjCMethodDecl*>(
435 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
436 return 0;
437}
438
439unsigned ObjCContainerDecl::getNumInstanceMethods() const {
440 unsigned sum = 0;
441 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I != E; ++I)
442 sum++;
443 return sum;
444}
445unsigned ObjCContainerDecl::getNumClassMethods() const {
446 unsigned sum = 0;
447 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I != E; ++I)
448 sum++;
449 return sum;
450}
451
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000452/// mergeProperties - Adds properties to the end of list of current properties
453/// for this category.
454
455void ObjCCategoryDecl::mergeProperties(ObjCPropertyDecl **Properties,
456 unsigned NumNewProperties) {
457 if (NumNewProperties == 0) return;
458
459 if (PropertyDecl) {
460 ObjCPropertyDecl **newPropertyDecl =
461 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
462 ObjCPropertyDecl **buf = newPropertyDecl;
463 // put back original properties in buffer.
464 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
465 // Add new properties to this buffer.
466 memcpy(buf+NumPropertyDecl, Properties,
467 NumNewProperties*sizeof(ObjCPropertyDecl*));
468 delete[] PropertyDecl;
469 PropertyDecl = newPropertyDecl;
470 NumPropertyDecl += NumNewProperties;
471 }
472 else {
473 addProperties(Properties, NumNewProperties);
474 }
475}
476
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000477/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000478/// ObjCProtocolDecl's PropertyDecl field.
479///
480void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
481 unsigned NumProperties) {
482 if (NumProperties == 0) return;
483
484 NumPropertyDecl = NumProperties;
485 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
486 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
487}
488
489/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000490/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000491///
492void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
493 unsigned NumProperties) {
494 if (NumProperties == 0) return;
495
496 NumPropertyDecl = NumProperties;
497 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
498 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
499}
500
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000501/// FindPropertyDeclaration - Finds declaration of the property given its name
502/// in 'PropertyId' and returns it. It returns 0, if not found.
503///
504ObjCPropertyDecl *
505ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
506 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
507 E = classprop_end(); I != E; ++I) {
508 ObjCPropertyDecl *property = *I;
509 if (property->getIdentifier() == PropertyId)
510 return property;
511 }
512 return 0;
513}
514
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000515/// FindPropertyDeclaration - Finds declaration of the property given its name
516/// in 'PropertyId' and returns it. It returns 0, if not found.
517///
518ObjCPropertyDecl *
519ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
520 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
521 E = classprop_end(); I != E; ++I) {
522 ObjCPropertyDecl *property = *I;
523 if (property->getIdentifier() == PropertyId)
524 return property;
525 }
526 return 0;
527}
528
Chris Lattner10318b82008-03-16 00:19:01 +0000529ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
530 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
531 ObjCInterfaceDecl* ClassDecl = this;
532 while (ClassDecl != NULL) {
533 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
534 I != E; ++I) {
535 if ((*I)->getIdentifier() == ID) {
536 clsDeclared = ClassDecl;
537 return *I;
538 }
539 }
540 ClassDecl = ClassDecl->getSuperClass();
541 }
542 return NULL;
543}
544
545/// lookupInstanceMethod - This method returns an instance method by looking in
546/// the class, its categories, and its super classes (using a linear search).
547ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
548 ObjCInterfaceDecl* ClassDecl = this;
549 ObjCMethodDecl *MethodDecl = 0;
550
551 while (ClassDecl != NULL) {
552 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
553 return MethodDecl;
554
555 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000556 const ObjCList<ObjCProtocolDecl> &Protocols =
557 ClassDecl->getReferencedProtocols();
558 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
559 E = Protocols.end(); I != E; ++I)
560 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000561 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000562
Chris Lattner10318b82008-03-16 00:19:01 +0000563 // Didn't find one yet - now look through categories.
564 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
565 while (CatDecl) {
566 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
567 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000568
569 // Didn't find one yet - look through protocols.
570 const ObjCList<ObjCProtocolDecl> &Protocols =
571 CatDecl->getReferencedProtocols();
572 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
573 E = Protocols.end(); I != E; ++I)
574 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
575 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000576 CatDecl = CatDecl->getNextClassCategory();
577 }
578 ClassDecl = ClassDecl->getSuperClass();
579 }
580 return NULL;
581}
582
583// lookupClassMethod - This method returns a class method by looking in the
584// class, its categories, and its super classes (using a linear search).
585ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
586 ObjCInterfaceDecl* ClassDecl = this;
587 ObjCMethodDecl *MethodDecl = 0;
588
589 while (ClassDecl != NULL) {
590 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
591 return MethodDecl;
592
593 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000594 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
595 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000596 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000597 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000598
Chris Lattner10318b82008-03-16 00:19:01 +0000599 // Didn't find one yet - now look through categories.
600 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
601 while (CatDecl) {
602 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
603 return MethodDecl;
604 CatDecl = CatDecl->getNextClassCategory();
605 }
606 ClassDecl = ClassDecl->getSuperClass();
607 }
608 return NULL;
609}
610
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000611/// getInstanceMethod - This method returns an instance method by
612/// looking in the class implementation. Unlike interfaces, we don't
613/// look outside the implementation.
614ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000615 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
616 if ((*I)->getSelector() == Sel)
617 return *I;
618 return NULL;
619}
620
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000621/// getClassMethod - This method returns a class method by looking in
622/// the class implementation. Unlike interfaces, we don't look outside
623/// the implementation.
624ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000625 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
626 I != E; ++I)
627 if ((*I)->getSelector() == Sel)
628 return *I;
629 return NULL;
630}
631
Fariborz Jahanian68342282008-12-05 22:32:48 +0000632/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
633/// added to the list of those properties @synthesized/@dynamic in this
634/// @implementation block.
635///
636ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
637 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
638 ObjCPropertyImplDecl *PID = *i;
639 if (PID->getPropertyDecl()->getIdentifier() == Id)
640 return PID;
641 }
642 return 0;
643}
644
645/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000646/// properties implemented in this @implementation block and returns the
647/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000648///
649ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
650 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
651 ObjCPropertyImplDecl *PID = *i;
652 if (PID->getPropertyIvarDecl() &&
653 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
654 return PID;
655 }
656 return 0;
657}
658
659/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000660/// properties implemented in this category @implementation block and returns the
661/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000662///
663ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
664 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
665 ObjCPropertyImplDecl *PID = *i;
666 if (PID->getPropertyIvarDecl() &&
667 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
668 return PID;
669 }
670 return 0;
671}
672
673/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
674/// added to the list of those properties @synthesized/@dynamic in this
675/// category @implementation block.
676///
677ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
678 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
679 ObjCPropertyImplDecl *PID = *i;
680 if (PID->getPropertyDecl()->getIdentifier() == Id)
681 return PID;
682 }
683 return 0;
684}
685
Chris Lattner10318b82008-03-16 00:19:01 +0000686// lookupInstanceMethod - This method returns an instance method by looking in
687// the class implementation. Unlike interfaces, we don't look outside the
688// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000689ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000690 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
691 if ((*I)->getSelector() == Sel)
692 return *I;
693 return NULL;
694}
695
696// lookupClassMethod - This method returns an instance method by looking in
697// the class implementation. Unlike interfaces, we don't look outside the
698// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000699ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000700 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
701 I != E; ++I)
702 if ((*I)->getSelector() == Sel)
703 return *I;
704 return NULL;
705}
706
707// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
708// it inherited.
709ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
710 ObjCMethodDecl *MethodDecl = NULL;
711
712 if ((MethodDecl = getInstanceMethod(Sel)))
713 return MethodDecl;
714
Chris Lattner0be08822008-07-21 21:32:27 +0000715 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000716 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000717 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000718 return NULL;
719}
720
721// lookupInstanceMethod - Lookup a class method in the protocol and protocols
722// it inherited.
723ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
724 ObjCMethodDecl *MethodDecl = NULL;
725
726 if ((MethodDecl = getClassMethod(Sel)))
727 return MethodDecl;
728
Chris Lattner0be08822008-07-21 21:32:27 +0000729 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000730 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000731 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000732 return NULL;
733}
734
735/// getSynthesizedMethodSize - Compute size of synthesized method name
736/// as done be the rewrite.
737///
738unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
739 // syntesized method name is a concatenation of -/+[class-name selector]
740 // Get length of this name.
741 unsigned length = 3; // _I_ or _C_
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000742 length += getClassInterface()->getNameAsString().size()+1; // extra for _
Steve Naroff438be772009-01-08 19:41:02 +0000743 if (const ObjCCategoryImplDecl *CID =
744 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000745 length += CID->getNameAsString().size()+1;
Chris Lattner3a8f2942008-11-24 03:33:13 +0000746 length += getSelector().getAsString().size(); // selector name
Chris Lattner10318b82008-03-16 00:19:01 +0000747 return length;
748}
749
Chris Lattner052fbcd2008-04-06 05:25:03 +0000750ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Steve Naroff438be772009-01-08 19:41:02 +0000751 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000752 return ID;
Steve Naroff438be772009-01-08 19:41:02 +0000753 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000754 return CD->getClassInterface();
755 if (ObjCImplementationDecl *IMD =
Steve Naroff438be772009-01-08 19:41:02 +0000756 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000757 return IMD->getClassInterface();
Steve Naroff438be772009-01-08 19:41:02 +0000758 if (ObjCCategoryImplDecl *CID =
759 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000760 return CID->getClassInterface();
761 assert(false && "unknown method context");
762 return 0;
763}
Chris Lattner44859612008-03-17 01:19:02 +0000764
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000765ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
766 SourceLocation atLoc,
767 SourceLocation L,
768 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000769 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000770 ObjCIvarDecl *ivar) {
771 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000772 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000773}
Chris Lattner44859612008-03-17 01:19:02 +0000774
Chris Lattnereee57c02008-04-04 06:12:32 +0000775