blob: 55abe6f34d5f60a0b9e7182a1c0c7eb2dbcbdbcb [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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;
Ted Kremenek42730c52008-01-07 19:49:32 +000036static unsigned nObjCImplementationDecls = 0;
37static unsigned nObjCCategoryImpl = 0;
38static unsigned nObjCCompatibleAlias = 0;
39static unsigned nObjCPropertyDecl = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +000040static unsigned nLinkageSpecDecl = 0;
Anders Carlsson4f7f4412008-02-08 00:33:21 +000041static unsigned nFileScopeAsmDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000042
Chris Lattner4b009652007-07-25 00:24:17 +000043static bool StatSwitch = false;
44
Steve Naroff590aba82007-09-17 14:49:06 +000045const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000046 switch (DeclKind) {
47 default: assert(0 && "Unknown decl kind!");
48 case Typedef:
49 return "Typedef";
50 case Function:
51 return "Function";
Chris Lattner3289bca2007-10-08 21:37:32 +000052 case BlockVar:
53 return "BlockVar";
54 case FileVar:
55 return "FileVar";
56 case ParmVar:
57 return "ParmVar";
Steve Narofff0c31dd2007-09-16 16:16:00 +000058 case EnumConstant:
59 return "EnumConstant";
Ted Kremenek42730c52008-01-07 19:49:32 +000060 case ObjCInterface:
61 return "ObjCInterface";
62 case ObjCClass:
63 return "ObjCClass";
64 case ObjCMethod:
65 return "ObjCMethod";
66 case ObjCProtocol:
67 return "ObjCProtocol";
68 case ObjCForwardProtocol:
69 return "ObjCForwardProtocol";
Steve Narofff0c31dd2007-09-16 16:16:00 +000070 case Struct:
71 return "Struct";
72 case Union:
73 return "Union";
74 case Class:
75 return "Class";
76 case Enum:
77 return "Enum";
78 }
79}
80
Chris Lattner4b009652007-07-25 00:24:17 +000081bool Decl::CollectingStats(bool enable) {
82 if (enable) StatSwitch = true;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +000083 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000084}
85
86void Decl::PrintStats() {
87 fprintf(stderr, "*** Decl Stats:\n");
88 fprintf(stderr, " %d decls total.\n",
89 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff948fd372007-09-17 14:16:13 +000090 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000091 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000092 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
93 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
94 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
95 nBlockVars, (int)sizeof(BlockVarDecl),
96 int(nBlockVars*sizeof(BlockVarDecl)));
97 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
98 nFileVars, (int)sizeof(FileVarDecl),
99 int(nFileVars*sizeof(FileVarDecl)));
100 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
101 nParmVars, (int)sizeof(ParmVarDecl),
102 int(nParmVars*sizeof(ParmVarDecl)));
103 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
104 nFieldDecls, (int)sizeof(FieldDecl),
105 int(nFieldDecls*sizeof(FieldDecl)));
106 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
107 nSUC, (int)sizeof(RecordDecl),
108 int(nSUC*sizeof(RecordDecl)));
109 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
110 nEnumDecls, (int)sizeof(EnumDecl),
111 int(nEnumDecls*sizeof(EnumDecl)));
112 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
113 nEnumConst, (int)sizeof(EnumConstantDecl),
114 int(nEnumConst*sizeof(EnumConstantDecl)));
115 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
116 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000117 // Objective-C decls...
118 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000119 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
120 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000121 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000122 nIvarDecls, (int)sizeof(ObjCIvarDecl),
123 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000124 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000125 nClassDecls, (int)sizeof(ObjCClassDecl),
126 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000127 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000128 nMethodDecls, (int)sizeof(ObjCMethodDecl),
129 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000130 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000131 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
132 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000133 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000134 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
135 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000136 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000137 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
138 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000139
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000140 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000141 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
142 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000143
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000144 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000145 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
146 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000147
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000148 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000149 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
150 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000151
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000152 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000153 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
154 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000155
Chris Lattner4b009652007-07-25 00:24:17 +0000156 fprintf(stderr, "Total bytes = %d\n",
157 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
158 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
159 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
160 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Chris Lattner806a5f52008-01-12 07:05:38 +0000161 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000162 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
163 nIvarDecls*sizeof(ObjCIvarDecl)+
164 nClassDecls*sizeof(ObjCClassDecl)+
165 nMethodDecls*sizeof(ObjCMethodDecl)+
166 nProtocolDecls*sizeof(ObjCProtocolDecl)+
167 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
168 nCategoryDecls*sizeof(ObjCCategoryDecl)+
169 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
170 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
171 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
172 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000173 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
174 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000175
Chris Lattner4b009652007-07-25 00:24:17 +0000176}
177
178void Decl::addDeclKind(const Kind k) {
179 switch (k) {
180 case Typedef:
181 nTypedef++;
182 break;
183 case Function:
184 nFuncs++;
185 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000186 case BlockVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000187 nBlockVars++;
188 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000189 case FileVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000190 nFileVars++;
191 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000192 case ParmVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000193 nParmVars++;
194 break;
195 case EnumConstant:
196 nEnumConst++;
197 break;
198 case Field:
199 nFieldDecls++;
200 break;
201 case Struct:
202 case Union:
203 case Class:
204 nSUC++;
205 break;
206 case Enum:
207 nEnumDecls++;
208 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000209 case ObjCInterface:
Steve Naroff81f1bba2007-09-06 21:24:23 +0000210 nInterfaceDecls++;
211 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000212 case ObjCClass:
Steve Naroff948fd372007-09-17 14:16:13 +0000213 nClassDecls++;
214 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000215 case ObjCMethod:
Steve Naroff948fd372007-09-17 14:16:13 +0000216 nMethodDecls++;
217 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000218 case ObjCProtocol:
Steve Naroff948fd372007-09-17 14:16:13 +0000219 nProtocolDecls++;
220 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000221 case ObjCForwardProtocol:
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000222 nForwardProtocolDecls++;
223 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000224 case ObjCCategory:
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000225 nCategoryDecls++;
226 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000227 case ObjCIvar:
Steve Naroff948fd372007-09-17 14:16:13 +0000228 nIvarDecls++;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000229 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000230 case ObjCImplementation:
231 nObjCImplementationDecls++;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000232 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000233 case ObjCCategoryImpl:
234 nObjCCategoryImpl++;
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000235 break;
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000236 case CompatibleAlias:
Ted Kremenek42730c52008-01-07 19:49:32 +0000237 nObjCCompatibleAlias++;
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000238 break;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000239 case PropertyDecl:
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 nObjCPropertyDecl++;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000241 break;
Chris Lattner806a5f52008-01-12 07:05:38 +0000242 case LinkageSpec:
243 nLinkageSpecDecl++;
244 break;
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000245 case FileScopeAsm:
246 nFileScopeAsmDecl++;
247 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000248 }
249}
250
251// Out-of-line virtual method providing a home for Decl.
252Decl::~Decl() {
253}
254
Chris Lattner910435b2007-10-06 22:53:46 +0000255const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000256 if (const IdentifierInfo *II = getIdentifier())
257 return II->getName();
258 return "";
259}
260
261
262FunctionDecl::~FunctionDecl() {
263 delete[] ParamInfo;
264}
265
266unsigned FunctionDecl::getNumParams() const {
Chris Lattner23d411b2007-12-06 17:20:20 +0000267 if (isa<FunctionTypeNoProto>(getCanonicalType())) return 0;
268 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000269}
270
271void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
272 assert(ParamInfo == 0 && "Already has param info!");
273 assert(NumParams == getNumParams() && "Parameter count mismatch!");
274
275 // Zero params -> null pointer.
276 if (NumParams) {
277 ParamInfo = new ParmVarDecl*[NumParams];
278 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
279 }
280}
281
282
283/// defineBody - When created, RecordDecl's correspond to a forward declared
284/// record. This method is used to mark the decl as being defined, with the
285/// specified contents.
286void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
287 assert(!isDefinition() && "Cannot redefine record!");
288 setDefinition(true);
289 NumMembers = numMembers;
290 if (numMembers) {
291 Members = new FieldDecl*[numMembers];
292 memcpy(Members, members, numMembers*sizeof(Decl*));
293 }
294}
295
296FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
297 if (Members == 0 || NumMembers < 0)
298 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000299
Chris Lattner4b009652007-07-25 00:24:17 +0000300 // linear search. When C++ classes come along, will likely need to revisit.
301 for (int i = 0; i < NumMembers; ++i) {
302 if (Members[i]->getIdentifier() == name)
303 return Members[i];
304 }
305 return 0;
306}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000307
Ted Kremenek42730c52008-01-07 19:49:32 +0000308void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian84082af2007-10-04 17:06:28 +0000309 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000310 assert(ParamInfo == 0 && "Already has param info!");
311
312 // Zero params -> null pointer.
313 if (NumParams) {
314 ParamInfo = new ParmVarDecl*[NumParams];
315 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
316 NumMethodParams = NumParams;
317 }
318}
319
Ted Kremenek42730c52008-01-07 19:49:32 +0000320ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000321 delete[] ParamInfo;
322}
323
Ted Kremenek42730c52008-01-07 19:49:32 +0000324/// ObjCAddInstanceVariablesToClass - Inserts instance variables
325/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000326///
Ted Kremenek42730c52008-01-07 19:49:32 +0000327void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000328 unsigned numIvars,
Steve Naroffef20ed32007-10-30 02:23:23 +0000329 SourceLocation RBrac) {
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000330 NumIvars = numIvars;
331 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000332 Ivars = new ObjCIvarDecl*[numIvars];
333 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000334 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000335 setLocEnd(RBrac);
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000336}
337
Ted Kremenek42730c52008-01-07 19:49:32 +0000338/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000339/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000340/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000341///
Ted Kremenek42730c52008-01-07 19:49:32 +0000342void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
343 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000344 NumIvars = numIvars;
345 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000346 Ivars = new ObjCIvarDecl*[numIvars];
347 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000348 }
349}
350
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000351/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000352/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000353///
Ted Kremenek42730c52008-01-07 19:49:32 +0000354void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000355 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000356 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000357 unsigned numClsMembers,
358 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000359 NumInstanceMethods = numInsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000360 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000361 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
362 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000363 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000364 NumClassMethods = numClsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000365 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000366 ClassMethods = new ObjCMethodDecl*[numClsMembers];
367 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000368 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000369 AtEndLoc = endLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000370}
371
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000372/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000373/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000374///
Ted Kremenek42730c52008-01-07 19:49:32 +0000375void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000376 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000377 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000378 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000379 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000380 NumInstanceMethods = numInsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000381 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000382 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
383 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000384 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000385 NumClassMethods = numClsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000386 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000387 ClassMethods = new ObjCMethodDecl*[numClsMembers];
388 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000389 }
Steve Naroff667f1682007-10-30 13:30:57 +0000390 AtEndLoc = endLoc;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000391}
392
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000393/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000394/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000395///
Ted Kremenek42730c52008-01-07 19:49:32 +0000396void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000397 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000398 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000399 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000400 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000401 NumInstanceMethods = numInsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000402 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000403 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
404 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000405 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000406 NumClassMethods = numClsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000407 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000408 ClassMethods = new ObjCMethodDecl*[numClsMembers];
409 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000410 }
Steve Naroff667f1682007-10-30 13:30:57 +0000411 AtEndLoc = endLoc;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000412}
413
Ted Kremenek42730c52008-01-07 19:49:32 +0000414ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
415 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
416 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000417 while (ClassDecl != NULL) {
Chris Lattnerc7b06752007-12-12 07:56:42 +0000418 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
419 I != E; ++I) {
420 if ((*I)->getIdentifier() == ID) {
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000421 clsDeclared = ClassDecl;
Chris Lattnerc7b06752007-12-12 07:56:42 +0000422 return *I;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000423 }
424 }
425 ClassDecl = ClassDecl->getSuperClass();
426 }
427 return NULL;
428}
429
Chris Lattnerac9ab532007-12-12 07:30:05 +0000430/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner73e795c2007-12-12 08:17:45 +0000431/// the class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000432ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
433 ObjCInterfaceDecl* ClassDecl = this;
434 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000435
Steve Narofffa465d12007-10-02 20:01:56 +0000436 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000437 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000438 return MethodDecl;
439
Steve Naroff705380b2007-10-14 23:13:51 +0000440 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000441 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000442 int numProtocols = ClassDecl->getNumIntfRefProtocols();
443 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000444 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000445 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000446 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000447 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000448 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000449 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000450 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000451 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000452 CatDecl = CatDecl->getNextClassCategory();
453 }
Steve Narofffa465d12007-10-02 20:01:56 +0000454 ClassDecl = ClassDecl->getSuperClass();
455 }
456 return NULL;
457}
458
Steve Narofff66d84a2007-10-14 18:27:41 +0000459// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner73e795c2007-12-12 08:17:45 +0000460// class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000461ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
462 ObjCInterfaceDecl* ClassDecl = this;
463 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000464
Steve Narofffa465d12007-10-02 20:01:56 +0000465 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000466 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000467 return MethodDecl;
468
Steve Naroff705380b2007-10-14 23:13:51 +0000469 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000470 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000471 int numProtocols = ClassDecl->getNumIntfRefProtocols();
472 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000473 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000474 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000475 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000476 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000477 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000478 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000479 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000480 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000481 CatDecl = CatDecl->getNextClassCategory();
482 }
Steve Narofffa465d12007-10-02 20:01:56 +0000483 ClassDecl = ClassDecl->getSuperClass();
484 }
485 return NULL;
486}
487
Chris Lattnerac9ab532007-12-12 07:30:05 +0000488/// lookupInstanceMethod - This method returns an instance method by looking in
489/// the class implementation. Unlike interfaces, we don't look outside the
490/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000491ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerac9ab532007-12-12 07:30:05 +0000492 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
493 if ((*I)->getSelector() == Sel)
494 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000495 return NULL;
496}
497
Chris Lattnerac9ab532007-12-12 07:30:05 +0000498/// lookupClassMethod - This method returns a class method by looking in
499/// the class implementation. Unlike interfaces, we don't look outside the
500/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000501ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000502 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
503 I != E; ++I)
504 if ((*I)->getSelector() == Sel)
505 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000506 return NULL;
507}
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000508
Steve Naroff31233632007-11-12 22:05:31 +0000509// lookupInstanceMethod - This method returns an instance method by looking in
510// the class implementation. Unlike interfaces, we don't look outside the
511// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000512ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000513 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
514 if ((*I)->getSelector() == Sel)
515 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000516 return NULL;
517}
518
519// lookupClassMethod - This method returns an instance method by looking in
520// the class implementation. Unlike interfaces, we don't look outside the
521// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000522ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000523 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
524 I != E; ++I)
525 if ((*I)->getSelector() == Sel)
526 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000527 return NULL;
528}
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000529
530// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
531// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000532ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
533 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000534
Steve Naroff74273de2007-12-19 22:27:04 +0000535 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000536 return MethodDecl;
537
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000538 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000539 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000540
Fariborz Jahanian87829072007-12-20 19:24:10 +0000541 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000542 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000543 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000544 }
545 }
546 return NULL;
547}
548
549// lookupInstanceMethod - Lookup a class method in the protocol and protocols
550// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000551ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
552 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000553
Steve Naroff74273de2007-12-19 22:27:04 +0000554 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000555 return MethodDecl;
556
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000557 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000558 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000559
Fariborz Jahanian87829072007-12-20 19:24:10 +0000560 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000561 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000562 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000563 }
564 }
565 return NULL;
566}
Steve Naroff2ce399a2007-12-14 23:37:57 +0000567
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000568/// getSynthesizedMethodSize - Compute size of synthesized method name
569/// as done be the rewrite.
570///
571unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000572 // syntesized method name is a concatenation of -/+[class-name selector]
573 // Get length of this name.
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000574 unsigned length = 3; // _I_ or _C_
575 length += strlen(getClassInterface()->getName()) +1; // extra for _
576 NamedDecl *MethodContext = getMethodContext();
577 if (ObjCCategoryImplDecl *CID =
578 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
579 length += strlen(CID->getName()) +1;
580 length += getSelector().getName().size(); // selector name
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000581 return length;
582}
583
Ted Kremenek42730c52008-01-07 19:49:32 +0000584ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
585 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000586 return ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000587 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000588 return CD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000589 if (ObjCImplementationDecl *IMD =
590 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000591 return IMD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000592 if (ObjCCategoryImplDecl *CID =
593 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000594 return CID->getClassInterface();
595 assert(false && "unknown method context");
596 return 0;
597}