blob: 78916c53b7cecb9eff8c006eb92840c4b2b08e38 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Steve Naroff980e5082007-10-01 19:00:59 +000015#include "clang/AST/DeclObjC.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000016#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff3536b442007-09-06 21:24:23 +000029static unsigned nInterfaceDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000030static unsigned nClassDecls = 0;
31static unsigned nMethodDecls = 0;
32static unsigned nProtocolDecls = 0;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000033static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000034static unsigned nCategoryDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000035static unsigned nIvarDecls = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000036static unsigned nObjCImplementationDecls = 0;
37static unsigned nObjCCategoryImpl = 0;
38static unsigned nObjCCompatibleAlias = 0;
39static unsigned nObjCPropertyDecl = 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +000040static unsigned nLinkageSpecDecl = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000041
Reid Spencer5f016e22007-07-11 17:01:13 +000042static bool StatSwitch = false;
43
Steve Naroffe5ea3802007-09-17 14:49:06 +000044const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000045 switch (DeclKind) {
46 default: assert(0 && "Unknown decl kind!");
47 case Typedef:
48 return "Typedef";
49 case Function:
50 return "Function";
Chris Lattneraa9fc462007-10-08 21:37:32 +000051 case BlockVar:
52 return "BlockVar";
53 case FileVar:
54 return "FileVar";
55 case ParmVar:
56 return "ParmVar";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000057 case EnumConstant:
58 return "EnumConstant";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000059 case ObjCInterface:
60 return "ObjCInterface";
61 case ObjCClass:
62 return "ObjCClass";
63 case ObjCMethod:
64 return "ObjCMethod";
65 case ObjCProtocol:
66 return "ObjCProtocol";
67 case ObjCForwardProtocol:
68 return "ObjCForwardProtocol";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000069 case Struct:
70 return "Struct";
71 case Union:
72 return "Union";
73 case Class:
74 return "Class";
75 case Enum:
76 return "Enum";
77 }
78}
79
Reid Spencer5f016e22007-07-11 17:01:13 +000080bool Decl::CollectingStats(bool enable) {
81 if (enable) StatSwitch = true;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +000082 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
85void Decl::PrintStats() {
86 fprintf(stderr, "*** Decl Stats:\n");
87 fprintf(stderr, " %d decls total.\n",
88 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff3f128ad2007-09-17 14:16:13 +000089 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000090 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +000091 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
92 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
93 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
94 nBlockVars, (int)sizeof(BlockVarDecl),
95 int(nBlockVars*sizeof(BlockVarDecl)));
96 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
97 nFileVars, (int)sizeof(FileVarDecl),
98 int(nFileVars*sizeof(FileVarDecl)));
99 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
100 nParmVars, (int)sizeof(ParmVarDecl),
101 int(nParmVars*sizeof(ParmVarDecl)));
102 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
103 nFieldDecls, (int)sizeof(FieldDecl),
104 int(nFieldDecls*sizeof(FieldDecl)));
105 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
106 nSUC, (int)sizeof(RecordDecl),
107 int(nSUC*sizeof(RecordDecl)));
108 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
109 nEnumDecls, (int)sizeof(EnumDecl),
110 int(nEnumDecls*sizeof(EnumDecl)));
111 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
112 nEnumConst, (int)sizeof(EnumConstantDecl),
113 int(nEnumConst*sizeof(EnumConstantDecl)));
114 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
115 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000116 // Objective-C decls...
117 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000118 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
119 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000120 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000121 nIvarDecls, (int)sizeof(ObjCIvarDecl),
122 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000123 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000124 nClassDecls, (int)sizeof(ObjCClassDecl),
125 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000126 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000127 nMethodDecls, (int)sizeof(ObjCMethodDecl),
128 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000129 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000130 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
131 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000132 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000133 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
134 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000135 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000136 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
137 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000138
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000139 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000140 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
141 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000142
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000143 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000144 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
145 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000146
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000147 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000148 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
149 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000150
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000151 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000152 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
153 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 fprintf(stderr, "Total bytes = %d\n",
156 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
157 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
158 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
159 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000160 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000161 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
162 nIvarDecls*sizeof(ObjCIvarDecl)+
163 nClassDecls*sizeof(ObjCClassDecl)+
164 nMethodDecls*sizeof(ObjCMethodDecl)+
165 nProtocolDecls*sizeof(ObjCProtocolDecl)+
166 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
167 nCategoryDecls*sizeof(ObjCCategoryDecl)+
168 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
169 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
170 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
171 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
172 nLinkageSpecDecl*sizeof(LinkageSpecDecl)));
173
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
176void Decl::addDeclKind(const Kind k) {
177 switch (k) {
178 case Typedef:
179 nTypedef++;
180 break;
181 case Function:
182 nFuncs++;
183 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000184 case BlockVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 nBlockVars++;
186 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000187 case FileVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 nFileVars++;
189 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000190 case ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 nParmVars++;
192 break;
193 case EnumConstant:
194 nEnumConst++;
195 break;
196 case Field:
197 nFieldDecls++;
198 break;
199 case Struct:
200 case Union:
201 case Class:
202 nSUC++;
203 break;
204 case Enum:
205 nEnumDecls++;
206 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000207 case ObjCInterface:
Steve Naroff3536b442007-09-06 21:24:23 +0000208 nInterfaceDecls++;
209 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000210 case ObjCClass:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000211 nClassDecls++;
212 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000213 case ObjCMethod:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000214 nMethodDecls++;
215 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000216 case ObjCProtocol:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000217 nProtocolDecls++;
218 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000219 case ObjCForwardProtocol:
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000220 nForwardProtocolDecls++;
221 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000222 case ObjCCategory:
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000223 nCategoryDecls++;
224 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000225 case ObjCIvar:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000226 nIvarDecls++;
Chris Lattner7341c332007-09-16 19:23:04 +0000227 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000228 case ObjCImplementation:
229 nObjCImplementationDecls++;
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000230 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000231 case ObjCCategoryImpl:
232 nObjCCategoryImpl++;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000233 break;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000234 case CompatibleAlias:
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000235 nObjCCompatibleAlias++;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000236 break;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000237 case PropertyDecl:
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000238 nObjCPropertyDecl++;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000239 break;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000240 case LinkageSpec:
241 nLinkageSpecDecl++;
242 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 }
244}
245
246// Out-of-line virtual method providing a home for Decl.
247Decl::~Decl() {
248}
249
Chris Lattnerfd5de472007-10-06 22:53:46 +0000250const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 if (const IdentifierInfo *II = getIdentifier())
252 return II->getName();
253 return "";
254}
255
256
257FunctionDecl::~FunctionDecl() {
258 delete[] ParamInfo;
259}
260
261unsigned FunctionDecl::getNumParams() const {
Chris Lattnerec584d62007-12-06 17:20:20 +0000262 if (isa<FunctionTypeNoProto>(getCanonicalType())) return 0;
263 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000264}
265
266void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
267 assert(ParamInfo == 0 && "Already has param info!");
268 assert(NumParams == getNumParams() && "Parameter count mismatch!");
269
270 // Zero params -> null pointer.
271 if (NumParams) {
272 ParamInfo = new ParmVarDecl*[NumParams];
273 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
274 }
275}
276
277
278/// defineBody - When created, RecordDecl's correspond to a forward declared
279/// record. This method is used to mark the decl as being defined, with the
280/// specified contents.
281void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
282 assert(!isDefinition() && "Cannot redefine record!");
283 setDefinition(true);
284 NumMembers = numMembers;
285 if (numMembers) {
286 Members = new FieldDecl*[numMembers];
287 memcpy(Members, members, numMembers*sizeof(Decl*));
288 }
289}
290
291FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
292 if (Members == 0 || NumMembers < 0)
293 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // linear search. When C++ classes come along, will likely need to revisit.
296 for (int i = 0; i < NumMembers; ++i) {
297 if (Members[i]->getIdentifier() == name)
298 return Members[i];
299 }
300 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000301}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000302
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000303void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000304 unsigned NumParams) {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000305 assert(ParamInfo == 0 && "Already has param info!");
306
307 // Zero params -> null pointer.
308 if (NumParams) {
309 ParamInfo = new ParmVarDecl*[NumParams];
310 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
311 NumMethodParams = NumParams;
312 }
313}
314
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000315ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000316 delete[] ParamInfo;
317}
318
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000319/// ObjCAddInstanceVariablesToClass - Inserts instance variables
320/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000321///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000322void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff60fccee2007-10-29 21:38:07 +0000323 unsigned numIvars,
Steve Narofff908a872007-10-30 02:23:23 +0000324 SourceLocation RBrac) {
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000325 NumIvars = numIvars;
326 if (numIvars) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000327 Ivars = new ObjCIvarDecl*[numIvars];
328 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000329 }
Steve Narofff908a872007-10-30 02:23:23 +0000330 setLocEnd(RBrac);
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000331}
332
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000333/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000334/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000335/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000336///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000337void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
338 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000339 NumIvars = numIvars;
340 if (numIvars) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000341 Ivars = new ObjCIvarDecl*[numIvars];
342 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000343 }
344}
345
Steve Naroff60fccee2007-10-29 21:38:07 +0000346/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000347/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000348///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000349void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000350 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000351 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000352 unsigned numClsMembers,
353 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000354 NumInstanceMethods = numInsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000355 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000356 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
357 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000358 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000359 NumClassMethods = numClsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000360 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361 ClassMethods = new ObjCMethodDecl*[numClsMembers];
362 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000363 }
Steve Narofff908a872007-10-30 02:23:23 +0000364 AtEndLoc = endLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000365}
366
Steve Naroff60fccee2007-10-29 21:38:07 +0000367/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000368/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000369///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000370void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000371 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000372 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000373 unsigned numClsMembers,
Steve Naroff423cb562007-10-30 13:30:57 +0000374 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000375 NumInstanceMethods = numInsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000376 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000377 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
378 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000379 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000380 NumClassMethods = numClsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000381 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000382 ClassMethods = new ObjCMethodDecl*[numClsMembers];
383 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000384 }
Steve Naroff423cb562007-10-30 13:30:57 +0000385 AtEndLoc = endLoc;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000386}
387
Steve Naroff60fccee2007-10-29 21:38:07 +0000388/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000389/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000390///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000391void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000392 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000393 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000394 unsigned numClsMembers,
Steve Naroff423cb562007-10-30 13:30:57 +0000395 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000396 NumInstanceMethods = numInsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000397 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000398 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
399 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000400 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000401 NumClassMethods = numClsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000402 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000403 ClassMethods = new ObjCMethodDecl*[numClsMembers];
404 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000405 }
Steve Naroff423cb562007-10-30 13:30:57 +0000406 AtEndLoc = endLoc;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000407}
408
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000409ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
410 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
411 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroff03300712007-11-12 13:56:41 +0000412 while (ClassDecl != NULL) {
Chris Lattnerbe6df082007-12-12 07:56:42 +0000413 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
414 I != E; ++I) {
415 if ((*I)->getIdentifier() == ID) {
Steve Naroff03300712007-11-12 13:56:41 +0000416 clsDeclared = ClassDecl;
Chris Lattnerbe6df082007-12-12 07:56:42 +0000417 return *I;
Steve Naroff03300712007-11-12 13:56:41 +0000418 }
419 }
420 ClassDecl = ClassDecl->getSuperClass();
421 }
422 return NULL;
423}
424
Chris Lattner0157c512007-12-12 07:30:05 +0000425/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner33ef2592007-12-12 08:17:45 +0000426/// the class, its categories, and its super classes (using a linear search).
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000427ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
428 ObjCInterfaceDecl* ClassDecl = this;
429 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000430
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000431 while (ClassDecl != NULL) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000432 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000433 return MethodDecl;
434
Steve Naroffff1afdb2007-10-14 23:13:51 +0000435 // Didn't find one yet - look through protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000436 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroffff1afdb2007-10-14 23:13:51 +0000437 int numProtocols = ClassDecl->getNumIntfRefProtocols();
438 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000439 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000440 return MethodDecl;
Steve Naroffff1afdb2007-10-14 23:13:51 +0000441 }
Steve Naroff3d581382007-10-14 18:27:41 +0000442 // Didn't find one yet - now look through categories.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000443 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000444 while (CatDecl) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000445 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000446 return MethodDecl;
Steve Naroff3d581382007-10-14 18:27:41 +0000447 CatDecl = CatDecl->getNextClassCategory();
448 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000449 ClassDecl = ClassDecl->getSuperClass();
450 }
451 return NULL;
452}
453
Steve Naroff3d581382007-10-14 18:27:41 +0000454// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner33ef2592007-12-12 08:17:45 +0000455// class, its categories, and its super classes (using a linear search).
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000456ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
457 ObjCInterfaceDecl* ClassDecl = this;
458 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000459
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000460 while (ClassDecl != NULL) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000461 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000462 return MethodDecl;
463
Steve Naroffff1afdb2007-10-14 23:13:51 +0000464 // Didn't find one yet - look through protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000465 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroffff1afdb2007-10-14 23:13:51 +0000466 int numProtocols = ClassDecl->getNumIntfRefProtocols();
467 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000468 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000469 return MethodDecl;
Steve Naroffff1afdb2007-10-14 23:13:51 +0000470 }
Steve Naroff3d581382007-10-14 18:27:41 +0000471 // Didn't find one yet - now look through categories.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000472 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000473 while (CatDecl) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000474 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000475 return MethodDecl;
Steve Naroff3d581382007-10-14 18:27:41 +0000476 CatDecl = CatDecl->getNextClassCategory();
477 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000478 ClassDecl = ClassDecl->getSuperClass();
479 }
480 return NULL;
481}
482
Chris Lattner0157c512007-12-12 07:30:05 +0000483/// lookupInstanceMethod - This method returns an instance method by looking in
484/// the class implementation. Unlike interfaces, we don't look outside the
485/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000486ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattner0157c512007-12-12 07:30:05 +0000487 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
488 if ((*I)->getSelector() == Sel)
489 return *I;
Steve Naroffc43d8682007-11-11 00:10:47 +0000490 return NULL;
491}
492
Chris Lattner0157c512007-12-12 07:30:05 +0000493/// lookupClassMethod - This method returns a class method by looking in
494/// the class implementation. Unlike interfaces, we don't look outside the
495/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000496ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000497 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
498 I != E; ++I)
499 if ((*I)->getSelector() == Sel)
500 return *I;
Steve Naroffc43d8682007-11-11 00:10:47 +0000501 return NULL;
502}
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000503
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000504// lookupInstanceMethod - This method returns an instance method by looking in
505// the class implementation. Unlike interfaces, we don't look outside the
506// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000507ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000508 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
509 if ((*I)->getSelector() == Sel)
510 return *I;
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000511 return NULL;
512}
513
514// lookupClassMethod - This method returns an instance method by looking in
515// the class implementation. Unlike interfaces, we don't look outside the
516// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000517ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000518 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
519 I != E; ++I)
520 if ((*I)->getSelector() == Sel)
521 return *I;
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000522 return NULL;
523}
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000524
525// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
526// it inherited.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
528 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000529
Steve Naroff94a5c332007-12-19 22:27:04 +0000530 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000531 return MethodDecl;
532
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000533 if (getNumReferencedProtocols() > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000534 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000535
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000536 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000537 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000538 return MethodDecl;
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000539 }
540 }
541 return NULL;
542}
543
544// lookupInstanceMethod - Lookup a class method in the protocol and protocols
545// it inherited.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000546ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
547 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000548
Steve Naroff94a5c332007-12-19 22:27:04 +0000549 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000550 return MethodDecl;
551
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000552 if (getNumReferencedProtocols() > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000553 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000554
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000555 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000556 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000557 return MethodDecl;
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000558 }
559 }
560 return NULL;
561}
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000562
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000563/// getSynthesizedMethodSize - Compute size of synthesized method name
564/// as done be the rewrite.
565///
566unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
Fariborz Jahanian847794a2008-01-17 01:36:09 +0000567 // syntesized method name is a concatenation of -/+[class-name selector]
568 // Get length of this name.
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000569 unsigned length = 3; // _I_ or _C_
570 length += strlen(getClassInterface()->getName()) +1; // extra for _
571 NamedDecl *MethodContext = getMethodContext();
572 if (ObjCCategoryImplDecl *CID =
573 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
574 length += strlen(CID->getName()) +1;
575 length += getSelector().getName().size(); // selector name
Fariborz Jahanian847794a2008-01-17 01:36:09 +0000576 return length;
577}
578
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
580 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000581 return ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000583 return CD->getClassInterface();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584 if (ObjCImplementationDecl *IMD =
585 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000586 return IMD->getClassInterface();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587 if (ObjCCategoryImplDecl *CID =
588 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000589 return CID->getClassInterface();
590 assert(false && "unknown method context");
591 return 0;
592}