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