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