blob: 12c224501c646603e6483c493280e7c22da7aacf [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,
27 Decl *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
Ted Kremenek8c945b12008-06-06 16:45:15 +000063ObjCInterfaceDecl::~ObjCInterfaceDecl() {
64 delete [] Ivars;
65 delete [] InstanceMethods;
66 delete [] ClassMethods;
67 delete [] PropertyDecl;
68 // FIXME: CategoryList?
69}
70
71void ObjCInterfaceDecl::Destroy(ASTContext& C) {
72 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
73 if (*I) (*I)->Destroy(C);
74
75 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
76 if (*I) (*I)->Destroy(C);
77
78 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
79 if (*I) (*I)->Destroy(C);
Ted Kremenek22db1602008-06-06 17:21:42 +000080
81 // FIXME: Because there is no clear ownership
82 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
83 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
84
Ted Kremenek8c945b12008-06-06 16:45:15 +000085 Decl::Destroy(C);
86}
87
88
Chris Lattnerf3874bc2008-04-06 04:47:34 +000089ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Ted Kremenek173dd312008-07-23 18:04:17 +000090 IdentifierInfo *Id, QualType T,
91 AccessControl ac, Expr *BW) {
Chris Lattner0db541b2008-03-16 01:15:50 +000092 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Ted Kremenek173dd312008-07-23 18:04:17 +000093 return new (Mem) ObjCIvarDecl(L, Id, T, ac, BW);
Chris Lattner114add62008-03-16 00:49:28 +000094}
95
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000096
97ObjCAtDefsFieldDecl
Douglas Gregor8acb7272008-12-11 16:49:14 +000098*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000099 IdentifierInfo *Id, QualType T, Expr *BW) {
100 void *Mem = C.getAllocator().Allocate<ObjCAtDefsFieldDecl>();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000101 return new (Mem) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000102}
103
104void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
105 this->~ObjCAtDefsFieldDecl();
106 C.getAllocator().Deallocate((void *)this);
107}
108
Chris Lattnereee57c02008-04-04 06:12:32 +0000109ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
110 SourceLocation L,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000111 IdentifierInfo *Id) {
Chris Lattner180f7e22008-03-16 01:23:04 +0000112 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner0be08822008-07-21 21:32:27 +0000113 return new (Mem) ObjCProtocolDecl(L, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +0000114}
115
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000116ObjCProtocolDecl::~ObjCProtocolDecl() {
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000117 delete [] InstanceMethods;
118 delete [] ClassMethods;
119 delete [] PropertyDecl;
120}
121
122void ObjCProtocolDecl::Destroy(ASTContext& C) {
123
124 // Referenced Protocols are not owned, so don't Destroy them.
125
126 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
127 if (*I) (*I)->Destroy(C);
128
129 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
130 if (*I) (*I)->Destroy(C);
131
132 // FIXME: Because there is no clear ownership
133 // role between ObjCProtocolDecls and the ObjCPropertyDecls that they
134 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
135
136 Decl::Destroy(C);
137}
138
139
Chris Lattnereee57c02008-04-04 06:12:32 +0000140ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
141 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000142 ObjCInterfaceDecl **Elts, unsigned nElts) {
143 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
144 return new (Mem) ObjCClassDecl(L, Elts, nElts);
145}
146
Ted Kremenekff291622008-06-06 20:11:53 +0000147ObjCClassDecl::~ObjCClassDecl() {
148 delete [] ForwardDecls;
149}
150
151void ObjCClassDecl::Destroy(ASTContext& C) {
152
153 // FIXME: There is no clear ownership policy now for referenced
154 // ObjCInterfaceDecls. Some of them can be forward declarations that
155 // are never later defined (in which case the ObjCClassDecl owns them)
156 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
157 // we should have separate objects for forward declarations and definitions,
158 // obviating this problem. Because of this situation, referenced
159 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
160
161 Decl::Destroy(C);
162}
163
Chris Lattnere29dc832008-03-16 20:34:23 +0000164ObjCForwardProtocolDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000165ObjCForwardProtocolDecl::Create(ASTContext &C,
166 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000167 ObjCProtocolDecl **Elts, unsigned NumElts) {
168 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
169 return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
170}
171
Ted Kremenek1e5e0bf2008-06-06 21:05:33 +0000172ObjCForwardProtocolDecl::~ObjCForwardProtocolDecl() {
173 delete [] ReferencedProtocols;
174}
175
Chris Lattnereee57c02008-04-04 06:12:32 +0000176ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
177 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000178 IdentifierInfo *Id) {
179 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner321b5d12008-03-16 20:47:45 +0000180 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000181}
182
Chris Lattner1b6de332008-03-16 20:53:07 +0000183ObjCCategoryImplDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000184ObjCCategoryImplDecl::Create(ASTContext &C,
185 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000186 ObjCInterfaceDecl *ClassInterface) {
187 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
188 return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
189}
190
191ObjCImplementationDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000192ObjCImplementationDecl::Create(ASTContext &C,
193 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000194 IdentifierInfo *Id,
195 ObjCInterfaceDecl *ClassInterface,
196 ObjCInterfaceDecl *SuperDecl) {
197 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
198 return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
199}
Chris Lattner10318b82008-03-16 00:19:01 +0000200
Chris Lattner2d1c4312008-03-16 21:17:37 +0000201ObjCCompatibleAliasDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000202ObjCCompatibleAliasDecl::Create(ASTContext &C,
203 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000204 IdentifierInfo *Id,
205 ObjCInterfaceDecl* AliasedClass) {
206 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
207 return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
208}
209
Chris Lattnereee57c02008-04-04 06:12:32 +0000210ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000211 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000212 IdentifierInfo *Id,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000213 QualType T,
214 PropertyControl propControl) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000215 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000216 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000217}
218
Chris Lattner10318b82008-03-16 00:19:01 +0000219//===----------------------------------------------------------------------===//
220// Objective-C Decl Implementation
221//===----------------------------------------------------------------------===//
222
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000223void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
224 const ObjCInterfaceDecl *OID) {
Daniel Dunbar88116372008-08-26 06:08:30 +0000225 QualType selfTy;
226 if (isInstance()) {
227 // There may be no interface context due to error in declaration
228 // of the interface (which has been reported). Recover gracefully.
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +0000229 if (OID) {
230 selfTy = Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl *>(OID));
Daniel Dunbar88116372008-08-26 06:08:30 +0000231 selfTy = Context.getPointerType(selfTy);
232 } else {
233 selfTy = Context.getObjCIdType();
234 }
235 } else // we have a factory method.
236 selfTy = Context.getObjCClassType();
237
238 SelfDecl = ImplicitParamDecl::Create(Context, this,
239 SourceLocation(),
240 &Context.Idents.get("self"),
241 selfTy, 0);
242
243 CmdDecl = ImplicitParamDecl::Create(Context, this,
244 SourceLocation(),
245 &Context.Idents.get("_cmd"),
246 Context.getObjCSelType(), 0);
247}
248
Chris Lattner10318b82008-03-16 00:19:01 +0000249void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
250 unsigned NumParams) {
251 assert(ParamInfo == 0 && "Already has param info!");
252
253 // Zero params -> null pointer.
254 if (NumParams) {
255 ParamInfo = new ParmVarDecl*[NumParams];
256 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
257 NumMethodParams = NumParams;
258 }
259}
260
Fariborz Jahanian4739da12008-11-25 21:48:26 +0000261/// isPropertyReadonly - Return true if property is a readonly, by seaching
262/// for the property in the class and in its categories.
263///
264bool ObjCInterfaceDecl::isPropertyReadonly(ObjCPropertyDecl *PDecl) const
265{
266 if (PDecl->isReadOnly()) {
267 // Main class has the property as 'readyonly'. Must search
268 // through the category list to see if the property's
269 // attribute has been over-ridden to 'readwrite'.
270 for (ObjCCategoryDecl *Category = getCategoryList();
271 Category; Category = Category->getNextClassCategory()) {
272 PDecl= Category->FindPropertyDeclaration(PDecl->getIdentifier());
273 if (PDecl && !PDecl->isReadOnly())
274 return false;
275 }
276 return true;
277 }
278 return false;
279}
280
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000281/// FindPropertyDeclaration - Finds declaration of the property given its name
282/// in 'PropertyId' and returns it. It returns 0, if not found.
283///
284ObjCPropertyDecl *
285 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
286 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
287 E = classprop_end(); I != E; ++I) {
288 ObjCPropertyDecl *property = *I;
289 if (property->getIdentifier() == PropertyId)
290 return property;
291 }
Steve Naroff30faf472008-06-04 04:46:04 +0000292 // Look through categories.
293 for (ObjCCategoryDecl *Category = getCategoryList();
294 Category; Category = Category->getNextClassCategory()) {
295 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
296 if (property)
297 return property;
298 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000299 // Look through protocols.
300 for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(),
301 E = protocol_end(); I != E; ++I) {
302 ObjCProtocolDecl *Protocol = *I;
303 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
304 if (property)
305 return property;
306 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000307 if (getSuperClass())
308 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000309 return 0;
310}
311
312/// FindCategoryDeclaration - Finds category declaration in the list of
313/// categories for this class and returns it. Name of the category is passed
314/// in 'CategoryId'. If category not found, return 0;
315///
316ObjCCategoryDecl *
317 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
318 for (ObjCCategoryDecl *Category = getCategoryList();
319 Category; Category = Category->getNextClassCategory())
320 if (Category->getIdentifier() == CategoryId)
321 return Category;
322 return 0;
323}
324
325/// FindIvarDeclaration - Find an Ivar declaration in this class given its
326/// name in 'IvarId'. On failure to find, return 0;
327///
328ObjCIvarDecl *
329 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
330 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
331 IVE = ivar_end(); IVI != IVE; ++IVI) {
332 ObjCIvarDecl* Ivar = (*IVI);
333 if (Ivar->getIdentifier() == IvarId)
334 return Ivar;
335 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000336 if (getSuperClass())
337 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000338 return 0;
339}
340
Fariborz Jahanian624921a2008-12-13 20:28:25 +0000341void ObjCInterfaceDecl::CollectObjCIvars(std::vector<FieldDecl*> &Fields) {
342 ObjCInterfaceDecl *SuperClass = getSuperClass();
343 if (SuperClass)
344 SuperClass->CollectObjCIvars(Fields);
345 for (ObjCInterfaceDecl::ivar_iterator I = ivar_begin(),
346 E = ivar_end(); I != E; ++I) {
347 ObjCIvarDecl *IVDecl = (*I);
Fariborz Jahanian0c067092008-12-16 01:08:35 +0000348 if (!IVDecl->isInvalidDecl())
349 Fields.push_back(cast<FieldDecl>(IVDecl));
Fariborz Jahanian624921a2008-12-13 20:28:25 +0000350 }
351}
352
Chris Lattner10318b82008-03-16 00:19:01 +0000353/// ObjCAddInstanceVariablesToClass - Inserts instance variables
354/// into ObjCInterfaceDecl's fields.
355///
356void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
357 unsigned numIvars,
358 SourceLocation RBrac) {
359 NumIvars = numIvars;
360 if (numIvars) {
361 Ivars = new ObjCIvarDecl*[numIvars];
362 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
363 }
364 setLocEnd(RBrac);
365}
366
Fariborz Jahanian09772392008-12-13 22:20:28 +0000367/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
368/// storage which matches this 'ivar'.
369///
370FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000371 const ObjCIvarDecl *ivar) {
Steve Narofff188f2f2008-12-17 14:13:49 +0000372 if (!RecordForDecl)
373 addRecordToClass(Context);
Fariborz Jahanian09772392008-12-13 22:20:28 +0000374 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
375 DeclarationName Member = ivar->getDeclName();
376 DeclContext::lookup_result Lookup = RecordForDecl->lookup(Context, Member);
377 assert((Lookup.first != Lookup.second) && "field decl not found");
378 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
379 assert(MemberDecl && "field decl not found");
380 return MemberDecl;
381}
382
Fariborz Jahaniancb8e8032008-12-15 21:58:08 +0000383/// addRecordToClass - produces record info. for the class for its
Fariborz Jahanian624921a2008-12-13 20:28:25 +0000384/// ivars and all those inherited.
385///
Fariborz Jahaniancb8e8032008-12-15 21:58:08 +0000386void ObjCInterfaceDecl::addRecordToClass(ASTContext &Context)
Fariborz Jahanian624921a2008-12-13 20:28:25 +0000387{
388 std::vector<FieldDecl*> RecFields;
389 CollectObjCIvars(RecFields);
390 RecordDecl *RD = RecordDecl::Create(Context, TagDecl::TK_struct, 0,
391 getLocation(),
392 getIdentifier());
393 /// FIXME! Can do collection of ivars and adding to the record while
394 /// doing it.
395 for (unsigned int i = 0; i != RecFields.size(); i++) {
Fariborz Jahanian0c067092008-12-16 01:08:35 +0000396 FieldDecl *Field = FieldDecl::Create(Context, RD,
397 RecFields[i]->getLocation(),
Fariborz Jahanian624921a2008-12-13 20:28:25 +0000398 RecFields[i]->getIdentifier(),
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000399 RecFields[i]->getType(),
400 RecFields[i]->getBitWidth(), false, 0);
Fariborz Jahanian624921a2008-12-13 20:28:25 +0000401 RD->addDecl(Context, Field);
402 }
403 RD->completeDefinition(Context);
Fariborz Jahanian138f7bb2008-12-15 18:04:20 +0000404 RecordForDecl = RD;
Fariborz Jahanian624921a2008-12-13 20:28:25 +0000405}
406
Chris Lattner10318b82008-03-16 00:19:01 +0000407/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
408/// Variables (Ivars) relative to what declared in @implementation;s class.
409/// Ivars into ObjCImplementationDecl's fields.
410///
411void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
412 ObjCIvarDecl **ivars, unsigned numIvars) {
413 NumIvars = numIvars;
414 if (numIvars) {
415 Ivars = new ObjCIvarDecl*[numIvars];
416 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
417 }
418}
419
420/// addMethods - Insert instance and methods declarations into
421/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
422///
423void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
424 unsigned numInsMembers,
425 ObjCMethodDecl **clsMethods,
426 unsigned numClsMembers,
427 SourceLocation endLoc) {
428 NumInstanceMethods = numInsMembers;
429 if (numInsMembers) {
430 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
431 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
432 }
433 NumClassMethods = numClsMembers;
434 if (numClsMembers) {
435 ClassMethods = new ObjCMethodDecl*[numClsMembers];
436 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
437 }
438 AtEndLoc = endLoc;
439}
440
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000441/// addProperties - Insert property declaration AST nodes into
442/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-03-16 21:23:50 +0000443///
444void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
445 unsigned NumProperties) {
446 if (NumProperties == 0) return;
447
448 NumPropertyDecl = NumProperties;
449 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
450 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
451}
452
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000453/// mergeProperties - Adds properties to the end of list of current properties
454/// for this class.
455
456void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
457 unsigned NumNewProperties) {
458 if (NumNewProperties == 0) return;
459
460 if (PropertyDecl) {
461 ObjCPropertyDecl **newPropertyDecl =
462 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
463 ObjCPropertyDecl **buf = newPropertyDecl;
464 // put back original properties in buffer.
465 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
466 // Add new properties to this buffer.
467 memcpy(buf+NumPropertyDecl, Properties,
468 NumNewProperties*sizeof(ObjCPropertyDecl*));
Nuno Lopes1ee78042008-05-10 10:31:54 +0000469 delete[] PropertyDecl;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000470 PropertyDecl = newPropertyDecl;
471 NumPropertyDecl += NumNewProperties;
472 }
473 else {
Nuno Lopes1ee78042008-05-10 10:31:54 +0000474 addProperties(Properties, NumNewProperties);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000475 }
476}
477
Daniel Dunbard01a7022008-08-27 02:09:39 +0000478static void
479addPropertyMethods(Decl *D,
480 ASTContext &Context,
481 ObjCPropertyDecl *property,
Fariborz Jahanianecfbb492008-12-02 00:19:12 +0000482 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods,
483 llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) {
Daniel Dunbare7abcc42008-08-27 05:13:46 +0000484 ObjCMethodDecl *GetterDecl, *SetterDecl = 0;
Fariborz Jahanianecfbb492008-12-02 00:19:12 +0000485
486 GetterDecl = const_cast<ObjCMethodDecl*>(InsMap[property->getGetterName()]);
487 if (!property->isReadOnly())
488 SetterDecl = const_cast<ObjCMethodDecl*>(InsMap[property->getSetterName()]);
489
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000490 // FIXME: The synthesized property we set here is misleading. We
491 // almost always synthesize these methods unless the user explicitly
492 // provided prototypes (which is odd, but allowed). Sema should be
493 // typechecking that the declarations jive in that situation (which
494 // it is not currently).
495
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000496 // Find the default getter and if one not found, add one.
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000497 if (!GetterDecl) {
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000498 // No instance method of same name as property getter name was found.
499 // Declare a getter method and add it to the list of methods
500 // for this class.
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000501 GetterDecl =
502 ObjCMethodDecl::Create(Context, property->getLocation(),
503 property->getLocation(),
504 property->getGetterName(),
505 property->getType(),
Daniel Dunbard01a7022008-08-27 02:09:39 +0000506 D,
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000507 true, false, true, ObjCMethodDecl::Required);
508 insMethods.push_back(GetterDecl);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +0000509 InsMap[property->getGetterName()] = GetterDecl;
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000510 }
Fariborz Jahanian0b7716c2008-12-09 22:43:22 +0000511 else
512 // A user declared getter will be synthesize when @synthesize of
513 // the property with the same name is seen in the @implementation
514 GetterDecl->setIsSynthesized();
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000515 property->setGetterMethodDecl(GetterDecl);
516
Daniel Dunbardb3b4492008-08-26 07:16:44 +0000517 // Skip setter if property is read-only.
518 if (property->isReadOnly())
519 return;
520
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000521 // Find the default setter and if one not found, add one.
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000522 if (!SetterDecl) {
523 // No instance method of same name as property setter name was found.
524 // Declare a setter method and add it to the list of methods
525 // for this class.
526 SetterDecl =
527 ObjCMethodDecl::Create(Context, property->getLocation(),
528 property->getLocation(),
529 property->getSetterName(),
Daniel Dunbar4b8babe2008-08-26 02:53:23 +0000530 Context.VoidTy,
Daniel Dunbard01a7022008-08-27 02:09:39 +0000531 D,
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000532 true, false, true, ObjCMethodDecl::Required);
533 insMethods.push_back(SetterDecl);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +0000534 InsMap[property->getSetterName()] = SetterDecl;
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000535 // Invent the arguments for the setter. We don't bother making a
536 // nice name for the argument.
537 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
538 SetterDecl,
539 SourceLocation(),
540 property->getIdentifier(),
541 property->getType(),
542 VarDecl::None,
543 0, 0);
544 SetterDecl->setMethodParams(&Argument, 1);
545 }
Fariborz Jahanian0b7716c2008-12-09 22:43:22 +0000546 else
547 // A user declared setter will be synthesize when @synthesize of
548 // the property with the same name is seen in the @implementation
549 SetterDecl->setIsSynthesized();
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000550 property->setSetterMethodDecl(SetterDecl);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000551}
552
Daniel Dunbard01a7022008-08-27 02:09:39 +0000553/// addPropertyMethods - Goes through list of properties declared in this class
554/// and builds setter/getter method declartions depending on the setter/getter
555/// attributes of the property.
556///
557void ObjCInterfaceDecl::addPropertyMethods(
558 ASTContext &Context,
559 ObjCPropertyDecl *property,
Fariborz Jahanianecfbb492008-12-02 00:19:12 +0000560 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods,
561 llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) {
562 ::addPropertyMethods(this, Context, property, insMethods, InsMap);
Daniel Dunbard01a7022008-08-27 02:09:39 +0000563}
564
565/// addPropertyMethods - Goes through list of properties declared in this class
566/// and builds setter/getter method declartions depending on the setter/getter
567/// attributes of the property.
568///
569void ObjCCategoryDecl::addPropertyMethods(
570 ASTContext &Context,
571 ObjCPropertyDecl *property,
Fariborz Jahanianecfbb492008-12-02 00:19:12 +0000572 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods,
573 llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) {
574 ::addPropertyMethods(this, Context, property, insMethods, InsMap);
Daniel Dunbard01a7022008-08-27 02:09:39 +0000575}
576
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000577/// mergeProperties - Adds properties to the end of list of current properties
578/// for this category.
579
580void ObjCCategoryDecl::mergeProperties(ObjCPropertyDecl **Properties,
581 unsigned NumNewProperties) {
582 if (NumNewProperties == 0) return;
583
584 if (PropertyDecl) {
585 ObjCPropertyDecl **newPropertyDecl =
586 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
587 ObjCPropertyDecl **buf = newPropertyDecl;
588 // put back original properties in buffer.
589 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
590 // Add new properties to this buffer.
591 memcpy(buf+NumPropertyDecl, Properties,
592 NumNewProperties*sizeof(ObjCPropertyDecl*));
593 delete[] PropertyDecl;
594 PropertyDecl = newPropertyDecl;
595 NumPropertyDecl += NumNewProperties;
596 }
597 else {
598 addProperties(Properties, NumNewProperties);
599 }
600}
601
Daniel Dunbard01a7022008-08-27 02:09:39 +0000602/// addPropertyMethods - Goes through list of properties declared in this class
603/// and builds setter/getter method declartions depending on the setter/getter
604/// attributes of the property.
605///
606void ObjCProtocolDecl::addPropertyMethods(
607 ASTContext &Context,
608 ObjCPropertyDecl *property,
Fariborz Jahanianecfbb492008-12-02 00:19:12 +0000609 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods,
610 llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) {
611 ::addPropertyMethods(this, Context, property, insMethods, InsMap);
Daniel Dunbard01a7022008-08-27 02:09:39 +0000612}
613
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000614/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000615/// ObjCProtocolDecl's PropertyDecl field.
616///
617void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
618 unsigned NumProperties) {
619 if (NumProperties == 0) return;
620
621 NumPropertyDecl = NumProperties;
622 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
623 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
624}
625
626/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000627/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000628///
629void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
630 unsigned NumProperties) {
631 if (NumProperties == 0) return;
632
633 NumPropertyDecl = NumProperties;
634 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
635 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
636}
637
Chris Lattnercffe3662008-03-16 21:23:50 +0000638/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000639/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
640///
641void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
642 unsigned numInsMembers,
643 ObjCMethodDecl **clsMethods,
644 unsigned numClsMembers,
645 SourceLocation endLoc) {
646 NumInstanceMethods = numInsMembers;
647 if (numInsMembers) {
648 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
649 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
650 }
651 NumClassMethods = numClsMembers;
652 if (numClsMembers) {
653 ClassMethods = new ObjCMethodDecl*[numClsMembers];
654 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
655 }
656 AtEndLoc = endLoc;
657}
658
Chris Lattner321b5d12008-03-16 20:47:45 +0000659
660
Chris Lattner10318b82008-03-16 00:19:01 +0000661/// addMethods - Insert instance and methods declarations into
662/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
663///
664void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
665 unsigned numInsMembers,
666 ObjCMethodDecl **clsMethods,
667 unsigned numClsMembers,
668 SourceLocation endLoc) {
669 NumInstanceMethods = numInsMembers;
670 if (numInsMembers) {
671 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
672 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
673 }
674 NumClassMethods = numClsMembers;
675 if (numClsMembers) {
676 ClassMethods = new ObjCMethodDecl*[numClsMembers];
677 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
678 }
679 AtEndLoc = endLoc;
680}
681
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000682/// FindPropertyDeclaration - Finds declaration of the property given its name
683/// in 'PropertyId' and returns it. It returns 0, if not found.
684///
685ObjCPropertyDecl *
686ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
687 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
688 E = classprop_end(); I != E; ++I) {
689 ObjCPropertyDecl *property = *I;
690 if (property->getIdentifier() == PropertyId)
691 return property;
692 }
693 return 0;
694}
695
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000696/// FindPropertyDeclaration - Finds declaration of the property given its name
697/// in 'PropertyId' and returns it. It returns 0, if not found.
698///
699ObjCPropertyDecl *
700ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
701 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
702 E = classprop_end(); I != E; ++I) {
703 ObjCPropertyDecl *property = *I;
704 if (property->getIdentifier() == PropertyId)
705 return property;
706 }
707 return 0;
708}
709
Chris Lattner10318b82008-03-16 00:19:01 +0000710ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
711 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
712 ObjCInterfaceDecl* ClassDecl = this;
713 while (ClassDecl != NULL) {
714 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
715 I != E; ++I) {
716 if ((*I)->getIdentifier() == ID) {
717 clsDeclared = ClassDecl;
718 return *I;
719 }
720 }
721 ClassDecl = ClassDecl->getSuperClass();
722 }
723 return NULL;
724}
725
726/// lookupInstanceMethod - This method returns an instance method by looking in
727/// the class, its categories, and its super classes (using a linear search).
728ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
729 ObjCInterfaceDecl* ClassDecl = this;
730 ObjCMethodDecl *MethodDecl = 0;
731
732 while (ClassDecl != NULL) {
733 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
734 return MethodDecl;
735
736 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000737 const ObjCList<ObjCProtocolDecl> &Protocols =
738 ClassDecl->getReferencedProtocols();
739 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
740 E = Protocols.end(); I != E; ++I)
741 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000742 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000743
Chris Lattner10318b82008-03-16 00:19:01 +0000744 // Didn't find one yet - now look through categories.
745 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
746 while (CatDecl) {
747 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
748 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000749
750 // Didn't find one yet - look through protocols.
751 const ObjCList<ObjCProtocolDecl> &Protocols =
752 CatDecl->getReferencedProtocols();
753 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
754 E = Protocols.end(); I != E; ++I)
755 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
756 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000757 CatDecl = CatDecl->getNextClassCategory();
758 }
759 ClassDecl = ClassDecl->getSuperClass();
760 }
761 return NULL;
762}
763
764// lookupClassMethod - This method returns a class method by looking in the
765// class, its categories, and its super classes (using a linear search).
766ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
767 ObjCInterfaceDecl* ClassDecl = this;
768 ObjCMethodDecl *MethodDecl = 0;
769
770 while (ClassDecl != NULL) {
771 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
772 return MethodDecl;
773
774 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000775 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
776 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000777 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000778 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000779
Chris Lattner10318b82008-03-16 00:19:01 +0000780 // Didn't find one yet - now look through categories.
781 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
782 while (CatDecl) {
783 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
784 return MethodDecl;
785 CatDecl = CatDecl->getNextClassCategory();
786 }
787 ClassDecl = ClassDecl->getSuperClass();
788 }
789 return NULL;
790}
791
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000792/// getInstanceMethod - This method returns an instance method by
793/// looking in the class implementation. Unlike interfaces, we don't
794/// look outside the implementation.
795ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000796 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
797 if ((*I)->getSelector() == Sel)
798 return *I;
799 return NULL;
800}
801
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000802/// getClassMethod - This method returns a class method by looking in
803/// the class implementation. Unlike interfaces, we don't look outside
804/// the implementation.
805ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000806 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
807 I != E; ++I)
808 if ((*I)->getSelector() == Sel)
809 return *I;
810 return NULL;
811}
812
Fariborz Jahanian68342282008-12-05 22:32:48 +0000813/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
814/// added to the list of those properties @synthesized/@dynamic in this
815/// @implementation block.
816///
817ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
818 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
819 ObjCPropertyImplDecl *PID = *i;
820 if (PID->getPropertyDecl()->getIdentifier() == Id)
821 return PID;
822 }
823 return 0;
824}
825
826/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000827/// properties implemented in this @implementation block and returns the
828/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000829///
830ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
831 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
832 ObjCPropertyImplDecl *PID = *i;
833 if (PID->getPropertyIvarDecl() &&
834 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
835 return PID;
836 }
837 return 0;
838}
839
840/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian17eb8882008-12-05 22:36:19 +0000841/// properties implemented in this category @implementation block and returns the
842/// implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000843///
844ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
845 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
846 ObjCPropertyImplDecl *PID = *i;
847 if (PID->getPropertyIvarDecl() &&
848 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
849 return PID;
850 }
851 return 0;
852}
853
854/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
855/// added to the list of those properties @synthesized/@dynamic in this
856/// category @implementation block.
857///
858ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
859 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
860 ObjCPropertyImplDecl *PID = *i;
861 if (PID->getPropertyDecl()->getIdentifier() == Id)
862 return PID;
863 }
864 return 0;
865}
866
Chris Lattner10318b82008-03-16 00:19:01 +0000867// lookupInstanceMethod - This method returns an instance method by looking in
868// the class implementation. Unlike interfaces, we don't look outside the
869// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000870ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000871 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
872 if ((*I)->getSelector() == Sel)
873 return *I;
874 return NULL;
875}
876
877// lookupClassMethod - This method returns an instance method by looking in
878// the class implementation. Unlike interfaces, we don't look outside the
879// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000880ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000881 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
882 I != E; ++I)
883 if ((*I)->getSelector() == Sel)
884 return *I;
885 return NULL;
886}
887
888// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
889// it inherited.
890ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
891 ObjCMethodDecl *MethodDecl = NULL;
892
893 if ((MethodDecl = getInstanceMethod(Sel)))
894 return MethodDecl;
895
Chris Lattner0be08822008-07-21 21:32:27 +0000896 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
897 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
898 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000899 return NULL;
900}
901
902// lookupInstanceMethod - Lookup a class method in the protocol and protocols
903// it inherited.
904ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
905 ObjCMethodDecl *MethodDecl = NULL;
906
907 if ((MethodDecl = getClassMethod(Sel)))
908 return MethodDecl;
909
Chris Lattner0be08822008-07-21 21:32:27 +0000910 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
911 if ((MethodDecl = (*I)->getClassMethod(Sel)))
912 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000913 return NULL;
914}
915
916/// getSynthesizedMethodSize - Compute size of synthesized method name
917/// as done be the rewrite.
918///
919unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
920 // syntesized method name is a concatenation of -/+[class-name selector]
921 // Get length of this name.
922 unsigned length = 3; // _I_ or _C_
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000923 length += getClassInterface()->getNameAsString().size()+1; // extra for _
Chris Lattner10318b82008-03-16 00:19:01 +0000924 NamedDecl *MethodContext = getMethodContext();
925 if (ObjCCategoryImplDecl *CID =
926 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000927 length += CID->getNameAsString().size()+1;
Chris Lattner3a8f2942008-11-24 03:33:13 +0000928 length += getSelector().getAsString().size(); // selector name
Chris Lattner10318b82008-03-16 00:19:01 +0000929 return length;
930}
931
Chris Lattner052fbcd2008-04-06 05:25:03 +0000932ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000933 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
934 return ID;
935 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
936 return CD->getClassInterface();
937 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000938 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000939 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000940 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000941 return CID->getClassInterface();
942 assert(false && "unknown method context");
943 return 0;
944}
Chris Lattner44859612008-03-17 01:19:02 +0000945
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000946ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
947 SourceLocation atLoc,
948 SourceLocation L,
949 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000950 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000951 ObjCIvarDecl *ivar) {
952 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000953 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000954}
Chris Lattner44859612008-03-17 01:19:02 +0000955
Chris Lattnereee57c02008-04-04 06:12:32 +0000956