blob: 04d0d3cac2b2536932b7dfd6e870e9e12acaaca7 [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"
Anders Carlssonf78915f2008-02-15 07:04:12 +000016#include "clang/AST/Attr.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Anders Carlssonb0dd2682008-02-15 23:30:50 +000018#include "llvm/ADT/DenseMap.h"
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
22// temporary statistics gathering
23static unsigned nFuncs = 0;
24static unsigned nBlockVars = 0;
25static unsigned nFileVars = 0;
26static unsigned nParmVars = 0;
27static unsigned nSUC = 0;
28static unsigned nEnumConst = 0;
29static unsigned nEnumDecls = 0;
30static unsigned nTypedef = 0;
31static unsigned nFieldDecls = 0;
Steve Naroff3536b442007-09-06 21:24:23 +000032static unsigned nInterfaceDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000033static unsigned nClassDecls = 0;
34static unsigned nMethodDecls = 0;
35static unsigned nProtocolDecls = 0;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000036static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000037static unsigned nCategoryDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000038static unsigned nIvarDecls = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000039static unsigned nObjCImplementationDecls = 0;
40static unsigned nObjCCategoryImpl = 0;
41static unsigned nObjCCompatibleAlias = 0;
42static unsigned nObjCPropertyDecl = 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +000043static unsigned nLinkageSpecDecl = 0;
Anders Carlssondfab6cb2008-02-08 00:33:21 +000044static unsigned nFileScopeAsmDecl = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000045
Reid Spencer5f016e22007-07-11 17:01:13 +000046static bool StatSwitch = false;
47
Anders Carlssonb0dd2682008-02-15 23:30:50 +000048// This keeps track of all decl attributes. Since so few decls have attrs, we
49// keep them in a hash map instead of wasting space in the Decl class.
50typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
51
52static DeclAttrMapTy *DeclAttrs = 0;
53
Steve Naroffe5ea3802007-09-17 14:49:06 +000054const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000055 switch (DeclKind) {
56 default: assert(0 && "Unknown decl kind!");
57 case Typedef:
58 return "Typedef";
59 case Function:
60 return "Function";
Chris Lattneraa9fc462007-10-08 21:37:32 +000061 case BlockVar:
62 return "BlockVar";
63 case FileVar:
64 return "FileVar";
65 case ParmVar:
66 return "ParmVar";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000067 case EnumConstant:
68 return "EnumConstant";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000069 case ObjCInterface:
70 return "ObjCInterface";
71 case ObjCClass:
72 return "ObjCClass";
73 case ObjCMethod:
74 return "ObjCMethod";
75 case ObjCProtocol:
76 return "ObjCProtocol";
77 case ObjCForwardProtocol:
78 return "ObjCForwardProtocol";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000079 case Struct:
80 return "Struct";
81 case Union:
82 return "Union";
83 case Class:
84 return "Class";
85 case Enum:
86 return "Enum";
87 }
88}
89
Reid Spencer5f016e22007-07-11 17:01:13 +000090bool Decl::CollectingStats(bool enable) {
91 if (enable) StatSwitch = true;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +000092 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
95void Decl::PrintStats() {
96 fprintf(stderr, "*** Decl Stats:\n");
97 fprintf(stderr, " %d decls total.\n",
98 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff3f128ad2007-09-17 14:16:13 +000099 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000100 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
102 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
103 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
104 nBlockVars, (int)sizeof(BlockVarDecl),
105 int(nBlockVars*sizeof(BlockVarDecl)));
106 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
107 nFileVars, (int)sizeof(FileVarDecl),
108 int(nFileVars*sizeof(FileVarDecl)));
109 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
110 nParmVars, (int)sizeof(ParmVarDecl),
111 int(nParmVars*sizeof(ParmVarDecl)));
112 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
113 nFieldDecls, (int)sizeof(FieldDecl),
114 int(nFieldDecls*sizeof(FieldDecl)));
115 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
116 nSUC, (int)sizeof(RecordDecl),
117 int(nSUC*sizeof(RecordDecl)));
118 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
119 nEnumDecls, (int)sizeof(EnumDecl),
120 int(nEnumDecls*sizeof(EnumDecl)));
121 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
122 nEnumConst, (int)sizeof(EnumConstantDecl),
123 int(nEnumConst*sizeof(EnumConstantDecl)));
124 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
125 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000126 // Objective-C decls...
127 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000128 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
129 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000130 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000131 nIvarDecls, (int)sizeof(ObjCIvarDecl),
132 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000133 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000134 nClassDecls, (int)sizeof(ObjCClassDecl),
135 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000136 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000137 nMethodDecls, (int)sizeof(ObjCMethodDecl),
138 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000139 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000140 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
141 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000142 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000143 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
144 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000145 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000146 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
147 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000148
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000149 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000150 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
151 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000152
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000153 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000154 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
155 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000156
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000157 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000158 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
159 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000160
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000161 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000162 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
163 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 fprintf(stderr, "Total bytes = %d\n",
166 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
167 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
168 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
169 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000170 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000171 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
172 nIvarDecls*sizeof(ObjCIvarDecl)+
173 nClassDecls*sizeof(ObjCClassDecl)+
174 nMethodDecls*sizeof(ObjCMethodDecl)+
175 nProtocolDecls*sizeof(ObjCProtocolDecl)+
176 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
177 nCategoryDecls*sizeof(ObjCCategoryDecl)+
178 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
179 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
180 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
181 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000182 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
183 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185}
186
187void Decl::addDeclKind(const Kind k) {
188 switch (k) {
189 case Typedef:
190 nTypedef++;
191 break;
192 case Function:
193 nFuncs++;
194 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000195 case BlockVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 nBlockVars++;
197 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000198 case FileVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 nFileVars++;
200 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000201 case ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 nParmVars++;
203 break;
204 case EnumConstant:
205 nEnumConst++;
206 break;
207 case Field:
208 nFieldDecls++;
209 break;
210 case Struct:
211 case Union:
212 case Class:
213 nSUC++;
214 break;
215 case Enum:
216 nEnumDecls++;
217 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000218 case ObjCInterface:
Steve Naroff3536b442007-09-06 21:24:23 +0000219 nInterfaceDecls++;
220 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000221 case ObjCClass:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000222 nClassDecls++;
223 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000224 case ObjCMethod:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000225 nMethodDecls++;
226 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227 case ObjCProtocol:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000228 nProtocolDecls++;
229 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000230 case ObjCForwardProtocol:
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000231 nForwardProtocolDecls++;
232 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000233 case ObjCCategory:
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000234 nCategoryDecls++;
235 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000236 case ObjCIvar:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000237 nIvarDecls++;
Chris Lattner7341c332007-09-16 19:23:04 +0000238 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000239 case ObjCImplementation:
240 nObjCImplementationDecls++;
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000241 break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000242 case ObjCCategoryImpl:
243 nObjCCategoryImpl++;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000244 break;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000245 case CompatibleAlias:
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000246 nObjCCompatibleAlias++;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000247 break;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000248 case PropertyDecl:
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000249 nObjCPropertyDecl++;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000250 break;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000251 case LinkageSpec:
252 nLinkageSpecDecl++;
253 break;
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000254 case FileScopeAsm:
255 nFileScopeAsmDecl++;
256 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 }
258}
259
260// Out-of-line virtual method providing a home for Decl.
261Decl::~Decl() {
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000262 if (!DeclAttrs)
263 return;
264
265 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
266 if (it != DeclAttrs->end()) {
267 delete it->second;
268 DeclAttrs->erase(it);
269 if (DeclAttrs->empty()) {
270 delete DeclAttrs;
271 DeclAttrs = 0;
272 }
273 }
274}
275
276void Decl::addAttr(Attr *newattr)
277{
278 if (!DeclAttrs)
279 DeclAttrs = new llvm::DenseMap<const Decl*, Attr*>;
280
281 Attr *&attr = DeclAttrs->FindAndConstruct(this).second;
282
283 newattr->setNext(attr);
284 attr = newattr;
285
286 HasAttrs = true;
287}
288
289const Attr *Decl::getAttrs() const
290{
291 if (!HasAttrs || !DeclAttrs)
292 return 0;
293
294 return DeclAttrs->find(this)->second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000295}
296
Chris Lattnerfd5de472007-10-06 22:53:46 +0000297const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 if (const IdentifierInfo *II = getIdentifier())
299 return II->getName();
300 return "";
301}
302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303FunctionDecl::~FunctionDecl() {
304 delete[] ParamInfo;
305}
306
307unsigned FunctionDecl::getNumParams() const {
Chris Lattnerec584d62007-12-06 17:20:20 +0000308 if (isa<FunctionTypeNoProto>(getCanonicalType())) return 0;
309 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000310}
311
312void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
313 assert(ParamInfo == 0 && "Already has param info!");
314 assert(NumParams == getNumParams() && "Parameter count mismatch!");
315
316 // Zero params -> null pointer.
317 if (NumParams) {
318 ParamInfo = new ParmVarDecl*[NumParams];
319 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
320 }
321}
322
323
324/// defineBody - When created, RecordDecl's correspond to a forward declared
325/// record. This method is used to mark the decl as being defined, with the
326/// specified contents.
327void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
328 assert(!isDefinition() && "Cannot redefine record!");
329 setDefinition(true);
330 NumMembers = numMembers;
331 if (numMembers) {
332 Members = new FieldDecl*[numMembers];
333 memcpy(Members, members, numMembers*sizeof(Decl*));
334 }
335}
336
337FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
338 if (Members == 0 || NumMembers < 0)
339 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // linear search. When C++ classes come along, will likely need to revisit.
342 for (int i = 0; i < NumMembers; ++i) {
343 if (Members[i]->getIdentifier() == name)
344 return Members[i];
345 }
346 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000347}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000348
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000349void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000350 unsigned NumParams) {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000351 assert(ParamInfo == 0 && "Already has param info!");
352
353 // Zero params -> null pointer.
354 if (NumParams) {
355 ParamInfo = new ParmVarDecl*[NumParams];
356 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
357 NumMethodParams = NumParams;
358 }
359}
360
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000362 delete[] ParamInfo;
363}
364
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000365/// ObjCAddInstanceVariablesToClass - Inserts instance variables
366/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000367///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000368void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff60fccee2007-10-29 21:38:07 +0000369 unsigned numIvars,
Steve Narofff908a872007-10-30 02:23:23 +0000370 SourceLocation RBrac) {
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000371 NumIvars = numIvars;
372 if (numIvars) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000373 Ivars = new ObjCIvarDecl*[numIvars];
374 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000375 }
Steve Narofff908a872007-10-30 02:23:23 +0000376 setLocEnd(RBrac);
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000377}
378
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000379/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000380/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000381/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000382///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000383void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
384 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000385 NumIvars = numIvars;
386 if (numIvars) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000387 Ivars = new ObjCIvarDecl*[numIvars];
388 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000389 }
390}
391
Steve Naroff60fccee2007-10-29 21:38:07 +0000392/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000393/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000394///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000395void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000396 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000397 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000398 unsigned numClsMembers,
399 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000400 NumInstanceMethods = numInsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000401 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
403 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000404 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000405 NumClassMethods = numClsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000406 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000407 ClassMethods = new ObjCMethodDecl*[numClsMembers];
408 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000409 }
Steve Narofff908a872007-10-30 02:23:23 +0000410 AtEndLoc = endLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000411}
412
Steve Naroff60fccee2007-10-29 21:38:07 +0000413/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000414/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000415///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000416void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000417 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000419 unsigned numClsMembers,
Steve Naroff423cb562007-10-30 13:30:57 +0000420 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000421 NumInstanceMethods = numInsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000422 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000423 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
424 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000425 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000426 NumClassMethods = numClsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000427 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000428 ClassMethods = new ObjCMethodDecl*[numClsMembers];
429 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000430 }
Steve Naroff423cb562007-10-30 13:30:57 +0000431 AtEndLoc = endLoc;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000432}
433
Steve Naroff60fccee2007-10-29 21:38:07 +0000434/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000435/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000436///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000437void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000438 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000439 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000440 unsigned numClsMembers,
Steve Naroff423cb562007-10-30 13:30:57 +0000441 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000442 NumInstanceMethods = numInsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000443 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000444 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
445 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000446 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000447 NumClassMethods = numClsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000448 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000449 ClassMethods = new ObjCMethodDecl*[numClsMembers];
450 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000451 }
Steve Naroff423cb562007-10-30 13:30:57 +0000452 AtEndLoc = endLoc;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000453}
454
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
456 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
457 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroff03300712007-11-12 13:56:41 +0000458 while (ClassDecl != NULL) {
Chris Lattnerbe6df082007-12-12 07:56:42 +0000459 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
460 I != E; ++I) {
461 if ((*I)->getIdentifier() == ID) {
Steve Naroff03300712007-11-12 13:56:41 +0000462 clsDeclared = ClassDecl;
Chris Lattnerbe6df082007-12-12 07:56:42 +0000463 return *I;
Steve Naroff03300712007-11-12 13:56:41 +0000464 }
465 }
466 ClassDecl = ClassDecl->getSuperClass();
467 }
468 return NULL;
469}
470
Chris Lattner0157c512007-12-12 07:30:05 +0000471/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner33ef2592007-12-12 08:17:45 +0000472/// the class, its categories, and its super classes (using a linear search).
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000473ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
474 ObjCInterfaceDecl* ClassDecl = this;
475 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000476
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000477 while (ClassDecl != NULL) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000478 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000479 return MethodDecl;
480
Steve Naroffff1afdb2007-10-14 23:13:51 +0000481 // Didn't find one yet - look through protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000482 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroffff1afdb2007-10-14 23:13:51 +0000483 int numProtocols = ClassDecl->getNumIntfRefProtocols();
484 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000485 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000486 return MethodDecl;
Steve Naroffff1afdb2007-10-14 23:13:51 +0000487 }
Steve Naroff3d581382007-10-14 18:27:41 +0000488 // Didn't find one yet - now look through categories.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000489 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000490 while (CatDecl) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000491 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000492 return MethodDecl;
Steve Naroff3d581382007-10-14 18:27:41 +0000493 CatDecl = CatDecl->getNextClassCategory();
494 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000495 ClassDecl = ClassDecl->getSuperClass();
496 }
497 return NULL;
498}
499
Steve Naroff3d581382007-10-14 18:27:41 +0000500// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner33ef2592007-12-12 08:17:45 +0000501// class, its categories, and its super classes (using a linear search).
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000502ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
503 ObjCInterfaceDecl* ClassDecl = this;
504 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000505
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000506 while (ClassDecl != NULL) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000507 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000508 return MethodDecl;
509
Steve Naroffff1afdb2007-10-14 23:13:51 +0000510 // Didn't find one yet - look through protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000511 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroffff1afdb2007-10-14 23:13:51 +0000512 int numProtocols = ClassDecl->getNumIntfRefProtocols();
513 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000514 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000515 return MethodDecl;
Steve Naroffff1afdb2007-10-14 23:13:51 +0000516 }
Steve Naroff3d581382007-10-14 18:27:41 +0000517 // Didn't find one yet - now look through categories.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000518 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000519 while (CatDecl) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000520 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000521 return MethodDecl;
Steve Naroff3d581382007-10-14 18:27:41 +0000522 CatDecl = CatDecl->getNextClassCategory();
523 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000524 ClassDecl = ClassDecl->getSuperClass();
525 }
526 return NULL;
527}
528
Chris Lattner0157c512007-12-12 07:30:05 +0000529/// lookupInstanceMethod - This method returns an instance method by looking in
530/// the class implementation. Unlike interfaces, we don't look outside the
531/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000532ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattner0157c512007-12-12 07:30:05 +0000533 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
534 if ((*I)->getSelector() == Sel)
535 return *I;
Steve Naroffc43d8682007-11-11 00:10:47 +0000536 return NULL;
537}
538
Chris Lattner0157c512007-12-12 07:30:05 +0000539/// lookupClassMethod - This method returns a class method by looking in
540/// the class implementation. Unlike interfaces, we don't look outside the
541/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000542ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000543 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
544 I != E; ++I)
545 if ((*I)->getSelector() == Sel)
546 return *I;
Steve Naroffc43d8682007-11-11 00:10:47 +0000547 return NULL;
548}
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000549
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000550// lookupInstanceMethod - This method returns an instance method by looking in
551// the class implementation. Unlike interfaces, we don't look outside the
552// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000553ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000554 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
555 if ((*I)->getSelector() == Sel)
556 return *I;
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000557 return NULL;
558}
559
560// lookupClassMethod - This method returns an instance method by looking in
561// the class implementation. Unlike interfaces, we don't look outside the
562// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000563ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000564 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
565 I != E; ++I)
566 if ((*I)->getSelector() == Sel)
567 return *I;
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000568 return NULL;
569}
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000570
571// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
572// it inherited.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000573ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
574 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000575
Steve Naroff94a5c332007-12-19 22:27:04 +0000576 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000577 return MethodDecl;
578
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000579 if (getNumReferencedProtocols() > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000580 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000581
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000582 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000583 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000584 return MethodDecl;
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000585 }
586 }
587 return NULL;
588}
589
590// lookupInstanceMethod - Lookup a class method in the protocol and protocols
591// it inherited.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
593 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000594
Steve Naroff94a5c332007-12-19 22:27:04 +0000595 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000596 return MethodDecl;
597
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000598 if (getNumReferencedProtocols() > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000599 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000600
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000601 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000602 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000603 return MethodDecl;
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000604 }
605 }
606 return NULL;
607}
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000608
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000609/// getSynthesizedMethodSize - Compute size of synthesized method name
610/// as done be the rewrite.
611///
612unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
Fariborz Jahanian847794a2008-01-17 01:36:09 +0000613 // syntesized method name is a concatenation of -/+[class-name selector]
614 // Get length of this name.
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000615 unsigned length = 3; // _I_ or _C_
616 length += strlen(getClassInterface()->getName()) +1; // extra for _
617 NamedDecl *MethodContext = getMethodContext();
618 if (ObjCCategoryImplDecl *CID =
619 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
620 length += strlen(CID->getName()) +1;
621 length += getSelector().getName().size(); // selector name
Fariborz Jahanian847794a2008-01-17 01:36:09 +0000622 return length;
623}
624
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
626 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000627 return ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000628 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000629 return CD->getClassInterface();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630 if (ObjCImplementationDecl *IMD =
631 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000632 return IMD->getClassInterface();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633 if (ObjCCategoryImplDecl *CID =
634 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000635 return CID->getClassInterface();
636 assert(false && "unknown method context");
637 return 0;
638}