blob: 89c61aea6a06339ab6837c14be3c64d49e86cd03 [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;
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;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000209 case ImplicitParam:
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000210 case TranslationUnit: break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000211
212 // FIXME: Statistics for C++ decls.
213 case CXXField:
214 case CXXStruct: case CXXUnion: case CXXClass:
215 case CXXMethod:
216 case CXXClassVar:
217 break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000218 }
219}
220
221//===----------------------------------------------------------------------===//
222// Decl Implementation
223//===----------------------------------------------------------------------===//
224
225// Out-of-line virtual method providing a home for Decl.
226Decl::~Decl() {
227 if (!HasAttrs)
228 return;
229
230 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
231 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
232
233 // release attributes.
234 delete it->second;
235 invalidateAttrs();
236}
237
238void Decl::addAttr(Attr *NewAttr) {
239 if (!DeclAttrs)
240 DeclAttrs = new DeclAttrMapTy();
241
242 Attr *&ExistingAttr = (*DeclAttrs)[this];
243
244 NewAttr->setNext(ExistingAttr);
245 ExistingAttr = NewAttr;
246
247 HasAttrs = true;
248}
249
250void Decl::invalidateAttrs() {
251 if (!HasAttrs) return;
252
253 HasAttrs = false;
254 (*DeclAttrs)[this] = 0;
255 DeclAttrs->erase(this);
256
257 if (DeclAttrs->empty()) {
258 delete DeclAttrs;
259 DeclAttrs = 0;
260 }
261}
262
263const Attr *Decl::getAttrs() const {
264 if (!HasAttrs)
265 return 0;
266
267 return (*DeclAttrs)[this];
268}
269
270void Decl::swapAttrs(Decl *RHS) {
271 bool HasLHSAttr = this->HasAttrs;
272 bool HasRHSAttr = RHS->HasAttrs;
273
274 // Usually, neither decl has attrs, nothing to do.
275 if (!HasLHSAttr && !HasRHSAttr) return;
276
277 // If 'this' has no attrs, swap the other way.
278 if (!HasLHSAttr)
279 return RHS->swapAttrs(this);
280
281 // Handle the case when both decls have attrs.
282 if (HasRHSAttr) {
283 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
284 return;
285 }
286
287 // Otherwise, LHS has an attr and RHS doesn't.
288 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
289 (*DeclAttrs).erase(this);
290 this->HasAttrs = false;
291 RHS->HasAttrs = true;
292}
293
294
295void Decl::Destroy(ASTContext& C) {
296
297 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
298
299 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
300 // within the loop, only the Destroy method for the first ScopedDecl
301 // will deallocate all of the ScopedDecls in a chain.
302
303 ScopedDecl* N = SD->getNextDeclarator();
304
305 while (N) {
306 ScopedDecl* Tmp = N->getNextDeclarator();
307 N->NextDeclarator = 0x0;
308 N->Destroy(C);
309 N = Tmp;
310 }
311 }
312
313 this->~Decl();
314 C.getAllocator().Deallocate((void *)this);
315}
316
317//===----------------------------------------------------------------------===//
318// DeclContext Implementation
319//===----------------------------------------------------------------------===//
320
321DeclContext *DeclContext::getParent() const {
322 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
323 return SD->getDeclContext();
324 else
325 return NULL;
326}
327
328Decl *DeclContext::ToDecl (const DeclContext *D) {
329 return CastTo<Decl>(D);
330}
331
332DeclContext *DeclContext::FromDecl (const Decl *D) {
333 return CastTo<DeclContext>(D);
334}