blob: 75d6bc6ce9a7b27fe45b162a08d7dcc29cc98cb3 [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;
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000044static unsigned nAtDefsFieldDecls = 0;
Eli Friedmana8a09ac2008-06-07 16:52:53 +000045static unsigned nObjCImplementationDecls = 0;
46static unsigned nObjCCategoryImpl = 0;
47static unsigned nObjCCompatibleAlias = 0;
48static unsigned nObjCPropertyDecl = 0;
49static unsigned nObjCPropertyImplDecl = 0;
50static unsigned nLinkageSpecDecl = 0;
51static unsigned nFileScopeAsmDecl = 0;
Steve Naroff9ac456d2008-10-08 17:01:13 +000052static unsigned nBlockDecls = 0;
Eli Friedmana8a09ac2008-06-07 16:52:53 +000053
54static bool StatSwitch = false;
55
56// This keeps track of all decl attributes. Since so few decls have attrs, we
57// keep them in a hash map instead of wasting space in the Decl class.
58typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
59
60static DeclAttrMapTy *DeclAttrs = 0;
61
62const char *Decl::getDeclKindName() const {
63 switch (DeclKind) {
64 default: assert(0 && "Unknown decl kind!");
65 case Namespace: return "Namespace";
66 case Typedef: return "Typedef";
67 case Function: return "Function";
68 case Var: return "Var";
69 case ParmVar: return "ParmVar";
70 case EnumConstant: return "EnumConstant";
71 case ObjCIvar: return "ObjCIvar";
72 case ObjCInterface: return "ObjCInterface";
73 case ObjCClass: return "ObjCClass";
74 case ObjCMethod: return "ObjCMethod";
75 case ObjCProtocol: return "ObjCProtocol";
76 case ObjCForwardProtocol: return "ObjCForwardProtocol";
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +000077 case Record: return "Record";
78 case CXXRecord: return "CXXRecord";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000079 case Enum: return "Enum";
Steve Naroff9ac456d2008-10-08 17:01:13 +000080 case Block: return "Block";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000081 }
82}
83
84bool Decl::CollectingStats(bool Enable) {
85 if (Enable)
86 StatSwitch = true;
87 return StatSwitch;
88}
89
90void Decl::PrintStats() {
91 fprintf(stderr, "*** Decl Stats:\n");
92 fprintf(stderr, " %d decls total.\n",
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +000093 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+nCXXFieldDecls+nCXXSUC+
Eli Friedmana8a09ac2008-06-07 16:52:53 +000094 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
95 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000096 nAtDefsFieldDecls+nNamespaces));
Eli Friedmana8a09ac2008-06-07 16:52:53 +000097 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
98 nNamespaces, (int)sizeof(NamespaceDecl),
99 int(nNamespaces*sizeof(NamespaceDecl)));
100 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
101 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
102 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
103 nVars, (int)sizeof(VarDecl),
104 int(nVars*sizeof(VarDecl)));
105 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
106 nParmVars, (int)sizeof(ParmVarDecl),
107 int(nParmVars*sizeof(ParmVarDecl)));
108 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
109 nFieldDecls, (int)sizeof(FieldDecl),
110 int(nFieldDecls*sizeof(FieldDecl)));
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000111 fprintf(stderr, " %d @defs generated field decls, %d each (%d bytes)\n",
112 nAtDefsFieldDecls, (int)sizeof(ObjCAtDefsFieldDecl),
113 int(nAtDefsFieldDecls*sizeof(ObjCAtDefsFieldDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000114 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
115 nSUC, (int)sizeof(RecordDecl),
116 int(nSUC*sizeof(RecordDecl)));
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +0000117 fprintf(stderr, " %d C++ field decls, %d each (%d bytes)\n",
118 nCXXFieldDecls, (int)sizeof(CXXFieldDecl),
119 int(nCXXFieldDecls*sizeof(CXXFieldDecl)));
120 fprintf(stderr, " %d C++ struct/union/class decls, %d each (%d bytes)\n",
121 nCXXSUC, (int)sizeof(CXXRecordDecl),
122 int(nCXXSUC*sizeof(CXXRecordDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000123 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
124 nEnumDecls, (int)sizeof(EnumDecl),
125 int(nEnumDecls*sizeof(EnumDecl)));
126 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
127 nEnumConst, (int)sizeof(EnumConstantDecl),
128 int(nEnumConst*sizeof(EnumConstantDecl)));
129 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
130 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
131 // Objective-C decls...
132 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
133 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
134 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
135 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
136 nIvarDecls, (int)sizeof(ObjCIvarDecl),
137 int(nIvarDecls*sizeof(ObjCIvarDecl)));
138 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
139 nClassDecls, (int)sizeof(ObjCClassDecl),
140 int(nClassDecls*sizeof(ObjCClassDecl)));
141 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
142 nMethodDecls, (int)sizeof(ObjCMethodDecl),
143 int(nMethodDecls*sizeof(ObjCMethodDecl)));
144 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
145 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
146 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
147 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
148 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
149 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
150 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
151 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
152 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
153
154 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
155 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
156 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
157
158 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
159 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
160 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
161
162 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
163 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
164 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
165
166 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
167 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
168 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
169
170 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
171 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
172 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
173
174 fprintf(stderr, "Total bytes = %d\n",
175 int(nFuncs*sizeof(FunctionDecl)+
176 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
177 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +0000178 nCXXFieldDecls*sizeof(CXXFieldDecl)+nCXXSUC*sizeof(CXXRecordDecl)+
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000179 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
180 nTypedef*sizeof(TypedefDecl)+
181 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
182 nIvarDecls*sizeof(ObjCIvarDecl)+
183 nClassDecls*sizeof(ObjCClassDecl)+
184 nMethodDecls*sizeof(ObjCMethodDecl)+
185 nProtocolDecls*sizeof(ObjCProtocolDecl)+
186 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
187 nCategoryDecls*sizeof(ObjCCategoryDecl)+
188 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
189 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
190 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
191 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
192 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
193 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
194 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
195 nNamespaces*sizeof(NamespaceDecl)));
196
197}
198
199void Decl::addDeclKind(Kind k) {
200 switch (k) {
201 case Namespace: nNamespaces++; break;
202 case Typedef: nTypedef++; break;
203 case Function: nFuncs++; break;
204 case Var: nVars++; break;
205 case ParmVar: nParmVars++; break;
206 case EnumConstant: nEnumConst++; break;
207 case Field: nFieldDecls++; break;
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000208 case Record: nSUC++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000209 case Enum: nEnumDecls++; break;
210 case ObjCInterface: nInterfaceDecls++; break;
211 case ObjCClass: nClassDecls++; break;
212 case ObjCMethod: nMethodDecls++; break;
213 case ObjCProtocol: nProtocolDecls++; break;
214 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
215 case ObjCCategory: nCategoryDecls++; break;
216 case ObjCIvar: nIvarDecls++; break;
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000217 case ObjCAtDefsField: nAtDefsFieldDecls++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000218 case ObjCImplementation: nObjCImplementationDecls++; break;
219 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
220 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
221 case ObjCProperty: nObjCPropertyDecl++; break;
222 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
223 case LinkageSpec: nLinkageSpecDecl++; break;
224 case FileScopeAsm: nFileScopeAsmDecl++; break;
Steve Naroff9ac456d2008-10-08 17:01:13 +0000225 case Block: nBlockDecls++; break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000226 case ImplicitParam:
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000227 case TranslationUnit: break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000228
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +0000229 case CXXField: nCXXFieldDecls++; break;
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000230 case CXXRecord: nCXXSUC++; break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000231 // FIXME: Statistics for C++ decls.
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000232 case CXXMethod:
233 case CXXClassVar:
234 break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000235 }
236}
237
238//===----------------------------------------------------------------------===//
239// Decl Implementation
240//===----------------------------------------------------------------------===//
241
242// Out-of-line virtual method providing a home for Decl.
243Decl::~Decl() {
244 if (!HasAttrs)
245 return;
246
247 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
248 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
249
250 // release attributes.
251 delete it->second;
252 invalidateAttrs();
253}
254
255void Decl::addAttr(Attr *NewAttr) {
256 if (!DeclAttrs)
257 DeclAttrs = new DeclAttrMapTy();
258
259 Attr *&ExistingAttr = (*DeclAttrs)[this];
260
261 NewAttr->setNext(ExistingAttr);
262 ExistingAttr = NewAttr;
263
264 HasAttrs = true;
265}
266
267void Decl::invalidateAttrs() {
268 if (!HasAttrs) return;
269
270 HasAttrs = false;
271 (*DeclAttrs)[this] = 0;
272 DeclAttrs->erase(this);
273
274 if (DeclAttrs->empty()) {
275 delete DeclAttrs;
276 DeclAttrs = 0;
277 }
278}
279
280const Attr *Decl::getAttrs() const {
281 if (!HasAttrs)
282 return 0;
283
284 return (*DeclAttrs)[this];
285}
286
287void Decl::swapAttrs(Decl *RHS) {
288 bool HasLHSAttr = this->HasAttrs;
289 bool HasRHSAttr = RHS->HasAttrs;
290
291 // Usually, neither decl has attrs, nothing to do.
292 if (!HasLHSAttr && !HasRHSAttr) return;
293
294 // If 'this' has no attrs, swap the other way.
295 if (!HasLHSAttr)
296 return RHS->swapAttrs(this);
297
298 // Handle the case when both decls have attrs.
299 if (HasRHSAttr) {
300 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
301 return;
302 }
303
304 // Otherwise, LHS has an attr and RHS doesn't.
305 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
306 (*DeclAttrs).erase(this);
307 this->HasAttrs = false;
308 RHS->HasAttrs = true;
309}
310
311
312void Decl::Destroy(ASTContext& C) {
313
314 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
315
316 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
317 // within the loop, only the Destroy method for the first ScopedDecl
318 // will deallocate all of the ScopedDecls in a chain.
319
320 ScopedDecl* N = SD->getNextDeclarator();
321
322 while (N) {
323 ScopedDecl* Tmp = N->getNextDeclarator();
324 N->NextDeclarator = 0x0;
325 N->Destroy(C);
326 N = Tmp;
327 }
328 }
329
330 this->~Decl();
331 C.getAllocator().Deallocate((void *)this);
332}
333
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000334Decl *Decl::castFromDeclContext (const DeclContext *D) {
335 return DeclContext::CastTo<Decl>(D);
336}
337
338DeclContext *Decl::castToDeclContext(const Decl *D) {
339 return DeclContext::CastTo<DeclContext>(D);
340}
341
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000342//===----------------------------------------------------------------------===//
343// DeclContext Implementation
344//===----------------------------------------------------------------------===//
345
Argiris Kirtzidis6bbe3d72008-10-12 18:40:01 +0000346DeclContext *DeclContext::getParent() {
347 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000348 return SD->getDeclContext();
Argiris Kirtzidis6bbe3d72008-10-12 18:40:01 +0000349 else if (BlockDecl *BD = dyn_cast<BlockDecl>(this))
Steve Naroff52059382008-10-10 01:28:17 +0000350 return BD->getParentContext();
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000351 else
352 return NULL;
353}