blob: 4b464ee5e6e0e9480e7c2abe5ebe1c365d37c00c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
15#include "clang/Lex/IdentifierTable.h"
16using namespace clang;
17
18// temporary statistics gathering
19static unsigned nFuncs = 0;
20static unsigned nBlockVars = 0;
21static unsigned nFileVars = 0;
22static unsigned nParmVars = 0;
23static unsigned nSUC = 0;
24static unsigned nEnumConst = 0;
25static unsigned nEnumDecls = 0;
26static unsigned nTypedef = 0;
27static unsigned nFieldDecls = 0;
Steve Naroff3536b442007-09-06 21:24:23 +000028static unsigned nInterfaceDecls = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000029static bool StatSwitch = false;
30
Steve Naroff8c9f13e2007-09-16 16:16:00 +000031const char *Decl::getDeclKindName() {
32 switch (DeclKind) {
33 default: assert(0 && "Unknown decl kind!");
34 case Typedef:
35 return "Typedef";
36 case Function:
37 return "Function";
38 case BlockVariable:
39 return "BlockVariable";
40 case FileVariable:
41 return "FileVariable";
42 case ParmVariable:
43 return "ParmVariable";
44 case EnumConstant:
45 return "EnumConstant";
46 case ObjcInterface:
47 return "ObjcInterface";
48 case ObjcClass:
49 return "ObjcClass";
50 case ObjcMethod:
51 return "ObjcMethod";
52 case ObjcProtoMethod:
53 return "ObjcProtoMethod";
54 case ObjcProtocol:
55 return "ObjcProtocol";
56 case Struct:
57 return "Struct";
58 case Union:
59 return "Union";
60 case Class:
61 return "Class";
62 case Enum:
63 return "Enum";
64 }
65}
66
Reid Spencer5f016e22007-07-11 17:01:13 +000067bool Decl::CollectingStats(bool enable) {
68 if (enable) StatSwitch = true;
69 return StatSwitch;
70}
71
72void Decl::PrintStats() {
73 fprintf(stderr, "*** Decl Stats:\n");
74 fprintf(stderr, " %d decls total.\n",
75 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
76 nEnumDecls+nEnumConst+nTypedef));
77 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
78 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
79 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
80 nBlockVars, (int)sizeof(BlockVarDecl),
81 int(nBlockVars*sizeof(BlockVarDecl)));
82 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
83 nFileVars, (int)sizeof(FileVarDecl),
84 int(nFileVars*sizeof(FileVarDecl)));
85 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
86 nParmVars, (int)sizeof(ParmVarDecl),
87 int(nParmVars*sizeof(ParmVarDecl)));
88 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
89 nFieldDecls, (int)sizeof(FieldDecl),
90 int(nFieldDecls*sizeof(FieldDecl)));
91 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
92 nSUC, (int)sizeof(RecordDecl),
93 int(nSUC*sizeof(RecordDecl)));
94 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
95 nEnumDecls, (int)sizeof(EnumDecl),
96 int(nEnumDecls*sizeof(EnumDecl)));
97 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
98 nEnumConst, (int)sizeof(EnumConstantDecl),
99 int(nEnumConst*sizeof(EnumConstantDecl)));
100 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
101 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
102 fprintf(stderr, "Total bytes = %d\n",
103 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
104 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
105 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
106 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
107 nTypedef*sizeof(TypedefDecl)));
108}
109
110void Decl::addDeclKind(const Kind k) {
111 switch (k) {
112 case Typedef:
113 nTypedef++;
114 break;
115 case Function:
116 nFuncs++;
117 break;
118 case BlockVariable:
119 nBlockVars++;
120 break;
121 case FileVariable:
122 nFileVars++;
123 break;
124 case ParmVariable:
125 nParmVars++;
126 break;
127 case EnumConstant:
128 nEnumConst++;
129 break;
130 case Field:
131 nFieldDecls++;
132 break;
133 case Struct:
134 case Union:
135 case Class:
136 nSUC++;
137 break;
138 case Enum:
139 nEnumDecls++;
140 break;
Steve Naroff3536b442007-09-06 21:24:23 +0000141 case ObjcInterface:
142 nInterfaceDecls++;
143 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 }
145}
146
147// Out-of-line virtual method providing a home for Decl.
148Decl::~Decl() {
149}
150
Steve Naroff8e74c932007-09-13 21:41:19 +0000151const char *FieldDecl::getName() const {
152 if (const IdentifierInfo *II = getIdentifier())
153 return II->getName();
154 return "";
155}
156
157const char *ScopedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 if (const IdentifierInfo *II = getIdentifier())
159 return II->getName();
160 return "";
161}
162
163
164FunctionDecl::~FunctionDecl() {
165 delete[] ParamInfo;
166}
167
168unsigned FunctionDecl::getNumParams() const {
169 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
170}
171
172void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
173 assert(ParamInfo == 0 && "Already has param info!");
174 assert(NumParams == getNumParams() && "Parameter count mismatch!");
175
176 // Zero params -> null pointer.
177 if (NumParams) {
178 ParamInfo = new ParmVarDecl*[NumParams];
179 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
180 }
181}
182
183
184/// defineBody - When created, RecordDecl's correspond to a forward declared
185/// record. This method is used to mark the decl as being defined, with the
186/// specified contents.
187void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
188 assert(!isDefinition() && "Cannot redefine record!");
189 setDefinition(true);
190 NumMembers = numMembers;
191 if (numMembers) {
192 Members = new FieldDecl*[numMembers];
193 memcpy(Members, members, numMembers*sizeof(Decl*));
194 }
195}
196
197FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
198 if (Members == 0 || NumMembers < 0)
199 return 0;
200
201 // linear search. When C++ classes come along, will likely need to revisit.
202 for (int i = 0; i < NumMembers; ++i) {
203 if (Members[i]->getIdentifier() == name)
204 return Members[i];
205 }
206 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000207}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000208
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000209void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
210 unsigned NumParams) {
211 assert(ParamInfo == 0 && "Already has param info!");
212
213 // Zero params -> null pointer.
214 if (NumParams) {
215 ParamInfo = new ParmVarDecl*[NumParams];
216 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
217 NumMethodParams = NumParams;
218 }
219}
220
221ObjcMethodDecl::~ObjcMethodDecl() {
222 delete[] ParamInfo;
223}
224
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000225/// ObjcAddInstanceVariablesToClass - Inserts instance variables
226/// into ObjcInterfaceDecl's fields.
227///
228void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
229 unsigned numIvars) {
230 NumIvars = numIvars;
231 if (numIvars) {
232 Ivars = new ObjcIvarDecl*[numIvars];
233 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
234 }
235}
236
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000237/// addObjcMethods - Insert instance and methods declarations into
238/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
239///
240void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
241 unsigned numInsMembers,
242 ObjcMethodDecl **clsMethods,
243 unsigned numClsMembers) {
244 NumInsMethods = numInsMembers;
245 if (numInsMembers) {
246 InsMethods = new ObjcMethodDecl*[numInsMembers];
247 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
248 }
249 NumClsMethods = numClsMembers;
250 if (numClsMembers) {
251 ClsMethods = new ObjcMethodDecl*[numClsMembers];
252 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
253 }
254}
255