blob: 296304a66dda72ef471c81630b40b29ad8b73907 [file] [log] [blame]
Eli Friedman56d29372008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000015#include "clang/AST/DeclCXX.h"
Eli Friedman56d29372008-06-07 16:52:53 +000016#include "clang/AST/ASTContext.h"
17#include "llvm/ADT/DenseMap.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Statistics
22//===----------------------------------------------------------------------===//
23
24// temporary statistics gathering
25static unsigned nFuncs = 0;
26static unsigned nVars = 0;
27static unsigned nParmVars = 0;
28static unsigned nSUC = 0;
29static unsigned nEnumConst = 0;
30static unsigned nEnumDecls = 0;
31static unsigned nNamespaces = 0;
32static unsigned nTypedef = 0;
33static unsigned nFieldDecls = 0;
34static unsigned nInterfaceDecls = 0;
35static unsigned nClassDecls = 0;
36static unsigned nMethodDecls = 0;
37static unsigned nProtocolDecls = 0;
38static unsigned nForwardProtocolDecls = 0;
39static unsigned nCategoryDecls = 0;
40static unsigned nIvarDecls = 0;
41static unsigned nObjCImplementationDecls = 0;
42static unsigned nObjCCategoryImpl = 0;
43static unsigned nObjCCompatibleAlias = 0;
44static unsigned nObjCPropertyDecl = 0;
45static unsigned nObjCPropertyImplDecl = 0;
46static unsigned nLinkageSpecDecl = 0;
47static unsigned nFileScopeAsmDecl = 0;
48
49static bool StatSwitch = false;
50
51// This keeps track of all decl attributes. Since so few decls have attrs, we
52// keep them in a hash map instead of wasting space in the Decl class.
53typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
54
55static DeclAttrMapTy *DeclAttrs = 0;
56
57const char *Decl::getDeclKindName() const {
58 switch (DeclKind) {
59 default: assert(0 && "Unknown decl kind!");
60 case Namespace: return "Namespace";
61 case Typedef: return "Typedef";
62 case Function: return "Function";
63 case Var: return "Var";
64 case ParmVar: return "ParmVar";
65 case EnumConstant: return "EnumConstant";
66 case ObjCIvar: return "ObjCIvar";
67 case ObjCInterface: return "ObjCInterface";
68 case ObjCClass: return "ObjCClass";
69 case ObjCMethod: return "ObjCMethod";
70 case ObjCProtocol: return "ObjCProtocol";
71 case ObjCForwardProtocol: return "ObjCForwardProtocol";
72 case Struct: return "Struct";
73 case Union: return "Union";
74 case Class: return "Class";
75 case Enum: return "Enum";
76 }
77}
78
79bool Decl::CollectingStats(bool Enable) {
80 if (Enable)
81 StatSwitch = true;
82 return StatSwitch;
83}
84
85void Decl::PrintStats() {
86 fprintf(stderr, "*** Decl Stats:\n");
87 fprintf(stderr, " %d decls total.\n",
88 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
89 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
90 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
91 nNamespaces));
92 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
93 nNamespaces, (int)sizeof(NamespaceDecl),
94 int(nNamespaces*sizeof(NamespaceDecl)));
95 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
96 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
97 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
98 nVars, (int)sizeof(VarDecl),
99 int(nVars*sizeof(VarDecl)));
100 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
101 nParmVars, (int)sizeof(ParmVarDecl),
102 int(nParmVars*sizeof(ParmVarDecl)));
103 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
104 nFieldDecls, (int)sizeof(FieldDecl),
105 int(nFieldDecls*sizeof(FieldDecl)));
106 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
107 nSUC, (int)sizeof(RecordDecl),
108 int(nSUC*sizeof(RecordDecl)));
109 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
110 nEnumDecls, (int)sizeof(EnumDecl),
111 int(nEnumDecls*sizeof(EnumDecl)));
112 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
113 nEnumConst, (int)sizeof(EnumConstantDecl),
114 int(nEnumConst*sizeof(EnumConstantDecl)));
115 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
116 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
117 // Objective-C decls...
118 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
119 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
120 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
121 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
122 nIvarDecls, (int)sizeof(ObjCIvarDecl),
123 int(nIvarDecls*sizeof(ObjCIvarDecl)));
124 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
125 nClassDecls, (int)sizeof(ObjCClassDecl),
126 int(nClassDecls*sizeof(ObjCClassDecl)));
127 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
128 nMethodDecls, (int)sizeof(ObjCMethodDecl),
129 int(nMethodDecls*sizeof(ObjCMethodDecl)));
130 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
131 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
132 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
133 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
134 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
135 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
136 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
137 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
138 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
139
140 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
141 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
142 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
143
144 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
145 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
146 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
147
148 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
149 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
150 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
151
152 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
153 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
154 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
155
156 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
157 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
158 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
159
160 fprintf(stderr, "Total bytes = %d\n",
161 int(nFuncs*sizeof(FunctionDecl)+
162 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
163 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
164 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
165 nTypedef*sizeof(TypedefDecl)+
166 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
167 nIvarDecls*sizeof(ObjCIvarDecl)+
168 nClassDecls*sizeof(ObjCClassDecl)+
169 nMethodDecls*sizeof(ObjCMethodDecl)+
170 nProtocolDecls*sizeof(ObjCProtocolDecl)+
171 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
172 nCategoryDecls*sizeof(ObjCCategoryDecl)+
173 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
174 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
175 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
176 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
177 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
178 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
179 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
180 nNamespaces*sizeof(NamespaceDecl)));
181
182}
183
184void Decl::addDeclKind(Kind k) {
185 switch (k) {
186 case Namespace: nNamespaces++; break;
187 case Typedef: nTypedef++; break;
188 case Function: nFuncs++; break;
189 case Var: nVars++; break;
190 case ParmVar: nParmVars++; break;
191 case EnumConstant: nEnumConst++; break;
192 case Field: nFieldDecls++; break;
193 case Struct: case Union: case Class: nSUC++; break;
194 case Enum: nEnumDecls++; break;
195 case ObjCInterface: nInterfaceDecls++; break;
196 case ObjCClass: nClassDecls++; break;
197 case ObjCMethod: nMethodDecls++; break;
198 case ObjCProtocol: nProtocolDecls++; break;
199 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
200 case ObjCCategory: nCategoryDecls++; break;
201 case ObjCIvar: nIvarDecls++; break;
202 case ObjCImplementation: nObjCImplementationDecls++; break;
203 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
204 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
205 case ObjCProperty: nObjCPropertyDecl++; break;
206 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
207 case LinkageSpec: nLinkageSpecDecl++; break;
208 case FileScopeAsm: nFileScopeAsmDecl++; break;
209 case TranslationUnit: break;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000210
211 // FIXME: Statistics for C++ decls.
212 case CXXField:
213 case CXXStruct: case CXXUnion: case CXXClass:
214 case CXXMethod:
215 case CXXClassVar:
216 break;
Eli Friedman56d29372008-06-07 16:52:53 +0000217 }
218}
219
220//===----------------------------------------------------------------------===//
221// Decl Implementation
222//===----------------------------------------------------------------------===//
223
224// Out-of-line virtual method providing a home for Decl.
225Decl::~Decl() {
226 if (!HasAttrs)
227 return;
228
229 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
230 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
231
232 // release attributes.
233 delete it->second;
234 invalidateAttrs();
235}
236
237void Decl::addAttr(Attr *NewAttr) {
238 if (!DeclAttrs)
239 DeclAttrs = new DeclAttrMapTy();
240
241 Attr *&ExistingAttr = (*DeclAttrs)[this];
242
243 NewAttr->setNext(ExistingAttr);
244 ExistingAttr = NewAttr;
245
246 HasAttrs = true;
247}
248
249void Decl::invalidateAttrs() {
250 if (!HasAttrs) return;
251
252 HasAttrs = false;
253 (*DeclAttrs)[this] = 0;
254 DeclAttrs->erase(this);
255
256 if (DeclAttrs->empty()) {
257 delete DeclAttrs;
258 DeclAttrs = 0;
259 }
260}
261
262const Attr *Decl::getAttrs() const {
263 if (!HasAttrs)
264 return 0;
265
266 return (*DeclAttrs)[this];
267}
268
269void Decl::swapAttrs(Decl *RHS) {
270 bool HasLHSAttr = this->HasAttrs;
271 bool HasRHSAttr = RHS->HasAttrs;
272
273 // Usually, neither decl has attrs, nothing to do.
274 if (!HasLHSAttr && !HasRHSAttr) return;
275
276 // If 'this' has no attrs, swap the other way.
277 if (!HasLHSAttr)
278 return RHS->swapAttrs(this);
279
280 // Handle the case when both decls have attrs.
281 if (HasRHSAttr) {
282 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
283 return;
284 }
285
286 // Otherwise, LHS has an attr and RHS doesn't.
287 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
288 (*DeclAttrs).erase(this);
289 this->HasAttrs = false;
290 RHS->HasAttrs = true;
291}
292
293
294void Decl::Destroy(ASTContext& C) {
295
296 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
297
298 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
299 // within the loop, only the Destroy method for the first ScopedDecl
300 // will deallocate all of the ScopedDecls in a chain.
301
302 ScopedDecl* N = SD->getNextDeclarator();
303
304 while (N) {
305 ScopedDecl* Tmp = N->getNextDeclarator();
306 N->NextDeclarator = 0x0;
307 N->Destroy(C);
308 N = Tmp;
309 }
310 }
311
312 this->~Decl();
313 C.getAllocator().Deallocate((void *)this);
314}
315
316//===----------------------------------------------------------------------===//
317// DeclContext Implementation
318//===----------------------------------------------------------------------===//
319
320DeclContext *DeclContext::getParent() const {
321 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
322 return SD->getDeclContext();
323 else
324 return NULL;
325}
326
327Decl *DeclContext::ToDecl (const DeclContext *D) {
328 return CastTo<Decl>(D);
329}
330
331DeclContext *DeclContext::FromDecl (const Decl *D) {
332 return CastTo<DeclContext>(D);
333}