blob: f22b5b435b068da5f4566880e92223ed4e8a338f [file] [log] [blame]
Chris Lattner1e03a562008-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 Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000017using namespace clang;
18
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000019//===----------------------------------------------------------------------===//
20// ObjC Decl Allocation/Deallocation Method Implementations
21//===----------------------------------------------------------------------===//
22
Chris Lattner0ed844b2008-04-04 06:12:32 +000023ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
24 SourceLocation beginLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000025 SourceLocation endLoc,
26 Selector SelInfo, QualType T,
27 Decl *contextDecl,
Daniel Dunbarf6414922008-08-20 18:02:42 +000028 bool isInstance,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000029 bool isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +000030 bool isSynthesized,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +000031 ImplementationControl impControl) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000032 void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +000033 return new (Mem) ObjCMethodDecl(beginLoc, endLoc,
34 SelInfo, T, contextDecl,
Daniel Dunbarf6414922008-08-20 18:02:42 +000035 isInstance,
Fariborz Jahanian46070342008-05-07 20:53:44 +000036 isVariadic, isSynthesized, impControl);
Chris Lattner0e77ba02008-03-16 01:15:50 +000037}
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000038
Ted Kremenek8a779312008-06-06 16:45:15 +000039ObjCMethodDecl::~ObjCMethodDecl() {
40 delete [] ParamInfo;
Ted Kremenek8a779312008-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 Lattner0ed844b2008-04-04 06:12:32 +000053ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
54 SourceLocation atLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000055 IdentifierInfo *Id,
56 SourceLocation ClassLoc,
Chris Lattner0e77ba02008-03-16 01:15:50 +000057 bool ForwardDecl, bool isInternal){
58 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Chris Lattnerb752f282008-07-21 07:06:49 +000059 return new (Mem) ObjCInterfaceDecl(atLoc, Id, ClassLoc, ForwardDecl,
Chris Lattner0e77ba02008-03-16 01:15:50 +000060 isInternal);
61}
62
Ted Kremenek8a779312008-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 Kremenek1a726d72008-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 Kremenek8a779312008-06-06 16:45:15 +000085 Decl::Destroy(C);
86}
87
88
Chris Lattnerb048c982008-04-06 04:47:34 +000089ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Ted Kremenekb8db21d2008-07-23 18:04:17 +000090 IdentifierInfo *Id, QualType T,
91 AccessControl ac, Expr *BW) {
Chris Lattner0e77ba02008-03-16 01:15:50 +000092 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Ted Kremenekb8db21d2008-07-23 18:04:17 +000093 return new (Mem) ObjCIvarDecl(L, Id, T, ac, BW);
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000094}
95
Ted Kremenek01e67792008-08-20 03:26:33 +000096
97ObjCAtDefsFieldDecl
Douglas Gregor44b43212008-12-11 16:49:14 +000098*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Ted Kremenek01e67792008-08-20 03:26:33 +000099 IdentifierInfo *Id, QualType T, Expr *BW) {
100 void *Mem = C.getAllocator().Allocate<ObjCAtDefsFieldDecl>();
Douglas Gregor44b43212008-12-11 16:49:14 +0000101 return new (Mem) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
Ted Kremenek01e67792008-08-20 03:26:33 +0000102}
103
104void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
105 this->~ObjCAtDefsFieldDecl();
106 C.getAllocator().Deallocate((void *)this);
107}
108
Chris Lattner0ed844b2008-04-04 06:12:32 +0000109ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
110 SourceLocation L,
Chris Lattnerc8581052008-03-16 20:19:15 +0000111 IdentifierInfo *Id) {
Chris Lattnercca59d72008-03-16 01:23:04 +0000112 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner780f3292008-07-21 21:32:27 +0000113 return new (Mem) ObjCProtocolDecl(L, Id);
Chris Lattnercca59d72008-03-16 01:23:04 +0000114}
115
Ted Kremenek1c8a4132008-06-06 19:48:57 +0000116ObjCProtocolDecl::~ObjCProtocolDecl() {
Ted Kremenek1c8a4132008-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 Lattner0ed844b2008-04-04 06:12:32 +0000140ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
141 SourceLocation L,
Chris Lattner61f9d412008-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 Kremenek400d95f2008-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 Lattner61f9d412008-03-16 20:34:23 +0000164ObjCForwardProtocolDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +0000165ObjCForwardProtocolDecl::Create(ASTContext &C,
166 SourceLocation L,
Chris Lattner61f9d412008-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 Kremenek05ac3ef2008-06-06 21:05:33 +0000172ObjCForwardProtocolDecl::~ObjCForwardProtocolDecl() {
173 delete [] ReferencedProtocols;
174}
175
Chris Lattner0ed844b2008-04-04 06:12:32 +0000176ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
177 SourceLocation L,
Chris Lattner61f9d412008-03-16 20:34:23 +0000178 IdentifierInfo *Id) {
179 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner68c82cf2008-03-16 20:47:45 +0000180 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattner61f9d412008-03-16 20:34:23 +0000181}
182
Chris Lattner75c9cae2008-03-16 20:53:07 +0000183ObjCCategoryImplDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +0000184ObjCCategoryImplDecl::Create(ASTContext &C,
185 SourceLocation L,IdentifierInfo *Id,
Chris Lattner75c9cae2008-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 Lattner0ed844b2008-04-04 06:12:32 +0000192ObjCImplementationDecl::Create(ASTContext &C,
193 SourceLocation L,
Chris Lattner75c9cae2008-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 Lattner1e03a562008-03-16 00:19:01 +0000200
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000201ObjCCompatibleAliasDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +0000202ObjCCompatibleAliasDecl::Create(ASTContext &C,
203 SourceLocation L,
Chris Lattnerf8d17a52008-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 Lattner0ed844b2008-04-04 06:12:32 +0000210ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000211 SourceLocation L,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000212 IdentifierInfo *Id,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000213 QualType T,
214 PropertyControl propControl) {
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000215 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000216 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000217}
218
Chris Lattner1e03a562008-03-16 00:19:01 +0000219//===----------------------------------------------------------------------===//
220// Objective-C Decl Implementation
221//===----------------------------------------------------------------------===//
222
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000223void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
224 const ObjCInterfaceDecl *OID) {
Daniel Dunbar08a356c2008-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 Jahanianfef30b52008-12-09 20:23:04 +0000229 if (OID) {
230 selfTy = Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl *>(OID));
Daniel Dunbar08a356c2008-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 Lattner1e03a562008-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 Jahanian31afbf02008-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 Jahanian559c0c42008-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 Naroff6c930f22008-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 Naroff3d2c22b2008-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 Jahanianc70bee82008-04-21 23:57:08 +0000307 if (getSuperClass())
308 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-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 Jahanianc70bee82008-04-21 23:57:08 +0000336 if (getSuperClass())
337 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000338 return 0;
339}
340
Fariborz Jahanian60f8c862008-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);
348 Fields.push_back(cast<FieldDecl>(IVDecl));
349 }
350}
351
Chris Lattner1e03a562008-03-16 00:19:01 +0000352/// ObjCAddInstanceVariablesToClass - Inserts instance variables
353/// into ObjCInterfaceDecl's fields.
354///
355void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
356 unsigned numIvars,
357 SourceLocation RBrac) {
358 NumIvars = numIvars;
359 if (numIvars) {
360 Ivars = new ObjCIvarDecl*[numIvars];
361 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
362 }
363 setLocEnd(RBrac);
364}
365
Fariborz Jahanianaaa63a72008-12-13 22:20:28 +0000366/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
367/// storage which matches this 'ivar'.
368///
369FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
370 ObjCIvarDecl *ivar) {
371 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
372 DeclarationName Member = ivar->getDeclName();
373 DeclContext::lookup_result Lookup = RecordForDecl->lookup(Context, Member);
374 assert((Lookup.first != Lookup.second) && "field decl not found");
375 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
376 assert(MemberDecl && "field decl not found");
377 return MemberDecl;
378}
379
380/// addLayoutToClass - produces layout info. for the class for its
Fariborz Jahanian60f8c862008-12-13 20:28:25 +0000381/// ivars and all those inherited.
382///
383void ObjCInterfaceDecl::addLayoutToClass(ASTContext &Context)
384{
385 std::vector<FieldDecl*> RecFields;
386 CollectObjCIvars(RecFields);
387 RecordDecl *RD = RecordDecl::Create(Context, TagDecl::TK_struct, 0,
388 getLocation(),
389 getIdentifier());
390 /// FIXME! Can do collection of ivars and adding to the record while
391 /// doing it.
392 for (unsigned int i = 0; i != RecFields.size(); i++) {
393 FieldDecl *Field = FieldDecl::Create(Context, RD, SourceLocation(),
394 RecFields[i]->getIdentifier(),
395 RecFields[i]->getType(), 0, false, 0);
396 RD->addDecl(Context, Field);
397 }
398 RD->completeDefinition(Context);
399 setRecordForDecl(RD);
400}
401
Chris Lattner1e03a562008-03-16 00:19:01 +0000402/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
403/// Variables (Ivars) relative to what declared in @implementation;s class.
404/// Ivars into ObjCImplementationDecl's fields.
405///
406void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
407 ObjCIvarDecl **ivars, unsigned numIvars) {
408 NumIvars = numIvars;
409 if (numIvars) {
410 Ivars = new ObjCIvarDecl*[numIvars];
411 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
412 }
413}
414
415/// addMethods - Insert instance and methods declarations into
416/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
417///
418void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
419 unsigned numInsMembers,
420 ObjCMethodDecl **clsMethods,
421 unsigned numClsMembers,
422 SourceLocation endLoc) {
423 NumInstanceMethods = numInsMembers;
424 if (numInsMembers) {
425 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
426 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
427 }
428 NumClassMethods = numClsMembers;
429 if (numClsMembers) {
430 ClassMethods = new ObjCMethodDecl*[numClsMembers];
431 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
432 }
433 AtEndLoc = endLoc;
434}
435
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000436/// addProperties - Insert property declaration AST nodes into
437/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattner55d13b42008-03-16 21:23:50 +0000438///
439void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
440 unsigned NumProperties) {
441 if (NumProperties == 0) return;
442
443 NumPropertyDecl = NumProperties;
444 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
445 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
446}
447
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000448/// mergeProperties - Adds properties to the end of list of current properties
449/// for this class.
450
451void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
452 unsigned NumNewProperties) {
453 if (NumNewProperties == 0) return;
454
455 if (PropertyDecl) {
456 ObjCPropertyDecl **newPropertyDecl =
457 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
458 ObjCPropertyDecl **buf = newPropertyDecl;
459 // put back original properties in buffer.
460 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
461 // Add new properties to this buffer.
462 memcpy(buf+NumPropertyDecl, Properties,
463 NumNewProperties*sizeof(ObjCPropertyDecl*));
Nuno Lopes6b3502c2008-05-10 10:31:54 +0000464 delete[] PropertyDecl;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000465 PropertyDecl = newPropertyDecl;
466 NumPropertyDecl += NumNewProperties;
467 }
468 else {
Nuno Lopes6b3502c2008-05-10 10:31:54 +0000469 addProperties(Properties, NumNewProperties);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000470 }
471}
472
Daniel Dunbar7b473222008-08-27 02:09:39 +0000473static void
474addPropertyMethods(Decl *D,
475 ASTContext &Context,
476 ObjCPropertyDecl *property,
Fariborz Jahanianb85cce62008-12-02 00:19:12 +0000477 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods,
478 llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) {
Daniel Dunbare86d9232008-08-27 05:13:46 +0000479 ObjCMethodDecl *GetterDecl, *SetterDecl = 0;
Fariborz Jahanianb85cce62008-12-02 00:19:12 +0000480
481 GetterDecl = const_cast<ObjCMethodDecl*>(InsMap[property->getGetterName()]);
482 if (!property->isReadOnly())
483 SetterDecl = const_cast<ObjCMethodDecl*>(InsMap[property->getSetterName()]);
484
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000485 // FIXME: The synthesized property we set here is misleading. We
486 // almost always synthesize these methods unless the user explicitly
487 // provided prototypes (which is odd, but allowed). Sema should be
488 // typechecking that the declarations jive in that situation (which
489 // it is not currently).
490
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000491 // Find the default getter and if one not found, add one.
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000492 if (!GetterDecl) {
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000493 // No instance method of same name as property getter name was found.
494 // Declare a getter method and add it to the list of methods
495 // for this class.
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000496 GetterDecl =
497 ObjCMethodDecl::Create(Context, property->getLocation(),
498 property->getLocation(),
499 property->getGetterName(),
500 property->getType(),
Daniel Dunbar7b473222008-08-27 02:09:39 +0000501 D,
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000502 true, false, true, ObjCMethodDecl::Required);
503 insMethods.push_back(GetterDecl);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +0000504 InsMap[property->getGetterName()] = GetterDecl;
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000505 }
Fariborz Jahanian91b51a92008-12-09 22:43:22 +0000506 else
507 // A user declared getter will be synthesize when @synthesize of
508 // the property with the same name is seen in the @implementation
509 GetterDecl->setIsSynthesized();
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000510 property->setGetterMethodDecl(GetterDecl);
511
Daniel Dunbar394d33f2008-08-26 07:16:44 +0000512 // Skip setter if property is read-only.
513 if (property->isReadOnly())
514 return;
515
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000516 // Find the default setter and if one not found, add one.
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000517 if (!SetterDecl) {
518 // No instance method of same name as property setter name was found.
519 // Declare a setter method and add it to the list of methods
520 // for this class.
521 SetterDecl =
522 ObjCMethodDecl::Create(Context, property->getLocation(),
523 property->getLocation(),
524 property->getSetterName(),
Daniel Dunbar5fa63312008-08-26 02:53:23 +0000525 Context.VoidTy,
Daniel Dunbar7b473222008-08-27 02:09:39 +0000526 D,
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000527 true, false, true, ObjCMethodDecl::Required);
528 insMethods.push_back(SetterDecl);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +0000529 InsMap[property->getSetterName()] = SetterDecl;
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000530 // Invent the arguments for the setter. We don't bother making a
531 // nice name for the argument.
532 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
533 SetterDecl,
534 SourceLocation(),
535 property->getIdentifier(),
536 property->getType(),
537 VarDecl::None,
538 0, 0);
539 SetterDecl->setMethodParams(&Argument, 1);
540 }
Fariborz Jahanian91b51a92008-12-09 22:43:22 +0000541 else
542 // A user declared setter will be synthesize when @synthesize of
543 // the property with the same name is seen in the @implementation
544 SetterDecl->setIsSynthesized();
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000545 property->setSetterMethodDecl(SetterDecl);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000546}
547
Daniel Dunbar7b473222008-08-27 02:09:39 +0000548/// addPropertyMethods - Goes through list of properties declared in this class
549/// and builds setter/getter method declartions depending on the setter/getter
550/// attributes of the property.
551///
552void ObjCInterfaceDecl::addPropertyMethods(
553 ASTContext &Context,
554 ObjCPropertyDecl *property,
Fariborz Jahanianb85cce62008-12-02 00:19:12 +0000555 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods,
556 llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) {
557 ::addPropertyMethods(this, Context, property, insMethods, InsMap);
Daniel Dunbar7b473222008-08-27 02:09:39 +0000558}
559
560/// addPropertyMethods - Goes through list of properties declared in this class
561/// and builds setter/getter method declartions depending on the setter/getter
562/// attributes of the property.
563///
564void ObjCCategoryDecl::addPropertyMethods(
565 ASTContext &Context,
566 ObjCPropertyDecl *property,
Fariborz Jahanianb85cce62008-12-02 00:19:12 +0000567 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods,
568 llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) {
569 ::addPropertyMethods(this, Context, property, insMethods, InsMap);
Daniel Dunbar7b473222008-08-27 02:09:39 +0000570}
571
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000572/// mergeProperties - Adds properties to the end of list of current properties
573/// for this category.
574
575void ObjCCategoryDecl::mergeProperties(ObjCPropertyDecl **Properties,
576 unsigned NumNewProperties) {
577 if (NumNewProperties == 0) return;
578
579 if (PropertyDecl) {
580 ObjCPropertyDecl **newPropertyDecl =
581 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
582 ObjCPropertyDecl **buf = newPropertyDecl;
583 // put back original properties in buffer.
584 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
585 // Add new properties to this buffer.
586 memcpy(buf+NumPropertyDecl, Properties,
587 NumNewProperties*sizeof(ObjCPropertyDecl*));
588 delete[] PropertyDecl;
589 PropertyDecl = newPropertyDecl;
590 NumPropertyDecl += NumNewProperties;
591 }
592 else {
593 addProperties(Properties, NumNewProperties);
594 }
595}
596
Daniel Dunbar7b473222008-08-27 02:09:39 +0000597/// addPropertyMethods - Goes through list of properties declared in this class
598/// and builds setter/getter method declartions depending on the setter/getter
599/// attributes of the property.
600///
601void ObjCProtocolDecl::addPropertyMethods(
602 ASTContext &Context,
603 ObjCPropertyDecl *property,
Fariborz Jahanianb85cce62008-12-02 00:19:12 +0000604 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods,
605 llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) {
606 ::addPropertyMethods(this, Context, property, insMethods, InsMap);
Daniel Dunbar7b473222008-08-27 02:09:39 +0000607}
608
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000609/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000610/// ObjCProtocolDecl's PropertyDecl field.
611///
612void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
613 unsigned NumProperties) {
614 if (NumProperties == 0) return;
615
616 NumPropertyDecl = NumProperties;
617 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
618 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
619}
620
621/// addProperties - Insert property declaration AST nodes into
Fariborz Jahaniand9a3c332008-04-16 21:11:25 +0000622/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000623///
624void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
625 unsigned NumProperties) {
626 if (NumProperties == 0) return;
627
628 NumPropertyDecl = NumProperties;
629 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
630 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
631}
632
Chris Lattner55d13b42008-03-16 21:23:50 +0000633/// addMethods - Insert instance and methods declarations into
Chris Lattner1e03a562008-03-16 00:19:01 +0000634/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
635///
636void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
637 unsigned numInsMembers,
638 ObjCMethodDecl **clsMethods,
639 unsigned numClsMembers,
640 SourceLocation endLoc) {
641 NumInstanceMethods = numInsMembers;
642 if (numInsMembers) {
643 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
644 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
645 }
646 NumClassMethods = numClsMembers;
647 if (numClsMembers) {
648 ClassMethods = new ObjCMethodDecl*[numClsMembers];
649 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
650 }
651 AtEndLoc = endLoc;
652}
653
Chris Lattner68c82cf2008-03-16 20:47:45 +0000654
655
Chris Lattner1e03a562008-03-16 00:19:01 +0000656/// addMethods - Insert instance and methods declarations into
657/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
658///
659void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
660 unsigned numInsMembers,
661 ObjCMethodDecl **clsMethods,
662 unsigned numClsMembers,
663 SourceLocation endLoc) {
664 NumInstanceMethods = numInsMembers;
665 if (numInsMembers) {
666 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
667 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
668 }
669 NumClassMethods = numClsMembers;
670 if (numClsMembers) {
671 ClassMethods = new ObjCMethodDecl*[numClsMembers];
672 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
673 }
674 AtEndLoc = endLoc;
675}
676
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000677/// FindPropertyDeclaration - Finds declaration of the property given its name
678/// in 'PropertyId' and returns it. It returns 0, if not found.
679///
680ObjCPropertyDecl *
681ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
682 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
683 E = classprop_end(); I != E; ++I) {
684 ObjCPropertyDecl *property = *I;
685 if (property->getIdentifier() == PropertyId)
686 return property;
687 }
688 return 0;
689}
690
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000691/// FindPropertyDeclaration - Finds declaration of the property given its name
692/// in 'PropertyId' and returns it. It returns 0, if not found.
693///
694ObjCPropertyDecl *
695ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
696 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
697 E = classprop_end(); I != E; ++I) {
698 ObjCPropertyDecl *property = *I;
699 if (property->getIdentifier() == PropertyId)
700 return property;
701 }
702 return 0;
703}
704
Chris Lattner1e03a562008-03-16 00:19:01 +0000705ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
706 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
707 ObjCInterfaceDecl* ClassDecl = this;
708 while (ClassDecl != NULL) {
709 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
710 I != E; ++I) {
711 if ((*I)->getIdentifier() == ID) {
712 clsDeclared = ClassDecl;
713 return *I;
714 }
715 }
716 ClassDecl = ClassDecl->getSuperClass();
717 }
718 return NULL;
719}
720
721/// lookupInstanceMethod - This method returns an instance method by looking in
722/// the class, its categories, and its super classes (using a linear search).
723ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
724 ObjCInterfaceDecl* ClassDecl = this;
725 ObjCMethodDecl *MethodDecl = 0;
726
727 while (ClassDecl != NULL) {
728 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
729 return MethodDecl;
730
731 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000732 const ObjCList<ObjCProtocolDecl> &Protocols =
733 ClassDecl->getReferencedProtocols();
734 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
735 E = Protocols.end(); I != E; ++I)
736 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000737 return MethodDecl;
Chris Lattner3db6cae2008-07-21 18:19:38 +0000738
Chris Lattner1e03a562008-03-16 00:19:01 +0000739 // Didn't find one yet - now look through categories.
740 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
741 while (CatDecl) {
742 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
743 return MethodDecl;
Steve Naroffb79c01e2008-12-08 20:57:28 +0000744
745 // Didn't find one yet - look through protocols.
746 const ObjCList<ObjCProtocolDecl> &Protocols =
747 CatDecl->getReferencedProtocols();
748 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
749 E = Protocols.end(); I != E; ++I)
750 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
751 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000752 CatDecl = CatDecl->getNextClassCategory();
753 }
754 ClassDecl = ClassDecl->getSuperClass();
755 }
756 return NULL;
757}
758
759// lookupClassMethod - This method returns a class method by looking in the
760// class, its categories, and its super classes (using a linear search).
761ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
762 ObjCInterfaceDecl* ClassDecl = this;
763 ObjCMethodDecl *MethodDecl = 0;
764
765 while (ClassDecl != NULL) {
766 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
767 return MethodDecl;
768
769 // Didn't find one yet - look through protocols.
Chris Lattner780f3292008-07-21 21:32:27 +0000770 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
771 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner3db6cae2008-07-21 18:19:38 +0000772 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000773 return MethodDecl;
Chris Lattner780f3292008-07-21 21:32:27 +0000774
Chris Lattner1e03a562008-03-16 00:19:01 +0000775 // Didn't find one yet - now look through categories.
776 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
777 while (CatDecl) {
778 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
779 return MethodDecl;
780 CatDecl = CatDecl->getNextClassCategory();
781 }
782 ClassDecl = ClassDecl->getSuperClass();
783 }
784 return NULL;
785}
786
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000787/// getInstanceMethod - This method returns an instance method by
788/// looking in the class implementation. Unlike interfaces, we don't
789/// look outside the implementation.
790ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000791 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
792 if ((*I)->getSelector() == Sel)
793 return *I;
794 return NULL;
795}
796
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000797/// getClassMethod - This method returns a class method by looking in
798/// the class implementation. Unlike interfaces, we don't look outside
799/// the implementation.
800ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000801 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
802 I != E; ++I)
803 if ((*I)->getSelector() == Sel)
804 return *I;
805 return NULL;
806}
807
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000808/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
809/// added to the list of those properties @synthesized/@dynamic in this
810/// @implementation block.
811///
812ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
813 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
814 ObjCPropertyImplDecl *PID = *i;
815 if (PID->getPropertyDecl()->getIdentifier() == Id)
816 return PID;
817 }
818 return 0;
819}
820
821/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian9482a4f2008-12-05 22:36:19 +0000822/// properties implemented in this @implementation block and returns the
823/// implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000824///
825ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
826 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
827 ObjCPropertyImplDecl *PID = *i;
828 if (PID->getPropertyIvarDecl() &&
829 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
830 return PID;
831 }
832 return 0;
833}
834
835/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian9482a4f2008-12-05 22:36:19 +0000836/// properties implemented in this category @implementation block and returns the
837/// implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000838///
839ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
840 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
841 ObjCPropertyImplDecl *PID = *i;
842 if (PID->getPropertyIvarDecl() &&
843 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
844 return PID;
845 }
846 return 0;
847}
848
849/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
850/// added to the list of those properties @synthesized/@dynamic in this
851/// category @implementation block.
852///
853ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplDecl(IdentifierInfo *Id) const {
854 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) {
855 ObjCPropertyImplDecl *PID = *i;
856 if (PID->getPropertyDecl()->getIdentifier() == Id)
857 return PID;
858 }
859 return 0;
860}
861
Chris Lattner1e03a562008-03-16 00:19:01 +0000862// lookupInstanceMethod - This method returns an instance method by looking in
863// the class implementation. Unlike interfaces, we don't look outside the
864// implementation.
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000865ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000866 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
867 if ((*I)->getSelector() == Sel)
868 return *I;
869 return NULL;
870}
871
872// lookupClassMethod - This method returns an instance method by looking in
873// the class implementation. Unlike interfaces, we don't look outside the
874// implementation.
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000875ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000876 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
877 I != E; ++I)
878 if ((*I)->getSelector() == Sel)
879 return *I;
880 return NULL;
881}
882
883// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
884// it inherited.
885ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
886 ObjCMethodDecl *MethodDecl = NULL;
887
888 if ((MethodDecl = getInstanceMethod(Sel)))
889 return MethodDecl;
890
Chris Lattner780f3292008-07-21 21:32:27 +0000891 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
892 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
893 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000894 return NULL;
895}
896
897// lookupInstanceMethod - Lookup a class method in the protocol and protocols
898// it inherited.
899ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
900 ObjCMethodDecl *MethodDecl = NULL;
901
902 if ((MethodDecl = getClassMethod(Sel)))
903 return MethodDecl;
904
Chris Lattner780f3292008-07-21 21:32:27 +0000905 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
906 if ((MethodDecl = (*I)->getClassMethod(Sel)))
907 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000908 return NULL;
909}
910
911/// getSynthesizedMethodSize - Compute size of synthesized method name
912/// as done be the rewrite.
913///
914unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
915 // syntesized method name is a concatenation of -/+[class-name selector]
916 // Get length of this name.
917 unsigned length = 3; // _I_ or _C_
Chris Lattner8ec03f52008-11-24 03:54:41 +0000918 length += getClassInterface()->getNameAsString().size()+1; // extra for _
Chris Lattner1e03a562008-03-16 00:19:01 +0000919 NamedDecl *MethodContext = getMethodContext();
920 if (ObjCCategoryImplDecl *CID =
921 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner8ec03f52008-11-24 03:54:41 +0000922 length += CID->getNameAsString().size()+1;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000923 length += getSelector().getAsString().size(); // selector name
Chris Lattner1e03a562008-03-16 00:19:01 +0000924 return length;
925}
926
Chris Lattner56196882008-04-06 05:25:03 +0000927ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner1e03a562008-03-16 00:19:01 +0000928 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
929 return ID;
930 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
931 return CD->getClassInterface();
932 if (ObjCImplementationDecl *IMD =
Chris Lattner56196882008-04-06 05:25:03 +0000933 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner1e03a562008-03-16 00:19:01 +0000934 return IMD->getClassInterface();
Chris Lattner56196882008-04-06 05:25:03 +0000935 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner1e03a562008-03-16 00:19:01 +0000936 return CID->getClassInterface();
937 assert(false && "unknown method context");
938 return 0;
939}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000940
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000941ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
942 SourceLocation atLoc,
943 SourceLocation L,
944 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000945 Kind PK,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000946 ObjCIvarDecl *ivar) {
947 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000948 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, PK, ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000949}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000950
Chris Lattner0ed844b2008-04-04 06:12:32 +0000951