blob: f67d7124f5af95adf212d5a9b35e894a792e1680 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000015#include "clang/AST/DeclObjC.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000016#include "clang/Basic/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017using namespace clang;
18
19// temporary statistics gathering
20static unsigned nFuncs = 0;
21static unsigned nBlockVars = 0;
22static unsigned nFileVars = 0;
23static unsigned nParmVars = 0;
24static unsigned nSUC = 0;
25static unsigned nEnumConst = 0;
26static unsigned nEnumDecls = 0;
27static unsigned nTypedef = 0;
28static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000029static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000030static unsigned nClassDecls = 0;
31static unsigned nMethodDecls = 0;
32static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000033static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000034static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000035static unsigned nIvarDecls = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000036static unsigned nObjCImplementationDecls = 0;
37static unsigned nObjCCategoryImpl = 0;
38static unsigned nObjCCompatibleAlias = 0;
39static unsigned nObjCPropertyDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000040
Chris Lattner4b009652007-07-25 00:24:17 +000041static bool StatSwitch = false;
42
Steve Naroff590aba82007-09-17 14:49:06 +000043const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000044 switch (DeclKind) {
45 default: assert(0 && "Unknown decl kind!");
46 case Typedef:
47 return "Typedef";
48 case Function:
49 return "Function";
Chris Lattner3289bca2007-10-08 21:37:32 +000050 case BlockVar:
51 return "BlockVar";
52 case FileVar:
53 return "FileVar";
54 case ParmVar:
55 return "ParmVar";
Steve Narofff0c31dd2007-09-16 16:16:00 +000056 case EnumConstant:
57 return "EnumConstant";
Ted Kremenek42730c52008-01-07 19:49:32 +000058 case ObjCInterface:
59 return "ObjCInterface";
60 case ObjCClass:
61 return "ObjCClass";
62 case ObjCMethod:
63 return "ObjCMethod";
64 case ObjCProtocol:
65 return "ObjCProtocol";
66 case ObjCForwardProtocol:
67 return "ObjCForwardProtocol";
Steve Narofff0c31dd2007-09-16 16:16:00 +000068 case Struct:
69 return "Struct";
70 case Union:
71 return "Union";
72 case Class:
73 return "Class";
74 case Enum:
75 return "Enum";
76 }
77}
78
Chris Lattner4b009652007-07-25 00:24:17 +000079bool Decl::CollectingStats(bool enable) {
80 if (enable) StatSwitch = true;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +000081 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000082}
83
84void Decl::PrintStats() {
85 fprintf(stderr, "*** Decl Stats:\n");
86 fprintf(stderr, " %d decls total.\n",
87 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff948fd372007-09-17 14:16:13 +000088 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000089 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000090 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
91 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
92 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
93 nBlockVars, (int)sizeof(BlockVarDecl),
94 int(nBlockVars*sizeof(BlockVarDecl)));
95 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
96 nFileVars, (int)sizeof(FileVarDecl),
97 int(nFileVars*sizeof(FileVarDecl)));
98 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
99 nParmVars, (int)sizeof(ParmVarDecl),
100 int(nParmVars*sizeof(ParmVarDecl)));
101 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
102 nFieldDecls, (int)sizeof(FieldDecl),
103 int(nFieldDecls*sizeof(FieldDecl)));
104 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
105 nSUC, (int)sizeof(RecordDecl),
106 int(nSUC*sizeof(RecordDecl)));
107 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
108 nEnumDecls, (int)sizeof(EnumDecl),
109 int(nEnumDecls*sizeof(EnumDecl)));
110 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
111 nEnumConst, (int)sizeof(EnumConstantDecl),
112 int(nEnumConst*sizeof(EnumConstantDecl)));
113 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
114 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000115 // Objective-C decls...
116 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000117 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
118 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000119 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000120 nIvarDecls, (int)sizeof(ObjCIvarDecl),
121 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000122 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000123 nClassDecls, (int)sizeof(ObjCClassDecl),
124 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000125 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000126 nMethodDecls, (int)sizeof(ObjCMethodDecl),
127 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000128 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
130 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000131 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000132 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
133 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000134 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000135 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
136 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000137
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000138 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000139 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
140 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000141
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000142 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000143 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
144 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000145
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000146 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000147 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
148 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000149
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000150 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000151 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
152 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000153
Chris Lattner4b009652007-07-25 00:24:17 +0000154 fprintf(stderr, "Total bytes = %d\n",
155 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
156 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
157 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
158 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Ted Kremenek42730c52008-01-07 19:49:32 +0000159 nTypedef*sizeof(TypedefDecl)) /* FIXME: add ObjC decls */);
Chris Lattner4b009652007-07-25 00:24:17 +0000160}
161
162void Decl::addDeclKind(const Kind k) {
163 switch (k) {
164 case Typedef:
165 nTypedef++;
166 break;
167 case Function:
168 nFuncs++;
169 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000170 case BlockVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000171 nBlockVars++;
172 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000173 case FileVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000174 nFileVars++;
175 break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000176 case ParmVar:
Chris Lattner4b009652007-07-25 00:24:17 +0000177 nParmVars++;
178 break;
179 case EnumConstant:
180 nEnumConst++;
181 break;
182 case Field:
183 nFieldDecls++;
184 break;
185 case Struct:
186 case Union:
187 case Class:
188 nSUC++;
189 break;
190 case Enum:
191 nEnumDecls++;
192 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000193 case ObjCInterface:
Steve Naroff81f1bba2007-09-06 21:24:23 +0000194 nInterfaceDecls++;
195 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000196 case ObjCClass:
Steve Naroff948fd372007-09-17 14:16:13 +0000197 nClassDecls++;
198 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000199 case ObjCMethod:
Steve Naroff948fd372007-09-17 14:16:13 +0000200 nMethodDecls++;
201 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000202 case ObjCProtocol:
Steve Naroff948fd372007-09-17 14:16:13 +0000203 nProtocolDecls++;
204 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000205 case ObjCForwardProtocol:
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000206 nForwardProtocolDecls++;
207 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000208 case ObjCCategory:
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000209 nCategoryDecls++;
210 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000211 case ObjCIvar:
Steve Naroff948fd372007-09-17 14:16:13 +0000212 nIvarDecls++;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000213 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000214 case ObjCImplementation:
215 nObjCImplementationDecls++;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000216 break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000217 case ObjCCategoryImpl:
218 nObjCCategoryImpl++;
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000219 break;
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000220 case CompatibleAlias:
Ted Kremenek42730c52008-01-07 19:49:32 +0000221 nObjCCompatibleAlias++;
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000222 break;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000223 case PropertyDecl:
Ted Kremenek42730c52008-01-07 19:49:32 +0000224 nObjCPropertyDecl++;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000225 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000226 }
227}
228
229// Out-of-line virtual method providing a home for Decl.
230Decl::~Decl() {
231}
232
Chris Lattner910435b2007-10-06 22:53:46 +0000233const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000234 if (const IdentifierInfo *II = getIdentifier())
235 return II->getName();
236 return "";
237}
238
239
240FunctionDecl::~FunctionDecl() {
241 delete[] ParamInfo;
242}
243
244unsigned FunctionDecl::getNumParams() const {
Chris Lattner23d411b2007-12-06 17:20:20 +0000245 if (isa<FunctionTypeNoProto>(getCanonicalType())) return 0;
246 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000247}
248
249void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
250 assert(ParamInfo == 0 && "Already has param info!");
251 assert(NumParams == getNumParams() && "Parameter count mismatch!");
252
253 // Zero params -> null pointer.
254 if (NumParams) {
255 ParamInfo = new ParmVarDecl*[NumParams];
256 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
257 }
258}
259
260
261/// defineBody - When created, RecordDecl's correspond to a forward declared
262/// record. This method is used to mark the decl as being defined, with the
263/// specified contents.
264void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
265 assert(!isDefinition() && "Cannot redefine record!");
266 setDefinition(true);
267 NumMembers = numMembers;
268 if (numMembers) {
269 Members = new FieldDecl*[numMembers];
270 memcpy(Members, members, numMembers*sizeof(Decl*));
271 }
272}
273
274FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
275 if (Members == 0 || NumMembers < 0)
276 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000277
Chris Lattner4b009652007-07-25 00:24:17 +0000278 // linear search. When C++ classes come along, will likely need to revisit.
279 for (int i = 0; i < NumMembers; ++i) {
280 if (Members[i]->getIdentifier() == name)
281 return Members[i];
282 }
283 return 0;
284}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000285
Ted Kremenek42730c52008-01-07 19:49:32 +0000286void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian84082af2007-10-04 17:06:28 +0000287 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000288 assert(ParamInfo == 0 && "Already has param info!");
289
290 // Zero params -> null pointer.
291 if (NumParams) {
292 ParamInfo = new ParmVarDecl*[NumParams];
293 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
294 NumMethodParams = NumParams;
295 }
296}
297
Ted Kremenek42730c52008-01-07 19:49:32 +0000298ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000299 delete[] ParamInfo;
300}
301
Ted Kremenek42730c52008-01-07 19:49:32 +0000302/// ObjCAddInstanceVariablesToClass - Inserts instance variables
303/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000304///
Ted Kremenek42730c52008-01-07 19:49:32 +0000305void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000306 unsigned numIvars,
Steve Naroffef20ed32007-10-30 02:23:23 +0000307 SourceLocation RBrac) {
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000308 NumIvars = numIvars;
309 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000310 Ivars = new ObjCIvarDecl*[numIvars];
311 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000312 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000313 setLocEnd(RBrac);
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000314}
315
Ted Kremenek42730c52008-01-07 19:49:32 +0000316/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000317/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000318/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000319///
Ted Kremenek42730c52008-01-07 19:49:32 +0000320void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
321 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000322 NumIvars = numIvars;
323 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000324 Ivars = new ObjCIvarDecl*[numIvars];
325 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000326 }
327}
328
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000329/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000330/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000331///
Ted Kremenek42730c52008-01-07 19:49:32 +0000332void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000333 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000334 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000335 unsigned numClsMembers,
336 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000337 NumInstanceMethods = numInsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000338 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000339 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
340 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000341 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000342 NumClassMethods = numClsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000343 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000344 ClassMethods = new ObjCMethodDecl*[numClsMembers];
345 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000346 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000347 AtEndLoc = endLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000348}
349
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000350/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000351/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000352///
Ted Kremenek42730c52008-01-07 19:49:32 +0000353void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000354 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000355 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000356 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000357 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000358 NumInstanceMethods = numInsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000359 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000360 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
361 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000362 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000363 NumClassMethods = numClsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000364 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000365 ClassMethods = new ObjCMethodDecl*[numClsMembers];
366 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000367 }
Steve Naroff667f1682007-10-30 13:30:57 +0000368 AtEndLoc = endLoc;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000369}
370
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000371/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000372/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000373///
Ted Kremenek42730c52008-01-07 19:49:32 +0000374void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000375 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000376 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000377 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000378 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000379 NumInstanceMethods = numInsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000380 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000381 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
382 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000383 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000384 NumClassMethods = numClsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000385 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000386 ClassMethods = new ObjCMethodDecl*[numClsMembers];
387 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000388 }
Steve Naroff667f1682007-10-30 13:30:57 +0000389 AtEndLoc = endLoc;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000390}
391
Ted Kremenek42730c52008-01-07 19:49:32 +0000392ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
393 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
394 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000395 while (ClassDecl != NULL) {
Chris Lattnerc7b06752007-12-12 07:56:42 +0000396 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
397 I != E; ++I) {
398 if ((*I)->getIdentifier() == ID) {
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000399 clsDeclared = ClassDecl;
Chris Lattnerc7b06752007-12-12 07:56:42 +0000400 return *I;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000401 }
402 }
403 ClassDecl = ClassDecl->getSuperClass();
404 }
405 return NULL;
406}
407
Chris Lattnerac9ab532007-12-12 07:30:05 +0000408/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner73e795c2007-12-12 08:17:45 +0000409/// the class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000410ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
411 ObjCInterfaceDecl* ClassDecl = this;
412 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000413
Steve Narofffa465d12007-10-02 20:01:56 +0000414 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000415 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000416 return MethodDecl;
417
Steve Naroff705380b2007-10-14 23:13:51 +0000418 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000419 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000420 int numProtocols = ClassDecl->getNumIntfRefProtocols();
421 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000422 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000423 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000424 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000425 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000426 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000427 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000428 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000429 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000430 CatDecl = CatDecl->getNextClassCategory();
431 }
Steve Narofffa465d12007-10-02 20:01:56 +0000432 ClassDecl = ClassDecl->getSuperClass();
433 }
434 return NULL;
435}
436
Steve Narofff66d84a2007-10-14 18:27:41 +0000437// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner73e795c2007-12-12 08:17:45 +0000438// class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000439ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
440 ObjCInterfaceDecl* ClassDecl = this;
441 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000442
Steve Narofffa465d12007-10-02 20:01:56 +0000443 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000444 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000445 return MethodDecl;
446
Steve Naroff705380b2007-10-14 23:13:51 +0000447 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000448 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000449 int numProtocols = ClassDecl->getNumIntfRefProtocols();
450 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000451 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000452 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000453 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000454 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000455 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000456 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000457 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000458 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000459 CatDecl = CatDecl->getNextClassCategory();
460 }
Steve Narofffa465d12007-10-02 20:01:56 +0000461 ClassDecl = ClassDecl->getSuperClass();
462 }
463 return NULL;
464}
465
Chris Lattnerac9ab532007-12-12 07:30:05 +0000466/// lookupInstanceMethod - This method returns an instance method by looking in
467/// the class implementation. Unlike interfaces, we don't look outside the
468/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000469ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerac9ab532007-12-12 07:30:05 +0000470 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
471 if ((*I)->getSelector() == Sel)
472 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000473 return NULL;
474}
475
Chris Lattnerac9ab532007-12-12 07:30:05 +0000476/// lookupClassMethod - This method returns a class method by looking in
477/// the class implementation. Unlike interfaces, we don't look outside the
478/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000479ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000480 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
481 I != E; ++I)
482 if ((*I)->getSelector() == Sel)
483 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000484 return NULL;
485}
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000486
Steve Naroff31233632007-11-12 22:05:31 +0000487// lookupInstanceMethod - This method returns an instance method by looking in
488// the class implementation. Unlike interfaces, we don't look outside the
489// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000490ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000491 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
492 if ((*I)->getSelector() == Sel)
493 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000494 return NULL;
495}
496
497// lookupClassMethod - This method returns an instance method by looking in
498// the class implementation. Unlike interfaces, we don't look outside the
499// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000500ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000501 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
502 I != E; ++I)
503 if ((*I)->getSelector() == Sel)
504 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000505 return NULL;
506}
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000507
508// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
509// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000510ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
511 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000512
Steve Naroff74273de2007-12-19 22:27:04 +0000513 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000514 return MethodDecl;
515
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000516 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000517 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000518
Fariborz Jahanian87829072007-12-20 19:24:10 +0000519 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000520 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000521 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000522 }
523 }
524 return NULL;
525}
526
527// lookupInstanceMethod - Lookup a class method in the protocol and protocols
528// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000529ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
530 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000531
Steve Naroff74273de2007-12-19 22:27:04 +0000532 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000533 return MethodDecl;
534
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000535 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000536 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000537
Fariborz Jahanian87829072007-12-20 19:24:10 +0000538 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000539 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000540 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000541 }
542 }
543 return NULL;
544}
Steve Naroff2ce399a2007-12-14 23:37:57 +0000545
Ted Kremenek42730c52008-01-07 19:49:32 +0000546ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
547 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000548 return ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000549 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000550 return CD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000551 if (ObjCImplementationDecl *IMD =
552 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000553 return IMD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000554 if (ObjCCategoryImplDecl *CID =
555 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000556 return CID->getClassInterface();
557 assert(false && "unknown method context");
558 return 0;
559}