blob: 0ed028ca540101169e00a47cf46ca75c286f2255 [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"
Anders Carlsson3f70c542008-02-15 07:04:12 +000016#include "clang/AST/Attr.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018using namespace clang;
19
20// temporary statistics gathering
21static unsigned nFuncs = 0;
22static unsigned nBlockVars = 0;
23static unsigned nFileVars = 0;
24static unsigned nParmVars = 0;
25static unsigned nSUC = 0;
26static unsigned nEnumConst = 0;
27static unsigned nEnumDecls = 0;
28static unsigned nTypedef = 0;
29static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000030static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000031static unsigned nClassDecls = 0;
32static unsigned nMethodDecls = 0;
33static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000034static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000035static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000036static unsigned nIvarDecls = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000037static unsigned nObjCImplementationDecls = 0;
38static unsigned nObjCCategoryImpl = 0;
39static unsigned nObjCCompatibleAlias = 0;
40static unsigned nObjCPropertyDecl = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +000041static unsigned nLinkageSpecDecl = 0;
Anders Carlsson4f7f4412008-02-08 00:33:21 +000042static unsigned nFileScopeAsmDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000043
Chris Lattner4b009652007-07-25 00:24:17 +000044static bool StatSwitch = false;
45
Steve Naroff590aba82007-09-17 14:49:06 +000046const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000047 switch (DeclKind) {
48 default: assert(0 && "Unknown decl kind!");
49 case Typedef:
50 return "Typedef";
51 case Function:
52 return "Function";
Chris Lattner3289bca2007-10-08 21:37:32 +000053 case BlockVar:
54 return "BlockVar";
55 case FileVar:
56 return "FileVar";
57 case ParmVar:
58 return "ParmVar";
Steve Narofff0c31dd2007-09-16 16:16:00 +000059 case EnumConstant:
60 return "EnumConstant";
Ted Kremenek42730c52008-01-07 19:49:32 +000061 case ObjCInterface:
62 return "ObjCInterface";
63 case ObjCClass:
64 return "ObjCClass";
65 case ObjCMethod:
66 return "ObjCMethod";
67 case ObjCProtocol:
68 return "ObjCProtocol";
69 case ObjCForwardProtocol:
70 return "ObjCForwardProtocol";
Steve Narofff0c31dd2007-09-16 16:16:00 +000071 case Struct:
72 return "Struct";
73 case Union:
74 return "Union";
75 case Class:
76 return "Class";
77 case Enum:
78 return "Enum";
79 }
80}
81
Chris Lattner4b009652007-07-25 00:24:17 +000082bool Decl::CollectingStats(bool enable) {
83 if (enable) StatSwitch = true;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +000084 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
87void Decl::PrintStats() {
88 fprintf(stderr, "*** Decl Stats:\n");
89 fprintf(stderr, " %d decls total.\n",
90 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff948fd372007-09-17 14:16:13 +000091 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000092 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000093 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
94 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
95 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
96 nBlockVars, (int)sizeof(BlockVarDecl),
97 int(nBlockVars*sizeof(BlockVarDecl)));
98 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
99 nFileVars, (int)sizeof(FileVarDecl),
100 int(nFileVars*sizeof(FileVarDecl)));
101 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
102 nParmVars, (int)sizeof(ParmVarDecl),
103 int(nParmVars*sizeof(ParmVarDecl)));
104 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
105 nFieldDecls, (int)sizeof(FieldDecl),
106 int(nFieldDecls*sizeof(FieldDecl)));
107 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
108 nSUC, (int)sizeof(RecordDecl),
109 int(nSUC*sizeof(RecordDecl)));
110 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
111 nEnumDecls, (int)sizeof(EnumDecl),
112 int(nEnumDecls*sizeof(EnumDecl)));
113 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
114 nEnumConst, (int)sizeof(EnumConstantDecl),
115 int(nEnumConst*sizeof(EnumConstantDecl)));
116 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
117 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000118 // Objective-C decls...
119 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000120 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
121 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000122 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000123 nIvarDecls, (int)sizeof(ObjCIvarDecl),
124 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000125 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000126 nClassDecls, (int)sizeof(ObjCClassDecl),
127 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000128 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 nMethodDecls, (int)sizeof(ObjCMethodDecl),
130 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000131 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000132 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
133 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000134 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000135 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
136 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000137 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000138 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
139 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000140
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000141 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000142 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
143 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000144
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000145 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000146 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
147 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000148
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000149 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000150 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
151 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000152
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000153 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000154 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
155 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000156
Chris Lattner4b009652007-07-25 00:24:17 +0000157 fprintf(stderr, "Total bytes = %d\n",
158 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
159 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
160 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
161 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Chris Lattner806a5f52008-01-12 07:05:38 +0000162 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000163 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
164 nIvarDecls*sizeof(ObjCIvarDecl)+
165 nClassDecls*sizeof(ObjCClassDecl)+
166 nMethodDecls*sizeof(ObjCMethodDecl)+
167 nProtocolDecls*sizeof(ObjCProtocolDecl)+
168 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
169 nCategoryDecls*sizeof(ObjCCategoryDecl)+
170 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
171 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
172 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
173 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000174 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
175 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000176
Chris Lattner4b009652007-07-25 00:24:17 +0000177}
178
179void Decl::addDeclKind(const Kind k) {
180 switch (k) {
181 case Typedef:
182 nTypedef++;
183 break;
184 case Function:
185 nFuncs++;
186 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000187 case BlockVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000188 nBlockVars++;
189 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000190 case FileVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000191 nFileVars++;
192 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000193 case ParmVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000194 nParmVars++;
195 break;
196 case EnumConstant:
197 nEnumConst++;
198 break;
199 case Field:
200 nFieldDecls++;
201 break;
202 case Struct:
203 case Union:
204 case Class:
205 nSUC++;
206 break;
207 case Enum:
208 nEnumDecls++;
209 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000210 case ObjCInterface:
Steve Naroff81f1bba2007-09-06 21:24:23 +0000211 nInterfaceDecls++;
212 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000213 case ObjCClass:
Steve Naroff948fd372007-09-17 14:16:13 +0000214 nClassDecls++;
215 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000216 case ObjCMethod:
Steve Naroff948fd372007-09-17 14:16:13 +0000217 nMethodDecls++;
218 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000219 case ObjCProtocol:
Steve Naroff948fd372007-09-17 14:16:13 +0000220 nProtocolDecls++;
221 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000222 case ObjCForwardProtocol:
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000223 nForwardProtocolDecls++;
224 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000225 case ObjCCategory:
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000226 nCategoryDecls++;
227 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000228 case ObjCIvar:
Steve Naroff948fd372007-09-17 14:16:13 +0000229 nIvarDecls++;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000230 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000231 case ObjCImplementation:
232 nObjCImplementationDecls++;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000233 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000234 case ObjCCategoryImpl:
235 nObjCCategoryImpl++;
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000236 break;
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000237 case CompatibleAlias:
Ted Kremenek42730c52008-01-07 19:49:32 +0000238 nObjCCompatibleAlias++;
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000239 break;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000240 case PropertyDecl:
Ted Kremenek42730c52008-01-07 19:49:32 +0000241 nObjCPropertyDecl++;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000242 break;
Chris Lattner806a5f52008-01-12 07:05:38 +0000243 case LinkageSpec:
244 nLinkageSpecDecl++;
245 break;
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000246 case FileScopeAsm:
247 nFileScopeAsmDecl++;
248 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000249 }
250}
251
252// Out-of-line virtual method providing a home for Decl.
253Decl::~Decl() {
254}
255
Chris Lattner910435b2007-10-06 22:53:46 +0000256const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000257 if (const IdentifierInfo *II = getIdentifier())
258 return II->getName();
259 return "";
260}
261
Anders Carlsson3f70c542008-02-15 07:04:12 +0000262void ValueDecl::addAttr(Attr *attr)
263{
264 attr->setNext(Attrs);
265 Attrs = attr;
266}
Chris Lattner4b009652007-07-25 00:24:17 +0000267
268FunctionDecl::~FunctionDecl() {
269 delete[] ParamInfo;
270}
271
272unsigned FunctionDecl::getNumParams() const {
Chris Lattner23d411b2007-12-06 17:20:20 +0000273 if (isa<FunctionTypeNoProto>(getCanonicalType())) return 0;
274 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000275}
276
277void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
278 assert(ParamInfo == 0 && "Already has param info!");
279 assert(NumParams == getNumParams() && "Parameter count mismatch!");
280
281 // Zero params -> null pointer.
282 if (NumParams) {
283 ParamInfo = new ParmVarDecl*[NumParams];
284 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
285 }
286}
287
288
289/// defineBody - When created, RecordDecl's correspond to a forward declared
290/// record. This method is used to mark the decl as being defined, with the
291/// specified contents.
292void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
293 assert(!isDefinition() && "Cannot redefine record!");
294 setDefinition(true);
295 NumMembers = numMembers;
296 if (numMembers) {
297 Members = new FieldDecl*[numMembers];
298 memcpy(Members, members, numMembers*sizeof(Decl*));
299 }
300}
301
302FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
303 if (Members == 0 || NumMembers < 0)
304 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000305
Chris Lattner4b009652007-07-25 00:24:17 +0000306 // linear search. When C++ classes come along, will likely need to revisit.
307 for (int i = 0; i < NumMembers; ++i) {
308 if (Members[i]->getIdentifier() == name)
309 return Members[i];
310 }
311 return 0;
312}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000313
Ted Kremenek42730c52008-01-07 19:49:32 +0000314void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian84082af2007-10-04 17:06:28 +0000315 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000316 assert(ParamInfo == 0 && "Already has param info!");
317
318 // Zero params -> null pointer.
319 if (NumParams) {
320 ParamInfo = new ParmVarDecl*[NumParams];
321 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
322 NumMethodParams = NumParams;
323 }
324}
325
Ted Kremenek42730c52008-01-07 19:49:32 +0000326ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000327 delete[] ParamInfo;
328}
329
Ted Kremenek42730c52008-01-07 19:49:32 +0000330/// ObjCAddInstanceVariablesToClass - Inserts instance variables
331/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000332///
Ted Kremenek42730c52008-01-07 19:49:32 +0000333void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000334 unsigned numIvars,
Steve Naroffef20ed32007-10-30 02:23:23 +0000335 SourceLocation RBrac) {
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000336 NumIvars = numIvars;
337 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000338 Ivars = new ObjCIvarDecl*[numIvars];
339 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000340 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000341 setLocEnd(RBrac);
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000342}
343
Ted Kremenek42730c52008-01-07 19:49:32 +0000344/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000345/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000346/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000347///
Ted Kremenek42730c52008-01-07 19:49:32 +0000348void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
349 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000350 NumIvars = numIvars;
351 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000352 Ivars = new ObjCIvarDecl*[numIvars];
353 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000354 }
355}
356
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000357/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000358/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000359///
Ted Kremenek42730c52008-01-07 19:49:32 +0000360void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000361 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000362 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000363 unsigned numClsMembers,
364 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000365 NumInstanceMethods = numInsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000366 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000367 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
368 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000369 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000370 NumClassMethods = numClsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000371 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000372 ClassMethods = new ObjCMethodDecl*[numClsMembers];
373 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000374 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000375 AtEndLoc = endLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000376}
377
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000378/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000379/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000380///
Ted Kremenek42730c52008-01-07 19:49:32 +0000381void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000382 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000383 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000384 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000385 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000386 NumInstanceMethods = numInsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000387 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000388 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
389 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000390 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000391 NumClassMethods = numClsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000392 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000393 ClassMethods = new ObjCMethodDecl*[numClsMembers];
394 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000395 }
Steve Naroff667f1682007-10-30 13:30:57 +0000396 AtEndLoc = endLoc;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000397}
398
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000399/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000400/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000401///
Ted Kremenek42730c52008-01-07 19:49:32 +0000402void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000403 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000404 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000405 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000406 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000407 NumInstanceMethods = numInsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000408 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000409 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
410 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000411 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000412 NumClassMethods = numClsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000413 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000414 ClassMethods = new ObjCMethodDecl*[numClsMembers];
415 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000416 }
Steve Naroff667f1682007-10-30 13:30:57 +0000417 AtEndLoc = endLoc;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000418}
419
Ted Kremenek42730c52008-01-07 19:49:32 +0000420ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
421 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
422 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000423 while (ClassDecl != NULL) {
Chris Lattnerc7b06752007-12-12 07:56:42 +0000424 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
425 I != E; ++I) {
426 if ((*I)->getIdentifier() == ID) {
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000427 clsDeclared = ClassDecl;
Chris Lattnerc7b06752007-12-12 07:56:42 +0000428 return *I;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000429 }
430 }
431 ClassDecl = ClassDecl->getSuperClass();
432 }
433 return NULL;
434}
435
Chris Lattnerac9ab532007-12-12 07:30:05 +0000436/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner73e795c2007-12-12 08:17:45 +0000437/// the class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000438ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
439 ObjCInterfaceDecl* ClassDecl = this;
440 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000441
Steve Narofffa465d12007-10-02 20:01:56 +0000442 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000443 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000444 return MethodDecl;
445
Steve Naroff705380b2007-10-14 23:13:51 +0000446 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000447 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000448 int numProtocols = ClassDecl->getNumIntfRefProtocols();
449 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000450 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000451 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000452 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000453 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000454 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000455 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000456 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000457 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000458 CatDecl = CatDecl->getNextClassCategory();
459 }
Steve Narofffa465d12007-10-02 20:01:56 +0000460 ClassDecl = ClassDecl->getSuperClass();
461 }
462 return NULL;
463}
464
Steve Narofff66d84a2007-10-14 18:27:41 +0000465// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner73e795c2007-12-12 08:17:45 +0000466// class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000467ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
468 ObjCInterfaceDecl* ClassDecl = this;
469 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000470
Steve Narofffa465d12007-10-02 20:01:56 +0000471 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000472 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000473 return MethodDecl;
474
Steve Naroff705380b2007-10-14 23:13:51 +0000475 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000476 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000477 int numProtocols = ClassDecl->getNumIntfRefProtocols();
478 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000479 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000480 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000481 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000482 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000483 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000484 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000485 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000486 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000487 CatDecl = CatDecl->getNextClassCategory();
488 }
Steve Narofffa465d12007-10-02 20:01:56 +0000489 ClassDecl = ClassDecl->getSuperClass();
490 }
491 return NULL;
492}
493
Chris Lattnerac9ab532007-12-12 07:30:05 +0000494/// lookupInstanceMethod - This method returns an instance method by looking in
495/// the class implementation. Unlike interfaces, we don't look outside the
496/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000497ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerac9ab532007-12-12 07:30:05 +0000498 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
499 if ((*I)->getSelector() == Sel)
500 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000501 return NULL;
502}
503
Chris Lattnerac9ab532007-12-12 07:30:05 +0000504/// lookupClassMethod - This method returns a class method by looking in
505/// the class implementation. Unlike interfaces, we don't look outside the
506/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000507ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000508 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
509 I != E; ++I)
510 if ((*I)->getSelector() == Sel)
511 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000512 return NULL;
513}
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000514
Steve Naroff31233632007-11-12 22:05:31 +0000515// lookupInstanceMethod - This method returns an instance method by looking in
516// the class implementation. Unlike interfaces, we don't look outside the
517// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000518ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000519 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
520 if ((*I)->getSelector() == Sel)
521 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000522 return NULL;
523}
524
525// lookupClassMethod - This method returns an instance method by looking in
526// the class implementation. Unlike interfaces, we don't look outside the
527// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000528ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000529 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
530 I != E; ++I)
531 if ((*I)->getSelector() == Sel)
532 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000533 return NULL;
534}
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000535
536// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
537// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000538ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
539 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000540
Steve Naroff74273de2007-12-19 22:27:04 +0000541 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000542 return MethodDecl;
543
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000544 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000545 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000546
Fariborz Jahanian87829072007-12-20 19:24:10 +0000547 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000548 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000549 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000550 }
551 }
552 return NULL;
553}
554
555// lookupInstanceMethod - Lookup a class method in the protocol and protocols
556// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000557ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
558 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000559
Steve Naroff74273de2007-12-19 22:27:04 +0000560 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000561 return MethodDecl;
562
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000563 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000564 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000565
Fariborz Jahanian87829072007-12-20 19:24:10 +0000566 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000567 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000568 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000569 }
570 }
571 return NULL;
572}
Steve Naroff2ce399a2007-12-14 23:37:57 +0000573
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000574/// getSynthesizedMethodSize - Compute size of synthesized method name
575/// as done be the rewrite.
576///
577unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000578 // syntesized method name is a concatenation of -/+[class-name selector]
579 // Get length of this name.
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000580 unsigned length = 3; // _I_ or _C_
581 length += strlen(getClassInterface()->getName()) +1; // extra for _
582 NamedDecl *MethodContext = getMethodContext();
583 if (ObjCCategoryImplDecl *CID =
584 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
585 length += strlen(CID->getName()) +1;
586 length += getSelector().getName().size(); // selector name
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000587 return length;
588}
589
Ted Kremenek42730c52008-01-07 19:49:32 +0000590ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
591 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000592 return ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000593 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000594 return CD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000595 if (ObjCImplementationDecl *IMD =
596 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000597 return IMD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000598 if (ObjCCategoryImplDecl *CID =
599 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000600 return CID->getClassInterface();
601 assert(false && "unknown method context");
602 return 0;
603}