blob: d80b5eea80bb7a0f29a057abd21fce015876d56a [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)+
161 nLinkageSpecDecl*sizeof(LinkageSpecDecl))
162 /* FIXME: add ObjC decls */);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163}
164
165void Decl::addDeclKind(const Kind k) {
166 switch (k) {
167 case Typedef:
168 nTypedef++;
169 break;
170 case Function:
171 nFuncs++;
172 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000173 case BlockVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 nBlockVars++;
175 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000176 case FileVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 nFileVars++;
178 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000179 case ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 nParmVars++;
181 break;
182 case EnumConstant:
183 nEnumConst++;
184 break;
185 case Field:
186 nFieldDecls++;
187 break;
188 case Struct:
189 case Union:
190 case Class:
191 nSUC++;
192 break;
193 case Enum:
194 nEnumDecls++;
195 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 case ObjCInterface:
Steve Naroff3536b442007-09-06 21:24:23 +0000197 nInterfaceDecls++;
198 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000199 case ObjCClass:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000200 nClassDecls++;
201 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000202 case ObjCMethod:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000203 nMethodDecls++;
204 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000205 case ObjCProtocol:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000206 nProtocolDecls++;
207 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000208 case ObjCForwardProtocol:
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000209 nForwardProtocolDecls++;
210 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000211 case ObjCCategory:
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000212 nCategoryDecls++;
213 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000214 case ObjCIvar:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000215 nIvarDecls++;
Chris Lattner7341c332007-09-16 19:23:04 +0000216 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000217 case ObjCImplementation:
218 nObjCImplementationDecls++;
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000219 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000220 case ObjCCategoryImpl:
221 nObjCCategoryImpl++;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000222 break;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000223 case CompatibleAlias:
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000224 nObjCCompatibleAlias++;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000225 break;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000226 case PropertyDecl:
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227 nObjCPropertyDecl++;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000228 break;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000229 case LinkageSpec:
230 nLinkageSpecDecl++;
231 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 }
233}
234
235// Out-of-line virtual method providing a home for Decl.
236Decl::~Decl() {
237}
238
Chris Lattnerfd5de472007-10-06 22:53:46 +0000239const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 if (const IdentifierInfo *II = getIdentifier())
241 return II->getName();
242 return "";
243}
244
245
246FunctionDecl::~FunctionDecl() {
247 delete[] ParamInfo;
248}
249
250unsigned FunctionDecl::getNumParams() const {
Chris Lattnerec584d62007-12-06 17:20:20 +0000251 if (isa<FunctionTypeNoProto>(getCanonicalType())) return 0;
252 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000253}
254
255void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
256 assert(ParamInfo == 0 && "Already has param info!");
257 assert(NumParams == getNumParams() && "Parameter count mismatch!");
258
259 // Zero params -> null pointer.
260 if (NumParams) {
261 ParamInfo = new ParmVarDecl*[NumParams];
262 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
263 }
264}
265
266
267/// defineBody - When created, RecordDecl's correspond to a forward declared
268/// record. This method is used to mark the decl as being defined, with the
269/// specified contents.
270void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
271 assert(!isDefinition() && "Cannot redefine record!");
272 setDefinition(true);
273 NumMembers = numMembers;
274 if (numMembers) {
275 Members = new FieldDecl*[numMembers];
276 memcpy(Members, members, numMembers*sizeof(Decl*));
277 }
278}
279
280FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
281 if (Members == 0 || NumMembers < 0)
282 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 // linear search. When C++ classes come along, will likely need to revisit.
285 for (int i = 0; i < NumMembers; ++i) {
286 if (Members[i]->getIdentifier() == name)
287 return Members[i];
288 }
289 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000290}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000291
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000292void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000293 unsigned NumParams) {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000294 assert(ParamInfo == 0 && "Already has param info!");
295
296 // Zero params -> null pointer.
297 if (NumParams) {
298 ParamInfo = new ParmVarDecl*[NumParams];
299 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
300 NumMethodParams = NumParams;
301 }
302}
303
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000304ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000305 delete[] ParamInfo;
306}
307
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000308/// ObjCAddInstanceVariablesToClass - Inserts instance variables
309/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000310///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000311void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff60fccee2007-10-29 21:38:07 +0000312 unsigned numIvars,
Steve Narofff908a872007-10-30 02:23:23 +0000313 SourceLocation RBrac) {
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000314 NumIvars = numIvars;
315 if (numIvars) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000316 Ivars = new ObjCIvarDecl*[numIvars];
317 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000318 }
Steve Narofff908a872007-10-30 02:23:23 +0000319 setLocEnd(RBrac);
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000320}
321
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000322/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000323/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000324/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000325///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000326void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
327 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000328 NumIvars = numIvars;
329 if (numIvars) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000330 Ivars = new ObjCIvarDecl*[numIvars];
331 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000332 }
333}
334
Steve Naroff60fccee2007-10-29 21:38:07 +0000335/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000336/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000337///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000338void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000339 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000340 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000341 unsigned numClsMembers,
342 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000343 NumInstanceMethods = numInsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000344 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000345 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
346 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000347 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000348 NumClassMethods = numClsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000349 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000350 ClassMethods = new ObjCMethodDecl*[numClsMembers];
351 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000352 }
Steve Narofff908a872007-10-30 02:23:23 +0000353 AtEndLoc = endLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000354}
355
Steve Naroff60fccee2007-10-29 21:38:07 +0000356/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000357/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000358///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000359void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000360 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000362 unsigned numClsMembers,
Steve Naroff423cb562007-10-30 13:30:57 +0000363 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000364 NumInstanceMethods = numInsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000365 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000366 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
367 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000368 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000369 NumClassMethods = numClsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000370 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000371 ClassMethods = new ObjCMethodDecl*[numClsMembers];
372 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000373 }
Steve Naroff423cb562007-10-30 13:30:57 +0000374 AtEndLoc = endLoc;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000375}
376
Steve Naroff60fccee2007-10-29 21:38:07 +0000377/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000378/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000379///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000380void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000381 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000382 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000383 unsigned numClsMembers,
Steve Naroff423cb562007-10-30 13:30:57 +0000384 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000385 NumInstanceMethods = numInsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000386 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000387 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
388 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000389 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000390 NumClassMethods = numClsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000391 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000392 ClassMethods = new ObjCMethodDecl*[numClsMembers];
393 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000394 }
Steve Naroff423cb562007-10-30 13:30:57 +0000395 AtEndLoc = endLoc;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000396}
397
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000398ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
399 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
400 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroff03300712007-11-12 13:56:41 +0000401 while (ClassDecl != NULL) {
Chris Lattnerbe6df082007-12-12 07:56:42 +0000402 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
403 I != E; ++I) {
404 if ((*I)->getIdentifier() == ID) {
Steve Naroff03300712007-11-12 13:56:41 +0000405 clsDeclared = ClassDecl;
Chris Lattnerbe6df082007-12-12 07:56:42 +0000406 return *I;
Steve Naroff03300712007-11-12 13:56:41 +0000407 }
408 }
409 ClassDecl = ClassDecl->getSuperClass();
410 }
411 return NULL;
412}
413
Chris Lattner0157c512007-12-12 07:30:05 +0000414/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner33ef2592007-12-12 08:17:45 +0000415/// the class, its categories, and its super classes (using a linear search).
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000416ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
417 ObjCInterfaceDecl* ClassDecl = this;
418 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000419
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000420 while (ClassDecl != NULL) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000421 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000422 return MethodDecl;
423
Steve Naroffff1afdb2007-10-14 23:13:51 +0000424 // Didn't find one yet - look through protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000425 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroffff1afdb2007-10-14 23:13:51 +0000426 int numProtocols = ClassDecl->getNumIntfRefProtocols();
427 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000428 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000429 return MethodDecl;
Steve Naroffff1afdb2007-10-14 23:13:51 +0000430 }
Steve Naroff3d581382007-10-14 18:27:41 +0000431 // Didn't find one yet - now look through categories.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000432 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000433 while (CatDecl) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000434 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000435 return MethodDecl;
Steve Naroff3d581382007-10-14 18:27:41 +0000436 CatDecl = CatDecl->getNextClassCategory();
437 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000438 ClassDecl = ClassDecl->getSuperClass();
439 }
440 return NULL;
441}
442
Steve Naroff3d581382007-10-14 18:27:41 +0000443// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner33ef2592007-12-12 08:17:45 +0000444// class, its categories, and its super classes (using a linear search).
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000445ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
446 ObjCInterfaceDecl* ClassDecl = this;
447 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000448
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000449 while (ClassDecl != NULL) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000450 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000451 return MethodDecl;
452
Steve Naroffff1afdb2007-10-14 23:13:51 +0000453 // Didn't find one yet - look through protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000454 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroffff1afdb2007-10-14 23:13:51 +0000455 int numProtocols = ClassDecl->getNumIntfRefProtocols();
456 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000457 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000458 return MethodDecl;
Steve Naroffff1afdb2007-10-14 23:13:51 +0000459 }
Steve Naroff3d581382007-10-14 18:27:41 +0000460 // Didn't find one yet - now look through categories.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000461 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000462 while (CatDecl) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000463 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000464 return MethodDecl;
Steve Naroff3d581382007-10-14 18:27:41 +0000465 CatDecl = CatDecl->getNextClassCategory();
466 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000467 ClassDecl = ClassDecl->getSuperClass();
468 }
469 return NULL;
470}
471
Chris Lattner0157c512007-12-12 07:30:05 +0000472/// lookupInstanceMethod - This method returns an instance method by looking in
473/// the class implementation. Unlike interfaces, we don't look outside the
474/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000475ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattner0157c512007-12-12 07:30:05 +0000476 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
477 if ((*I)->getSelector() == Sel)
478 return *I;
Steve Naroffc43d8682007-11-11 00:10:47 +0000479 return NULL;
480}
481
Chris Lattner0157c512007-12-12 07:30:05 +0000482/// lookupClassMethod - This method returns a class method by looking in
483/// the class implementation. Unlike interfaces, we don't look outside the
484/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000485ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000486 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
487 I != E; ++I)
488 if ((*I)->getSelector() == Sel)
489 return *I;
Steve Naroffc43d8682007-11-11 00:10:47 +0000490 return NULL;
491}
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000492
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000493// lookupInstanceMethod - This method returns an instance 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 *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000497 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
498 if ((*I)->getSelector() == Sel)
499 return *I;
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000500 return NULL;
501}
502
503// lookupClassMethod - This method returns an instance method by looking in
504// the class implementation. Unlike interfaces, we don't look outside the
505// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000506ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000507 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
508 I != E; ++I)
509 if ((*I)->getSelector() == Sel)
510 return *I;
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000511 return NULL;
512}
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000513
514// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
515// it inherited.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000516ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
517 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000518
Steve Naroff94a5c332007-12-19 22:27:04 +0000519 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000520 return MethodDecl;
521
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000522 if (getNumReferencedProtocols() > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000523 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000524
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000525 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000526 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000527 return MethodDecl;
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000528 }
529 }
530 return NULL;
531}
532
533// lookupInstanceMethod - Lookup a class method in the protocol and protocols
534// it inherited.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000535ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
536 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000537
Steve Naroff94a5c332007-12-19 22:27:04 +0000538 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000539 return MethodDecl;
540
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000541 if (getNumReferencedProtocols() > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000542 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000543
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000544 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000545 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000546 return MethodDecl;
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000547 }
548 }
549 return NULL;
550}
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000551
Fariborz Jahanian847794a2008-01-17 01:36:09 +0000552int ObjCMethodDecl::getSynthesizedSelectorSize() const {
553 // syntesized method name is a concatenation of -/+[class-name selector]
554 // Get length of this name.
555 int length = 4; // for '+' or '-', '[', space in between and ']'
556 length += getSelector().getName().size(); // for selector name.
557 length += strlen(getMethodContext()->getName()); // for its class name
558 return length;
559}
560
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000561ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
562 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000563 return ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000565 return CD->getClassInterface();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000566 if (ObjCImplementationDecl *IMD =
567 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000568 return IMD->getClassInterface();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000569 if (ObjCCategoryImplDecl *CID =
570 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000571 return CID->getClassInterface();
572 assert(false && "unknown method context");
573 return 0;
574}