blob: 711b3257ec850766e278965a62a9d88170b4750c [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;
Fariborz Jahanianf76f2b02007-11-06 22:01:00 +000039static unsigned nObjcPropertyDecl = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000040
Steve Narofff84d11f2007-05-23 21:48:04 +000041static bool StatSwitch = false;
42
Steve Naroff83763f22007-09-17 14:49:06 +000043const char *Decl::getDeclKindName() const {
Steve Naroff2f742082007-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 Lattnerba9dddb2007-10-08 21:37:32 +000050 case BlockVar:
51 return "BlockVar";
52 case FileVar:
53 return "FileVar";
54 case ParmVar:
55 return "ParmVar";
Steve Naroff2f742082007-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 Naroff2f742082007-09-16 16:16:00 +000064 case ObjcProtocol:
65 return "ObjcProtocol";
Fariborz Jahanian876e27d2007-09-21 15:40:54 +000066 case ObjcForwardProtocol:
67 return "ObjcForwardProtocol";
Steve Naroff2f742082007-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
Steve Narofff84d11f2007-05-23 21:48:04 +000079bool Decl::CollectingStats(bool enable) {
80 if (enable) StatSwitch = true;
Fariborz Jahanian67341402007-10-04 00:45:27 +000081 return StatSwitch;
Steve Narofff84d11f2007-05-23 21:48:04 +000082}
83
84void Decl::PrintStats() {
Chris Lattnerfc234de2007-05-24 00:40:54 +000085 fprintf(stderr, "*** Decl Stats:\n");
86 fprintf(stderr, " %d decls total.\n",
Chris Lattnera5490952007-05-24 01:09:39 +000087 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff73d534a2007-09-17 14:16:13 +000088 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +000089 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattnerfc234de2007-05-24 00:40:54 +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)));
Chris Lattnera5490952007-05-24 01:09:39 +0000104 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
105 nSUC, (int)sizeof(RecordDecl),
106 int(nSUC*sizeof(RecordDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000107 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 Naroff73d534a2007-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 Jahanian876e27d2007-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 Jahanian867a7eb2007-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 Naroff73d534a2007-09-17 14:16:13 +0000137
Fariborz Jahanianbfe13c52007-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 Jahanian89b8ef92007-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 Jahanian49c64252007-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 Jahanianf76f2b02007-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 Lattnerfc234de2007-05-24 00:40:54 +0000154 fprintf(stderr, "Total bytes = %d\n",
155 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
156 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattnera5490952007-05-24 01:09:39 +0000157 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Chris Lattnerfc234de2007-05-24 00:40:54 +0000158 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff73d534a2007-09-17 14:16:13 +0000159 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Steve Narofff84d11f2007-05-23 21:48:04 +0000160}
161
162void Decl::addDeclKind(const Kind k) {
Chris Lattnerfc234de2007-05-24 00:40:54 +0000163 switch (k) {
164 case Typedef:
165 nTypedef++;
166 break;
167 case Function:
168 nFuncs++;
169 break;
Chris Lattnerba9dddb2007-10-08 21:37:32 +0000170 case BlockVar:
Chris Lattnerfc234de2007-05-24 00:40:54 +0000171 nBlockVars++;
172 break;
Chris Lattnerba9dddb2007-10-08 21:37:32 +0000173 case FileVar:
Chris Lattnerfc234de2007-05-24 00:40:54 +0000174 nFileVars++;
175 break;
Chris Lattnerba9dddb2007-10-08 21:37:32 +0000176 case ParmVar:
Chris Lattnerfc234de2007-05-24 00:40:54 +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:
Chris Lattnera5490952007-05-24 01:09:39 +0000188 nSUC++;
Chris Lattnerfc234de2007-05-24 00:40:54 +0000189 break;
190 case Enum:
191 nEnumDecls++;
192 break;
Steve Naroff09bf8152007-09-06 21:24:23 +0000193 case ObjcInterface:
194 nInterfaceDecls++;
195 break;
Chris Lattner81b76242007-09-16 19:23:04 +0000196 case ObjcClass:
Steve Naroff73d534a2007-09-17 14:16:13 +0000197 nClassDecls++;
198 break;
Chris Lattner81b76242007-09-16 19:23:04 +0000199 case ObjcMethod:
Steve Naroff73d534a2007-09-17 14:16:13 +0000200 nMethodDecls++;
201 break;
Chris Lattner81b76242007-09-16 19:23:04 +0000202 case ObjcProtocol:
Steve Naroff73d534a2007-09-17 14:16:13 +0000203 nProtocolDecls++;
204 break;
Fariborz Jahanian876e27d2007-09-21 15:40:54 +0000205 case ObjcForwardProtocol:
206 nForwardProtocolDecls++;
207 break;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000208 case ObjcCategory:
209 nCategoryDecls++;
210 break;
Chris Lattner81b76242007-09-16 19:23:04 +0000211 case ObjcIvar:
Steve Naroff73d534a2007-09-17 14:16:13 +0000212 nIvarDecls++;
Chris Lattner81b76242007-09-16 19:23:04 +0000213 break;
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +0000214 case ObjcImplementation:
215 nObjcImplementationDecls++;
216 break;
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000217 case ObjcCategoryImpl:
218 nObjcCategoryImpl++;
219 break;
Fariborz Jahanian49c64252007-10-11 23:42:27 +0000220 case CompatibleAlias:
221 nObjcCompatibleAlias++;
222 break;
Fariborz Jahanianf76f2b02007-11-06 22:01:00 +0000223 case PropertyDecl:
224 nObjcPropertyDecl++;
225 break;
Chris Lattnerfc234de2007-05-24 00:40:54 +0000226 }
Steve Narofff84d11f2007-05-23 21:48:04 +0000227}
228
Chris Lattner6d9a6852006-10-25 05:11:20 +0000229// Out-of-line virtual method providing a home for Decl.
230Decl::~Decl() {
231}
Chris Lattner17ed4872006-11-20 04:58:19 +0000232
Chris Lattnera4016552007-10-06 22:53:46 +0000233const char *NamedDecl::getName() const {
Chris Lattnerec040b12007-01-21 23:09:50 +0000234 if (const IdentifierInfo *II = getIdentifier())
235 return II->getName();
236 return "";
Chris Lattner17ed4872006-11-20 04:58:19 +0000237}
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000238
239
240FunctionDecl::~FunctionDecl() {
241 delete[] ParamInfo;
242}
243
244unsigned FunctionDecl::getNumParams() const {
Chris Lattnerb36a98e2007-12-06 17:20:20 +0000245 if (isa<FunctionTypeNoProto>(getCanonicalType())) return 0;
246 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000247}
248
Chris Lattner53621a52007-06-13 20:44:40 +0000249void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000250 assert(ParamInfo == 0 && "Already has param info!");
251 assert(NumParams == getNumParams() && "Parameter count mismatch!");
252
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000253 // Zero params -> null pointer.
254 if (NumParams) {
Chris Lattner53621a52007-06-13 20:44:40 +0000255 ParamInfo = new ParmVarDecl*[NumParams];
256 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000257 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000258}
Chris Lattner41943152007-01-25 04:52:46 +0000259
260
261/// defineBody - When created, RecordDecl's correspond to a forward declared
262/// record. This method is used to mark the decl as being defined, with the
263/// specified contents.
Steve Naroffcc321422007-03-26 23:09:51 +0000264void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
Chris Lattner41943152007-01-25 04:52:46 +0000265 assert(!isDefinition() && "Cannot redefine record!");
266 setDefinition(true);
Chris Lattner5f521502007-01-25 06:27:24 +0000267 NumMembers = numMembers;
268 if (numMembers) {
Steve Naroffcc321422007-03-26 23:09:51 +0000269 Members = new FieldDecl*[numMembers];
Chris Lattner5f521502007-01-25 06:27:24 +0000270 memcpy(Members, members, numMembers*sizeof(Decl*));
Chris Lattner41943152007-01-25 04:52:46 +0000271 }
272}
Steve Naroffcc321422007-03-26 23:09:51 +0000273
274FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
275 if (Members == 0 || NumMembers < 0)
276 return 0;
Fariborz Jahanian67341402007-10-04 00:45:27 +0000277
Steve Naroffcc321422007-03-26 23:09:51 +0000278 // linear search. When C++ classes come along, will likely need to revisit.
279 for (int i = 0; i < NumMembers; ++i) {
280 if (Members[i]->getIdentifier() == name)
281 return Members[i];
282 }
283 return 0;
Chris Lattnerbd4de5df2007-07-12 15:43:07 +0000284}
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000285
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000286void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
287 unsigned NumParams) {
Fariborz Jahaniancfb31fa2007-09-12 18:23:47 +0000288 assert(ParamInfo == 0 && "Already has param info!");
289
290 // Zero params -> null pointer.
291 if (NumParams) {
292 ParamInfo = new ParmVarDecl*[NumParams];
293 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
294 NumMethodParams = NumParams;
295 }
296}
297
298ObjcMethodDecl::~ObjcMethodDecl() {
299 delete[] ParamInfo;
300}
301
Fariborz Jahanianf3287bf2007-09-14 21:08:27 +0000302/// ObjcAddInstanceVariablesToClass - Inserts instance variables
303/// into ObjcInterfaceDecl's fields.
304///
Steve Naroff33a1e802007-10-29 21:38:07 +0000305void ObjcInterfaceDecl::addInstanceVariablesToClass(ObjcIvarDecl **ivars,
306 unsigned numIvars,
Steve Naroffc5484042007-10-30 02:23:23 +0000307 SourceLocation RBrac) {
Fariborz Jahanianf3287bf2007-09-14 21:08:27 +0000308 NumIvars = numIvars;
309 if (numIvars) {
310 Ivars = new ObjcIvarDecl*[numIvars];
311 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
312 }
Steve Naroffc5484042007-10-30 02:23:23 +0000313 setLocEnd(RBrac);
Fariborz Jahanianf3287bf2007-09-14 21:08:27 +0000314}
315
Fariborz Jahanian2a4dd312007-09-26 18:27:25 +0000316/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
317/// Variables (Ivars) relative to what declared in @implementation;s class.
318/// Ivars into ObjcImplementationDecl's fields.
319///
320void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
Fariborz Jahanian76aff362007-10-04 17:06:28 +0000321 ObjcIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahanian2a4dd312007-09-26 18:27:25 +0000322 NumIvars = numIvars;
323 if (numIvars) {
324 Ivars = new ObjcIvarDecl*[numIvars];
325 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
326 }
327}
328
Steve Naroff33a1e802007-10-29 21:38:07 +0000329/// addMethods - Insert instance and methods declarations into
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000330/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
331///
Steve Naroff33a1e802007-10-29 21:38:07 +0000332void ObjcInterfaceDecl::addMethods(ObjcMethodDecl **insMethods,
333 unsigned numInsMembers,
334 ObjcMethodDecl **clsMethods,
335 unsigned numClsMembers,
336 SourceLocation endLoc) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000337 NumInstanceMethods = numInsMembers;
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000338 if (numInsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000339 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
340 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000341 }
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000342 NumClassMethods = numClsMembers;
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000343 if (numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000344 ClassMethods = new ObjcMethodDecl*[numClsMembers];
345 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000346 }
Steve Naroffc5484042007-10-30 02:23:23 +0000347 AtEndLoc = endLoc;
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000348}
349
Steve Naroff33a1e802007-10-29 21:38:07 +0000350/// addMethods - Insert instance and methods declarations into
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000351/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
352///
Steve Naroff33a1e802007-10-29 21:38:07 +0000353void ObjcProtocolDecl::addMethods(ObjcMethodDecl **insMethods,
354 unsigned numInsMembers,
355 ObjcMethodDecl **clsMethods,
356 unsigned numClsMembers,
Steve Naroff5448cf62007-10-30 13:30:57 +0000357 SourceLocation endLoc) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000358 NumInstanceMethods = numInsMembers;
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000359 if (numInsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000360 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
361 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000362 }
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000363 NumClassMethods = numClsMembers;
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000364 if (numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000365 ClassMethods = new ObjcMethodDecl*[numClsMembers];
366 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000367 }
Steve Naroff5448cf62007-10-30 13:30:57 +0000368 AtEndLoc = endLoc;
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000369}
370
Steve Naroff33a1e802007-10-29 21:38:07 +0000371/// addMethods - Insert instance and methods declarations into
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000372/// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000373///
Steve Naroff33a1e802007-10-29 21:38:07 +0000374void ObjcCategoryDecl::addMethods(ObjcMethodDecl **insMethods,
375 unsigned numInsMembers,
376 ObjcMethodDecl **clsMethods,
377 unsigned numClsMembers,
Steve Naroff5448cf62007-10-30 13:30:57 +0000378 SourceLocation endLoc) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000379 NumInstanceMethods = numInsMembers;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000380 if (numInsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000381 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
382 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000383 }
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000384 NumClassMethods = numClsMembers;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000385 if (numClsMembers) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000386 ClassMethods = new ObjcMethodDecl*[numClsMembers];
387 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000388 }
Steve Naroff5448cf62007-10-30 13:30:57 +0000389 AtEndLoc = endLoc;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000390}
391
Steve Naroffe3d1ab22007-11-12 13:56:41 +0000392ObjcIvarDecl *ObjcInterfaceDecl::lookupInstanceVariable(
393 IdentifierInfo *ID, ObjcInterfaceDecl *&clsDeclared) {
394 ObjcInterfaceDecl* ClassDecl = this;
395 while (ClassDecl != NULL) {
Chris Lattner30d23e82007-12-12 07:56:42 +0000396 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
397 I != E; ++I) {
398 if ((*I)->getIdentifier() == ID) {
Steve Naroffe3d1ab22007-11-12 13:56:41 +0000399 clsDeclared = ClassDecl;
Chris Lattner30d23e82007-12-12 07:56:42 +0000400 return *I;
Steve Naroffe3d1ab22007-11-12 13:56:41 +0000401 }
402 }
403 ClassDecl = ClassDecl->getSuperClass();
404 }
405 return NULL;
406}
407
Chris Lattner854f3162007-12-12 07:30:05 +0000408/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner7354e6a2007-12-12 08:17:45 +0000409/// the class, its categories, and its super classes (using a linear search).
Steve Naroffc6814ea2007-10-02 20:01:56 +0000410ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) {
411 ObjcInterfaceDecl* ClassDecl = this;
412 while (ClassDecl != NULL) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000413 ObjcMethodDecl **methods = ClassDecl->getInstanceMethods();
414 int methodCount = ClassDecl->getNumInstanceMethods();
Steve Naroffc6814ea2007-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 Naroff5d152542007-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 Naroffca0ecfe2007-10-14 18:27:41 +0000432 // Didn't find one yet - now look through categories.
Steve Naroff5d152542007-10-14 23:13:51 +0000433 ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroffca0ecfe2007-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 Naroffc6814ea2007-10-02 20:01:56 +0000444 ClassDecl = ClassDecl->getSuperClass();
445 }
446 return NULL;
447}
448
Steve Naroffca0ecfe2007-10-14 18:27:41 +0000449// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner7354e6a2007-12-12 08:17:45 +0000450// class, its categories, and its super classes (using a linear search).
Steve Naroffc6814ea2007-10-02 20:01:56 +0000451ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) {
452 ObjcInterfaceDecl* ClassDecl = this;
453 while (ClassDecl != NULL) {
Fariborz Jahanianb8d9e082007-10-02 22:05:16 +0000454 ObjcMethodDecl **methods = ClassDecl->getClassMethods();
455 int methodCount = ClassDecl->getNumClassMethods();
Steve Naroffc6814ea2007-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 Naroff5d152542007-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 Naroffca0ecfe2007-10-14 18:27:41 +0000473 // Didn't find one yet - now look through categories.
Steve Naroff5d152542007-10-14 23:13:51 +0000474 ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroffca0ecfe2007-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 Naroffc6814ea2007-10-02 20:01:56 +0000485 ClassDecl = ClassDecl->getSuperClass();
486 }
487 return NULL;
488}
489
Chris Lattner854f3162007-12-12 07:30:05 +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) {
494 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
495 if ((*I)->getSelector() == Sel)
496 return *I;
Steve Naroff22e078e2007-11-11 00:10:47 +0000497 return NULL;
498}
499
Chris Lattner854f3162007-12-12 07:30:05 +0000500/// lookupClassMethod - This method returns a class method by looking in
501/// the class implementation. Unlike interfaces, we don't look outside the
502/// implementation.
503ObjcMethodDecl *ObjcImplementationDecl::lookupClassMethod(Selector Sel) {
Chris Lattner31bc07e2007-12-12 07:46:12 +0000504 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
505 I != E; ++I)
506 if ((*I)->getSelector() == Sel)
507 return *I;
Steve Naroff22e078e2007-11-11 00:10:47 +0000508 return NULL;
509}
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000510
Steve Narofffedb4972007-11-12 22:05:31 +0000511// lookupInstanceMethod - This method returns an instance method by looking in
512// the class implementation. Unlike interfaces, we don't look outside the
513// implementation.
514ObjcMethodDecl *ObjcCategoryImplDecl::lookupInstanceMethod(Selector &Sel) {
Chris Lattner31bc07e2007-12-12 07:46:12 +0000515 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
516 if ((*I)->getSelector() == Sel)
517 return *I;
Steve Narofffedb4972007-11-12 22:05:31 +0000518 return NULL;
519}
520
521// lookupClassMethod - This method returns an instance method by looking in
522// the class implementation. Unlike interfaces, we don't look outside the
523// implementation.
524ObjcMethodDecl *ObjcCategoryImplDecl::lookupClassMethod(Selector &Sel) {
Chris Lattner31bc07e2007-12-12 07:46:12 +0000525 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
526 I != E; ++I)
527 if ((*I)->getSelector() == Sel)
528 return *I;
Steve Narofffedb4972007-11-12 22:05:31 +0000529 return NULL;
530}
Fariborz Jahanianff6a4552007-12-07 21:21:21 +0000531
532// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
533// it inherited.
534ObjcMethodDecl *ObjcProtocolDecl::lookupInstanceMethod(Selector &Sel) {
535 ObjcMethodDecl *const*methods = getInstanceMethods();
536 int methodCount = getNumInstanceMethods();
537 for (int i = 0; i < methodCount; ++i) {
538 if (methods[i]->getSelector() == Sel) {
539 return methods[i];
540 }
541 }
542 if (getNumReferencedProtocols() > 0) {
543 ObjcProtocolDecl **RefPDecl = getReferencedProtocols();
544
545 for (int i = 0; i < getNumReferencedProtocols(); i++) {
546 if (ObjcMethodDecl *Method = RefPDecl[i]->lookupInstanceMethod(Sel))
547 return Method;
548 }
549 }
550 return NULL;
551}
552
553// lookupInstanceMethod - Lookup a class method in the protocol and protocols
554// it inherited.
555ObjcMethodDecl *ObjcProtocolDecl::lookupClassMethod(Selector &Sel) {
556 ObjcMethodDecl *const*methods = getClassMethods();
557 int methodCount = getNumClassMethods();
558 for (int i = 0; i < methodCount; ++i) {
559 if (methods[i]->getSelector() == Sel) {
560 return methods[i];
561 }
562 }
563 if (getNumReferencedProtocols() > 0) {
564 ObjcProtocolDecl **RefPDecl = getReferencedProtocols();
565
566 for (int i = 0; i < getNumReferencedProtocols(); i++) {
567 if (ObjcMethodDecl *Method = RefPDecl[i]->lookupClassMethod(Sel))
568 return Method;
569 }
570 }
571 return NULL;
572}