blob: e74e21dbbca2555695b64b885dfaeef3fb940356 [file] [log] [blame]
Chris Lattner10318b82008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Chris Lattner10318b82008-03-16 00:19:01 +000017using namespace clang;
18
Chris Lattner114add62008-03-16 00:49:28 +000019//===----------------------------------------------------------------------===//
20// ObjC Decl Allocation/Deallocation Method Implementations
21//===----------------------------------------------------------------------===//
22
Chris Lattnereee57c02008-04-04 06:12:32 +000023ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
24 SourceLocation beginLoc,
Chris Lattner114add62008-03-16 00:49:28 +000025 SourceLocation endLoc,
26 Selector SelInfo, QualType T,
27 Decl *contextDecl,
Daniel Dunbard8bd6822008-08-20 18:02:42 +000028 bool isInstance,
Chris Lattner114add62008-03-16 00:49:28 +000029 bool isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +000030 bool isSynthesized,
Chris Lattnerf7355832008-03-16 00:58:16 +000031 ImplementationControl impControl) {
Chris Lattner114add62008-03-16 00:49:28 +000032 void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000033 return new (Mem) ObjCMethodDecl(beginLoc, endLoc,
34 SelInfo, T, contextDecl,
Daniel Dunbard8bd6822008-08-20 18:02:42 +000035 isInstance,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +000036 isVariadic, isSynthesized, impControl);
Chris Lattner0db541b2008-03-16 01:15:50 +000037}
Chris Lattner114add62008-03-16 00:49:28 +000038
Ted Kremenek8c945b12008-06-06 16:45:15 +000039ObjCMethodDecl::~ObjCMethodDecl() {
40 delete [] ParamInfo;
Ted Kremenek8c945b12008-06-06 16:45:15 +000041}
42
43void ObjCMethodDecl::Destroy(ASTContext& C) {
44 if (Body) Body->Destroy(C);
45 if (SelfDecl) SelfDecl->Destroy(C);
46
47 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
48 if (*I) (*I)->Destroy(C);
49
50 Decl::Destroy(C);
51}
52
Chris Lattnereee57c02008-04-04 06:12:32 +000053ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
54 SourceLocation atLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000055 IdentifierInfo *Id,
56 SourceLocation ClassLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000057 bool ForwardDecl, bool isInternal){
58 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Chris Lattner5cece462008-07-21 07:06:49 +000059 return new (Mem) ObjCInterfaceDecl(atLoc, Id, ClassLoc, ForwardDecl,
Chris Lattner0db541b2008-03-16 01:15:50 +000060 isInternal);
61}
62
Ted Kremenek8c945b12008-06-06 16:45:15 +000063ObjCInterfaceDecl::~ObjCInterfaceDecl() {
64 delete [] Ivars;
65 delete [] InstanceMethods;
66 delete [] ClassMethods;
67 delete [] PropertyDecl;
68 // FIXME: CategoryList?
69}
70
71void ObjCInterfaceDecl::Destroy(ASTContext& C) {
72 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
73 if (*I) (*I)->Destroy(C);
74
75 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
76 if (*I) (*I)->Destroy(C);
77
78 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
79 if (*I) (*I)->Destroy(C);
Ted Kremenek22db1602008-06-06 17:21:42 +000080
81 // FIXME: Because there is no clear ownership
82 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
83 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
84
Ted Kremenek8c945b12008-06-06 16:45:15 +000085 Decl::Destroy(C);
86}
87
88
Chris Lattnerf3874bc2008-04-06 04:47:34 +000089ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Ted Kremenek173dd312008-07-23 18:04:17 +000090 IdentifierInfo *Id, QualType T,
91 AccessControl ac, Expr *BW) {
Chris Lattner0db541b2008-03-16 01:15:50 +000092 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Ted Kremenek173dd312008-07-23 18:04:17 +000093 return new (Mem) ObjCIvarDecl(L, Id, T, ac, BW);
Chris Lattner114add62008-03-16 00:49:28 +000094}
95
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000096
97ObjCAtDefsFieldDecl
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 Lattnereee57c02008-04-04 06:12:32 +0000109ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
110 SourceLocation L,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000111 IdentifierInfo *Id) {
Chris Lattner180f7e22008-03-16 01:23:04 +0000112 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner0be08822008-07-21 21:32:27 +0000113 return new (Mem) ObjCProtocolDecl(L, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +0000114}
115
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000116ObjCProtocolDecl::~ObjCProtocolDecl() {
Ted Kremenekc906e5e2008-06-06 19:48:57 +0000117 delete [] InstanceMethods;
118 delete [] ClassMethods;
119 delete [] PropertyDecl;
120}
121
122void ObjCProtocolDecl::Destroy(ASTContext& C) {
123
124 // Referenced Protocols are not owned, so don't Destroy them.
125
126 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
127 if (*I) (*I)->Destroy(C);
128
129 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
130 if (*I) (*I)->Destroy(C);
131
132 // FIXME: Because there is no clear ownership
133 // role between ObjCProtocolDecls and the ObjCPropertyDecls that they
134 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
135
136 Decl::Destroy(C);
137}
138
139
Chris Lattnereee57c02008-04-04 06:12:32 +0000140ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
141 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000142 ObjCInterfaceDecl **Elts, unsigned nElts) {
143 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
144 return new (Mem) ObjCClassDecl(L, Elts, nElts);
145}
146
Ted Kremenekff291622008-06-06 20:11:53 +0000147ObjCClassDecl::~ObjCClassDecl() {
148 delete [] ForwardDecls;
149}
150
151void ObjCClassDecl::Destroy(ASTContext& C) {
152
153 // FIXME: There is no clear ownership policy now for referenced
154 // ObjCInterfaceDecls. Some of them can be forward declarations that
155 // are never later defined (in which case the ObjCClassDecl owns them)
156 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
157 // we should have separate objects for forward declarations and definitions,
158 // obviating this problem. Because of this situation, referenced
159 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
160
161 Decl::Destroy(C);
162}
163
Chris Lattnere29dc832008-03-16 20:34:23 +0000164ObjCForwardProtocolDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000165ObjCForwardProtocolDecl::Create(ASTContext &C,
166 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000167 ObjCProtocolDecl **Elts, unsigned NumElts) {
168 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
169 return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
170}
171
Ted Kremenek1e5e0bf2008-06-06 21:05:33 +0000172ObjCForwardProtocolDecl::~ObjCForwardProtocolDecl() {
173 delete [] ReferencedProtocols;
174}
175
Chris Lattnereee57c02008-04-04 06:12:32 +0000176ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
177 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000178 IdentifierInfo *Id) {
179 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner321b5d12008-03-16 20:47:45 +0000180 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000181}
182
Chris Lattner1b6de332008-03-16 20:53:07 +0000183ObjCCategoryImplDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000184ObjCCategoryImplDecl::Create(ASTContext &C,
185 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000186 ObjCInterfaceDecl *ClassInterface) {
187 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
188 return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
189}
190
191ObjCImplementationDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000192ObjCImplementationDecl::Create(ASTContext &C,
193 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000194 IdentifierInfo *Id,
195 ObjCInterfaceDecl *ClassInterface,
196 ObjCInterfaceDecl *SuperDecl) {
197 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
198 return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
199}
Chris Lattner10318b82008-03-16 00:19:01 +0000200
Chris Lattner2d1c4312008-03-16 21:17:37 +0000201ObjCCompatibleAliasDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000202ObjCCompatibleAliasDecl::Create(ASTContext &C,
203 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000204 IdentifierInfo *Id,
205 ObjCInterfaceDecl* AliasedClass) {
206 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
207 return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
208}
209
Chris Lattnereee57c02008-04-04 06:12:32 +0000210ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000211 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000212 IdentifierInfo *Id,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000213 QualType T,
214 PropertyControl propControl) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000215 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000216 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000217}
218
Chris Lattner10318b82008-03-16 00:19:01 +0000219//===----------------------------------------------------------------------===//
220// Objective-C Decl Implementation
221//===----------------------------------------------------------------------===//
222
Daniel Dunbar88116372008-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 Lattner10318b82008-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 Jahanianef8a3df2008-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 Naroff30faf472008-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 Naroffd1f0eb42008-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 Jahanian8acf3352008-04-21 23:57:08 +0000286 if (getSuperClass())
287 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-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 Jahanian8acf3352008-04-21 23:57:08 +0000315 if (getSuperClass())
316 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000317 return 0;
318}
319
Chris Lattner10318b82008-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 Jahanian52ff8442008-04-16 21:08:45 +0000368/// addProperties - Insert property declaration AST nodes into
369/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-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 Jahanian33973a22008-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 Lopes1ee78042008-05-10 10:31:54 +0000396 delete[] PropertyDecl;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000397 PropertyDecl = newPropertyDecl;
398 NumPropertyDecl += NumNewProperties;
399 }
400 else {
Nuno Lopes1ee78042008-05-10 10:31:54 +0000401 addProperties(Properties, NumNewProperties);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000402 }
403}
404
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000405/// addPropertyMethods - Goes through list of properties declared in this class
406/// and builds setter/getter method declartions depending on the setter/getter
407/// attributes of the property.
408///
409void ObjCInterfaceDecl::addPropertyMethods(
410 ASTContext &Context,
411 ObjCPropertyDecl *property,
412 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000413 // FIXME: The synthesized property we set here is misleading. We
414 // almost always synthesize these methods unless the user explicitly
415 // provided prototypes (which is odd, but allowed). Sema should be
416 // typechecking that the declarations jive in that situation (which
417 // it is not currently).
418
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000419 // Find the default getter and if one not found, add one.
420 ObjCMethodDecl *GetterDecl = getInstanceMethod(property->getGetterName());
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000421 if (!GetterDecl) {
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000422 // No instance method of same name as property getter name was found.
423 // Declare a getter method and add it to the list of methods
424 // for this class.
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000425 GetterDecl =
426 ObjCMethodDecl::Create(Context, property->getLocation(),
427 property->getLocation(),
428 property->getGetterName(),
429 property->getType(),
430 this,
431 true, false, true, ObjCMethodDecl::Required);
432 insMethods.push_back(GetterDecl);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000433 }
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000434 property->setGetterMethodDecl(GetterDecl);
435
Daniel Dunbardb3b4492008-08-26 07:16:44 +0000436 // Skip setter if property is read-only.
437 if (property->isReadOnly())
438 return;
439
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000440 // Find the default setter and if one not found, add one.
441 ObjCMethodDecl *SetterDecl = getInstanceMethod(property->getSetterName());
442 if (!SetterDecl) {
443 // No instance method of same name as property setter name was found.
444 // Declare a setter method and add it to the list of methods
445 // for this class.
446 SetterDecl =
447 ObjCMethodDecl::Create(Context, property->getLocation(),
448 property->getLocation(),
449 property->getSetterName(),
Daniel Dunbar4b8babe2008-08-26 02:53:23 +0000450 Context.VoidTy,
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000451 this,
452 true, false, true, ObjCMethodDecl::Required);
453 insMethods.push_back(SetterDecl);
454
455 // Invent the arguments for the setter. We don't bother making a
456 // nice name for the argument.
457 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
458 SetterDecl,
459 SourceLocation(),
460 property->getIdentifier(),
461 property->getType(),
462 VarDecl::None,
463 0, 0);
464 SetterDecl->setMethodParams(&Argument, 1);
465 }
466 property->setSetterMethodDecl(SetterDecl);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000467}
468
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000469/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000470/// ObjCProtocolDecl's PropertyDecl field.
471///
472void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
473 unsigned NumProperties) {
474 if (NumProperties == 0) return;
475
476 NumPropertyDecl = NumProperties;
477 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
478 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
479}
480
481/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000482/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000483///
484void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
485 unsigned NumProperties) {
486 if (NumProperties == 0) return;
487
488 NumPropertyDecl = NumProperties;
489 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
490 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
491}
492
Chris Lattnercffe3662008-03-16 21:23:50 +0000493/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000494/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
495///
496void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
497 unsigned numInsMembers,
498 ObjCMethodDecl **clsMethods,
499 unsigned numClsMembers,
500 SourceLocation endLoc) {
501 NumInstanceMethods = numInsMembers;
502 if (numInsMembers) {
503 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
504 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
505 }
506 NumClassMethods = numClsMembers;
507 if (numClsMembers) {
508 ClassMethods = new ObjCMethodDecl*[numClsMembers];
509 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
510 }
511 AtEndLoc = endLoc;
512}
513
Chris Lattner321b5d12008-03-16 20:47:45 +0000514
515
Chris Lattner10318b82008-03-16 00:19:01 +0000516/// addMethods - Insert instance and methods declarations into
517/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
518///
519void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
520 unsigned numInsMembers,
521 ObjCMethodDecl **clsMethods,
522 unsigned numClsMembers,
523 SourceLocation endLoc) {
524 NumInstanceMethods = numInsMembers;
525 if (numInsMembers) {
526 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
527 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
528 }
529 NumClassMethods = numClsMembers;
530 if (numClsMembers) {
531 ClassMethods = new ObjCMethodDecl*[numClsMembers];
532 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
533 }
534 AtEndLoc = endLoc;
535}
536
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000537/// FindPropertyDeclaration - Finds declaration of the property given its name
538/// in 'PropertyId' and returns it. It returns 0, if not found.
539///
540ObjCPropertyDecl *
541ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
542 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
543 E = classprop_end(); I != E; ++I) {
544 ObjCPropertyDecl *property = *I;
545 if (property->getIdentifier() == PropertyId)
546 return property;
547 }
548 return 0;
549}
550
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000551/// FindPropertyDeclaration - Finds declaration of the property given its name
552/// in 'PropertyId' and returns it. It returns 0, if not found.
553///
554ObjCPropertyDecl *
555ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
556 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
557 E = classprop_end(); I != E; ++I) {
558 ObjCPropertyDecl *property = *I;
559 if (property->getIdentifier() == PropertyId)
560 return property;
561 }
562 return 0;
563}
564
Chris Lattner10318b82008-03-16 00:19:01 +0000565ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
566 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
567 ObjCInterfaceDecl* ClassDecl = this;
568 while (ClassDecl != NULL) {
569 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
570 I != E; ++I) {
571 if ((*I)->getIdentifier() == ID) {
572 clsDeclared = ClassDecl;
573 return *I;
574 }
575 }
576 ClassDecl = ClassDecl->getSuperClass();
577 }
578 return NULL;
579}
580
581/// lookupInstanceMethod - This method returns an instance method by looking in
582/// the class, its categories, and its super classes (using a linear search).
583ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
584 ObjCInterfaceDecl* ClassDecl = this;
585 ObjCMethodDecl *MethodDecl = 0;
586
587 while (ClassDecl != NULL) {
588 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
589 return MethodDecl;
590
591 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000592 const ObjCList<ObjCProtocolDecl> &Protocols =
593 ClassDecl->getReferencedProtocols();
594 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
595 E = Protocols.end(); I != E; ++I)
596 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000597 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000598
Chris Lattner10318b82008-03-16 00:19:01 +0000599 // Didn't find one yet - now look through categories.
600 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
601 while (CatDecl) {
602 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
603 return MethodDecl;
604 CatDecl = CatDecl->getNextClassCategory();
605 }
606 ClassDecl = ClassDecl->getSuperClass();
607 }
608 return NULL;
609}
610
611// lookupClassMethod - This method returns a class method by looking in the
612// class, its categories, and its super classes (using a linear search).
613ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
614 ObjCInterfaceDecl* ClassDecl = this;
615 ObjCMethodDecl *MethodDecl = 0;
616
617 while (ClassDecl != NULL) {
618 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
619 return MethodDecl;
620
621 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000622 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
623 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000624 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000625 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000626
Chris Lattner10318b82008-03-16 00:19:01 +0000627 // Didn't find one yet - now look through categories.
628 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
629 while (CatDecl) {
630 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
631 return MethodDecl;
632 CatDecl = CatDecl->getNextClassCategory();
633 }
634 ClassDecl = ClassDecl->getSuperClass();
635 }
636 return NULL;
637}
638
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000639/// getInstanceMethod - This method returns an instance method by
640/// looking in the class implementation. Unlike interfaces, we don't
641/// look outside the implementation.
642ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000643 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
644 if ((*I)->getSelector() == Sel)
645 return *I;
646 return NULL;
647}
648
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000649/// getClassMethod - This method returns a class method by looking in
650/// the class implementation. Unlike interfaces, we don't look outside
651/// the implementation.
652ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000653 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
654 I != E; ++I)
655 if ((*I)->getSelector() == Sel)
656 return *I;
657 return NULL;
658}
659
660// lookupInstanceMethod - This method returns an instance method by looking in
661// the class implementation. Unlike interfaces, we don't look outside the
662// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000663ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000664 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
665 if ((*I)->getSelector() == Sel)
666 return *I;
667 return NULL;
668}
669
670// lookupClassMethod - This method returns an instance method by looking in
671// the class implementation. Unlike interfaces, we don't look outside the
672// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000673ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000674 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
675 I != E; ++I)
676 if ((*I)->getSelector() == Sel)
677 return *I;
678 return NULL;
679}
680
681// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
682// it inherited.
683ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
684 ObjCMethodDecl *MethodDecl = NULL;
685
686 if ((MethodDecl = getInstanceMethod(Sel)))
687 return MethodDecl;
688
Chris Lattner0be08822008-07-21 21:32:27 +0000689 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
690 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
691 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000692 return NULL;
693}
694
695// lookupInstanceMethod - Lookup a class method in the protocol and protocols
696// it inherited.
697ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
698 ObjCMethodDecl *MethodDecl = NULL;
699
700 if ((MethodDecl = getClassMethod(Sel)))
701 return MethodDecl;
702
Chris Lattner0be08822008-07-21 21:32:27 +0000703 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
704 if ((MethodDecl = (*I)->getClassMethod(Sel)))
705 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000706 return NULL;
707}
708
709/// getSynthesizedMethodSize - Compute size of synthesized method name
710/// as done be the rewrite.
711///
712unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
713 // syntesized method name is a concatenation of -/+[class-name selector]
714 // Get length of this name.
715 unsigned length = 3; // _I_ or _C_
716 length += strlen(getClassInterface()->getName()) +1; // extra for _
717 NamedDecl *MethodContext = getMethodContext();
718 if (ObjCCategoryImplDecl *CID =
719 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
720 length += strlen(CID->getName()) +1;
721 length += getSelector().getName().size(); // selector name
722 return length;
723}
724
Chris Lattner052fbcd2008-04-06 05:25:03 +0000725ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000726 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
727 return ID;
728 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
729 return CD->getClassInterface();
730 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000731 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000732 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000733 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000734 return CID->getClassInterface();
735 assert(false && "unknown method context");
736 return 0;
737}
Chris Lattner44859612008-03-17 01:19:02 +0000738
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000739ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
740 SourceLocation atLoc,
741 SourceLocation L,
742 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000743 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000744 ObjCIvarDecl *ivar) {
745 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000746 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000747}
Chris Lattner44859612008-03-17 01:19:02 +0000748
Chris Lattnereee57c02008-04-04 06:12:32 +0000749