blob: 194544f9fa81a0f0693cff6a77eed0cda14d8755 [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,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000054 DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000055 SourceLocation atLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000056 IdentifierInfo *Id,
57 SourceLocation ClassLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000058 bool ForwardDecl, bool isInternal){
59 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000060 return new (Mem) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
Chris Lattner0db541b2008-03-16 01:15:50 +000061 isInternal);
62}
63
Steve Naroffd9d4d502009-01-07 17:57:40 +000064ObjCContainerDecl::~ObjCContainerDecl() {
Steve Naroffd9d4d502009-01-07 17:57:40 +000065}
66
67ObjCInterfaceDecl::~ObjCInterfaceDecl() {
68 delete [] Ivars;
Ted Kremenek8c945b12008-06-06 16:45:15 +000069 delete [] PropertyDecl;
70 // FIXME: CategoryList?
71}
72
73void ObjCInterfaceDecl::Destroy(ASTContext& C) {
74 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
75 if (*I) (*I)->Destroy(C);
76
Ted Kremenek788ef0a2009-01-08 20:49:27 +000077 for (method_iterator I=meth_begin(), E=meth_end(); I!=E; ++I)
78 if (*I) const_cast<ObjCMethodDecl*>((*I))->Destroy(C);
Ted Kremenek22db1602008-06-06 17:21:42 +000079
80 // FIXME: Because there is no clear ownership
81 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
82 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
83
Ted Kremenek8c945b12008-06-06 16:45:15 +000084 Decl::Destroy(C);
85}
86
87
Chris Lattnerf3874bc2008-04-06 04:47:34 +000088ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Ted Kremenek173dd312008-07-23 18:04:17 +000089 IdentifierInfo *Id, QualType T,
90 AccessControl ac, Expr *BW) {
Chris Lattner0db541b2008-03-16 01:15:50 +000091 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Ted Kremenek173dd312008-07-23 18:04:17 +000092 return new (Mem) ObjCIvarDecl(L, Id, T, ac, BW);
Chris Lattner114add62008-03-16 00:49:28 +000093}
94
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000095
96ObjCAtDefsFieldDecl
Douglas Gregor8acb7272008-12-11 16:49:14 +000097*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000098 IdentifierInfo *Id, QualType T, Expr *BW) {
99 void *Mem = C.getAllocator().Allocate<ObjCAtDefsFieldDecl>();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000100 return new (Mem) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000101}
102
103void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
104 this->~ObjCAtDefsFieldDecl();
105 C.getAllocator().Deallocate((void *)this);
106}
107
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000108ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000109 SourceLocation L,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000110 IdentifierInfo *Id) {
Chris Lattner180f7e22008-03-16 01:23:04 +0000111 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000112 return new (Mem) ObjCProtocolDecl(DC, L, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +0000113}
114
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000115ObjCProtocolDecl::~ObjCProtocolDecl() {
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000116 delete [] PropertyDecl;
117}
118
119void ObjCProtocolDecl::Destroy(ASTContext& C) {
120
121 // Referenced Protocols are not owned, so don't Destroy them.
122
Ted Kremenek788ef0a2009-01-08 20:49:27 +0000123 for (method_iterator I=meth_begin(), E=meth_end(); I!=E; ++I)
124 if (*I) const_cast<ObjCMethodDecl*>((*I))->Destroy(C);
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000125
126 // FIXME: Because there is no clear ownership
127 // role between ObjCProtocolDecls and the ObjCPropertyDecls that they
128 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
129
130 Decl::Destroy(C);
131}
132
133
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000134ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000135 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000136 ObjCInterfaceDecl **Elts, unsigned nElts) {
137 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000138 return new (Mem) ObjCClassDecl(DC, L, Elts, nElts);
Chris Lattnere29dc832008-03-16 20:34:23 +0000139}
140
Ted Kremenekff291622008-06-06 20:11:53 +0000141ObjCClassDecl::~ObjCClassDecl() {
142 delete [] ForwardDecls;
143}
144
145void ObjCClassDecl::Destroy(ASTContext& C) {
146
147 // FIXME: There is no clear ownership policy now for referenced
148 // ObjCInterfaceDecls. Some of them can be forward declarations that
149 // are never later defined (in which case the ObjCClassDecl owns them)
150 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
151 // we should have separate objects for forward declarations and definitions,
152 // obviating this problem. Because of this situation, referenced
153 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
154
155 Decl::Destroy(C);
156}
157
Chris Lattnere29dc832008-03-16 20:34:23 +0000158ObjCForwardProtocolDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000159ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000160 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000161 ObjCProtocolDecl **Elts, unsigned NumElts) {
162 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000163 return new (Mem) ObjCForwardProtocolDecl(DC, L, Elts, NumElts);
Chris Lattnere29dc832008-03-16 20:34:23 +0000164}
165
Ted Kremenek1e5e0bf2008-06-06 21:05:33 +0000166ObjCForwardProtocolDecl::~ObjCForwardProtocolDecl() {
167 delete [] ReferencedProtocols;
168}
169
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000170ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000171 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000172 IdentifierInfo *Id) {
173 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000174 return new (Mem) ObjCCategoryDecl(DC, L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000175}
176
Chris Lattner1b6de332008-03-16 20:53:07 +0000177ObjCCategoryImplDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000178ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000179 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000180 ObjCInterfaceDecl *ClassInterface) {
181 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000182 return new (Mem) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
Chris Lattner1b6de332008-03-16 20:53:07 +0000183}
184
185ObjCImplementationDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000186ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000187 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000188 IdentifierInfo *Id,
189 ObjCInterfaceDecl *ClassInterface,
190 ObjCInterfaceDecl *SuperDecl) {
191 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000192 return new (Mem) ObjCImplementationDecl(DC, L, Id, ClassInterface, SuperDecl);
Chris Lattner1b6de332008-03-16 20:53:07 +0000193}
Chris Lattner10318b82008-03-16 00:19:01 +0000194
Chris Lattner2d1c4312008-03-16 21:17:37 +0000195ObjCCompatibleAliasDecl *
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000196ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000197 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000198 IdentifierInfo *Id,
199 ObjCInterfaceDecl* AliasedClass) {
200 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000201 return new (Mem) 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) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000209 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000210 return new (Mem) ObjCPropertyDecl(DC, L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000211}
212
Chris Lattner10318b82008-03-16 00:19:01 +0000213//===----------------------------------------------------------------------===//
214// Objective-C Decl Implementation
215//===----------------------------------------------------------------------===//
216
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000217void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
218 const ObjCInterfaceDecl *OID) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000219 QualType selfTy;
220 if (isInstance()) {
221 // There may be no interface context due to error in declaration
222 // of the interface (which has been reported). Recover gracefully.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000223 if (OID) {
224 selfTy = Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl *>(OID));
Daniel Dunbar88116372008-08-26 06:08:30 +0000225 selfTy = Context.getPointerType(selfTy);
226 } else {
227 selfTy = Context.getObjCIdType();
228 }
229 } else // we have a factory method.
230 selfTy = Context.getObjCClassType();
231
232 SelfDecl = ImplicitParamDecl::Create(Context, this,
233 SourceLocation(),
234 &Context.Idents.get("self"),
235 selfTy, 0);
236
237 CmdDecl = ImplicitParamDecl::Create(Context, this,
238 SourceLocation(),
239 &Context.Idents.get("_cmd"),
240 Context.getObjCSelType(), 0);
241}
242
Chris Lattner10318b82008-03-16 00:19:01 +0000243void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
244 unsigned NumParams) {
245 assert(ParamInfo == 0 && "Already has param info!");
246
247 // Zero params -> null pointer.
248 if (NumParams) {
249 ParamInfo = new ParmVarDecl*[NumParams];
250 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
251 NumMethodParams = NumParams;
252 }
253}
254
Fariborz Jahanian4739da12008-11-25 21:48:26 +0000255/// isPropertyReadonly - Return true if property is a readonly, by seaching
256/// for the property in the class and in its categories.
257///
258bool ObjCInterfaceDecl::isPropertyReadonly(ObjCPropertyDecl *PDecl) const
259{
260 if (PDecl->isReadOnly()) {
261 // Main class has the property as 'readyonly'. Must search
262 // through the category list to see if the property's
263 // attribute has been over-ridden to 'readwrite'.
264 for (ObjCCategoryDecl *Category = getCategoryList();
265 Category; Category = Category->getNextClassCategory()) {
266 PDecl= Category->FindPropertyDeclaration(PDecl->getIdentifier());
267 if (PDecl && !PDecl->isReadOnly())
268 return false;
269 }
270 return true;
271 }
272 return false;
273}
274
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000275/// FindPropertyDeclaration - Finds declaration of the property given its name
276/// in 'PropertyId' and returns it. It returns 0, if not found.
277///
278ObjCPropertyDecl *
279 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
280 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
281 E = classprop_end(); I != E; ++I) {
282 ObjCPropertyDecl *property = *I;
283 if (property->getIdentifier() == PropertyId)
284 return property;
285 }
Steve Naroff30faf472008-06-04 04:46:04 +0000286 // Look through categories.
287 for (ObjCCategoryDecl *Category = getCategoryList();
288 Category; Category = Category->getNextClassCategory()) {
289 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
290 if (property)
291 return property;
292 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000293 // Look through protocols.
294 for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(),
295 E = protocol_end(); I != E; ++I) {
296 ObjCProtocolDecl *Protocol = *I;
297 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
298 if (property)
299 return property;
300 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000301 if (getSuperClass())
302 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000303 return 0;
304}
305
306/// FindCategoryDeclaration - Finds category declaration in the list of
307/// categories for this class and returns it. Name of the category is passed
308/// in 'CategoryId'. If category not found, return 0;
309///
310ObjCCategoryDecl *
311 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
312 for (ObjCCategoryDecl *Category = getCategoryList();
313 Category; Category = Category->getNextClassCategory())
314 if (Category->getIdentifier() == CategoryId)
315 return Category;
316 return 0;
317}
318
319/// FindIvarDeclaration - Find an Ivar declaration in this class given its
320/// name in 'IvarId'. On failure to find, return 0;
321///
322ObjCIvarDecl *
323 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
324 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
325 IVE = ivar_end(); IVI != IVE; ++IVI) {
326 ObjCIvarDecl* Ivar = (*IVI);
327 if (Ivar->getIdentifier() == IvarId)
328 return Ivar;
329 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000330 if (getSuperClass())
331 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000332 return 0;
333}
334
Chris Lattner10318b82008-03-16 00:19:01 +0000335/// ObjCAddInstanceVariablesToClass - Inserts instance variables
336/// into ObjCInterfaceDecl's fields.
337///
338void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
339 unsigned numIvars,
340 SourceLocation RBrac) {
341 NumIvars = numIvars;
342 if (numIvars) {
343 Ivars = new ObjCIvarDecl*[numIvars];
344 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
345 }
346 setLocEnd(RBrac);
347}
348
Fariborz Jahanian09772392008-12-13 22:20:28 +0000349/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
350/// storage which matches this 'ivar'.
351///
352FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000353 const ObjCIvarDecl *ivar) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000354 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000355 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
356 DeclarationName Member = ivar->getDeclName();
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000357 DeclContext::lookup_result Lookup = (const_cast< RecordDecl *>(RecordForDecl))
Steve Naroffab63fd62009-01-08 17:28:14 +0000358 ->lookup(Member);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000359 assert((Lookup.first != Lookup.second) && "field decl not found");
360 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
361 assert(MemberDecl && "field decl not found");
362 return MemberDecl;
363}
364
Chris Lattner10318b82008-03-16 00:19:01 +0000365/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
366/// Variables (Ivars) relative to what declared in @implementation;s class.
367/// Ivars into ObjCImplementationDecl's fields.
368///
369void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
370 ObjCIvarDecl **ivars, unsigned numIvars) {
371 NumIvars = numIvars;
372 if (numIvars) {
373 Ivars = new ObjCIvarDecl*[numIvars];
374 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
375 }
376}
377
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000378/// addProperties - Insert property declaration AST nodes into
379/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-03-16 21:23:50 +0000380///
381void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
382 unsigned NumProperties) {
383 if (NumProperties == 0) return;
384
385 NumPropertyDecl = NumProperties;
386 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
387 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
388}
389
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000390/// mergeProperties - Adds properties to the end of list of current properties
391/// for this class.
392
393void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
394 unsigned NumNewProperties) {
395 if (NumNewProperties == 0) return;
396
397 if (PropertyDecl) {
398 ObjCPropertyDecl **newPropertyDecl =
399 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
400 ObjCPropertyDecl **buf = newPropertyDecl;
401 // put back original properties in buffer.
402 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
403 // Add new properties to this buffer.
404 memcpy(buf+NumPropertyDecl, Properties,
405 NumNewProperties*sizeof(ObjCPropertyDecl*));
Nuno Lopes1ee78042008-05-10 10:31:54 +0000406 delete[] PropertyDecl;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000407 PropertyDecl = newPropertyDecl;
408 NumPropertyDecl += NumNewProperties;
409 }
410 else {
Nuno Lopes1ee78042008-05-10 10:31:54 +0000411 addProperties(Properties, NumNewProperties);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000412 }
413}
414
Steve Naroffab63fd62009-01-08 17:28:14 +0000415// Get the local instance method declared in this interface.
416// FIXME: handle overloading, instance & class methods can have the same name.
417ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
418 lookup_const_result MethodResult = lookup(Sel);
419 if (MethodResult.first)
420 return const_cast<ObjCMethodDecl*>(
421 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
422 return 0;
423}
424
425// Get the local class method declared in this interface.
426ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
427 lookup_const_result MethodResult = lookup(Sel);
428 if (MethodResult.first)
429 return const_cast<ObjCMethodDecl*>(
430 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
431 return 0;
432}
433
434unsigned ObjCContainerDecl::getNumInstanceMethods() const {
435 unsigned sum = 0;
436 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I != E; ++I)
437 sum++;
438 return sum;
439}
440unsigned ObjCContainerDecl::getNumClassMethods() const {
441 unsigned sum = 0;
442 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I != E; ++I)
443 sum++;
444 return sum;
445}
446
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000447/// mergeProperties - Adds properties to the end of list of current properties
448/// for this category.
449
450void ObjCCategoryDecl::mergeProperties(ObjCPropertyDecl **Properties,
451 unsigned NumNewProperties) {
452 if (NumNewProperties == 0) return;
453
454 if (PropertyDecl) {
455 ObjCPropertyDecl **newPropertyDecl =
456 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
457 ObjCPropertyDecl **buf = newPropertyDecl;
458 // put back original properties in buffer.
459 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
460 // Add new properties to this buffer.
461 memcpy(buf+NumPropertyDecl, Properties,
462 NumNewProperties*sizeof(ObjCPropertyDecl*));
463 delete[] PropertyDecl;
464 PropertyDecl = newPropertyDecl;
465 NumPropertyDecl += NumNewProperties;
466 }
467 else {
468 addProperties(Properties, NumNewProperties);
469 }
470}
471
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000472/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000473/// ObjCProtocolDecl's PropertyDecl field.
474///
475void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
476 unsigned NumProperties) {
477 if (NumProperties == 0) return;
478
479 NumPropertyDecl = NumProperties;
480 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
481 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
482}
483
484/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000485/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000486///
487void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
488 unsigned NumProperties) {
489 if (NumProperties == 0) return;
490
491 NumPropertyDecl = NumProperties;
492 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
493 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
494}
495
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000496/// FindPropertyDeclaration - Finds declaration of the property given its name
497/// in 'PropertyId' and returns it. It returns 0, if not found.
498///
499ObjCPropertyDecl *
500ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
501 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
502 E = classprop_end(); I != E; ++I) {
503 ObjCPropertyDecl *property = *I;
504 if (property->getIdentifier() == PropertyId)
505 return property;
506 }
507 return 0;
508}
509
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000510/// FindPropertyDeclaration - Finds declaration of the property given its name
511/// in 'PropertyId' and returns it. It returns 0, if not found.
512///
513ObjCPropertyDecl *
514ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
515 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
516 E = classprop_end(); I != E; ++I) {
517 ObjCPropertyDecl *property = *I;
518 if (property->getIdentifier() == PropertyId)
519 return property;
520 }
521 return 0;
522}
523
Chris Lattner10318b82008-03-16 00:19:01 +0000524ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
525 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
526 ObjCInterfaceDecl* ClassDecl = this;
527 while (ClassDecl != NULL) {
528 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
529 I != E; ++I) {
530 if ((*I)->getIdentifier() == ID) {
531 clsDeclared = ClassDecl;
532 return *I;
533 }
534 }
535 ClassDecl = ClassDecl->getSuperClass();
536 }
537 return NULL;
538}
539
540/// lookupInstanceMethod - This method returns an instance method by looking in
541/// the class, its categories, and its super classes (using a linear search).
542ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
543 ObjCInterfaceDecl* ClassDecl = this;
544 ObjCMethodDecl *MethodDecl = 0;
545
546 while (ClassDecl != NULL) {
547 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
548 return MethodDecl;
549
550 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000551 const ObjCList<ObjCProtocolDecl> &Protocols =
552 ClassDecl->getReferencedProtocols();
553 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
554 E = Protocols.end(); I != E; ++I)
555 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000556 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000557
Chris Lattner10318b82008-03-16 00:19:01 +0000558 // Didn't find one yet - now look through categories.
559 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
560 while (CatDecl) {
561 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
562 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000563
564 // Didn't find one yet - look through protocols.
565 const ObjCList<ObjCProtocolDecl> &Protocols =
566 CatDecl->getReferencedProtocols();
567 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
568 E = Protocols.end(); I != E; ++I)
569 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
570 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000571 CatDecl = CatDecl->getNextClassCategory();
572 }
573 ClassDecl = ClassDecl->getSuperClass();
574 }
575 return NULL;
576}
577
578// lookupClassMethod - This method returns a class method by looking in the
579// class, its categories, and its super classes (using a linear search).
580ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
581 ObjCInterfaceDecl* ClassDecl = this;
582 ObjCMethodDecl *MethodDecl = 0;
583
584 while (ClassDecl != NULL) {
585 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
586 return MethodDecl;
587
588 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000589 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
590 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000591 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000592 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000593
Chris Lattner10318b82008-03-16 00:19:01 +0000594 // Didn't find one yet - now look through categories.
595 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
596 while (CatDecl) {
597 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
598 return MethodDecl;
599 CatDecl = CatDecl->getNextClassCategory();
600 }
601 ClassDecl = ClassDecl->getSuperClass();
602 }
603 return NULL;
604}
605
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000606/// getInstanceMethod - This method returns an instance method by
607/// looking in the class implementation. Unlike interfaces, we don't
608/// look outside the implementation.
609ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000610 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
611 if ((*I)->getSelector() == Sel)
612 return *I;
613 return NULL;
614}
615
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000616/// getClassMethod - This method returns a class method by looking in
617/// the class implementation. Unlike interfaces, we don't look outside
618/// the implementation.
619ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000620 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
621 I != E; ++I)
622 if ((*I)->getSelector() == Sel)
623 return *I;
624 return NULL;
625}
626
Fariborz Jahanian68342282008-12-05 22:32:48 +0000627/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
628/// added to the list of those properties @synthesized/@dynamic in this
629/// @implementation block.
630///
631ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
632 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
633 ObjCPropertyImplDecl *PID = *i;
634 if (PID->getPropertyDecl()->getIdentifier() == Id)
635 return PID;
636 }
637 return 0;
638}
639
640/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000641/// properties implemented in this @implementation block and returns the
642/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000643///
644ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
645 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
646 ObjCPropertyImplDecl *PID = *i;
647 if (PID->getPropertyIvarDecl() &&
648 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
649 return PID;
650 }
651 return 0;
652}
653
654/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000655/// properties implemented in this category @implementation block and returns the
656/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000657///
658ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
659 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
660 ObjCPropertyImplDecl *PID = *i;
661 if (PID->getPropertyIvarDecl() &&
662 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
663 return PID;
664 }
665 return 0;
666}
667
668/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
669/// added to the list of those properties @synthesized/@dynamic in this
670/// category @implementation block.
671///
672ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
673 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
674 ObjCPropertyImplDecl *PID = *i;
675 if (PID->getPropertyDecl()->getIdentifier() == Id)
676 return PID;
677 }
678 return 0;
679}
680
Chris Lattner10318b82008-03-16 00:19:01 +0000681// lookupInstanceMethod - This method returns an instance method by looking in
682// the class implementation. Unlike interfaces, we don't look outside the
683// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000684ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000685 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
686 if ((*I)->getSelector() == Sel)
687 return *I;
688 return NULL;
689}
690
691// lookupClassMethod - This method returns an instance method by looking in
692// the class implementation. Unlike interfaces, we don't look outside the
693// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000694ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000695 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
696 I != E; ++I)
697 if ((*I)->getSelector() == Sel)
698 return *I;
699 return NULL;
700}
701
702// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
703// it inherited.
704ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
705 ObjCMethodDecl *MethodDecl = NULL;
706
707 if ((MethodDecl = getInstanceMethod(Sel)))
708 return MethodDecl;
709
Chris Lattner0be08822008-07-21 21:32:27 +0000710 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000711 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000712 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000713 return NULL;
714}
715
716// lookupInstanceMethod - Lookup a class method in the protocol and protocols
717// it inherited.
718ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
719 ObjCMethodDecl *MethodDecl = NULL;
720
721 if ((MethodDecl = getClassMethod(Sel)))
722 return MethodDecl;
723
Chris Lattner0be08822008-07-21 21:32:27 +0000724 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Steve Naroff30fb1da2008-12-18 15:50:41 +0000725 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner0be08822008-07-21 21:32:27 +0000726 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000727 return NULL;
728}
729
730/// getSynthesizedMethodSize - Compute size of synthesized method name
731/// as done be the rewrite.
732///
733unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
734 // syntesized method name is a concatenation of -/+[class-name selector]
735 // Get length of this name.
736 unsigned length = 3; // _I_ or _C_
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000737 length += getClassInterface()->getNameAsString().size()+1; // extra for _
Steve Naroff438be772009-01-08 19:41:02 +0000738 if (const ObjCCategoryImplDecl *CID =
739 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000740 length += CID->getNameAsString().size()+1;
Chris Lattner3a8f2942008-11-24 03:33:13 +0000741 length += getSelector().getAsString().size(); // selector name
Chris Lattner10318b82008-03-16 00:19:01 +0000742 return length;
743}
744
Chris Lattner052fbcd2008-04-06 05:25:03 +0000745ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Steve Naroff438be772009-01-08 19:41:02 +0000746 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000747 return ID;
Steve Naroff438be772009-01-08 19:41:02 +0000748 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000749 return CD->getClassInterface();
750 if (ObjCImplementationDecl *IMD =
Steve Naroff438be772009-01-08 19:41:02 +0000751 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000752 return IMD->getClassInterface();
Steve Naroff438be772009-01-08 19:41:02 +0000753 if (ObjCCategoryImplDecl *CID =
754 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
Chris Lattner10318b82008-03-16 00:19:01 +0000755 return CID->getClassInterface();
756 assert(false && "unknown method context");
757 return 0;
758}
Chris Lattner44859612008-03-17 01:19:02 +0000759
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000760ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000761 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000762 SourceLocation atLoc,
763 SourceLocation L,
764 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000765 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000766 ObjCIvarDecl *ivar) {
767 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000768 return new (Mem) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000769}
Chris Lattner44859612008-03-17 01:19:02 +0000770
Chris Lattnereee57c02008-04-04 06:12:32 +0000771