blob: 30ca5e6e1ba2549e1297a3bfab9161e83860762c [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +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 Naroff67391b82007-10-01 19:00:59 +000015#include "clang/AST/DeclObjC.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000016#include "clang/Basic/IdentifierTable.h"
Chris Lattner6d9a6852006-10-25 05:11:20 +000017using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000018
Steve Narofff84d11f2007-05-23 21:48:04 +000019// temporary statistics gathering
20static unsigned nFuncs = 0;
21static unsigned nBlockVars = 0;
22static unsigned nFileVars = 0;
23static unsigned nParmVars = 0;
Chris Lattnera5490952007-05-24 01:09:39 +000024static unsigned nSUC = 0;
Steve Narofff84d11f2007-05-23 21:48:04 +000025static unsigned nEnumConst = 0;
26static unsigned nEnumDecls = 0;
27static unsigned nTypedef = 0;
28static unsigned nFieldDecls = 0;
Steve Naroff09bf8152007-09-06 21:24:23 +000029static unsigned nInterfaceDecls = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000030static unsigned nClassDecls = 0;
31static unsigned nMethodDecls = 0;
32static unsigned nProtocolDecls = 0;
Fariborz Jahanian876e27d2007-09-21 15:40:54 +000033static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +000034static unsigned nCategoryDecls = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000035static unsigned nIvarDecls = 0;
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +000036static unsigned nObjcImplementationDecls = 0;
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +000037static unsigned nObjcCategoryImpl = 0;
Fariborz Jahanian49c64252007-10-11 23:42:27 +000038static unsigned nObjcCompatibleAlias = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000039
Steve Narofff84d11f2007-05-23 21:48:04 +000040static bool StatSwitch = false;
41
Steve Naroff83763f22007-09-17 14:49:06 +000042const char *Decl::getDeclKindName() const {
Steve Naroff2f742082007-09-16 16:16:00 +000043 switch (DeclKind) {
44 default: assert(0 && "Unknown decl kind!");
45 case Typedef:
46 return "Typedef";
47 case Function:
48 return "Function";
Chris Lattnerba9dddb2007-10-08 21:37:32 +000049 case BlockVar:
50 return "BlockVar";
51 case FileVar:
52 return "FileVar";
53 case ParmVar:
54 return "ParmVar";
Steve Naroff2f742082007-09-16 16:16:00 +000055 case EnumConstant:
56 return "EnumConstant";
57 case ObjcInterface:
58 return "ObjcInterface";
59 case ObjcClass:
60 return "ObjcClass";
61 case ObjcMethod:
62 return "ObjcMethod";
Steve Naroff2f742082007-09-16 16:16:00 +000063 case ObjcProtocol:
64 return "ObjcProtocol";
Fariborz Jahanian876e27d2007-09-21 15:40:54 +000065 case ObjcForwardProtocol:
66 return "ObjcForwardProtocol";
Steve Naroff2f742082007-09-16 16:16:00 +000067 case Struct:
68 return "Struct";
69 case Union:
70 return "Union";
71 case Class:
72 return "Class";
73 case Enum:
74 return "Enum";
75 }
76}
77
Steve Narofff84d11f2007-05-23 21:48:04 +000078bool Decl::CollectingStats(bool enable) {
79 if (enable) StatSwitch = true;
Fariborz Jahanian67341402007-10-04 00:45:27 +000080 return StatSwitch;
Steve Narofff84d11f2007-05-23 21:48:04 +000081}
82
83void Decl::PrintStats() {
Chris Lattnerfc234de2007-05-24 00:40:54 +000084 fprintf(stderr, "*** Decl Stats:\n");
85 fprintf(stderr, " %d decls total.\n",
Chris Lattnera5490952007-05-24 01:09:39 +000086 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff73d534a2007-09-17 14:16:13 +000087 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +000088 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattnerfc234de2007-05-24 00:40:54 +000089 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
90 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
91 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
92 nBlockVars, (int)sizeof(BlockVarDecl),
93 int(nBlockVars*sizeof(BlockVarDecl)));
94 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
95 nFileVars, (int)sizeof(FileVarDecl),
96 int(nFileVars*sizeof(FileVarDecl)));
97 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
98 nParmVars, (int)sizeof(ParmVarDecl),
99 int(nParmVars*sizeof(ParmVarDecl)));
100 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
101 nFieldDecls, (int)sizeof(FieldDecl),
102 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattnera5490952007-05-24 01:09:39 +0000103 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
104 nSUC, (int)sizeof(RecordDecl),
105 int(nSUC*sizeof(RecordDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000106 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
107 nEnumDecls, (int)sizeof(EnumDecl),
108 int(nEnumDecls*sizeof(EnumDecl)));
109 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
110 nEnumConst, (int)sizeof(EnumConstantDecl),
111 int(nEnumConst*sizeof(EnumConstantDecl)));
112 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
113 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000114 // Objective-C decls...
115 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
116 nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl),
117 int(nInterfaceDecls*sizeof(ObjcInterfaceDecl)));
118 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
119 nIvarDecls, (int)sizeof(ObjcIvarDecl),
120 int(nIvarDecls*sizeof(ObjcIvarDecl)));
121 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
122 nClassDecls, (int)sizeof(ObjcClassDecl),
123 int(nClassDecls*sizeof(ObjcClassDecl)));
124 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
125 nMethodDecls, (int)sizeof(ObjcMethodDecl),
126 int(nMethodDecls*sizeof(ObjcMethodDecl)));
127 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
128 nProtocolDecls, (int)sizeof(ObjcProtocolDecl),
129 int(nProtocolDecls*sizeof(ObjcProtocolDecl)));
Fariborz Jahanian876e27d2007-09-21 15:40:54 +0000130 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
131 nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl),
132 int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl)));
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000133 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
134 nCategoryDecls, (int)sizeof(ObjcCategoryDecl),
135 int(nCategoryDecls*sizeof(ObjcCategoryDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000136
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +0000137 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
138 nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl),
139 int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl)));
140
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000141 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
142 nObjcCategoryImpl, (int)sizeof(ObjcCategoryImplDecl),
143 int(nObjcCategoryImpl*sizeof(ObjcCategoryImplDecl)));
144
Fariborz Jahanian49c64252007-10-11 23:42:27 +0000145 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
146 nObjcCompatibleAlias, (int)sizeof(ObjcCompatibleAliasDecl),
147 int(nObjcCompatibleAlias*sizeof(ObjcCompatibleAliasDecl)));
148
Chris Lattnerfc234de2007-05-24 00:40:54 +0000149 fprintf(stderr, "Total bytes = %d\n",
150 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
151 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattnera5490952007-05-24 01:09:39 +0000152 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Chris Lattnerfc234de2007-05-24 00:40:54 +0000153 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff73d534a2007-09-17 14:16:13 +0000154 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Steve Narofff84d11f2007-05-23 21:48:04 +0000155}
156
157void Decl::addDeclKind(const Kind k) {
Chris Lattnerfc234de2007-05-24 00:40:54 +0000158 switch (k) {
159 case Typedef:
160 nTypedef++;
161 break;
162 case Function:
163 nFuncs++;
164 break;
Chris Lattnerba9dddb2007-10-08 21:37:32 +0000165 case BlockVar:
Chris Lattnerfc234de2007-05-24 00:40:54 +0000166 nBlockVars++;
167 break;
Chris Lattnerba9dddb2007-10-08 21:37:32 +0000168 case FileVar:
Chris Lattnerfc234de2007-05-24 00:40:54 +0000169 nFileVars++;
170 break;
Chris Lattnerba9dddb2007-10-08 21:37:32 +0000171 case ParmVar:
Chris Lattnerfc234de2007-05-24 00:40:54 +0000172 nParmVars++;
173 break;
174 case EnumConstant:
175 nEnumConst++;
176 break;
177 case Field:
178 nFieldDecls++;
179 break;
180 case Struct:
181 case Union:
182 case Class:
Chris Lattnera5490952007-05-24 01:09:39 +0000183 nSUC++;
Chris Lattnerfc234de2007-05-24 00:40:54 +0000184 break;
185 case Enum:
186 nEnumDecls++;
187 break;
Steve Naroff09bf8152007-09-06 21:24:23 +0000188 case ObjcInterface:
189 nInterfaceDecls++;
190 break;
Chris Lattner81b76242007-09-16 19:23:04 +0000191 case ObjcClass:
Steve Naroff73d534a2007-09-17 14:16:13 +0000192 nClassDecls++;
193 break;
Chris Lattner81b76242007-09-16 19:23:04 +0000194 case ObjcMethod:
Steve Naroff73d534a2007-09-17 14:16:13 +0000195 nMethodDecls++;
196 break;
Chris Lattner81b76242007-09-16 19:23:04 +0000197 case ObjcProtocol:
Steve Naroff73d534a2007-09-17 14:16:13 +0000198 nProtocolDecls++;
199 break;
Fariborz Jahanian876e27d2007-09-21 15:40:54 +0000200 case ObjcForwardProtocol:
201 nForwardProtocolDecls++;
202 break;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000203 case ObjcCategory:
204 nCategoryDecls++;
205 break;
Chris Lattner81b76242007-09-16 19:23:04 +0000206 case ObjcIvar:
Steve Naroff73d534a2007-09-17 14:16:13 +0000207 nIvarDecls++;
Chris Lattner81b76242007-09-16 19:23:04 +0000208 break;
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +0000209 case ObjcImplementation:
210 nObjcImplementationDecls++;
211 break;
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000212 case ObjcCategoryImpl:
213 nObjcCategoryImpl++;
214 break;
Fariborz Jahanian49c64252007-10-11 23:42:27 +0000215 case CompatibleAlias:
216 nObjcCompatibleAlias++;
217 break;
Chris Lattnerfc234de2007-05-24 00:40:54 +0000218 }
Steve Narofff84d11f2007-05-23 21:48:04 +0000219}
220
Chris Lattner6d9a6852006-10-25 05:11:20 +0000221// Out-of-line virtual method providing a home for Decl.
222Decl::~Decl() {
223}
Chris Lattner17ed4872006-11-20 04:58:19 +0000224
Chris Lattnera4016552007-10-06 22:53:46 +0000225const char *NamedDecl::getName() const {
Chris Lattnerec040b12007-01-21 23:09:50 +0000226 if (const IdentifierInfo *II = getIdentifier())
227 return II->getName();
228 return "";
Chris Lattner17ed4872006-11-20 04:58:19 +0000229}
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000230
231
232FunctionDecl::~FunctionDecl() {
233 delete[] ParamInfo;
234}
235
236unsigned FunctionDecl::getNumParams() const {
237 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
238}
239
Chris Lattner53621a52007-06-13 20:44:40 +0000240void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000241 assert(ParamInfo == 0 && "Already has param info!");
242 assert(NumParams == getNumParams() && "Parameter count mismatch!");
243
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000244 // Zero params -> null pointer.
245 if (NumParams) {
Chris Lattner53621a52007-06-13 20:44:40 +0000246 ParamInfo = new ParmVarDecl*[NumParams];
247 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000248 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000249}
Chris Lattner41943152007-01-25 04:52:46 +0000250
251
252/// defineBody - When created, RecordDecl's correspond to a forward declared
253/// record. This method is used to mark the decl as being defined, with the
254/// specified contents.
Steve Naroffcc321422007-03-26 23:09:51 +0000255void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
Chris Lattner41943152007-01-25 04:52:46 +0000256 assert(!isDefinition() && "Cannot redefine record!");
257 setDefinition(true);
Chris Lattner5f521502007-01-25 06:27:24 +0000258 NumMembers = numMembers;
259 if (numMembers) {
Steve Naroffcc321422007-03-26 23:09:51 +0000260 Members = new FieldDecl*[numMembers];
Chris Lattner5f521502007-01-25 06:27:24 +0000261 memcpy(Members, members, numMembers*sizeof(Decl*));
Chris Lattner41943152007-01-25 04:52:46 +0000262 }
263}
Steve Naroffcc321422007-03-26 23:09:51 +0000264
265FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
266 if (Members == 0 || NumMembers < 0)
267 return 0;
Fariborz Jahanian67341402007-10-04 00:45:27 +0000268
Steve Naroffcc321422007-03-26 23:09:51 +0000269 // linear search. When C++ classes come along, will likely need to revisit.
270 for (int i = 0; i < NumMembers; ++i) {
271 if (Members[i]->getIdentifier() == name)
272 return Members[i];
273 }
274 return 0;
Chris Lattnerbd4de5df2007-07-12 15:43:07 +0000275}
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000276
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000277void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
278 unsigned NumParams) {
Fariborz Jahaniancfb31fa2007-09-12 18:23:47 +0000279 assert(ParamInfo == 0 && "Already has param info!");
280
281 // Zero params -> null pointer.
282 if (NumParams) {
283 ParamInfo = new ParmVarDecl*[NumParams];
284 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
285 NumMethodParams = NumParams;
286 }
287}
288
289ObjcMethodDecl::~ObjcMethodDecl() {
290 delete[] ParamInfo;
291}
292
Fariborz Jahanianf3287bf2007-09-14 21:08:27 +0000293/// ObjcAddInstanceVariablesToClass - Inserts instance variables
294/// into ObjcInterfaceDecl's fields.
295///
296void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000297 unsigned numIvars) {
Fariborz Jahanianf3287bf2007-09-14 21:08:27 +0000298 NumIvars = numIvars;
299 if (numIvars) {
300 Ivars = new ObjcIvarDecl*[numIvars];
301 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
302 }
303}
304
Fariborz Jahanian2a4dd312007-09-26 18:27:25 +0000305/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
306/// Variables (Ivars) relative to what declared in @implementation;s class.
307/// Ivars into ObjcImplementationDecl's fields.
308///
309void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000310 ObjcIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahanian2a4dd312007-09-26 18:27:25 +0000311 NumIvars = numIvars;
312 if (numIvars) {
313 Ivars = new ObjcIvarDecl*[numIvars];
314 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
315 }
316}
317
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000318/// addObjcMethods - Insert instance and methods declarations into
319/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
320///
321void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000322 unsigned numInsMembers,
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000323 ObjcMethodDecl **clsMethods,
324 unsigned numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000325 NumInstanceMethods = numInsMembers;
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000326 if (numInsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000327 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
328 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000329 }
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000330 NumClassMethods = numClsMembers;
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000331 if (numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000332 ClassMethods = new ObjcMethodDecl*[numClsMembers];
333 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000334 }
335}
336
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000337/// ObjcAddProtoMethods - Insert instance and methods declarations into
338/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
339///
340void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000341 unsigned numInsMembers,
342 ObjcMethodDecl **clsMethods,
343 unsigned numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000344 NumInstanceMethods = numInsMembers;
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000345 if (numInsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000346 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
347 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000348 }
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000349 NumClassMethods = numClsMembers;
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000350 if (numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000351 ClassMethods = new ObjcMethodDecl*[numClsMembers];
352 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000353 }
354}
355
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000356/// ObjcAddCat - Insert instance and methods declarations into
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000357/// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000358///
359void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000360 unsigned numInsMembers,
361 ObjcMethodDecl **clsMethods,
362 unsigned numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000363 NumInstanceMethods = numInsMembers;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000364 if (numInsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000365 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
366 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000367 }
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000368 NumClassMethods = numClsMembers;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000369 if (numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000370 ClassMethods = new ObjcMethodDecl*[numClsMembers];
371 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000372 }
373}
374
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000375/// ObjcAddCatImplMethods - Insert instance and methods declarations into
376/// ObjcCategoryImplDecl's CatInsMethods and CatClsMethods fields.
377///
378void ObjcCategoryImplDecl::ObjcAddCatImplMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000379 unsigned numInsMembers,
380 ObjcMethodDecl **clsMethods,
381 unsigned numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000382 NumInstanceMethods = numInsMembers;
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000383 if (numInsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000384 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
385 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000386 }
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000387 NumClassMethods = numClsMembers;
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000388 if (numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000389 ClassMethods = new ObjcMethodDecl*[numClsMembers];
390 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000391 }
392}
393
Fariborz Jahanianf6546b32007-09-27 18:57:03 +0000394/// ObjcAddImplMethods - Insert instance and methods declarations into
395/// ObjcImplementationDecl's InsMethods and ClsMethods fields.
396///
397void ObjcImplementationDecl::ObjcAddImplMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000398 unsigned numInsMembers,
399 ObjcMethodDecl **clsMethods,
400 unsigned numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000401 NumInstanceMethods = numInsMembers;
Fariborz Jahanianf6546b32007-09-27 18:57:03 +0000402 if (numInsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000403 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
404 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanianf6546b32007-09-27 18:57:03 +0000405 }
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000406 NumClassMethods = numClsMembers;
Fariborz Jahanianf6546b32007-09-27 18:57:03 +0000407 if (numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000408 ClassMethods = new ObjcMethodDecl*[numClsMembers];
409 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanianf6546b32007-09-27 18:57:03 +0000410 }
411}
412
Steve Naroffca0ecfe2007-10-14 18:27:41 +0000413// lookupInstanceMethod - This method returns an instance method by looking in
414// the class, it's categories, and it's super classes (using a linear search).
Steve Naroffc6814ea2007-10-02 20:01:56 +0000415ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) {
416 ObjcInterfaceDecl* ClassDecl = this;
417 while (ClassDecl != NULL) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000418 ObjcMethodDecl **methods = ClassDecl->getInstanceMethods();
419 int methodCount = ClassDecl->getNumInstanceMethods();
Steve Naroffc6814ea2007-10-02 20:01:56 +0000420 for (int i = 0; i < methodCount; ++i) {
421 if (methods[i]->getSelector() == Sel) {
422 return methods[i];
423 }
424 }
Steve Naroff5d152542007-10-14 23:13:51 +0000425 // Didn't find one yet - look through protocols.
426 ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
427 int numProtocols = ClassDecl->getNumIntfRefProtocols();
428 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
429 ObjcMethodDecl **methods = protocols[pIdx]->getInstanceMethods();
430 int methodCount = protocols[pIdx]->getNumInstanceMethods();
431 for (int i = 0; i < methodCount; ++i) {
432 if (methods[i]->getSelector() == Sel) {
433 return methods[i];
434 }
435 }
436 }
Steve Naroffca0ecfe2007-10-14 18:27:41 +0000437 // Didn't find one yet - now look through categories.
Steve Naroff5d152542007-10-14 23:13:51 +0000438 ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroffca0ecfe2007-10-14 18:27:41 +0000439 while (CatDecl) {
440 ObjcMethodDecl **methods = CatDecl->getInstanceMethods();
441 int methodCount = CatDecl->getNumInstanceMethods();
442 for (int i = 0; i < methodCount; ++i) {
443 if (methods[i]->getSelector() == Sel) {
444 return methods[i];
445 }
446 }
447 CatDecl = CatDecl->getNextClassCategory();
448 }
Steve Naroffc6814ea2007-10-02 20:01:56 +0000449 ClassDecl = ClassDecl->getSuperClass();
450 }
451 return NULL;
452}
453
Steve Naroffca0ecfe2007-10-14 18:27:41 +0000454// lookupClassMethod - This method returns a class method by looking in the
455// class, it's categories, and it's super classes (using a linear search).
Steve Naroffc6814ea2007-10-02 20:01:56 +0000456ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) {
457 ObjcInterfaceDecl* ClassDecl = this;
458 while (ClassDecl != NULL) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000459 ObjcMethodDecl **methods = ClassDecl->getClassMethods();
460 int methodCount = ClassDecl->getNumClassMethods();
Steve Naroffc6814ea2007-10-02 20:01:56 +0000461 for (int i = 0; i < methodCount; ++i) {
462 if (methods[i]->getSelector() == Sel) {
463 return methods[i];
464 }
465 }
Steve Naroff5d152542007-10-14 23:13:51 +0000466 // Didn't find one yet - look through protocols.
467 ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
468 int numProtocols = ClassDecl->getNumIntfRefProtocols();
469 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
470 ObjcMethodDecl **methods = protocols[pIdx]->getClassMethods();
471 int methodCount = protocols[pIdx]->getNumClassMethods();
472 for (int i = 0; i < methodCount; ++i) {
473 if (methods[i]->getSelector() == Sel) {
474 return methods[i];
475 }
476 }
477 }
Steve Naroffca0ecfe2007-10-14 18:27:41 +0000478 // Didn't find one yet - now look through categories.
Steve Naroff5d152542007-10-14 23:13:51 +0000479 ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroffca0ecfe2007-10-14 18:27:41 +0000480 while (CatDecl) {
481 ObjcMethodDecl **methods = CatDecl->getClassMethods();
482 int methodCount = CatDecl->getNumClassMethods();
483 for (int i = 0; i < methodCount; ++i) {
484 if (methods[i]->getSelector() == Sel) {
485 return methods[i];
486 }
487 }
488 CatDecl = CatDecl->getNextClassCategory();
489 }
Steve Naroffc6814ea2007-10-02 20:01:56 +0000490 ClassDecl = ClassDecl->getSuperClass();
491 }
492 return NULL;
493}
494
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000495