blob: 0d006d676d7385b37dde99baa77e7349fe8c59d5 [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
98*ObjCAtDefsFieldDecl::Create(ASTContext &C, SourceLocation L,
99 IdentifierInfo *Id, QualType T, Expr *BW) {
100 void *Mem = C.getAllocator().Allocate<ObjCAtDefsFieldDecl>();
101 return new (Mem) ObjCAtDefsFieldDecl(L, Id, T, BW);
102}
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
Daniel Dunbar08a356c2008-08-26 06:08:30 +0000223void ObjCMethodDecl::createImplicitParams(ASTContext &Context) {
224 QualType selfTy;
225 if (isInstance()) {
226 // There may be no interface context due to error in declaration
227 // of the interface (which has been reported). Recover gracefully.
228 if (ObjCInterfaceDecl *OID = getClassInterface()) {
229 selfTy = Context.getObjCInterfaceType(OID);
230 selfTy = Context.getPointerType(selfTy);
231 } else {
232 selfTy = Context.getObjCIdType();
233 }
234 } else // we have a factory method.
235 selfTy = Context.getObjCClassType();
236
237 SelfDecl = ImplicitParamDecl::Create(Context, this,
238 SourceLocation(),
239 &Context.Idents.get("self"),
240 selfTy, 0);
241
242 CmdDecl = ImplicitParamDecl::Create(Context, this,
243 SourceLocation(),
244 &Context.Idents.get("_cmd"),
245 Context.getObjCSelType(), 0);
246}
247
Chris Lattner1e03a562008-03-16 00:19:01 +0000248void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
249 unsigned NumParams) {
250 assert(ParamInfo == 0 && "Already has param info!");
251
252 // Zero params -> null pointer.
253 if (NumParams) {
254 ParamInfo = new ParmVarDecl*[NumParams];
255 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
256 NumMethodParams = NumParams;
257 }
258}
259
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000260/// FindPropertyDeclaration - Finds declaration of the property given its name
261/// in 'PropertyId' and returns it. It returns 0, if not found.
262///
263ObjCPropertyDecl *
264 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
265 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
266 E = classprop_end(); I != E; ++I) {
267 ObjCPropertyDecl *property = *I;
268 if (property->getIdentifier() == PropertyId)
269 return property;
270 }
Steve Naroff6c930f22008-06-04 04:46:04 +0000271 // Look through categories.
272 for (ObjCCategoryDecl *Category = getCategoryList();
273 Category; Category = Category->getNextClassCategory()) {
274 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
275 if (property)
276 return property;
277 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000278 // Look through protocols.
279 for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(),
280 E = protocol_end(); I != E; ++I) {
281 ObjCProtocolDecl *Protocol = *I;
282 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
283 if (property)
284 return property;
285 }
Fariborz Jahanianc70bee82008-04-21 23:57:08 +0000286 if (getSuperClass())
287 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000288 return 0;
289}
290
291/// FindCategoryDeclaration - Finds category declaration in the list of
292/// categories for this class and returns it. Name of the category is passed
293/// in 'CategoryId'. If category not found, return 0;
294///
295ObjCCategoryDecl *
296 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
297 for (ObjCCategoryDecl *Category = getCategoryList();
298 Category; Category = Category->getNextClassCategory())
299 if (Category->getIdentifier() == CategoryId)
300 return Category;
301 return 0;
302}
303
304/// FindIvarDeclaration - Find an Ivar declaration in this class given its
305/// name in 'IvarId'. On failure to find, return 0;
306///
307ObjCIvarDecl *
308 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
309 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
310 IVE = ivar_end(); IVI != IVE; ++IVI) {
311 ObjCIvarDecl* Ivar = (*IVI);
312 if (Ivar->getIdentifier() == IvarId)
313 return Ivar;
314 }
Fariborz Jahanianc70bee82008-04-21 23:57:08 +0000315 if (getSuperClass())
316 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000317 return 0;
318}
319
Chris Lattner1e03a562008-03-16 00:19:01 +0000320/// ObjCAddInstanceVariablesToClass - Inserts instance variables
321/// into ObjCInterfaceDecl's fields.
322///
323void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
324 unsigned numIvars,
325 SourceLocation RBrac) {
326 NumIvars = numIvars;
327 if (numIvars) {
328 Ivars = new ObjCIvarDecl*[numIvars];
329 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
330 }
331 setLocEnd(RBrac);
332}
333
334/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
335/// Variables (Ivars) relative to what declared in @implementation;s class.
336/// Ivars into ObjCImplementationDecl's fields.
337///
338void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
339 ObjCIvarDecl **ivars, unsigned numIvars) {
340 NumIvars = numIvars;
341 if (numIvars) {
342 Ivars = new ObjCIvarDecl*[numIvars];
343 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
344 }
345}
346
347/// addMethods - Insert instance and methods declarations into
348/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
349///
350void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
351 unsigned numInsMembers,
352 ObjCMethodDecl **clsMethods,
353 unsigned numClsMembers,
354 SourceLocation endLoc) {
355 NumInstanceMethods = numInsMembers;
356 if (numInsMembers) {
357 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
358 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
359 }
360 NumClassMethods = numClsMembers;
361 if (numClsMembers) {
362 ClassMethods = new ObjCMethodDecl*[numClsMembers];
363 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
364 }
365 AtEndLoc = endLoc;
366}
367
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000368/// addProperties - Insert property declaration AST nodes into
369/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattner55d13b42008-03-16 21:23:50 +0000370///
371void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
372 unsigned NumProperties) {
373 if (NumProperties == 0) return;
374
375 NumPropertyDecl = NumProperties;
376 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
377 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
378}
379
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000380/// mergeProperties - Adds properties to the end of list of current properties
381/// for this class.
382
383void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
384 unsigned NumNewProperties) {
385 if (NumNewProperties == 0) return;
386
387 if (PropertyDecl) {
388 ObjCPropertyDecl **newPropertyDecl =
389 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
390 ObjCPropertyDecl **buf = newPropertyDecl;
391 // put back original properties in buffer.
392 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
393 // Add new properties to this buffer.
394 memcpy(buf+NumPropertyDecl, Properties,
395 NumNewProperties*sizeof(ObjCPropertyDecl*));
Nuno Lopes6b3502c2008-05-10 10:31:54 +0000396 delete[] PropertyDecl;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000397 PropertyDecl = newPropertyDecl;
398 NumPropertyDecl += NumNewProperties;
399 }
400 else {
Nuno Lopes6b3502c2008-05-10 10:31:54 +0000401 addProperties(Properties, NumNewProperties);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000402 }
403}
404
Daniel Dunbar7b473222008-08-27 02:09:39 +0000405static void
406addPropertyMethods(Decl *D,
407 ASTContext &Context,
408 ObjCPropertyDecl *property,
409 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
Daniel Dunbare86d9232008-08-27 05:13:46 +0000410 ObjCMethodDecl *GetterDecl, *SetterDecl = 0;
Daniel Dunbar7b473222008-08-27 02:09:39 +0000411
412 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
413 GetterDecl = OID->getInstanceMethod(property->getGetterName());
414 if (!property->isReadOnly())
415 SetterDecl = OID->getInstanceMethod(property->getSetterName());
416 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
417 GetterDecl = OCD->getInstanceMethod(property->getGetterName());
418 if (!property->isReadOnly())
419 SetterDecl = OCD->getInstanceMethod(property->getSetterName());
420 } else {
421 ObjCProtocolDecl *OPD = cast<ObjCProtocolDecl>(D);
422 GetterDecl = OPD->getInstanceMethod(property->getGetterName());
423 if (!property->isReadOnly())
424 SetterDecl = OPD->getInstanceMethod(property->getSetterName());
425 }
426
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000427 // FIXME: The synthesized property we set here is misleading. We
428 // almost always synthesize these methods unless the user explicitly
429 // provided prototypes (which is odd, but allowed). Sema should be
430 // typechecking that the declarations jive in that situation (which
431 // it is not currently).
432
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000433 // Find the default getter and if one not found, add one.
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000434 if (!GetterDecl) {
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000435 // No instance method of same name as property getter name was found.
436 // Declare a getter method and add it to the list of methods
437 // for this class.
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000438 GetterDecl =
439 ObjCMethodDecl::Create(Context, property->getLocation(),
440 property->getLocation(),
441 property->getGetterName(),
442 property->getType(),
Daniel Dunbar7b473222008-08-27 02:09:39 +0000443 D,
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000444 true, false, true, ObjCMethodDecl::Required);
445 insMethods.push_back(GetterDecl);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000446 }
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000447 property->setGetterMethodDecl(GetterDecl);
448
Daniel Dunbar394d33f2008-08-26 07:16:44 +0000449 // Skip setter if property is read-only.
450 if (property->isReadOnly())
451 return;
452
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000453 // Find the default setter and if one not found, add one.
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000454 if (!SetterDecl) {
455 // No instance method of same name as property setter name was found.
456 // Declare a setter method and add it to the list of methods
457 // for this class.
458 SetterDecl =
459 ObjCMethodDecl::Create(Context, property->getLocation(),
460 property->getLocation(),
461 property->getSetterName(),
Daniel Dunbar5fa63312008-08-26 02:53:23 +0000462 Context.VoidTy,
Daniel Dunbar7b473222008-08-27 02:09:39 +0000463 D,
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000464 true, false, true, ObjCMethodDecl::Required);
465 insMethods.push_back(SetterDecl);
466
467 // Invent the arguments for the setter. We don't bother making a
468 // nice name for the argument.
469 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
470 SetterDecl,
471 SourceLocation(),
472 property->getIdentifier(),
473 property->getType(),
474 VarDecl::None,
475 0, 0);
476 SetterDecl->setMethodParams(&Argument, 1);
477 }
478 property->setSetterMethodDecl(SetterDecl);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000479}
480
Daniel Dunbar7b473222008-08-27 02:09:39 +0000481/// addPropertyMethods - Goes through list of properties declared in this class
482/// and builds setter/getter method declartions depending on the setter/getter
483/// attributes of the property.
484///
485void ObjCInterfaceDecl::addPropertyMethods(
486 ASTContext &Context,
487 ObjCPropertyDecl *property,
488 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
489 ::addPropertyMethods(this, Context, property, insMethods);
490}
491
492/// addPropertyMethods - Goes through list of properties declared in this class
493/// and builds setter/getter method declartions depending on the setter/getter
494/// attributes of the property.
495///
496void ObjCCategoryDecl::addPropertyMethods(
497 ASTContext &Context,
498 ObjCPropertyDecl *property,
499 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
500 ::addPropertyMethods(this, Context, property, insMethods);
501}
502
503/// addPropertyMethods - Goes through list of properties declared in this class
504/// and builds setter/getter method declartions depending on the setter/getter
505/// attributes of the property.
506///
507void ObjCProtocolDecl::addPropertyMethods(
508 ASTContext &Context,
509 ObjCPropertyDecl *property,
510 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
511 ::addPropertyMethods(this, Context, property, insMethods);
512}
513
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000514/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000515/// ObjCProtocolDecl's PropertyDecl field.
516///
517void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
518 unsigned NumProperties) {
519 if (NumProperties == 0) return;
520
521 NumPropertyDecl = NumProperties;
522 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
523 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
524}
525
526/// addProperties - Insert property declaration AST nodes into
Fariborz Jahaniand9a3c332008-04-16 21:11:25 +0000527/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000528///
529void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
530 unsigned NumProperties) {
531 if (NumProperties == 0) return;
532
533 NumPropertyDecl = NumProperties;
534 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
535 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
536}
537
Chris Lattner55d13b42008-03-16 21:23:50 +0000538/// addMethods - Insert instance and methods declarations into
Chris Lattner1e03a562008-03-16 00:19:01 +0000539/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
540///
541void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
542 unsigned numInsMembers,
543 ObjCMethodDecl **clsMethods,
544 unsigned numClsMembers,
545 SourceLocation endLoc) {
546 NumInstanceMethods = numInsMembers;
547 if (numInsMembers) {
548 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
549 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
550 }
551 NumClassMethods = numClsMembers;
552 if (numClsMembers) {
553 ClassMethods = new ObjCMethodDecl*[numClsMembers];
554 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
555 }
556 AtEndLoc = endLoc;
557}
558
Chris Lattner68c82cf2008-03-16 20:47:45 +0000559
560
Chris Lattner1e03a562008-03-16 00:19:01 +0000561/// addMethods - Insert instance and methods declarations into
562/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
563///
564void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
565 unsigned numInsMembers,
566 ObjCMethodDecl **clsMethods,
567 unsigned numClsMembers,
568 SourceLocation endLoc) {
569 NumInstanceMethods = numInsMembers;
570 if (numInsMembers) {
571 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
572 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
573 }
574 NumClassMethods = numClsMembers;
575 if (numClsMembers) {
576 ClassMethods = new ObjCMethodDecl*[numClsMembers];
577 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
578 }
579 AtEndLoc = endLoc;
580}
581
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000582/// FindPropertyDeclaration - Finds declaration of the property given its name
583/// in 'PropertyId' and returns it. It returns 0, if not found.
584///
585ObjCPropertyDecl *
586ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
587 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
588 E = classprop_end(); I != E; ++I) {
589 ObjCPropertyDecl *property = *I;
590 if (property->getIdentifier() == PropertyId)
591 return property;
592 }
593 return 0;
594}
595
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000596/// FindPropertyDeclaration - Finds declaration of the property given its name
597/// in 'PropertyId' and returns it. It returns 0, if not found.
598///
599ObjCPropertyDecl *
600ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
601 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
602 E = classprop_end(); I != E; ++I) {
603 ObjCPropertyDecl *property = *I;
604 if (property->getIdentifier() == PropertyId)
605 return property;
606 }
607 return 0;
608}
609
Chris Lattner1e03a562008-03-16 00:19:01 +0000610ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
611 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
612 ObjCInterfaceDecl* ClassDecl = this;
613 while (ClassDecl != NULL) {
614 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
615 I != E; ++I) {
616 if ((*I)->getIdentifier() == ID) {
617 clsDeclared = ClassDecl;
618 return *I;
619 }
620 }
621 ClassDecl = ClassDecl->getSuperClass();
622 }
623 return NULL;
624}
625
626/// lookupInstanceMethod - This method returns an instance method by looking in
627/// the class, its categories, and its super classes (using a linear search).
628ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
629 ObjCInterfaceDecl* ClassDecl = this;
630 ObjCMethodDecl *MethodDecl = 0;
631
632 while (ClassDecl != NULL) {
633 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
634 return MethodDecl;
635
636 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000637 const ObjCList<ObjCProtocolDecl> &Protocols =
638 ClassDecl->getReferencedProtocols();
639 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
640 E = Protocols.end(); I != E; ++I)
641 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000642 return MethodDecl;
Chris Lattner3db6cae2008-07-21 18:19:38 +0000643
Chris Lattner1e03a562008-03-16 00:19:01 +0000644 // Didn't find one yet - now look through categories.
645 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
646 while (CatDecl) {
647 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
648 return MethodDecl;
649 CatDecl = CatDecl->getNextClassCategory();
650 }
651 ClassDecl = ClassDecl->getSuperClass();
652 }
653 return NULL;
654}
655
656// lookupClassMethod - This method returns a class method by looking in the
657// class, its categories, and its super classes (using a linear search).
658ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
659 ObjCInterfaceDecl* ClassDecl = this;
660 ObjCMethodDecl *MethodDecl = 0;
661
662 while (ClassDecl != NULL) {
663 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
664 return MethodDecl;
665
666 // Didn't find one yet - look through protocols.
Chris Lattner780f3292008-07-21 21:32:27 +0000667 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
668 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner3db6cae2008-07-21 18:19:38 +0000669 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000670 return MethodDecl;
Chris Lattner780f3292008-07-21 21:32:27 +0000671
Chris Lattner1e03a562008-03-16 00:19:01 +0000672 // Didn't find one yet - now look through categories.
673 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
674 while (CatDecl) {
675 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
676 return MethodDecl;
677 CatDecl = CatDecl->getNextClassCategory();
678 }
679 ClassDecl = ClassDecl->getSuperClass();
680 }
681 return NULL;
682}
683
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000684/// getInstanceMethod - This method returns an instance method by
685/// looking in the class implementation. Unlike interfaces, we don't
686/// look outside the implementation.
687ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000688 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
689 if ((*I)->getSelector() == Sel)
690 return *I;
691 return NULL;
692}
693
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000694/// getClassMethod - This method returns a class method by looking in
695/// the class implementation. Unlike interfaces, we don't look outside
696/// the implementation.
697ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000698 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
699 I != E; ++I)
700 if ((*I)->getSelector() == Sel)
701 return *I;
702 return NULL;
703}
704
705// lookupInstanceMethod - This method returns an instance method by looking in
706// the class implementation. Unlike interfaces, we don't look outside the
707// implementation.
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000708ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000709 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
710 if ((*I)->getSelector() == Sel)
711 return *I;
712 return NULL;
713}
714
715// lookupClassMethod - This method returns an instance method by looking in
716// the class implementation. Unlike interfaces, we don't look outside the
717// implementation.
Daniel Dunbar3216dcd2008-08-26 06:53:45 +0000718ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000719 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
720 I != E; ++I)
721 if ((*I)->getSelector() == Sel)
722 return *I;
723 return NULL;
724}
725
726// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
727// it inherited.
728ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
729 ObjCMethodDecl *MethodDecl = NULL;
730
731 if ((MethodDecl = getInstanceMethod(Sel)))
732 return MethodDecl;
733
Chris Lattner780f3292008-07-21 21:32:27 +0000734 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
735 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
736 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000737 return NULL;
738}
739
740// lookupInstanceMethod - Lookup a class method in the protocol and protocols
741// it inherited.
742ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
743 ObjCMethodDecl *MethodDecl = NULL;
744
745 if ((MethodDecl = getClassMethod(Sel)))
746 return MethodDecl;
747
Chris Lattner780f3292008-07-21 21:32:27 +0000748 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
749 if ((MethodDecl = (*I)->getClassMethod(Sel)))
750 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000751 return NULL;
752}
753
754/// getSynthesizedMethodSize - Compute size of synthesized method name
755/// as done be the rewrite.
756///
757unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
758 // syntesized method name is a concatenation of -/+[class-name selector]
759 // Get length of this name.
760 unsigned length = 3; // _I_ or _C_
761 length += strlen(getClassInterface()->getName()) +1; // extra for _
762 NamedDecl *MethodContext = getMethodContext();
763 if (ObjCCategoryImplDecl *CID =
764 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
765 length += strlen(CID->getName()) +1;
766 length += getSelector().getName().size(); // selector name
767 return length;
768}
769
Chris Lattner56196882008-04-06 05:25:03 +0000770ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner1e03a562008-03-16 00:19:01 +0000771 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
772 return ID;
773 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
774 return CD->getClassInterface();
775 if (ObjCImplementationDecl *IMD =
Chris Lattner56196882008-04-06 05:25:03 +0000776 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner1e03a562008-03-16 00:19:01 +0000777 return IMD->getClassInterface();
Chris Lattner56196882008-04-06 05:25:03 +0000778 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner1e03a562008-03-16 00:19:01 +0000779 return CID->getClassInterface();
780 assert(false && "unknown method context");
781 return 0;
782}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000783
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000784ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
785 SourceLocation atLoc,
786 SourceLocation L,
787 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000788 Kind PK,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000789 ObjCIvarDecl *ivar) {
790 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000791 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, PK, ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000792}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000793
Chris Lattner0ed844b2008-04-04 06:12:32 +0000794