blob: 2d959445d4abfd752c42250c7fddaf5ef7624ff1 [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"
16using namespace clang;
17
Chris Lattner114add62008-03-16 00:49:28 +000018//===----------------------------------------------------------------------===//
19// ObjC Decl Allocation/Deallocation Method Implementations
20//===----------------------------------------------------------------------===//
21
Chris Lattnereee57c02008-04-04 06:12:32 +000022ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
23 SourceLocation beginLoc,
Chris Lattner114add62008-03-16 00:49:28 +000024 SourceLocation endLoc,
25 Selector SelInfo, QualType T,
26 Decl *contextDecl,
Chris Lattner114add62008-03-16 00:49:28 +000027 AttributeList *M, bool isInstance,
28 bool isVariadic,
Chris Lattnerf7355832008-03-16 00:58:16 +000029 ImplementationControl impControl) {
Chris Lattner114add62008-03-16 00:49:28 +000030 void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000031 return new (Mem) ObjCMethodDecl(beginLoc, endLoc,
32 SelInfo, T, contextDecl,
Chris Lattnerf7355832008-03-16 00:58:16 +000033 M, isInstance,
34 isVariadic, impControl);
Chris Lattner0db541b2008-03-16 01:15:50 +000035}
Chris Lattner114add62008-03-16 00:49:28 +000036
Chris Lattnereee57c02008-04-04 06:12:32 +000037ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
38 SourceLocation atLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000039 unsigned numRefProtos,
Steve Naroff7c371742008-04-11 19:35:35 +000040 IdentifierInfo *Id,
41 SourceLocation ClassLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000042 bool ForwardDecl, bool isInternal){
43 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000044 return new (Mem) ObjCInterfaceDecl(atLoc, numRefProtos,
Steve Naroff7c371742008-04-11 19:35:35 +000045 Id, ClassLoc, ForwardDecl,
Chris Lattner0db541b2008-03-16 01:15:50 +000046 isInternal);
47}
48
Chris Lattnerf3874bc2008-04-06 04:47:34 +000049ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner0db541b2008-03-16 01:15:50 +000050 IdentifierInfo *Id, QualType T) {
51 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +000052 return new (Mem) ObjCIvarDecl(L, Id, T);
Chris Lattner114add62008-03-16 00:49:28 +000053}
54
Chris Lattnereee57c02008-04-04 06:12:32 +000055ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
56 SourceLocation L,
Chris Lattner180f7e22008-03-16 01:23:04 +000057 unsigned numRefProtos,
Chris Lattner7afba9c2008-03-16 20:19:15 +000058 IdentifierInfo *Id) {
Chris Lattner180f7e22008-03-16 01:23:04 +000059 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner7afba9c2008-03-16 20:19:15 +000060 return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +000061}
62
Chris Lattnereee57c02008-04-04 06:12:32 +000063ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
64 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +000065 ObjCInterfaceDecl **Elts, unsigned nElts) {
66 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
67 return new (Mem) ObjCClassDecl(L, Elts, nElts);
68}
69
70ObjCForwardProtocolDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +000071ObjCForwardProtocolDecl::Create(ASTContext &C,
72 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +000073 ObjCProtocolDecl **Elts, unsigned NumElts) {
74 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
75 return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
76}
77
Chris Lattnereee57c02008-04-04 06:12:32 +000078ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
79 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +000080 IdentifierInfo *Id) {
81 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner321b5d12008-03-16 20:47:45 +000082 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +000083}
84
Chris Lattner1b6de332008-03-16 20:53:07 +000085ObjCCategoryImplDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +000086ObjCCategoryImplDecl::Create(ASTContext &C,
87 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +000088 ObjCInterfaceDecl *ClassInterface) {
89 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
90 return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
91}
92
93ObjCImplementationDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +000094ObjCImplementationDecl::Create(ASTContext &C,
95 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +000096 IdentifierInfo *Id,
97 ObjCInterfaceDecl *ClassInterface,
98 ObjCInterfaceDecl *SuperDecl) {
99 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
100 return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
101}
Chris Lattner10318b82008-03-16 00:19:01 +0000102
Chris Lattner2d1c4312008-03-16 21:17:37 +0000103ObjCCompatibleAliasDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000104ObjCCompatibleAliasDecl::Create(ASTContext &C,
105 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000106 IdentifierInfo *Id,
107 ObjCInterfaceDecl* AliasedClass) {
108 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
109 return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
110}
111
Chris Lattnereee57c02008-04-04 06:12:32 +0000112ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000113 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000114 IdentifierInfo *Id,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000115 QualType T) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000116 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000117 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000118}
119
Chris Lattner10318b82008-03-16 00:19:01 +0000120//===----------------------------------------------------------------------===//
121// Objective-C Decl Implementation
122//===----------------------------------------------------------------------===//
123
124void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
125 unsigned NumParams) {
126 assert(ParamInfo == 0 && "Already has param info!");
127
128 // Zero params -> null pointer.
129 if (NumParams) {
130 ParamInfo = new ParmVarDecl*[NumParams];
131 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
132 NumMethodParams = NumParams;
133 }
134}
135
136ObjCMethodDecl::~ObjCMethodDecl() {
137 delete[] ParamInfo;
138}
139
140/// ObjCAddInstanceVariablesToClass - Inserts instance variables
141/// into ObjCInterfaceDecl's fields.
142///
143void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
144 unsigned numIvars,
145 SourceLocation RBrac) {
146 NumIvars = numIvars;
147 if (numIvars) {
148 Ivars = new ObjCIvarDecl*[numIvars];
149 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
150 }
151 setLocEnd(RBrac);
152}
153
154/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
155/// Variables (Ivars) relative to what declared in @implementation;s class.
156/// Ivars into ObjCImplementationDecl's fields.
157///
158void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
159 ObjCIvarDecl **ivars, unsigned numIvars) {
160 NumIvars = numIvars;
161 if (numIvars) {
162 Ivars = new ObjCIvarDecl*[numIvars];
163 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
164 }
165}
166
167/// addMethods - Insert instance and methods declarations into
168/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
169///
170void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
171 unsigned numInsMembers,
172 ObjCMethodDecl **clsMethods,
173 unsigned numClsMembers,
174 SourceLocation endLoc) {
175 NumInstanceMethods = numInsMembers;
176 if (numInsMembers) {
177 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
178 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
179 }
180 NumClassMethods = numClsMembers;
181 if (numClsMembers) {
182 ClassMethods = new ObjCMethodDecl*[numClsMembers];
183 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
184 }
185 AtEndLoc = endLoc;
186}
187
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000188/// addProperties - Insert property declaration AST nodes into
189/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-03-16 21:23:50 +0000190///
191void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
192 unsigned NumProperties) {
193 if (NumProperties == 0) return;
194
195 NumPropertyDecl = NumProperties;
196 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
197 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
198}
199
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000200/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000201/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000202///
203void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
204 unsigned NumProperties) {
205 if (NumProperties == 0) return;
206
207 NumPropertyDecl = NumProperties;
208 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
209 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
210}
211
Chris Lattnercffe3662008-03-16 21:23:50 +0000212/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000213/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
214///
215void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
216 unsigned numInsMembers,
217 ObjCMethodDecl **clsMethods,
218 unsigned numClsMembers,
219 SourceLocation endLoc) {
220 NumInstanceMethods = numInsMembers;
221 if (numInsMembers) {
222 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
223 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
224 }
225 NumClassMethods = numClsMembers;
226 if (numClsMembers) {
227 ClassMethods = new ObjCMethodDecl*[numClsMembers];
228 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
229 }
230 AtEndLoc = endLoc;
231}
232
Chris Lattner321b5d12008-03-16 20:47:45 +0000233void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List,
234 unsigned NumRPs) {
235 assert(NumReferencedProtocols == 0 && "Protocol list already set");
236 if (NumRPs == 0) return;
237
238 ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
239 memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
240 NumReferencedProtocols = NumRPs;
241}
242
243
Chris Lattner10318b82008-03-16 00:19:01 +0000244/// addMethods - Insert instance and methods declarations into
245/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
246///
247void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
248 unsigned numInsMembers,
249 ObjCMethodDecl **clsMethods,
250 unsigned numClsMembers,
251 SourceLocation endLoc) {
252 NumInstanceMethods = numInsMembers;
253 if (numInsMembers) {
254 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
255 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
256 }
257 NumClassMethods = numClsMembers;
258 if (numClsMembers) {
259 ClassMethods = new ObjCMethodDecl*[numClsMembers];
260 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
261 }
262 AtEndLoc = endLoc;
263}
264
265ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
266 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
267 ObjCInterfaceDecl* ClassDecl = this;
268 while (ClassDecl != NULL) {
269 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
270 I != E; ++I) {
271 if ((*I)->getIdentifier() == ID) {
272 clsDeclared = ClassDecl;
273 return *I;
274 }
275 }
276 ClassDecl = ClassDecl->getSuperClass();
277 }
278 return NULL;
279}
280
281/// lookupInstanceMethod - This method returns an instance method by looking in
282/// the class, its categories, and its super classes (using a linear search).
283ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
284 ObjCInterfaceDecl* ClassDecl = this;
285 ObjCMethodDecl *MethodDecl = 0;
286
287 while (ClassDecl != NULL) {
288 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
289 return MethodDecl;
290
291 // Didn't find one yet - look through protocols.
292 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
293 int numProtocols = ClassDecl->getNumIntfRefProtocols();
294 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
295 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
296 return MethodDecl;
297 }
298 // Didn't find one yet - now look through categories.
299 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
300 while (CatDecl) {
301 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
302 return MethodDecl;
303 CatDecl = CatDecl->getNextClassCategory();
304 }
305 ClassDecl = ClassDecl->getSuperClass();
306 }
307 return NULL;
308}
309
310// lookupClassMethod - This method returns a class method by looking in the
311// class, its categories, and its super classes (using a linear search).
312ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
313 ObjCInterfaceDecl* ClassDecl = this;
314 ObjCMethodDecl *MethodDecl = 0;
315
316 while (ClassDecl != NULL) {
317 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
318 return MethodDecl;
319
320 // Didn't find one yet - look through protocols.
321 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
322 int numProtocols = ClassDecl->getNumIntfRefProtocols();
323 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
324 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
325 return MethodDecl;
326 }
327 // Didn't find one yet - now look through categories.
328 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
329 while (CatDecl) {
330 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
331 return MethodDecl;
332 CatDecl = CatDecl->getNextClassCategory();
333 }
334 ClassDecl = ClassDecl->getSuperClass();
335 }
336 return NULL;
337}
338
339/// lookupInstanceMethod - This method returns an instance method by looking in
340/// the class implementation. Unlike interfaces, we don't look outside the
341/// implementation.
342ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
343 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
344 if ((*I)->getSelector() == Sel)
345 return *I;
346 return NULL;
347}
348
349/// lookupClassMethod - This method returns a class method by looking in
350/// the class implementation. Unlike interfaces, we don't look outside the
351/// implementation.
352ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
353 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
354 I != E; ++I)
355 if ((*I)->getSelector() == Sel)
356 return *I;
357 return NULL;
358}
359
360// lookupInstanceMethod - This method returns an instance method by looking in
361// the class implementation. Unlike interfaces, we don't look outside the
362// implementation.
363ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
364 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
365 if ((*I)->getSelector() == Sel)
366 return *I;
367 return NULL;
368}
369
370// lookupClassMethod - This method returns an instance method by looking in
371// the class implementation. Unlike interfaces, we don't look outside the
372// implementation.
373ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
374 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
375 I != E; ++I)
376 if ((*I)->getSelector() == Sel)
377 return *I;
378 return NULL;
379}
380
381// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
382// it inherited.
383ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
384 ObjCMethodDecl *MethodDecl = NULL;
385
386 if ((MethodDecl = getInstanceMethod(Sel)))
387 return MethodDecl;
388
389 if (getNumReferencedProtocols() > 0) {
390 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
391
392 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
393 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
394 return MethodDecl;
395 }
396 }
397 return NULL;
398}
399
400// lookupInstanceMethod - Lookup a class method in the protocol and protocols
401// it inherited.
402ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
403 ObjCMethodDecl *MethodDecl = NULL;
404
405 if ((MethodDecl = getClassMethod(Sel)))
406 return MethodDecl;
407
408 if (getNumReferencedProtocols() > 0) {
409 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
410
411 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
412 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
413 return MethodDecl;
414 }
415 }
416 return NULL;
417}
418
419/// getSynthesizedMethodSize - Compute size of synthesized method name
420/// as done be the rewrite.
421///
422unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
423 // syntesized method name is a concatenation of -/+[class-name selector]
424 // Get length of this name.
425 unsigned length = 3; // _I_ or _C_
426 length += strlen(getClassInterface()->getName()) +1; // extra for _
427 NamedDecl *MethodContext = getMethodContext();
428 if (ObjCCategoryImplDecl *CID =
429 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
430 length += strlen(CID->getName()) +1;
431 length += getSelector().getName().size(); // selector name
432 return length;
433}
434
Chris Lattner052fbcd2008-04-06 05:25:03 +0000435ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000436 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
437 return ID;
438 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
439 return CD->getClassInterface();
440 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000441 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000442 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000443 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000444 return CID->getClassInterface();
445 assert(false && "unknown method context");
446 return 0;
447}
Chris Lattner44859612008-03-17 01:19:02 +0000448
Chris Lattner44859612008-03-17 01:19:02 +0000449
Chris Lattnereee57c02008-04-04 06:12:32 +0000450