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