blob: 5cb2a3e64fc24337be479d78fa1119a1ec10df49 [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";
Steve Naroff110be842008-12-01 20:33:01 +000075 case ObjCImplementation: return "ObjCImplementation";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000076 case ObjCClass: return "ObjCClass";
77 case ObjCMethod: return "ObjCMethod";
78 case ObjCProtocol: return "ObjCProtocol";
Steve Naroff110be842008-12-01 20:33:01 +000079 case ObjCProperty: return "ObjCProperty";
80 case ObjCPropertyImpl: return "ObjCPropertyImpl";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000081 case ObjCForwardProtocol: return "ObjCForwardProtocol";
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +000082 case Record: return "Record";
83 case CXXRecord: return "CXXRecord";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000084 case Enum: return "Enum";
Steve Naroff9ac456d2008-10-08 17:01:13 +000085 case Block: return "Block";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000086 }
87}
88
89bool Decl::CollectingStats(bool Enable) {
90 if (Enable)
91 StatSwitch = true;
92 return StatSwitch;
93}
94
95void Decl::PrintStats() {
96 fprintf(stderr, "*** Decl Stats:\n");
97 fprintf(stderr, " %d decls total.\n",
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +000098 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+nCXXFieldDecls+nCXXSUC+
Eli Friedmana8a09ac2008-06-07 16:52:53 +000099 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
100 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
Douglas Gregord2baafd2008-10-21 16:13:35 +0000101 nAtDefsFieldDecls+nNamespaces+nOverFuncs));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000102 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
103 nNamespaces, (int)sizeof(NamespaceDecl),
104 int(nNamespaces*sizeof(NamespaceDecl)));
Douglas Gregord2baafd2008-10-21 16:13:35 +0000105 fprintf(stderr, " %d overloaded function decls, %d each (%d bytes)\n",
106 nOverFuncs, (int)sizeof(OverloadedFunctionDecl),
107 int(nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000108 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
109 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
110 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
111 nVars, (int)sizeof(VarDecl),
112 int(nVars*sizeof(VarDecl)));
113 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
114 nParmVars, (int)sizeof(ParmVarDecl),
115 int(nParmVars*sizeof(ParmVarDecl)));
116 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
117 nFieldDecls, (int)sizeof(FieldDecl),
118 int(nFieldDecls*sizeof(FieldDecl)));
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000119 fprintf(stderr, " %d @defs generated field decls, %d each (%d bytes)\n",
120 nAtDefsFieldDecls, (int)sizeof(ObjCAtDefsFieldDecl),
121 int(nAtDefsFieldDecls*sizeof(ObjCAtDefsFieldDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000122 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
123 nSUC, (int)sizeof(RecordDecl),
124 int(nSUC*sizeof(RecordDecl)));
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +0000125 fprintf(stderr, " %d C++ field decls, %d each (%d bytes)\n",
126 nCXXFieldDecls, (int)sizeof(CXXFieldDecl),
127 int(nCXXFieldDecls*sizeof(CXXFieldDecl)));
128 fprintf(stderr, " %d C++ struct/union/class decls, %d each (%d bytes)\n",
129 nCXXSUC, (int)sizeof(CXXRecordDecl),
130 int(nCXXSUC*sizeof(CXXRecordDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000131 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
132 nEnumDecls, (int)sizeof(EnumDecl),
133 int(nEnumDecls*sizeof(EnumDecl)));
134 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
135 nEnumConst, (int)sizeof(EnumConstantDecl),
136 int(nEnumConst*sizeof(EnumConstantDecl)));
137 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
138 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
139 // Objective-C decls...
140 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
141 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
142 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
143 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
144 nIvarDecls, (int)sizeof(ObjCIvarDecl),
145 int(nIvarDecls*sizeof(ObjCIvarDecl)));
146 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
147 nClassDecls, (int)sizeof(ObjCClassDecl),
148 int(nClassDecls*sizeof(ObjCClassDecl)));
149 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
150 nMethodDecls, (int)sizeof(ObjCMethodDecl),
151 int(nMethodDecls*sizeof(ObjCMethodDecl)));
152 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
153 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
154 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
155 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
156 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
157 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
158 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
159 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
160 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
161
162 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
163 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
164 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
165
166 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
167 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
168 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
169
170 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
171 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
172 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
173
174 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
175 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
176 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
177
178 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
179 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
180 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
181
182 fprintf(stderr, "Total bytes = %d\n",
183 int(nFuncs*sizeof(FunctionDecl)+
184 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
185 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +0000186 nCXXFieldDecls*sizeof(CXXFieldDecl)+nCXXSUC*sizeof(CXXRecordDecl)+
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000187 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
188 nTypedef*sizeof(TypedefDecl)+
189 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
190 nIvarDecls*sizeof(ObjCIvarDecl)+
191 nClassDecls*sizeof(ObjCClassDecl)+
192 nMethodDecls*sizeof(ObjCMethodDecl)+
193 nProtocolDecls*sizeof(ObjCProtocolDecl)+
194 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
195 nCategoryDecls*sizeof(ObjCCategoryDecl)+
196 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
197 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
198 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
199 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
200 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
201 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
202 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
Douglas Gregord2baafd2008-10-21 16:13:35 +0000203 nNamespaces*sizeof(NamespaceDecl)+
204 nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000205
206}
207
208void Decl::addDeclKind(Kind k) {
209 switch (k) {
210 case Namespace: nNamespaces++; break;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000211 case OverloadedFunction: nOverFuncs++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000212 case Typedef: nTypedef++; break;
213 case Function: nFuncs++; break;
214 case Var: nVars++; break;
215 case ParmVar: nParmVars++; break;
216 case EnumConstant: nEnumConst++; break;
217 case Field: nFieldDecls++; break;
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000218 case Record: nSUC++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000219 case Enum: nEnumDecls++; break;
220 case ObjCInterface: nInterfaceDecls++; break;
221 case ObjCClass: nClassDecls++; break;
222 case ObjCMethod: nMethodDecls++; break;
223 case ObjCProtocol: nProtocolDecls++; break;
224 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
225 case ObjCCategory: nCategoryDecls++; break;
226 case ObjCIvar: nIvarDecls++; break;
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000227 case ObjCAtDefsField: nAtDefsFieldDecls++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000228 case ObjCImplementation: nObjCImplementationDecls++; break;
229 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
230 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
231 case ObjCProperty: nObjCPropertyDecl++; break;
232 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
233 case LinkageSpec: nLinkageSpecDecl++; break;
234 case FileScopeAsm: nFileScopeAsmDecl++; break;
Steve Naroff9ac456d2008-10-08 17:01:13 +0000235 case Block: nBlockDecls++; break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000236 case ImplicitParam:
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000237 case TranslationUnit: break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000238
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +0000239 case CXXField: nCXXFieldDecls++; break;
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000240 case CXXRecord: nCXXSUC++; break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000241 // FIXME: Statistics for C++ decls.
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000242 case CXXMethod:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000243 case CXXConstructor:
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000244 case CXXDestructor:
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000245 case CXXConversion:
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000246 case CXXClassVar:
247 break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000248 }
249}
250
251//===----------------------------------------------------------------------===//
252// Decl Implementation
253//===----------------------------------------------------------------------===//
254
255// Out-of-line virtual method providing a home for Decl.
256Decl::~Decl() {
257 if (!HasAttrs)
258 return;
259
260 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
261 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
262
263 // release attributes.
264 delete it->second;
265 invalidateAttrs();
266}
267
268void Decl::addAttr(Attr *NewAttr) {
269 if (!DeclAttrs)
270 DeclAttrs = new DeclAttrMapTy();
271
272 Attr *&ExistingAttr = (*DeclAttrs)[this];
273
274 NewAttr->setNext(ExistingAttr);
275 ExistingAttr = NewAttr;
276
277 HasAttrs = true;
278}
279
280void Decl::invalidateAttrs() {
281 if (!HasAttrs) return;
282
283 HasAttrs = false;
284 (*DeclAttrs)[this] = 0;
285 DeclAttrs->erase(this);
286
287 if (DeclAttrs->empty()) {
288 delete DeclAttrs;
289 DeclAttrs = 0;
290 }
291}
292
293const Attr *Decl::getAttrs() const {
294 if (!HasAttrs)
295 return 0;
296
297 return (*DeclAttrs)[this];
298}
299
300void Decl::swapAttrs(Decl *RHS) {
301 bool HasLHSAttr = this->HasAttrs;
302 bool HasRHSAttr = RHS->HasAttrs;
303
304 // Usually, neither decl has attrs, nothing to do.
305 if (!HasLHSAttr && !HasRHSAttr) return;
306
307 // If 'this' has no attrs, swap the other way.
308 if (!HasLHSAttr)
309 return RHS->swapAttrs(this);
310
311 // Handle the case when both decls have attrs.
312 if (HasRHSAttr) {
313 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
314 return;
315 }
316
317 // Otherwise, LHS has an attr and RHS doesn't.
318 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
319 (*DeclAttrs).erase(this);
320 this->HasAttrs = false;
321 RHS->HasAttrs = true;
322}
323
324
325void Decl::Destroy(ASTContext& C) {
326
327 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
328
329 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
330 // within the loop, only the Destroy method for the first ScopedDecl
331 // will deallocate all of the ScopedDecls in a chain.
332
333 ScopedDecl* N = SD->getNextDeclarator();
334
335 while (N) {
336 ScopedDecl* Tmp = N->getNextDeclarator();
337 N->NextDeclarator = 0x0;
338 N->Destroy(C);
339 N = Tmp;
340 }
341 }
342
343 this->~Decl();
344 C.getAllocator().Deallocate((void *)this);
345}
346
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000347Decl *Decl::castFromDeclContext (const DeclContext *D) {
348 return DeclContext::CastTo<Decl>(D);
349}
350
351DeclContext *Decl::castToDeclContext(const Decl *D) {
352 return DeclContext::CastTo<DeclContext>(D);
353}
354
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000355//===----------------------------------------------------------------------===//
356// DeclContext Implementation
357//===----------------------------------------------------------------------===//
358
Argiris Kirtzidis5703f442008-11-19 17:36:39 +0000359const DeclContext *DeclContext::getParent() const {
360 if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000361 return SD->getDeclContext();
Argiris Kirtzidis5703f442008-11-19 17:36:39 +0000362 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
Steve Naroff52059382008-10-10 01:28:17 +0000363 return BD->getParentContext();
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000364 else
365 return NULL;
366}
Argiris Kirtzidis9cd599b2008-11-19 18:01:13 +0000367
368const DeclContext *DeclContext::getLexicalParent() const {
369 if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
370 return SD->getLexicalDeclContext();
Argiris Kirtzidisbc470282008-11-19 18:07:24 +0000371 return getParent();
Argiris Kirtzidis9cd599b2008-11-19 18:01:13 +0000372}