blob: 7a99f0f5498b6beb523d7cdd4186e60c1542fec2 [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
188/// addMethods - Insert instance and methods declarations into
Chris Lattnercffe3662008-03-16 21:23:50 +0000189/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
190///
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
200/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000201/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
202///
203void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
204 unsigned numInsMembers,
205 ObjCMethodDecl **clsMethods,
206 unsigned numClsMembers,
207 SourceLocation endLoc) {
208 NumInstanceMethods = numInsMembers;
209 if (numInsMembers) {
210 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
211 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
212 }
213 NumClassMethods = numClsMembers;
214 if (numClsMembers) {
215 ClassMethods = new ObjCMethodDecl*[numClsMembers];
216 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
217 }
218 AtEndLoc = endLoc;
219}
220
Chris Lattner321b5d12008-03-16 20:47:45 +0000221void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List,
222 unsigned NumRPs) {
223 assert(NumReferencedProtocols == 0 && "Protocol list already set");
224 if (NumRPs == 0) return;
225
226 ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
227 memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
228 NumReferencedProtocols = NumRPs;
229}
230
231
Chris Lattner10318b82008-03-16 00:19:01 +0000232/// addMethods - Insert instance and methods declarations into
233/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
234///
235void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
236 unsigned numInsMembers,
237 ObjCMethodDecl **clsMethods,
238 unsigned numClsMembers,
239 SourceLocation endLoc) {
240 NumInstanceMethods = numInsMembers;
241 if (numInsMembers) {
242 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
243 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
244 }
245 NumClassMethods = numClsMembers;
246 if (numClsMembers) {
247 ClassMethods = new ObjCMethodDecl*[numClsMembers];
248 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
249 }
250 AtEndLoc = endLoc;
251}
252
253ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
254 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
255 ObjCInterfaceDecl* ClassDecl = this;
256 while (ClassDecl != NULL) {
257 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
258 I != E; ++I) {
259 if ((*I)->getIdentifier() == ID) {
260 clsDeclared = ClassDecl;
261 return *I;
262 }
263 }
264 ClassDecl = ClassDecl->getSuperClass();
265 }
266 return NULL;
267}
268
269/// lookupInstanceMethod - This method returns an instance method by looking in
270/// the class, its categories, and its super classes (using a linear search).
271ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
272 ObjCInterfaceDecl* ClassDecl = this;
273 ObjCMethodDecl *MethodDecl = 0;
274
275 while (ClassDecl != NULL) {
276 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
277 return MethodDecl;
278
279 // Didn't find one yet - look through protocols.
280 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
281 int numProtocols = ClassDecl->getNumIntfRefProtocols();
282 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
283 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
284 return MethodDecl;
285 }
286 // Didn't find one yet - now look through categories.
287 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
288 while (CatDecl) {
289 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
290 return MethodDecl;
291 CatDecl = CatDecl->getNextClassCategory();
292 }
293 ClassDecl = ClassDecl->getSuperClass();
294 }
295 return NULL;
296}
297
298// lookupClassMethod - This method returns a class method by looking in the
299// class, its categories, and its super classes (using a linear search).
300ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
301 ObjCInterfaceDecl* ClassDecl = this;
302 ObjCMethodDecl *MethodDecl = 0;
303
304 while (ClassDecl != NULL) {
305 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
306 return MethodDecl;
307
308 // Didn't find one yet - look through protocols.
309 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
310 int numProtocols = ClassDecl->getNumIntfRefProtocols();
311 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
312 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
313 return MethodDecl;
314 }
315 // Didn't find one yet - now look through categories.
316 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
317 while (CatDecl) {
318 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
319 return MethodDecl;
320 CatDecl = CatDecl->getNextClassCategory();
321 }
322 ClassDecl = ClassDecl->getSuperClass();
323 }
324 return NULL;
325}
326
327/// lookupInstanceMethod - This method returns an instance method by looking in
328/// the class implementation. Unlike interfaces, we don't look outside the
329/// implementation.
330ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
331 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
332 if ((*I)->getSelector() == Sel)
333 return *I;
334 return NULL;
335}
336
337/// lookupClassMethod - This method returns a class method by looking in
338/// the class implementation. Unlike interfaces, we don't look outside the
339/// implementation.
340ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
341 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
342 I != E; ++I)
343 if ((*I)->getSelector() == Sel)
344 return *I;
345 return NULL;
346}
347
348// lookupInstanceMethod - This method returns an instance method by looking in
349// the class implementation. Unlike interfaces, we don't look outside the
350// implementation.
351ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
352 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
353 if ((*I)->getSelector() == Sel)
354 return *I;
355 return NULL;
356}
357
358// lookupClassMethod - This method returns an instance method by looking in
359// the class implementation. Unlike interfaces, we don't look outside the
360// implementation.
361ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
362 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
363 I != E; ++I)
364 if ((*I)->getSelector() == Sel)
365 return *I;
366 return NULL;
367}
368
369// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
370// it inherited.
371ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
372 ObjCMethodDecl *MethodDecl = NULL;
373
374 if ((MethodDecl = getInstanceMethod(Sel)))
375 return MethodDecl;
376
377 if (getNumReferencedProtocols() > 0) {
378 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
379
380 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
381 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
382 return MethodDecl;
383 }
384 }
385 return NULL;
386}
387
388// lookupInstanceMethod - Lookup a class method in the protocol and protocols
389// it inherited.
390ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
391 ObjCMethodDecl *MethodDecl = NULL;
392
393 if ((MethodDecl = getClassMethod(Sel)))
394 return MethodDecl;
395
396 if (getNumReferencedProtocols() > 0) {
397 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
398
399 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
400 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
401 return MethodDecl;
402 }
403 }
404 return NULL;
405}
406
407/// getSynthesizedMethodSize - Compute size of synthesized method name
408/// as done be the rewrite.
409///
410unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
411 // syntesized method name is a concatenation of -/+[class-name selector]
412 // Get length of this name.
413 unsigned length = 3; // _I_ or _C_
414 length += strlen(getClassInterface()->getName()) +1; // extra for _
415 NamedDecl *MethodContext = getMethodContext();
416 if (ObjCCategoryImplDecl *CID =
417 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
418 length += strlen(CID->getName()) +1;
419 length += getSelector().getName().size(); // selector name
420 return length;
421}
422
Chris Lattner052fbcd2008-04-06 05:25:03 +0000423ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000424 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
425 return ID;
426 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
427 return CD->getClassInterface();
428 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000429 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000430 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000431 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000432 return CID->getClassInterface();
433 assert(false && "unknown method context");
434 return 0;
435}
Chris Lattner44859612008-03-17 01:19:02 +0000436
Chris Lattner44859612008-03-17 01:19:02 +0000437
Chris Lattnereee57c02008-04-04 06:12:32 +0000438