blob: a793ac608d69f5b1fcdf441d1290ec2a2934f8e0 [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,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000028 AttributeList *M, bool isInstance,
29 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,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +000035 M, 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;
41 //delete [] MethodAttrs; // FIXME: Also destroy the stored Expr*.
42}
43
44void ObjCMethodDecl::Destroy(ASTContext& C) {
45 if (Body) Body->Destroy(C);
46 if (SelfDecl) SelfDecl->Destroy(C);
47
48 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
49 if (*I) (*I)->Destroy(C);
50
51 Decl::Destroy(C);
52}
53
Chris Lattner0ed844b2008-04-04 06:12:32 +000054ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
55 SourceLocation atLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000056 IdentifierInfo *Id,
57 SourceLocation ClassLoc,
Chris Lattner0e77ba02008-03-16 01:15:50 +000058 bool ForwardDecl, bool isInternal){
59 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Chris Lattnerb752f282008-07-21 07:06:49 +000060 return new (Mem) ObjCInterfaceDecl(atLoc, Id, ClassLoc, ForwardDecl,
Chris Lattner0e77ba02008-03-16 01:15:50 +000061 isInternal);
62}
63
Ted Kremenek8a779312008-06-06 16:45:15 +000064ObjCInterfaceDecl::~ObjCInterfaceDecl() {
65 delete [] Ivars;
66 delete [] InstanceMethods;
67 delete [] ClassMethods;
68 delete [] PropertyDecl;
69 // FIXME: CategoryList?
70}
71
72void ObjCInterfaceDecl::Destroy(ASTContext& C) {
73 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
74 if (*I) (*I)->Destroy(C);
75
76 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
77 if (*I) (*I)->Destroy(C);
78
79 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
80 if (*I) (*I)->Destroy(C);
Ted Kremenek1a726d72008-06-06 17:21:42 +000081
82 // FIXME: Because there is no clear ownership
83 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
84 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
85
Ted Kremenek8a779312008-06-06 16:45:15 +000086 Decl::Destroy(C);
87}
88
89
Chris Lattnerb048c982008-04-06 04:47:34 +000090ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Ted Kremenekb8db21d2008-07-23 18:04:17 +000091 IdentifierInfo *Id, QualType T,
92 AccessControl ac, Expr *BW) {
Chris Lattner0e77ba02008-03-16 01:15:50 +000093 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Ted Kremenekb8db21d2008-07-23 18:04:17 +000094 return new (Mem) ObjCIvarDecl(L, Id, T, ac, BW);
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000095}
96
Chris Lattner0ed844b2008-04-04 06:12:32 +000097ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
98 SourceLocation L,
Chris Lattnerc8581052008-03-16 20:19:15 +000099 IdentifierInfo *Id) {
Chris Lattnercca59d72008-03-16 01:23:04 +0000100 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner780f3292008-07-21 21:32:27 +0000101 return new (Mem) ObjCProtocolDecl(L, Id);
Chris Lattnercca59d72008-03-16 01:23:04 +0000102}
103
Ted Kremenek1c8a4132008-06-06 19:48:57 +0000104ObjCProtocolDecl::~ObjCProtocolDecl() {
Ted Kremenek1c8a4132008-06-06 19:48:57 +0000105 delete [] InstanceMethods;
106 delete [] ClassMethods;
107 delete [] PropertyDecl;
108}
109
110void ObjCProtocolDecl::Destroy(ASTContext& C) {
111
112 // Referenced Protocols are not owned, so don't Destroy them.
113
114 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
115 if (*I) (*I)->Destroy(C);
116
117 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
118 if (*I) (*I)->Destroy(C);
119
120 // FIXME: Because there is no clear ownership
121 // role between ObjCProtocolDecls and the ObjCPropertyDecls that they
122 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
123
124 Decl::Destroy(C);
125}
126
127
Chris Lattner0ed844b2008-04-04 06:12:32 +0000128ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
129 SourceLocation L,
Chris Lattner61f9d412008-03-16 20:34:23 +0000130 ObjCInterfaceDecl **Elts, unsigned nElts) {
131 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
132 return new (Mem) ObjCClassDecl(L, Elts, nElts);
133}
134
Ted Kremenek400d95f2008-06-06 20:11:53 +0000135ObjCClassDecl::~ObjCClassDecl() {
136 delete [] ForwardDecls;
137}
138
139void ObjCClassDecl::Destroy(ASTContext& C) {
140
141 // FIXME: There is no clear ownership policy now for referenced
142 // ObjCInterfaceDecls. Some of them can be forward declarations that
143 // are never later defined (in which case the ObjCClassDecl owns them)
144 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
145 // we should have separate objects for forward declarations and definitions,
146 // obviating this problem. Because of this situation, referenced
147 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
148
149 Decl::Destroy(C);
150}
151
Chris Lattner61f9d412008-03-16 20:34:23 +0000152ObjCForwardProtocolDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +0000153ObjCForwardProtocolDecl::Create(ASTContext &C,
154 SourceLocation L,
Chris Lattner61f9d412008-03-16 20:34:23 +0000155 ObjCProtocolDecl **Elts, unsigned NumElts) {
156 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
157 return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
158}
159
Ted Kremenek05ac3ef2008-06-06 21:05:33 +0000160ObjCForwardProtocolDecl::~ObjCForwardProtocolDecl() {
161 delete [] ReferencedProtocols;
162}
163
Chris Lattner0ed844b2008-04-04 06:12:32 +0000164ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
165 SourceLocation L,
Chris Lattner61f9d412008-03-16 20:34:23 +0000166 IdentifierInfo *Id) {
167 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner68c82cf2008-03-16 20:47:45 +0000168 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattner61f9d412008-03-16 20:34:23 +0000169}
170
Chris Lattner75c9cae2008-03-16 20:53:07 +0000171ObjCCategoryImplDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +0000172ObjCCategoryImplDecl::Create(ASTContext &C,
173 SourceLocation L,IdentifierInfo *Id,
Chris Lattner75c9cae2008-03-16 20:53:07 +0000174 ObjCInterfaceDecl *ClassInterface) {
175 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
176 return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
177}
178
179ObjCImplementationDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +0000180ObjCImplementationDecl::Create(ASTContext &C,
181 SourceLocation L,
Chris Lattner75c9cae2008-03-16 20:53:07 +0000182 IdentifierInfo *Id,
183 ObjCInterfaceDecl *ClassInterface,
184 ObjCInterfaceDecl *SuperDecl) {
185 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
186 return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
187}
Chris Lattner1e03a562008-03-16 00:19:01 +0000188
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000189ObjCCompatibleAliasDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +0000190ObjCCompatibleAliasDecl::Create(ASTContext &C,
191 SourceLocation L,
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000192 IdentifierInfo *Id,
193 ObjCInterfaceDecl* AliasedClass) {
194 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
195 return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
196}
197
Chris Lattner0ed844b2008-04-04 06:12:32 +0000198ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000199 SourceLocation L,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000200 IdentifierInfo *Id,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000201 QualType T,
202 PropertyControl propControl) {
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000203 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000204 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000205}
206
Chris Lattner1e03a562008-03-16 00:19:01 +0000207//===----------------------------------------------------------------------===//
208// Objective-C Decl Implementation
209//===----------------------------------------------------------------------===//
210
211void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
212 unsigned NumParams) {
213 assert(ParamInfo == 0 && "Already has param info!");
214
215 // Zero params -> null pointer.
216 if (NumParams) {
217 ParamInfo = new ParmVarDecl*[NumParams];
218 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
219 NumMethodParams = NumParams;
220 }
221}
222
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000223/// FindPropertyDeclaration - Finds declaration of the property given its name
224/// in 'PropertyId' and returns it. It returns 0, if not found.
225///
226ObjCPropertyDecl *
227 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
228 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
229 E = classprop_end(); I != E; ++I) {
230 ObjCPropertyDecl *property = *I;
231 if (property->getIdentifier() == PropertyId)
232 return property;
233 }
Steve Naroff6c930f22008-06-04 04:46:04 +0000234 // Look through categories.
235 for (ObjCCategoryDecl *Category = getCategoryList();
236 Category; Category = Category->getNextClassCategory()) {
237 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
238 if (property)
239 return property;
240 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000241 // Look through protocols.
242 for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(),
243 E = protocol_end(); I != E; ++I) {
244 ObjCProtocolDecl *Protocol = *I;
245 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
246 if (property)
247 return property;
248 }
Fariborz Jahanianc70bee82008-04-21 23:57:08 +0000249 if (getSuperClass())
250 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000251 return 0;
252}
253
254/// FindCategoryDeclaration - Finds category declaration in the list of
255/// categories for this class and returns it. Name of the category is passed
256/// in 'CategoryId'. If category not found, return 0;
257///
258ObjCCategoryDecl *
259 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
260 for (ObjCCategoryDecl *Category = getCategoryList();
261 Category; Category = Category->getNextClassCategory())
262 if (Category->getIdentifier() == CategoryId)
263 return Category;
264 return 0;
265}
266
267/// FindIvarDeclaration - Find an Ivar declaration in this class given its
268/// name in 'IvarId'. On failure to find, return 0;
269///
270ObjCIvarDecl *
271 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
272 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
273 IVE = ivar_end(); IVI != IVE; ++IVI) {
274 ObjCIvarDecl* Ivar = (*IVI);
275 if (Ivar->getIdentifier() == IvarId)
276 return Ivar;
277 }
Fariborz Jahanianc70bee82008-04-21 23:57:08 +0000278 if (getSuperClass())
279 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000280 return 0;
281}
282
Chris Lattner1e03a562008-03-16 00:19:01 +0000283/// ObjCAddInstanceVariablesToClass - Inserts instance variables
284/// into ObjCInterfaceDecl's fields.
285///
286void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
287 unsigned numIvars,
288 SourceLocation RBrac) {
289 NumIvars = numIvars;
290 if (numIvars) {
291 Ivars = new ObjCIvarDecl*[numIvars];
292 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
293 }
294 setLocEnd(RBrac);
295}
296
297/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
298/// Variables (Ivars) relative to what declared in @implementation;s class.
299/// Ivars into ObjCImplementationDecl's fields.
300///
301void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
302 ObjCIvarDecl **ivars, unsigned numIvars) {
303 NumIvars = numIvars;
304 if (numIvars) {
305 Ivars = new ObjCIvarDecl*[numIvars];
306 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
307 }
308}
309
310/// addMethods - Insert instance and methods declarations into
311/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
312///
313void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
314 unsigned numInsMembers,
315 ObjCMethodDecl **clsMethods,
316 unsigned numClsMembers,
317 SourceLocation endLoc) {
318 NumInstanceMethods = numInsMembers;
319 if (numInsMembers) {
320 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
321 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
322 }
323 NumClassMethods = numClsMembers;
324 if (numClsMembers) {
325 ClassMethods = new ObjCMethodDecl*[numClsMembers];
326 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
327 }
328 AtEndLoc = endLoc;
329}
330
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000331/// addProperties - Insert property declaration AST nodes into
332/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattner55d13b42008-03-16 21:23:50 +0000333///
334void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
335 unsigned NumProperties) {
336 if (NumProperties == 0) return;
337
338 NumPropertyDecl = NumProperties;
339 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
340 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
341}
342
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000343/// mergeProperties - Adds properties to the end of list of current properties
344/// for this class.
345
346void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
347 unsigned NumNewProperties) {
348 if (NumNewProperties == 0) return;
349
350 if (PropertyDecl) {
351 ObjCPropertyDecl **newPropertyDecl =
352 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
353 ObjCPropertyDecl **buf = newPropertyDecl;
354 // put back original properties in buffer.
355 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
356 // Add new properties to this buffer.
357 memcpy(buf+NumPropertyDecl, Properties,
358 NumNewProperties*sizeof(ObjCPropertyDecl*));
Nuno Lopes6b3502c2008-05-10 10:31:54 +0000359 delete[] PropertyDecl;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000360 PropertyDecl = newPropertyDecl;
361 NumPropertyDecl += NumNewProperties;
362 }
363 else {
Nuno Lopes6b3502c2008-05-10 10:31:54 +0000364 addProperties(Properties, NumNewProperties);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000365 }
366}
367
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000368/// addPropertyMethods - Goes through list of properties declared in this class
369/// and builds setter/getter method declartions depending on the setter/getter
370/// attributes of the property.
371///
372void ObjCInterfaceDecl::addPropertyMethods(
373 ASTContext &Context,
374 ObjCPropertyDecl *property,
375 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
376 // Find the default getter and if one not found, add one.
377 ObjCMethodDecl *GetterDecl = getInstanceMethod(property->getGetterName());
378 if (GetterDecl) {
379 // An instance method with same name as property getter name found.
380 property->setGetterMethodDecl(GetterDecl);
381 }
382 else {
383 // No instance method of same name as property getter name was found.
384 // Declare a getter method and add it to the list of methods
385 // for this class.
386 QualType resultDeclType = property->getType();
387 ObjCMethodDecl* ObjCMethod =
388 ObjCMethodDecl::Create(Context, property->getLocation(),
389 property->getLocation(),
390 property->getGetterName(), resultDeclType,
391 this, 0,
Fariborz Jahanian46070342008-05-07 20:53:44 +0000392 true, false, true, ObjCMethodDecl::Required);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000393 property->setGetterMethodDecl(ObjCMethod);
394 insMethods.push_back(ObjCMethod);
395 }
396}
397
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000398/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000399/// ObjCProtocolDecl's PropertyDecl field.
400///
401void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
402 unsigned NumProperties) {
403 if (NumProperties == 0) return;
404
405 NumPropertyDecl = NumProperties;
406 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
407 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
408}
409
410/// addProperties - Insert property declaration AST nodes into
Fariborz Jahaniand9a3c332008-04-16 21:11:25 +0000411/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000412///
413void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
414 unsigned NumProperties) {
415 if (NumProperties == 0) return;
416
417 NumPropertyDecl = NumProperties;
418 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
419 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
420}
421
Chris Lattner55d13b42008-03-16 21:23:50 +0000422/// addMethods - Insert instance and methods declarations into
Chris Lattner1e03a562008-03-16 00:19:01 +0000423/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
424///
425void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
426 unsigned numInsMembers,
427 ObjCMethodDecl **clsMethods,
428 unsigned numClsMembers,
429 SourceLocation endLoc) {
430 NumInstanceMethods = numInsMembers;
431 if (numInsMembers) {
432 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
433 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
434 }
435 NumClassMethods = numClsMembers;
436 if (numClsMembers) {
437 ClassMethods = new ObjCMethodDecl*[numClsMembers];
438 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
439 }
440 AtEndLoc = endLoc;
441}
442
Chris Lattner68c82cf2008-03-16 20:47:45 +0000443
444
Chris Lattner1e03a562008-03-16 00:19:01 +0000445/// addMethods - Insert instance and methods declarations into
446/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
447///
448void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
449 unsigned numInsMembers,
450 ObjCMethodDecl **clsMethods,
451 unsigned numClsMembers,
452 SourceLocation endLoc) {
453 NumInstanceMethods = numInsMembers;
454 if (numInsMembers) {
455 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
456 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
457 }
458 NumClassMethods = numClsMembers;
459 if (numClsMembers) {
460 ClassMethods = new ObjCMethodDecl*[numClsMembers];
461 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
462 }
463 AtEndLoc = endLoc;
464}
465
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000466/// FindPropertyDeclaration - Finds declaration of the property given its name
467/// in 'PropertyId' and returns it. It returns 0, if not found.
468///
469ObjCPropertyDecl *
470ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
471 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
472 E = classprop_end(); I != E; ++I) {
473 ObjCPropertyDecl *property = *I;
474 if (property->getIdentifier() == PropertyId)
475 return property;
476 }
477 return 0;
478}
479
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000480/// FindPropertyDeclaration - Finds declaration of the property given its name
481/// in 'PropertyId' and returns it. It returns 0, if not found.
482///
483ObjCPropertyDecl *
484ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
485 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
486 E = classprop_end(); I != E; ++I) {
487 ObjCPropertyDecl *property = *I;
488 if (property->getIdentifier() == PropertyId)
489 return property;
490 }
491 return 0;
492}
493
Chris Lattner1e03a562008-03-16 00:19:01 +0000494ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
495 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
496 ObjCInterfaceDecl* ClassDecl = this;
497 while (ClassDecl != NULL) {
498 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
499 I != E; ++I) {
500 if ((*I)->getIdentifier() == ID) {
501 clsDeclared = ClassDecl;
502 return *I;
503 }
504 }
505 ClassDecl = ClassDecl->getSuperClass();
506 }
507 return NULL;
508}
509
510/// lookupInstanceMethod - This method returns an instance method by looking in
511/// the class, its categories, and its super classes (using a linear search).
512ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
513 ObjCInterfaceDecl* ClassDecl = this;
514 ObjCMethodDecl *MethodDecl = 0;
515
516 while (ClassDecl != NULL) {
517 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
518 return MethodDecl;
519
520 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000521 const ObjCList<ObjCProtocolDecl> &Protocols =
522 ClassDecl->getReferencedProtocols();
523 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
524 E = Protocols.end(); I != E; ++I)
525 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000526 return MethodDecl;
Chris Lattner3db6cae2008-07-21 18:19:38 +0000527
Chris Lattner1e03a562008-03-16 00:19:01 +0000528 // Didn't find one yet - now look through categories.
529 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
530 while (CatDecl) {
531 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
532 return MethodDecl;
533 CatDecl = CatDecl->getNextClassCategory();
534 }
535 ClassDecl = ClassDecl->getSuperClass();
536 }
537 return NULL;
538}
539
540// lookupClassMethod - This method returns a class method by looking in the
541// class, its categories, and its super classes (using a linear search).
542ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
543 ObjCInterfaceDecl* ClassDecl = this;
544 ObjCMethodDecl *MethodDecl = 0;
545
546 while (ClassDecl != NULL) {
547 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
548 return MethodDecl;
549
550 // Didn't find one yet - look through protocols.
Chris Lattner780f3292008-07-21 21:32:27 +0000551 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
552 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner3db6cae2008-07-21 18:19:38 +0000553 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000554 return MethodDecl;
Chris Lattner780f3292008-07-21 21:32:27 +0000555
Chris Lattner1e03a562008-03-16 00:19:01 +0000556 // Didn't find one yet - now look through categories.
557 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
558 while (CatDecl) {
559 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
560 return MethodDecl;
561 CatDecl = CatDecl->getNextClassCategory();
562 }
563 ClassDecl = ClassDecl->getSuperClass();
564 }
565 return NULL;
566}
567
568/// lookupInstanceMethod - This method returns an instance method by looking in
569/// the class implementation. Unlike interfaces, we don't look outside the
570/// implementation.
571ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
572 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
573 if ((*I)->getSelector() == Sel)
574 return *I;
575 return NULL;
576}
577
578/// lookupClassMethod - This method returns a class method by looking in
579/// the class implementation. Unlike interfaces, we don't look outside the
580/// implementation.
581ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
582 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
583 I != E; ++I)
584 if ((*I)->getSelector() == Sel)
585 return *I;
586 return NULL;
587}
588
589// lookupInstanceMethod - This method returns an instance method by looking in
590// the class implementation. Unlike interfaces, we don't look outside the
591// implementation.
592ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
593 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
594 if ((*I)->getSelector() == Sel)
595 return *I;
596 return NULL;
597}
598
599// lookupClassMethod - This method returns an instance method by looking in
600// the class implementation. Unlike interfaces, we don't look outside the
601// implementation.
602ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
603 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
604 I != E; ++I)
605 if ((*I)->getSelector() == Sel)
606 return *I;
607 return NULL;
608}
609
610// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
611// it inherited.
612ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
613 ObjCMethodDecl *MethodDecl = NULL;
614
615 if ((MethodDecl = getInstanceMethod(Sel)))
616 return MethodDecl;
617
Chris Lattner780f3292008-07-21 21:32:27 +0000618 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
619 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
620 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000621 return NULL;
622}
623
624// lookupInstanceMethod - Lookup a class method in the protocol and protocols
625// it inherited.
626ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
627 ObjCMethodDecl *MethodDecl = NULL;
628
629 if ((MethodDecl = getClassMethod(Sel)))
630 return MethodDecl;
631
Chris Lattner780f3292008-07-21 21:32:27 +0000632 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
633 if ((MethodDecl = (*I)->getClassMethod(Sel)))
634 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000635 return NULL;
636}
637
638/// getSynthesizedMethodSize - Compute size of synthesized method name
639/// as done be the rewrite.
640///
641unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
642 // syntesized method name is a concatenation of -/+[class-name selector]
643 // Get length of this name.
644 unsigned length = 3; // _I_ or _C_
645 length += strlen(getClassInterface()->getName()) +1; // extra for _
646 NamedDecl *MethodContext = getMethodContext();
647 if (ObjCCategoryImplDecl *CID =
648 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
649 length += strlen(CID->getName()) +1;
650 length += getSelector().getName().size(); // selector name
651 return length;
652}
653
Chris Lattner56196882008-04-06 05:25:03 +0000654ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner1e03a562008-03-16 00:19:01 +0000655 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
656 return ID;
657 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
658 return CD->getClassInterface();
659 if (ObjCImplementationDecl *IMD =
Chris Lattner56196882008-04-06 05:25:03 +0000660 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner1e03a562008-03-16 00:19:01 +0000661 return IMD->getClassInterface();
Chris Lattner56196882008-04-06 05:25:03 +0000662 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner1e03a562008-03-16 00:19:01 +0000663 return CID->getClassInterface();
664 assert(false && "unknown method context");
665 return 0;
666}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000667
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000668ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
669 SourceLocation atLoc,
670 SourceLocation L,
671 ObjCPropertyDecl *property,
672 PropertyImplKind kind,
673 ObjCIvarDecl *ivar) {
674 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
675 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, kind, ivar);
676}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000677
Chris Lattner0ed844b2008-04-04 06:12:32 +0000678