blob: e84532923be6c157574e8aaf3fb949ffd13e6ee3 [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
31bool Decl::CollectingStats(bool enable) {
32 if (enable) StatSwitch = true;
33 return StatSwitch;
34}
35
36void Decl::PrintStats() {
37 fprintf(stderr, "*** Decl Stats:\n");
38 fprintf(stderr, " %d decls total.\n",
39 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
40 nEnumDecls+nEnumConst+nTypedef));
41 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
42 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
43 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
44 nBlockVars, (int)sizeof(BlockVarDecl),
45 int(nBlockVars*sizeof(BlockVarDecl)));
46 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
47 nFileVars, (int)sizeof(FileVarDecl),
48 int(nFileVars*sizeof(FileVarDecl)));
49 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
50 nParmVars, (int)sizeof(ParmVarDecl),
51 int(nParmVars*sizeof(ParmVarDecl)));
52 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
53 nFieldDecls, (int)sizeof(FieldDecl),
54 int(nFieldDecls*sizeof(FieldDecl)));
55 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
56 nSUC, (int)sizeof(RecordDecl),
57 int(nSUC*sizeof(RecordDecl)));
58 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
59 nEnumDecls, (int)sizeof(EnumDecl),
60 int(nEnumDecls*sizeof(EnumDecl)));
61 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
62 nEnumConst, (int)sizeof(EnumConstantDecl),
63 int(nEnumConst*sizeof(EnumConstantDecl)));
64 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
65 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
66 fprintf(stderr, "Total bytes = %d\n",
67 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
68 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
69 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
70 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
71 nTypedef*sizeof(TypedefDecl)));
72}
73
74void Decl::addDeclKind(const Kind k) {
75 switch (k) {
76 case Typedef:
77 nTypedef++;
78 break;
79 case Function:
80 nFuncs++;
81 break;
82 case BlockVariable:
83 nBlockVars++;
84 break;
85 case FileVariable:
86 nFileVars++;
87 break;
88 case ParmVariable:
89 nParmVars++;
90 break;
91 case EnumConstant:
92 nEnumConst++;
93 break;
94 case Field:
95 nFieldDecls++;
96 break;
97 case Struct:
98 case Union:
99 case Class:
100 nSUC++;
101 break;
102 case Enum:
103 nEnumDecls++;
104 break;
Steve Naroff3536b442007-09-06 21:24:23 +0000105 case ObjcInterface:
106 nInterfaceDecls++;
107 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
109}
110
111// Out-of-line virtual method providing a home for Decl.
112Decl::~Decl() {
113}
114
115const char *Decl::getName() const {
116 if (const IdentifierInfo *II = getIdentifier())
117 return II->getName();
118 return "";
119}
120
121
122FunctionDecl::~FunctionDecl() {
123 delete[] ParamInfo;
124}
125
126unsigned FunctionDecl::getNumParams() const {
127 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
128}
129
130void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
131 assert(ParamInfo == 0 && "Already has param info!");
132 assert(NumParams == getNumParams() && "Parameter count mismatch!");
133
134 // Zero params -> null pointer.
135 if (NumParams) {
136 ParamInfo = new ParmVarDecl*[NumParams];
137 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
138 }
139}
140
141
142/// defineBody - When created, RecordDecl's correspond to a forward declared
143/// record. This method is used to mark the decl as being defined, with the
144/// specified contents.
145void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
146 assert(!isDefinition() && "Cannot redefine record!");
147 setDefinition(true);
148 NumMembers = numMembers;
149 if (numMembers) {
150 Members = new FieldDecl*[numMembers];
151 memcpy(Members, members, numMembers*sizeof(Decl*));
152 }
153}
154
155FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
156 if (Members == 0 || NumMembers < 0)
157 return 0;
158
159 // linear search. When C++ classes come along, will likely need to revisit.
160 for (int i = 0; i < NumMembers; ++i) {
161 if (Members[i]->getIdentifier() == name)
162 return Members[i];
163 }
164 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000165}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000166
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000167void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
168 unsigned NumParams) {
169 assert(ParamInfo == 0 && "Already has param info!");
170
171 // Zero params -> null pointer.
172 if (NumParams) {
173 ParamInfo = new ParmVarDecl*[NumParams];
174 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
175 NumMethodParams = NumParams;
176 }
177}
178
179ObjcMethodDecl::~ObjcMethodDecl() {
180 delete[] ParamInfo;
181}
182
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000183/// addObjcMethods - Insert instance and methods declarations into
184/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
185///
186void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
187 unsigned numInsMembers,
188 ObjcMethodDecl **clsMethods,
189 unsigned numClsMembers) {
190 NumInsMethods = numInsMembers;
191 if (numInsMembers) {
192 InsMethods = new ObjcMethodDecl*[numInsMembers];
193 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
194 }
195 NumClsMethods = numClsMembers;
196 if (numClsMembers) {
197 ClsMethods = new ObjcMethodDecl*[numClsMembers];
198 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
199 }
200}
201