blob: de7a1d3ca43060e9e9345a6c8f14877139282bf9 [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:
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000241 case CXXDestructor:
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000242 case CXXClassVar:
243 break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000244 }
245}
246
247//===----------------------------------------------------------------------===//
248// Decl Implementation
249//===----------------------------------------------------------------------===//
250
251// Out-of-line virtual method providing a home for Decl.
252Decl::~Decl() {
253 if (!HasAttrs)
254 return;
255
256 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
257 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
258
259 // release attributes.
260 delete it->second;
261 invalidateAttrs();
262}
263
264void Decl::addAttr(Attr *NewAttr) {
265 if (!DeclAttrs)
266 DeclAttrs = new DeclAttrMapTy();
267
268 Attr *&ExistingAttr = (*DeclAttrs)[this];
269
270 NewAttr->setNext(ExistingAttr);
271 ExistingAttr = NewAttr;
272
273 HasAttrs = true;
274}
275
276void Decl::invalidateAttrs() {
277 if (!HasAttrs) return;
278
279 HasAttrs = false;
280 (*DeclAttrs)[this] = 0;
281 DeclAttrs->erase(this);
282
283 if (DeclAttrs->empty()) {
284 delete DeclAttrs;
285 DeclAttrs = 0;
286 }
287}
288
289const Attr *Decl::getAttrs() const {
290 if (!HasAttrs)
291 return 0;
292
293 return (*DeclAttrs)[this];
294}
295
296void Decl::swapAttrs(Decl *RHS) {
297 bool HasLHSAttr = this->HasAttrs;
298 bool HasRHSAttr = RHS->HasAttrs;
299
300 // Usually, neither decl has attrs, nothing to do.
301 if (!HasLHSAttr && !HasRHSAttr) return;
302
303 // If 'this' has no attrs, swap the other way.
304 if (!HasLHSAttr)
305 return RHS->swapAttrs(this);
306
307 // Handle the case when both decls have attrs.
308 if (HasRHSAttr) {
309 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
310 return;
311 }
312
313 // Otherwise, LHS has an attr and RHS doesn't.
314 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
315 (*DeclAttrs).erase(this);
316 this->HasAttrs = false;
317 RHS->HasAttrs = true;
318}
319
320
321void Decl::Destroy(ASTContext& C) {
322
323 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
324
325 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
326 // within the loop, only the Destroy method for the first ScopedDecl
327 // will deallocate all of the ScopedDecls in a chain.
328
329 ScopedDecl* N = SD->getNextDeclarator();
330
331 while (N) {
332 ScopedDecl* Tmp = N->getNextDeclarator();
333 N->NextDeclarator = 0x0;
334 N->Destroy(C);
335 N = Tmp;
336 }
337 }
338
339 this->~Decl();
340 C.getAllocator().Deallocate((void *)this);
341}
342
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000343Decl *Decl::castFromDeclContext (const DeclContext *D) {
344 return DeclContext::CastTo<Decl>(D);
345}
346
347DeclContext *Decl::castToDeclContext(const Decl *D) {
348 return DeclContext::CastTo<DeclContext>(D);
349}
350
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000351//===----------------------------------------------------------------------===//
352// DeclContext Implementation
353//===----------------------------------------------------------------------===//
354
Argiris Kirtzidis6bbe3d72008-10-12 18:40:01 +0000355DeclContext *DeclContext::getParent() {
356 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000357 return SD->getDeclContext();
Argiris Kirtzidis6bbe3d72008-10-12 18:40:01 +0000358 else if (BlockDecl *BD = dyn_cast<BlockDecl>(this))
Steve Naroff52059382008-10-10 01:28:17 +0000359 return BD->getParentContext();
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000360 else
361 return NULL;
362}