blob: 7363bd02478809be48882177e551a70f84b9b931 [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"
Douglas Gregor8acb7272008-12-11 16:49:14 +000018#include "clang/AST/Type.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000019#include "llvm/ADT/DenseMap.h"
Douglas Gregor6e71edc2008-12-23 21:05:05 +000020#include <algorithm>
21#include <functional>
Douglas Gregorddfd9d52008-12-23 00:26:44 +000022#include <vector>
Eli Friedmana8a09ac2008-06-07 16:52:53 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Statistics
27//===----------------------------------------------------------------------===//
28
29// temporary statistics gathering
30static unsigned nFuncs = 0;
31static unsigned nVars = 0;
32static unsigned nParmVars = 0;
Fariborz Jahaniana7babab2008-12-20 21:06:28 +000033static unsigned nOriginalParmVars = 0;
Eli Friedmana8a09ac2008-06-07 16:52:53 +000034static unsigned nSUC = 0;
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +000035static unsigned nCXXSUC = 0;
Eli Friedmana8a09ac2008-06-07 16:52:53 +000036static unsigned nEnumConst = 0;
37static unsigned nEnumDecls = 0;
38static unsigned nNamespaces = 0;
Douglas Gregord2baafd2008-10-21 16:13:35 +000039static unsigned nOverFuncs = 0;
Eli Friedmana8a09ac2008-06-07 16:52:53 +000040static unsigned nTypedef = 0;
41static unsigned nFieldDecls = 0;
42static unsigned nInterfaceDecls = 0;
43static unsigned nClassDecls = 0;
44static unsigned nMethodDecls = 0;
45static unsigned nProtocolDecls = 0;
46static unsigned nForwardProtocolDecls = 0;
47static unsigned nCategoryDecls = 0;
48static unsigned nIvarDecls = 0;
Ted Kremeneke5bedfe2008-08-20 03:26:33 +000049static unsigned nAtDefsFieldDecls = 0;
Eli Friedmana8a09ac2008-06-07 16:52:53 +000050static unsigned nObjCImplementationDecls = 0;
51static unsigned nObjCCategoryImpl = 0;
52static unsigned nObjCCompatibleAlias = 0;
53static unsigned nObjCPropertyDecl = 0;
54static unsigned nObjCPropertyImplDecl = 0;
55static unsigned nLinkageSpecDecl = 0;
56static unsigned nFileScopeAsmDecl = 0;
Steve Naroff9ac456d2008-10-08 17:01:13 +000057static unsigned nBlockDecls = 0;
Eli Friedmana8a09ac2008-06-07 16:52:53 +000058
59static bool StatSwitch = false;
60
61// This keeps track of all decl attributes. Since so few decls have attrs, we
62// keep them in a hash map instead of wasting space in the Decl class.
63typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
64
65static DeclAttrMapTy *DeclAttrs = 0;
66
67const char *Decl::getDeclKindName() const {
68 switch (DeclKind) {
69 default: assert(0 && "Unknown decl kind!");
70 case Namespace: return "Namespace";
Douglas Gregord2baafd2008-10-21 16:13:35 +000071 case OverloadedFunction: return "OverloadedFunction";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000072 case Typedef: return "Typedef";
73 case Function: return "Function";
74 case Var: return "Var";
75 case ParmVar: return "ParmVar";
Fariborz Jahaniana7babab2008-12-20 21:06:28 +000076 case OriginalParmVar: return "OriginalParmVar";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000077 case EnumConstant: return "EnumConstant";
78 case ObjCIvar: return "ObjCIvar";
79 case ObjCInterface: return "ObjCInterface";
Steve Naroff110be842008-12-01 20:33:01 +000080 case ObjCImplementation: return "ObjCImplementation";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000081 case ObjCClass: return "ObjCClass";
82 case ObjCMethod: return "ObjCMethod";
83 case ObjCProtocol: return "ObjCProtocol";
Steve Naroff110be842008-12-01 20:33:01 +000084 case ObjCProperty: return "ObjCProperty";
85 case ObjCPropertyImpl: return "ObjCPropertyImpl";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000086 case ObjCForwardProtocol: return "ObjCForwardProtocol";
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +000087 case Record: return "Record";
88 case CXXRecord: return "CXXRecord";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000089 case Enum: return "Enum";
Steve Naroff9ac456d2008-10-08 17:01:13 +000090 case Block: return "Block";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000091 }
92}
93
94bool Decl::CollectingStats(bool Enable) {
95 if (Enable)
96 StatSwitch = true;
97 return StatSwitch;
98}
99
100void Decl::PrintStats() {
101 fprintf(stderr, "*** Decl Stats:\n");
102 fprintf(stderr, " %d decls total.\n",
Fariborz Jahaniana7babab2008-12-20 21:06:28 +0000103 int(nFuncs+nVars+nParmVars+nOriginalParmVars+nFieldDecls+nSUC+nCXXSUC+
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000104 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
105 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
Douglas Gregord2baafd2008-10-21 16:13:35 +0000106 nAtDefsFieldDecls+nNamespaces+nOverFuncs));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000107 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
108 nNamespaces, (int)sizeof(NamespaceDecl),
109 int(nNamespaces*sizeof(NamespaceDecl)));
Douglas Gregord2baafd2008-10-21 16:13:35 +0000110 fprintf(stderr, " %d overloaded function decls, %d each (%d bytes)\n",
111 nOverFuncs, (int)sizeof(OverloadedFunctionDecl),
112 int(nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000113 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
114 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
115 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
116 nVars, (int)sizeof(VarDecl),
117 int(nVars*sizeof(VarDecl)));
118 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
119 nParmVars, (int)sizeof(ParmVarDecl),
120 int(nParmVars*sizeof(ParmVarDecl)));
Fariborz Jahaniana7babab2008-12-20 21:06:28 +0000121 fprintf(stderr, " %d original parameter variable decls, %d each (%d bytes)\n",
122 nOriginalParmVars, (int)sizeof(ParmVarWithOriginalTypeDecl),
123 int(nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000124 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
125 nFieldDecls, (int)sizeof(FieldDecl),
126 int(nFieldDecls*sizeof(FieldDecl)));
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000127 fprintf(stderr, " %d @defs generated field decls, %d each (%d bytes)\n",
128 nAtDefsFieldDecls, (int)sizeof(ObjCAtDefsFieldDecl),
129 int(nAtDefsFieldDecls*sizeof(ObjCAtDefsFieldDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000130 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
131 nSUC, (int)sizeof(RecordDecl),
132 int(nSUC*sizeof(RecordDecl)));
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +0000133 fprintf(stderr, " %d C++ struct/union/class decls, %d each (%d bytes)\n",
134 nCXXSUC, (int)sizeof(CXXRecordDecl),
135 int(nCXXSUC*sizeof(CXXRecordDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000136 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
137 nEnumDecls, (int)sizeof(EnumDecl),
138 int(nEnumDecls*sizeof(EnumDecl)));
139 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
140 nEnumConst, (int)sizeof(EnumConstantDecl),
141 int(nEnumConst*sizeof(EnumConstantDecl)));
142 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
143 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
144 // Objective-C decls...
145 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
146 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
147 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
148 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
149 nIvarDecls, (int)sizeof(ObjCIvarDecl),
150 int(nIvarDecls*sizeof(ObjCIvarDecl)));
151 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
152 nClassDecls, (int)sizeof(ObjCClassDecl),
153 int(nClassDecls*sizeof(ObjCClassDecl)));
154 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
155 nMethodDecls, (int)sizeof(ObjCMethodDecl),
156 int(nMethodDecls*sizeof(ObjCMethodDecl)));
157 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
158 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
159 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
160 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
161 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
162 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
163 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
164 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
165 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
166
167 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
168 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
169 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
170
171 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
172 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
173 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
174
175 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
176 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
177 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
178
179 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
180 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
181 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
182
183 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
184 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
185 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
186
187 fprintf(stderr, "Total bytes = %d\n",
188 int(nFuncs*sizeof(FunctionDecl)+
189 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Fariborz Jahaniana7babab2008-12-20 21:06:28 +0000190 nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)+
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000191 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Douglas Gregor8acb7272008-12-11 16:49:14 +0000192 nCXXSUC*sizeof(CXXRecordDecl)+
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000193 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
194 nTypedef*sizeof(TypedefDecl)+
195 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
196 nIvarDecls*sizeof(ObjCIvarDecl)+
197 nClassDecls*sizeof(ObjCClassDecl)+
198 nMethodDecls*sizeof(ObjCMethodDecl)+
199 nProtocolDecls*sizeof(ObjCProtocolDecl)+
200 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
201 nCategoryDecls*sizeof(ObjCCategoryDecl)+
202 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
203 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
204 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
205 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
206 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
207 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
208 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
Douglas Gregord2baafd2008-10-21 16:13:35 +0000209 nNamespaces*sizeof(NamespaceDecl)+
210 nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000211
212}
213
214void Decl::addDeclKind(Kind k) {
215 switch (k) {
216 case Namespace: nNamespaces++; break;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000217 case OverloadedFunction: nOverFuncs++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000218 case Typedef: nTypedef++; break;
219 case Function: nFuncs++; break;
220 case Var: nVars++; break;
221 case ParmVar: nParmVars++; break;
Fariborz Jahaniana7babab2008-12-20 21:06:28 +0000222 case OriginalParmVar: nOriginalParmVars++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000223 case EnumConstant: nEnumConst++; break;
224 case Field: nFieldDecls++; break;
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000225 case Record: nSUC++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000226 case Enum: nEnumDecls++; break;
Steve Naroff451f83c2009-01-09 15:36:25 +0000227 case ObjCContainer: break; // is abstract...no need to account for.
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000228 case ObjCInterface: nInterfaceDecls++; break;
229 case ObjCClass: nClassDecls++; break;
230 case ObjCMethod: nMethodDecls++; break;
231 case ObjCProtocol: nProtocolDecls++; break;
232 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
233 case ObjCCategory: nCategoryDecls++; break;
234 case ObjCIvar: nIvarDecls++; break;
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000235 case ObjCAtDefsField: nAtDefsFieldDecls++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000236 case ObjCImplementation: nObjCImplementationDecls++; break;
237 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
238 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
239 case ObjCProperty: nObjCPropertyDecl++; break;
240 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
241 case LinkageSpec: nLinkageSpecDecl++; break;
242 case FileScopeAsm: nFileScopeAsmDecl++; break;
Steve Naroff9ac456d2008-10-08 17:01:13 +0000243 case Block: nBlockDecls++; break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000244 case ImplicitParam:
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000245 case TranslationUnit: break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000246
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000247 case CXXRecord: nCXXSUC++; break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000248 // FIXME: Statistics for C++ decls.
Douglas Gregordd861062008-12-05 18:15:24 +0000249 case TemplateTypeParm:
250 case NonTypeTemplateParm:
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000251 case CXXMethod:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000252 case CXXConstructor:
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000253 case CXXDestructor:
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000254 case CXXConversion:
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000255 case CXXClassVar:
256 break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000257 }
258}
259
260//===----------------------------------------------------------------------===//
261// Decl Implementation
262//===----------------------------------------------------------------------===//
263
264// Out-of-line virtual method providing a home for Decl.
265Decl::~Decl() {
266 if (!HasAttrs)
267 return;
268
269 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
270 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
271
272 // release attributes.
273 delete it->second;
274 invalidateAttrs();
275}
276
277void Decl::addAttr(Attr *NewAttr) {
278 if (!DeclAttrs)
279 DeclAttrs = new DeclAttrMapTy();
280
281 Attr *&ExistingAttr = (*DeclAttrs)[this];
282
283 NewAttr->setNext(ExistingAttr);
284 ExistingAttr = NewAttr;
285
286 HasAttrs = true;
287}
288
289void Decl::invalidateAttrs() {
290 if (!HasAttrs) return;
291
292 HasAttrs = false;
293 (*DeclAttrs)[this] = 0;
294 DeclAttrs->erase(this);
295
296 if (DeclAttrs->empty()) {
297 delete DeclAttrs;
298 DeclAttrs = 0;
299 }
300}
301
302const Attr *Decl::getAttrs() const {
303 if (!HasAttrs)
304 return 0;
305
306 return (*DeclAttrs)[this];
307}
308
309void Decl::swapAttrs(Decl *RHS) {
310 bool HasLHSAttr = this->HasAttrs;
311 bool HasRHSAttr = RHS->HasAttrs;
312
313 // Usually, neither decl has attrs, nothing to do.
314 if (!HasLHSAttr && !HasRHSAttr) return;
315
316 // If 'this' has no attrs, swap the other way.
317 if (!HasLHSAttr)
318 return RHS->swapAttrs(this);
319
320 // Handle the case when both decls have attrs.
321 if (HasRHSAttr) {
322 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
323 return;
324 }
325
326 // Otherwise, LHS has an attr and RHS doesn't.
327 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
328 (*DeclAttrs).erase(this);
329 this->HasAttrs = false;
330 RHS->HasAttrs = true;
331}
332
333
334void Decl::Destroy(ASTContext& C) {
Douglas Gregor8314d742009-01-13 19:47:12 +0000335#if 0
336 // FIXME: This causes double-destroys in some cases, so it is
337 // disabled at the moment.
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000338 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
339
340 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
341 // within the loop, only the Destroy method for the first ScopedDecl
342 // will deallocate all of the ScopedDecls in a chain.
343
344 ScopedDecl* N = SD->getNextDeclarator();
345
346 while (N) {
347 ScopedDecl* Tmp = N->getNextDeclarator();
348 N->NextDeclarator = 0x0;
349 N->Destroy(C);
350 N = Tmp;
351 }
352 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000353#endif
354
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000355 this->~Decl();
356 C.getAllocator().Deallocate((void *)this);
357}
358
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000359Decl *Decl::castFromDeclContext (const DeclContext *D) {
360 return DeclContext::CastTo<Decl>(D);
361}
362
363DeclContext *Decl::castToDeclContext(const Decl *D) {
364 return DeclContext::CastTo<DeclContext>(D);
365}
366
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000367//===----------------------------------------------------------------------===//
368// DeclContext Implementation
369//===----------------------------------------------------------------------===//
370
Argiris Kirtzidis5703f442008-11-19 17:36:39 +0000371const DeclContext *DeclContext::getParent() const {
372 if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000373 return SD->getDeclContext();
Argiris Kirtzidis5703f442008-11-19 17:36:39 +0000374 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
Steve Naroff52059382008-10-10 01:28:17 +0000375 return BD->getParentContext();
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000376 else
377 return NULL;
378}
Argiris Kirtzidis9cd599b2008-11-19 18:01:13 +0000379
380const DeclContext *DeclContext::getLexicalParent() const {
381 if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
382 return SD->getLexicalDeclContext();
Argiris Kirtzidisbc470282008-11-19 18:07:24 +0000383 return getParent();
Argiris Kirtzidis9cd599b2008-11-19 18:01:13 +0000384}
Douglas Gregor8acb7272008-12-11 16:49:14 +0000385
Douglas Gregor8acb7272008-12-11 16:49:14 +0000386// FIXME: We really want to use a DenseSet here to eliminate the
387// redundant storage of the declaration names, but (1) it doesn't give
388// us the ability to search based on DeclarationName, (2) we really
389// need something more like a DenseMultiSet, and (3) it's
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000390// implemented in terms of DenseMap anyway. However, this data
391// structure is really space-inefficient, so we'll have to do
392// something.
393typedef llvm::DenseMap<DeclarationName, std::vector<ScopedDecl*> >
394 StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000395
396DeclContext::~DeclContext() {
397 unsigned Size = LookupPtr.getInt();
398 if (Size == LookupIsMap) {
399 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
400 delete Map;
401 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000402 ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000403 delete [] Array;
404 }
405}
406
407void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregord1675382009-01-09 19:42:16 +0000408 for (decl_iterator D = decls_begin(); D != decls_end(); ++D) {
409 // FIXME: assert that this condition holds.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000410 if ((*D)->getLexicalDeclContext() == this)
411 (*D)->Destroy(C);
412 }
413}
414
Douglas Gregord8028382009-01-05 19:45:36 +0000415bool DeclContext::isTransparentContext() const {
416 if (DeclKind == Decl::Enum)
417 return true; // FIXME: Check for C++0x scoped enums
418 else if (DeclKind == Decl::LinkageSpec)
419 return true;
420 else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord)
Douglas Gregor723d3332009-01-07 00:43:41 +0000421 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000422 else if (DeclKind == Decl::Namespace)
423 return false; // FIXME: Check for C++0x inline namespaces
424
425 return false;
426}
427
Steve Naroffab63fd62009-01-08 17:28:14 +0000428DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000429 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000430 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000431 case Decl::LinkageSpec:
432 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000433 // There is only one DeclContext for these entities.
434 return this;
435
436 case Decl::Namespace:
437 // The original namespace is our primary context.
438 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
439
440 case Decl::Enum:
Douglas Gregord8028382009-01-05 19:45:36 +0000441#if 0
442 // FIXME: See the comment for CXXRecord, below.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000443 // The declaration associated with the enumeration type is our
444 // primary context.
445 return Context.getTypeDeclType(static_cast<EnumDecl*>(this))
446 ->getAsEnumType()->getDecl();
Douglas Gregord8028382009-01-05 19:45:36 +0000447#else
448 return this;
449#endif
Douglas Gregor8acb7272008-12-11 16:49:14 +0000450
451 case Decl::Record:
452 case Decl::CXXRecord: {
453 // The declaration associated with the type is be our primary
454 // context.
455#if 0
456 // FIXME: This is what we expect to do. However, it doesn't work
457 // because ASTContext::setTagDefinition changes the result of
458 // Context.getTypeDeclType, meaning that our "primary" declaration
459 // of a RecordDecl/CXXRecordDecl will change, and we won't be able
460 // to find any values inserted into the earlier "primary"
461 // declaration. We need better tracking of redeclarations and
462 // definitions.
463 QualType Type = Context.getTypeDeclType(static_cast<RecordDecl*>(this));
464 return Type->getAsRecordType()->getDecl();
465#else
466 // FIXME: This hack will work for now, because the declaration we
467 // create when we're defining the record is the one we'll use as
468 // the definition later.
469 return this;
470#endif
471 }
472
473 case Decl::ObjCMethod:
474 return this;
475
476 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000477 case Decl::ObjCProtocol:
478 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000479 // FIXME: Can Objective-C interfaces be forward-declared?
480 return this;
481
Steve Naroffab63fd62009-01-08 17:28:14 +0000482 case Decl::ObjCImplementation:
483 case Decl::ObjCCategoryImpl:
484 return this;
485
Douglas Gregor8acb7272008-12-11 16:49:14 +0000486 default:
487 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
488 "Unknown DeclContext kind");
489 return this;
490 }
491}
492
493DeclContext *DeclContext::getNextContext() {
494 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000495 case Decl::TranslationUnit:
496 case Decl::Enum:
497 case Decl::Record:
498 case Decl::CXXRecord:
499 case Decl::ObjCMethod:
500 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000501 case Decl::ObjCCategory:
502 case Decl::ObjCProtocol:
503 case Decl::ObjCImplementation:
504 case Decl::ObjCCategoryImpl:
Douglas Gregord8028382009-01-05 19:45:36 +0000505 case Decl::LinkageSpec:
506 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000507 // There is only one DeclContext for these entities.
508 return 0;
509
510 case Decl::Namespace:
511 // Return the next namespace
512 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
513
514 default:
515 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
516 "Unknown DeclContext kind");
517 return 0;
518 }
519}
520
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000521void DeclContext::addDecl(ScopedDecl *D) {
Douglas Gregord394a272009-01-09 18:51:29 +0000522 assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
Douglas Gregord1675382009-01-09 19:42:16 +0000523 assert(!D->NextDeclInScope && D != LastDecl &&
524 "Decl already inserted into a DeclContext");
525
526 if (FirstDecl) {
527 LastDecl->NextDeclInScope = D;
528 LastDecl = D;
529 } else {
530 FirstDecl = LastDecl = D;
531 }
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000532 D->getDeclContext()->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000533}
534
Douglas Gregord8028382009-01-05 19:45:36 +0000535/// buildLookup - Build the lookup data structure with all of the
536/// declarations in DCtx (and any other contexts linked to it or
537/// transparent contexts nested within it).
Steve Naroffab63fd62009-01-08 17:28:14 +0000538void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000539 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000540 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
541 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000542 // Insert this declaration into the lookup structure
543 insertImpl(*D);
544
545 // If this declaration is itself a transparent declaration context,
546 // add its members (recursively).
547 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
548 if (InnerCtx->isTransparentContext())
Steve Naroffab63fd62009-01-08 17:28:14 +0000549 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000550 }
551 }
552}
553
Douglas Gregor8acb7272008-12-11 16:49:14 +0000554DeclContext::lookup_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000555DeclContext::lookup(DeclarationName Name) {
556 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000557 if (PrimaryContext != this)
Steve Naroffab63fd62009-01-08 17:28:14 +0000558 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000559
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000560 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000561 /// all of the linked DeclContexts (in declaration order!) and
562 /// inserting their values.
Douglas Gregord8028382009-01-05 19:45:36 +0000563 if (LookupPtr.getPointer() == 0)
Steve Naroffab63fd62009-01-08 17:28:14 +0000564 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000565
Douglas Gregor8acb7272008-12-11 16:49:14 +0000566 if (isLookupMap()) {
567 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
568 StoredDeclsMap::iterator Pos = Map->find(Name);
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000569 if (Pos != Map->end())
570 return lookup_result(&Pos->second.front(),
571 &Pos->second.front() + Pos->second.size());
572 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000573 }
574
575 // We have a small array. Look into it.
576 unsigned Size = LookupPtr.getInt();
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000577 ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
Douglas Gregor39677622008-12-11 20:41:00 +0000578 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000579 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000580 unsigned Last = Idx + 1;
581 while (Last != Size && Array[Last]->getDeclName() == Name)
582 ++Last;
583 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000584 }
585
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000586 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000587}
588
589DeclContext::lookup_const_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000590DeclContext::lookup(DeclarationName Name) const {
591 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000592}
593
Douglas Gregor38d38a02009-01-07 02:48:43 +0000594const DeclContext *DeclContext::getLookupContext() const {
595 const DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000596 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000597 while (Ctx->isTransparentContext())
598 Ctx = Ctx->getParent();
599 return Ctx;
600}
601
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000602void DeclContext::insert(ScopedDecl *D) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000603 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000604 if (PrimaryContext != this) {
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000605 PrimaryContext->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000606 return;
607 }
608
609 // If we already have a lookup data structure, perform the insertion
610 // into it. Otherwise, be lazy and don't build that structure until
611 // someone asks for it.
612 if (LookupPtr.getPointer())
613 insertImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000614
Douglas Gregord8028382009-01-05 19:45:36 +0000615 // If we are a transparent context, insert into our parent context,
616 // too. This operation is recursive.
617 if (isTransparentContext())
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000618 getParent()->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000619}
620
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000621void DeclContext::insertImpl(ScopedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000622 // Skip unnamed declarations.
623 if (!D->getDeclName())
624 return;
625
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000626 bool MayBeRedeclaration = true;
627
Douglas Gregor8acb7272008-12-11 16:49:14 +0000628 if (!isLookupMap()) {
629 unsigned Size = LookupPtr.getInt();
630
631 // The lookup data is stored as an array. Search through the array
632 // to find the insertion location.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000633 ScopedDecl **Array;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000634 if (Size == 0) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000635 Array = new ScopedDecl*[LookupIsMap - 1];
Douglas Gregor8acb7272008-12-11 16:49:14 +0000636 LookupPtr.setPointer(Array);
637 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000638 Array = static_cast<ScopedDecl **>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000639 }
640
641 // We always keep declarations of the same name next to each other
642 // in the array, so that it is easy to return multiple results
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000643 // from lookup().
644 unsigned FirstMatch;
645 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
646 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregor39677622008-12-11 20:41:00 +0000647 break;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000648
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000649 unsigned InsertPos = FirstMatch;
650 if (FirstMatch != Size) {
651 // We found another declaration with the same name. First
652 // determine whether this is a redeclaration of an existing
653 // declaration in this scope, in which case we will replace the
654 // existing declaration.
655 unsigned LastMatch = FirstMatch;
656 for (; LastMatch != Size; ++LastMatch) {
657 if (Array[LastMatch]->getDeclName() != D->getDeclName())
658 break;
659
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000660 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000661 // D is a redeclaration of an existing element in the
662 // array. Replace that element with D.
663 Array[LastMatch] = D;
664 return;
665 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000666 }
667
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000668 // [FirstMatch, LastMatch) contains the set of declarations that
669 // have the same name as this declaration. Determine where the
670 // declaration D will be inserted into this range.
671 if (D->getIdentifierNamespace() == Decl::IDNS_Tag)
672 InsertPos = LastMatch;
673 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
674 InsertPos = LastMatch - 1;
675 else
676 InsertPos = LastMatch;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000677 }
678
679 if (Size < LookupIsMap - 1) {
680 // The new declaration will fit in the array. Insert the new
681 // declaration at the position Match in the array.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000682 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000683 Array[Idx] = Array[Idx-1];
684
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000685 Array[InsertPos] = D;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000686 LookupPtr.setInt(Size + 1);
687 return;
688 }
689
690 // We've reached capacity in this array. Create a map and copy in
691 // all of the declarations that were stored in the array.
692 StoredDeclsMap *Map = new StoredDeclsMap(16);
693 LookupPtr.setPointer(Map);
694 LookupPtr.setInt(LookupIsMap);
Douglas Gregor39677622008-12-11 20:41:00 +0000695 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000696 insertImpl(Array[Idx]);
697 delete [] Array;
698
699 // Fall through to perform insertion into the map.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000700 MayBeRedeclaration = false;
701 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000702
703 // Insert this declaration into the map.
704 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
705 StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000706 if (Pos != Map->end()) {
707 if (MayBeRedeclaration) {
708 // Determine if this declaration is actually a redeclaration.
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000709 std::vector<ScopedDecl *>::iterator Redecl
710 = std::find_if(Pos->second.begin(), Pos->second.end(),
711 std::bind1st(std::mem_fun(&ScopedDecl::declarationReplaces),
712 D));
713 if (Redecl != Pos->second.end()) {
714 *Redecl = D;
715 return;
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000716 }
717 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000718
Douglas Gregor8acb7272008-12-11 16:49:14 +0000719 // Put this declaration into the appropriate slot.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000720 if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
721 Pos->second.push_back(D);
722 else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
723 ScopedDecl *TagD = Pos->second.back();
724 Pos->second.back() = D;
725 Pos->second.push_back(TagD);
726 } else
727 Pos->second.push_back(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000728 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000729 (*Map)[D->getDeclName()].push_back(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000730 }
731}