blob: b7f85e9a53db9e421f04675b1766edb76edda514 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000015#include "clang/AST/DeclObjC.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000016#include "clang/Basic/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017using namespace clang;
18
19// temporary statistics gathering
20static unsigned nFuncs = 0;
21static unsigned nBlockVars = 0;
22static unsigned nFileVars = 0;
23static unsigned nParmVars = 0;
24static unsigned nSUC = 0;
25static unsigned nEnumConst = 0;
26static unsigned nEnumDecls = 0;
27static unsigned nTypedef = 0;
28static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000029static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000030static unsigned nClassDecls = 0;
31static unsigned nMethodDecls = 0;
32static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000033static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000034static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000035static unsigned nIvarDecls = 0;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +000036static unsigned nObjcImplementationDecls = 0;
Fariborz Jahaniana91aa322007-10-02 16:38:50 +000037static unsigned nObjcCategoryImpl = 0;
Fariborz Jahanian05d212a2007-10-11 23:42:27 +000038static unsigned nObjcCompatibleAlias = 0;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +000039static unsigned nObjcPropertyDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000040
Chris Lattner4b009652007-07-25 00:24:17 +000041static bool StatSwitch = false;
42
Steve Naroff590aba82007-09-17 14:49:06 +000043const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000044 switch (DeclKind) {
45 default: assert(0 && "Unknown decl kind!");
46 case Typedef:
47 return "Typedef";
48 case Function:
49 return "Function";
Chris Lattner3289bca2007-10-08 21:37:32 +000050 case BlockVar:
51 return "BlockVar";
52 case FileVar:
53 return "FileVar";
54 case ParmVar:
55 return "ParmVar";
Steve Narofff0c31dd2007-09-16 16:16:00 +000056 case EnumConstant:
57 return "EnumConstant";
58 case ObjcInterface:
59 return "ObjcInterface";
60 case ObjcClass:
61 return "ObjcClass";
62 case ObjcMethod:
63 return "ObjcMethod";
Steve Narofff0c31dd2007-09-16 16:16:00 +000064 case ObjcProtocol:
65 return "ObjcProtocol";
Fariborz Jahanianc716c942007-09-21 15:40:54 +000066 case ObjcForwardProtocol:
67 return "ObjcForwardProtocol";
Steve Narofff0c31dd2007-09-16 16:16:00 +000068 case Struct:
69 return "Struct";
70 case Union:
71 return "Union";
72 case Class:
73 return "Class";
74 case Enum:
75 return "Enum";
76 }
77}
78
Chris Lattner4b009652007-07-25 00:24:17 +000079bool Decl::CollectingStats(bool enable) {
80 if (enable) StatSwitch = true;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +000081 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000082}
83
84void Decl::PrintStats() {
85 fprintf(stderr, "*** Decl Stats:\n");
86 fprintf(stderr, " %d decls total.\n",
87 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff948fd372007-09-17 14:16:13 +000088 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000089 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000090 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
91 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
92 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
93 nBlockVars, (int)sizeof(BlockVarDecl),
94 int(nBlockVars*sizeof(BlockVarDecl)));
95 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
96 nFileVars, (int)sizeof(FileVarDecl),
97 int(nFileVars*sizeof(FileVarDecl)));
98 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
99 nParmVars, (int)sizeof(ParmVarDecl),
100 int(nParmVars*sizeof(ParmVarDecl)));
101 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
102 nFieldDecls, (int)sizeof(FieldDecl),
103 int(nFieldDecls*sizeof(FieldDecl)));
104 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
105 nSUC, (int)sizeof(RecordDecl),
106 int(nSUC*sizeof(RecordDecl)));
107 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
108 nEnumDecls, (int)sizeof(EnumDecl),
109 int(nEnumDecls*sizeof(EnumDecl)));
110 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
111 nEnumConst, (int)sizeof(EnumConstantDecl),
112 int(nEnumConst*sizeof(EnumConstantDecl)));
113 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
114 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000115 // Objective-C decls...
116 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
117 nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl),
118 int(nInterfaceDecls*sizeof(ObjcInterfaceDecl)));
119 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
120 nIvarDecls, (int)sizeof(ObjcIvarDecl),
121 int(nIvarDecls*sizeof(ObjcIvarDecl)));
122 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
123 nClassDecls, (int)sizeof(ObjcClassDecl),
124 int(nClassDecls*sizeof(ObjcClassDecl)));
125 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
126 nMethodDecls, (int)sizeof(ObjcMethodDecl),
127 int(nMethodDecls*sizeof(ObjcMethodDecl)));
128 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
129 nProtocolDecls, (int)sizeof(ObjcProtocolDecl),
130 int(nProtocolDecls*sizeof(ObjcProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000131 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
132 nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl),
133 int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000134 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
135 nCategoryDecls, (int)sizeof(ObjcCategoryDecl),
136 int(nCategoryDecls*sizeof(ObjcCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000137
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000138 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
139 nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl),
140 int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl)));
141
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000142 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
143 nObjcCategoryImpl, (int)sizeof(ObjcCategoryImplDecl),
144 int(nObjcCategoryImpl*sizeof(ObjcCategoryImplDecl)));
145
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000146 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
147 nObjcCompatibleAlias, (int)sizeof(ObjcCompatibleAliasDecl),
148 int(nObjcCompatibleAlias*sizeof(ObjcCompatibleAliasDecl)));
149
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000150 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
151 nObjcPropertyDecl, (int)sizeof(ObjcPropertyDecl),
152 int(nObjcPropertyDecl*sizeof(ObjcPropertyDecl)));
153
Chris Lattner4b009652007-07-25 00:24:17 +0000154 fprintf(stderr, "Total bytes = %d\n",
155 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
156 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
157 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
158 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff948fd372007-09-17 14:16:13 +0000159 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Chris Lattner4b009652007-07-25 00:24:17 +0000160}
161
162void Decl::addDeclKind(const Kind k) {
163 switch (k) {
164 case Typedef:
165 nTypedef++;
166 break;
167 case Function:
168 nFuncs++;
169 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000170 case BlockVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000171 nBlockVars++;
172 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000173 case FileVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000174 nFileVars++;
175 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000176 case ParmVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000177 nParmVars++;
178 break;
179 case EnumConstant:
180 nEnumConst++;
181 break;
182 case Field:
183 nFieldDecls++;
184 break;
185 case Struct:
186 case Union:
187 case Class:
188 nSUC++;
189 break;
190 case Enum:
191 nEnumDecls++;
192 break;
Steve Naroff81f1bba2007-09-06 21:24:23 +0000193 case ObjcInterface:
194 nInterfaceDecls++;
195 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000196 case ObjcClass:
Steve Naroff948fd372007-09-17 14:16:13 +0000197 nClassDecls++;
198 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000199 case ObjcMethod:
Steve Naroff948fd372007-09-17 14:16:13 +0000200 nMethodDecls++;
201 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000202 case ObjcProtocol:
Steve Naroff948fd372007-09-17 14:16:13 +0000203 nProtocolDecls++;
204 break;
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000205 case ObjcForwardProtocol:
206 nForwardProtocolDecls++;
207 break;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000208 case ObjcCategory:
209 nCategoryDecls++;
210 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000211 case ObjcIvar:
Steve Naroff948fd372007-09-17 14:16:13 +0000212 nIvarDecls++;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000213 break;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000214 case ObjcImplementation:
215 nObjcImplementationDecls++;
216 break;
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000217 case ObjcCategoryImpl:
218 nObjcCategoryImpl++;
219 break;
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000220 case CompatibleAlias:
221 nObjcCompatibleAlias++;
222 break;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000223 case PropertyDecl:
224 nObjcPropertyDecl++;
225 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000226 }
227}
228
229// Out-of-line virtual method providing a home for Decl.
230Decl::~Decl() {
231}
232
Chris Lattner910435b2007-10-06 22:53:46 +0000233const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000234 if (const IdentifierInfo *II = getIdentifier())
235 return II->getName();
236 return "";
237}
238
239
240FunctionDecl::~FunctionDecl() {
241 delete[] ParamInfo;
242}
243
244unsigned FunctionDecl::getNumParams() const {
245 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
246}
247
248void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
249 assert(ParamInfo == 0 && "Already has param info!");
250 assert(NumParams == getNumParams() && "Parameter count mismatch!");
251
252 // Zero params -> null pointer.
253 if (NumParams) {
254 ParamInfo = new ParmVarDecl*[NumParams];
255 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
256 }
257}
258
259
260/// defineBody - When created, RecordDecl's correspond to a forward declared
261/// record. This method is used to mark the decl as being defined, with the
262/// specified contents.
263void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
264 assert(!isDefinition() && "Cannot redefine record!");
265 setDefinition(true);
266 NumMembers = numMembers;
267 if (numMembers) {
268 Members = new FieldDecl*[numMembers];
269 memcpy(Members, members, numMembers*sizeof(Decl*));
270 }
271}
272
273FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
274 if (Members == 0 || NumMembers < 0)
275 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000276
Chris Lattner4b009652007-07-25 00:24:17 +0000277 // linear search. When C++ classes come along, will likely need to revisit.
278 for (int i = 0; i < NumMembers; ++i) {
279 if (Members[i]->getIdentifier() == name)
280 return Members[i];
281 }
282 return 0;
283}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000284
Fariborz Jahanian84082af2007-10-04 17:06:28 +0000285void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
286 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000287 assert(ParamInfo == 0 && "Already has param info!");
288
289 // Zero params -> null pointer.
290 if (NumParams) {
291 ParamInfo = new ParmVarDecl*[NumParams];
292 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
293 NumMethodParams = NumParams;
294 }
295}
296
297ObjcMethodDecl::~ObjcMethodDecl() {
298 delete[] ParamInfo;
299}
300
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000301/// ObjcAddInstanceVariablesToClass - Inserts instance variables
302/// into ObjcInterfaceDecl's fields.
303///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000304void ObjcInterfaceDecl::addInstanceVariablesToClass(ObjcIvarDecl **ivars,
305 unsigned numIvars,
Steve Naroffef20ed32007-10-30 02:23:23 +0000306 SourceLocation RBrac) {
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000307 NumIvars = numIvars;
308 if (numIvars) {
309 Ivars = new ObjcIvarDecl*[numIvars];
310 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
311 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000312 setLocEnd(RBrac);
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000313}
314
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000315/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
316/// Variables (Ivars) relative to what declared in @implementation;s class.
317/// Ivars into ObjcImplementationDecl's fields.
318///
319void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
Fariborz Jahanian84082af2007-10-04 17:06:28 +0000320 ObjcIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000321 NumIvars = numIvars;
322 if (numIvars) {
323 Ivars = new ObjcIvarDecl*[numIvars];
324 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
325 }
326}
327
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000328/// addMethods - Insert instance and methods declarations into
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000329/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
330///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000331void ObjcInterfaceDecl::addMethods(ObjcMethodDecl **insMethods,
332 unsigned numInsMembers,
333 ObjcMethodDecl **clsMethods,
334 unsigned numClsMembers,
335 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000336 NumInstanceMethods = numInsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000337 if (numInsMembers) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000338 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
339 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000340 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000341 NumClassMethods = numClsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000342 if (numClsMembers) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000343 ClassMethods = new ObjcMethodDecl*[numClsMembers];
344 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000345 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000346 AtEndLoc = endLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000347}
348
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000349/// addMethods - Insert instance and methods declarations into
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000350/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
351///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000352void ObjcProtocolDecl::addMethods(ObjcMethodDecl **insMethods,
353 unsigned numInsMembers,
354 ObjcMethodDecl **clsMethods,
355 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000356 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000357 NumInstanceMethods = numInsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000358 if (numInsMembers) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000359 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
360 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000361 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000362 NumClassMethods = numClsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000363 if (numClsMembers) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000364 ClassMethods = new ObjcMethodDecl*[numClsMembers];
365 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000366 }
Steve Naroff667f1682007-10-30 13:30:57 +0000367 AtEndLoc = endLoc;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000368}
369
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000370/// addMethods - Insert instance and methods declarations into
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000371/// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000372///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000373void ObjcCategoryDecl::addMethods(ObjcMethodDecl **insMethods,
374 unsigned numInsMembers,
375 ObjcMethodDecl **clsMethods,
376 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000377 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000378 NumInstanceMethods = numInsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000379 if (numInsMembers) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000380 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
381 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000382 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000383 NumClassMethods = numClsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000384 if (numClsMembers) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000385 ClassMethods = new ObjcMethodDecl*[numClsMembers];
386 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000387 }
Steve Naroff667f1682007-10-30 13:30:57 +0000388 AtEndLoc = endLoc;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000389}
390
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000391ObjcIvarDecl *ObjcInterfaceDecl::lookupInstanceVariable(
392 IdentifierInfo *ID, ObjcInterfaceDecl *&clsDeclared) {
393 ObjcInterfaceDecl* ClassDecl = this;
394 while (ClassDecl != NULL) {
395 ObjcIvarDecl **ivars = ClassDecl->getInstanceVariables();
396 int ivarCount = ClassDecl->getNumInstanceVariables();
397 for (int i = 0; i < ivarCount; ++i) {
398 if (ivars[i]->getIdentifier() == ID) {
399 clsDeclared = ClassDecl;
400 return ivars[i];
401 }
402 }
403 ClassDecl = ClassDecl->getSuperClass();
404 }
405 return NULL;
406}
407
Steve Narofff66d84a2007-10-14 18:27:41 +0000408// lookupInstanceMethod - This method returns an instance method by looking in
409// the class, it's categories, and it's super classes (using a linear search).
Steve Narofffa465d12007-10-02 20:01:56 +0000410ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) {
411 ObjcInterfaceDecl* ClassDecl = this;
412 while (ClassDecl != NULL) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000413 ObjcMethodDecl **methods = ClassDecl->getInstanceMethods();
414 int methodCount = ClassDecl->getNumInstanceMethods();
Steve Narofffa465d12007-10-02 20:01:56 +0000415 for (int i = 0; i < methodCount; ++i) {
416 if (methods[i]->getSelector() == Sel) {
417 return methods[i];
418 }
419 }
Steve Naroff705380b2007-10-14 23:13:51 +0000420 // Didn't find one yet - look through protocols.
421 ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
422 int numProtocols = ClassDecl->getNumIntfRefProtocols();
423 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
424 ObjcMethodDecl **methods = protocols[pIdx]->getInstanceMethods();
425 int methodCount = protocols[pIdx]->getNumInstanceMethods();
426 for (int i = 0; i < methodCount; ++i) {
427 if (methods[i]->getSelector() == Sel) {
428 return methods[i];
429 }
430 }
431 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000432 // Didn't find one yet - now look through categories.
Steve Naroff705380b2007-10-14 23:13:51 +0000433 ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000434 while (CatDecl) {
435 ObjcMethodDecl **methods = CatDecl->getInstanceMethods();
436 int methodCount = CatDecl->getNumInstanceMethods();
437 for (int i = 0; i < methodCount; ++i) {
438 if (methods[i]->getSelector() == Sel) {
439 return methods[i];
440 }
441 }
442 CatDecl = CatDecl->getNextClassCategory();
443 }
Steve Narofffa465d12007-10-02 20:01:56 +0000444 ClassDecl = ClassDecl->getSuperClass();
445 }
446 return NULL;
447}
448
Steve Narofff66d84a2007-10-14 18:27:41 +0000449// lookupClassMethod - This method returns a class method by looking in the
450// class, it's categories, and it's super classes (using a linear search).
Steve Narofffa465d12007-10-02 20:01:56 +0000451ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) {
452 ObjcInterfaceDecl* ClassDecl = this;
453 while (ClassDecl != NULL) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000454 ObjcMethodDecl **methods = ClassDecl->getClassMethods();
455 int methodCount = ClassDecl->getNumClassMethods();
Steve Narofffa465d12007-10-02 20:01:56 +0000456 for (int i = 0; i < methodCount; ++i) {
457 if (methods[i]->getSelector() == Sel) {
458 return methods[i];
459 }
460 }
Steve Naroff705380b2007-10-14 23:13:51 +0000461 // Didn't find one yet - look through protocols.
462 ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
463 int numProtocols = ClassDecl->getNumIntfRefProtocols();
464 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
465 ObjcMethodDecl **methods = protocols[pIdx]->getClassMethods();
466 int methodCount = protocols[pIdx]->getNumClassMethods();
467 for (int i = 0; i < methodCount; ++i) {
468 if (methods[i]->getSelector() == Sel) {
469 return methods[i];
470 }
471 }
472 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000473 // Didn't find one yet - now look through categories.
Steve Naroff705380b2007-10-14 23:13:51 +0000474 ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000475 while (CatDecl) {
476 ObjcMethodDecl **methods = CatDecl->getClassMethods();
477 int methodCount = CatDecl->getNumClassMethods();
478 for (int i = 0; i < methodCount; ++i) {
479 if (methods[i]->getSelector() == Sel) {
480 return methods[i];
481 }
482 }
483 CatDecl = CatDecl->getNextClassCategory();
484 }
Steve Narofffa465d12007-10-02 20:01:56 +0000485 ClassDecl = ClassDecl->getSuperClass();
486 }
487 return NULL;
488}
489
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000490// lookupInstanceMethod - This method returns an instance method by looking in
491// the class implementation. Unlike interfaces, we don't look outside the
492// implementation.
493ObjcMethodDecl *ObjcImplementationDecl::lookupInstanceMethod(Selector &Sel) {
Steve Naroffb82c50f2007-11-11 17:19:15 +0000494 ObjcMethodDecl *const*methods = getInstanceMethods();
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000495 int methodCount = getNumInstanceMethods();
496 for (int i = 0; i < methodCount; ++i) {
497 if (methods[i]->getSelector() == Sel) {
498 return methods[i];
499 }
500 }
501 return NULL;
502}
503
504// lookupClassMethod - This method returns an instance method by looking in
505// the class implementation. Unlike interfaces, we don't look outside the
506// implementation.
507ObjcMethodDecl *ObjcImplementationDecl::lookupClassMethod(Selector &Sel) {
Steve Naroffb82c50f2007-11-11 17:19:15 +0000508 ObjcMethodDecl *const*methods = getClassMethods();
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000509 int methodCount = getNumClassMethods();
510 for (int i = 0; i < methodCount; ++i) {
511 if (methods[i]->getSelector() == Sel) {
512 return methods[i];
513 }
514 }
515 return NULL;
516}
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000517
Steve Naroff31233632007-11-12 22:05:31 +0000518// lookupInstanceMethod - This method returns an instance method by looking in
519// the class implementation. Unlike interfaces, we don't look outside the
520// implementation.
521ObjcMethodDecl *ObjcCategoryImplDecl::lookupInstanceMethod(Selector &Sel) {
522 ObjcMethodDecl *const*methods = getInstanceMethods();
523 int methodCount = getNumInstanceMethods();
524 for (int i = 0; i < methodCount; ++i) {
525 if (methods[i]->getSelector() == Sel) {
526 return methods[i];
527 }
528 }
529 return NULL;
530}
531
532// lookupClassMethod - This method returns an instance method by looking in
533// the class implementation. Unlike interfaces, we don't look outside the
534// implementation.
535ObjcMethodDecl *ObjcCategoryImplDecl::lookupClassMethod(Selector &Sel) {
536 ObjcMethodDecl *const*methods = getClassMethods();
537 int methodCount = getNumClassMethods();
538 for (int i = 0; i < methodCount; ++i) {
539 if (methods[i]->getSelector() == Sel) {
540 return methods[i];
541 }
542 }
543 return NULL;
544}