blob: 860a65a1e3e117bed41596949c963f9591bbb829 [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";
Steve Naroffb72e1972009-01-14 01:27:31 +000091 case Field: return "Field";
Eli Friedmana8a09ac2008-06-07 16:52:53 +000092 }
93}
94
95bool Decl::CollectingStats(bool Enable) {
96 if (Enable)
97 StatSwitch = true;
98 return StatSwitch;
99}
100
101void Decl::PrintStats() {
102 fprintf(stderr, "*** Decl Stats:\n");
103 fprintf(stderr, " %d decls total.\n",
Fariborz Jahaniana7babab2008-12-20 21:06:28 +0000104 int(nFuncs+nVars+nParmVars+nOriginalParmVars+nFieldDecls+nSUC+nCXXSUC+
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000105 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
106 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
Douglas Gregord2baafd2008-10-21 16:13:35 +0000107 nAtDefsFieldDecls+nNamespaces+nOverFuncs));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000108 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
109 nNamespaces, (int)sizeof(NamespaceDecl),
110 int(nNamespaces*sizeof(NamespaceDecl)));
Douglas Gregord2baafd2008-10-21 16:13:35 +0000111 fprintf(stderr, " %d overloaded function decls, %d each (%d bytes)\n",
112 nOverFuncs, (int)sizeof(OverloadedFunctionDecl),
113 int(nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000114 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
115 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
116 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
117 nVars, (int)sizeof(VarDecl),
118 int(nVars*sizeof(VarDecl)));
119 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
120 nParmVars, (int)sizeof(ParmVarDecl),
121 int(nParmVars*sizeof(ParmVarDecl)));
Fariborz Jahaniana7babab2008-12-20 21:06:28 +0000122 fprintf(stderr, " %d original parameter variable decls, %d each (%d bytes)\n",
123 nOriginalParmVars, (int)sizeof(ParmVarWithOriginalTypeDecl),
124 int(nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000125 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
126 nFieldDecls, (int)sizeof(FieldDecl),
127 int(nFieldDecls*sizeof(FieldDecl)));
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000128 fprintf(stderr, " %d @defs generated field decls, %d each (%d bytes)\n",
129 nAtDefsFieldDecls, (int)sizeof(ObjCAtDefsFieldDecl),
130 int(nAtDefsFieldDecls*sizeof(ObjCAtDefsFieldDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000131 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
132 nSUC, (int)sizeof(RecordDecl),
133 int(nSUC*sizeof(RecordDecl)));
Argiris Kirtzidisaefcabd2008-08-10 01:47:31 +0000134 fprintf(stderr, " %d C++ struct/union/class decls, %d each (%d bytes)\n",
135 nCXXSUC, (int)sizeof(CXXRecordDecl),
136 int(nCXXSUC*sizeof(CXXRecordDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000137 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
138 nEnumDecls, (int)sizeof(EnumDecl),
139 int(nEnumDecls*sizeof(EnumDecl)));
140 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
141 nEnumConst, (int)sizeof(EnumConstantDecl),
142 int(nEnumConst*sizeof(EnumConstantDecl)));
143 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
144 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
145 // Objective-C decls...
146 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
147 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
148 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
149 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
150 nIvarDecls, (int)sizeof(ObjCIvarDecl),
151 int(nIvarDecls*sizeof(ObjCIvarDecl)));
152 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
153 nClassDecls, (int)sizeof(ObjCClassDecl),
154 int(nClassDecls*sizeof(ObjCClassDecl)));
155 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
156 nMethodDecls, (int)sizeof(ObjCMethodDecl),
157 int(nMethodDecls*sizeof(ObjCMethodDecl)));
158 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
159 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
160 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
161 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
162 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
163 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
164 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
165 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
166 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
167
168 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
169 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
170 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
171
172 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
173 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
174 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
175
176 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
177 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
178 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
179
180 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
181 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
182 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
183
184 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
185 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
186 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
187
188 fprintf(stderr, "Total bytes = %d\n",
189 int(nFuncs*sizeof(FunctionDecl)+
190 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Fariborz Jahaniana7babab2008-12-20 21:06:28 +0000191 nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)+
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000192 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Douglas Gregor8acb7272008-12-11 16:49:14 +0000193 nCXXSUC*sizeof(CXXRecordDecl)+
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000194 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
195 nTypedef*sizeof(TypedefDecl)+
196 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
197 nIvarDecls*sizeof(ObjCIvarDecl)+
198 nClassDecls*sizeof(ObjCClassDecl)+
199 nMethodDecls*sizeof(ObjCMethodDecl)+
200 nProtocolDecls*sizeof(ObjCProtocolDecl)+
201 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
202 nCategoryDecls*sizeof(ObjCCategoryDecl)+
203 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
204 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
205 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
206 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
207 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
208 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
209 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
Douglas Gregord2baafd2008-10-21 16:13:35 +0000210 nNamespaces*sizeof(NamespaceDecl)+
211 nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000212
213}
214
215void Decl::addDeclKind(Kind k) {
216 switch (k) {
217 case Namespace: nNamespaces++; break;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000218 case OverloadedFunction: nOverFuncs++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000219 case Typedef: nTypedef++; break;
220 case Function: nFuncs++; break;
221 case Var: nVars++; break;
222 case ParmVar: nParmVars++; break;
Fariborz Jahaniana7babab2008-12-20 21:06:28 +0000223 case OriginalParmVar: nOriginalParmVars++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000224 case EnumConstant: nEnumConst++; break;
225 case Field: nFieldDecls++; break;
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000226 case Record: nSUC++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000227 case Enum: nEnumDecls++; break;
Steve Naroff451f83c2009-01-09 15:36:25 +0000228 case ObjCContainer: break; // is abstract...no need to account for.
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000229 case ObjCInterface: nInterfaceDecls++; break;
230 case ObjCClass: nClassDecls++; break;
231 case ObjCMethod: nMethodDecls++; break;
232 case ObjCProtocol: nProtocolDecls++; break;
233 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
234 case ObjCCategory: nCategoryDecls++; break;
235 case ObjCIvar: nIvarDecls++; break;
Ted Kremeneke5bedfe2008-08-20 03:26:33 +0000236 case ObjCAtDefsField: nAtDefsFieldDecls++; break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000237 case ObjCImplementation: nObjCImplementationDecls++; break;
238 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
239 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
240 case ObjCProperty: nObjCPropertyDecl++; break;
241 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
242 case LinkageSpec: nLinkageSpecDecl++; break;
243 case FileScopeAsm: nFileScopeAsmDecl++; break;
Steve Naroff9ac456d2008-10-08 17:01:13 +0000244 case Block: nBlockDecls++; break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000245 case ImplicitParam:
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000246 case TranslationUnit: break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000247
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000248 case CXXRecord: nCXXSUC++; break;
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000249 // FIXME: Statistics for C++ decls.
Douglas Gregordd861062008-12-05 18:15:24 +0000250 case TemplateTypeParm:
251 case NonTypeTemplateParm:
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000252 case CXXMethod:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000253 case CXXConstructor:
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000254 case CXXDestructor:
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000255 case CXXConversion:
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +0000256 case CXXClassVar:
257 break;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000258 }
259}
260
261//===----------------------------------------------------------------------===//
262// Decl Implementation
263//===----------------------------------------------------------------------===//
264
265// Out-of-line virtual method providing a home for Decl.
266Decl::~Decl() {
267 if (!HasAttrs)
268 return;
269
270 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
271 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
272
273 // release attributes.
274 delete it->second;
275 invalidateAttrs();
276}
277
278void Decl::addAttr(Attr *NewAttr) {
279 if (!DeclAttrs)
280 DeclAttrs = new DeclAttrMapTy();
281
282 Attr *&ExistingAttr = (*DeclAttrs)[this];
283
284 NewAttr->setNext(ExistingAttr);
285 ExistingAttr = NewAttr;
286
287 HasAttrs = true;
288}
289
290void Decl::invalidateAttrs() {
291 if (!HasAttrs) return;
292
293 HasAttrs = false;
294 (*DeclAttrs)[this] = 0;
295 DeclAttrs->erase(this);
296
297 if (DeclAttrs->empty()) {
298 delete DeclAttrs;
299 DeclAttrs = 0;
300 }
301}
302
303const Attr *Decl::getAttrs() const {
304 if (!HasAttrs)
305 return 0;
306
307 return (*DeclAttrs)[this];
308}
309
310void Decl::swapAttrs(Decl *RHS) {
311 bool HasLHSAttr = this->HasAttrs;
312 bool HasRHSAttr = RHS->HasAttrs;
313
314 // Usually, neither decl has attrs, nothing to do.
315 if (!HasLHSAttr && !HasRHSAttr) return;
316
317 // If 'this' has no attrs, swap the other way.
318 if (!HasLHSAttr)
319 return RHS->swapAttrs(this);
320
321 // Handle the case when both decls have attrs.
322 if (HasRHSAttr) {
323 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
324 return;
325 }
326
327 // Otherwise, LHS has an attr and RHS doesn't.
328 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
329 (*DeclAttrs).erase(this);
330 this->HasAttrs = false;
331 RHS->HasAttrs = true;
332}
333
334
335void Decl::Destroy(ASTContext& C) {
Douglas Gregor8314d742009-01-13 19:47:12 +0000336#if 0
337 // FIXME: This causes double-destroys in some cases, so it is
338 // disabled at the moment.
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000339 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
340
341 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
342 // within the loop, only the Destroy method for the first ScopedDecl
343 // will deallocate all of the ScopedDecls in a chain.
344
345 ScopedDecl* N = SD->getNextDeclarator();
346
347 while (N) {
348 ScopedDecl* Tmp = N->getNextDeclarator();
349 N->NextDeclarator = 0x0;
350 N->Destroy(C);
351 N = Tmp;
352 }
353 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000354#endif
355
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000356 this->~Decl();
357 C.getAllocator().Deallocate((void *)this);
358}
359
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000360Decl *Decl::castFromDeclContext (const DeclContext *D) {
361 return DeclContext::CastTo<Decl>(D);
362}
363
364DeclContext *Decl::castToDeclContext(const Decl *D) {
365 return DeclContext::CastTo<DeclContext>(D);
366}
367
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000368//===----------------------------------------------------------------------===//
369// DeclContext Implementation
370//===----------------------------------------------------------------------===//
371
Argiris Kirtzidis5703f442008-11-19 17:36:39 +0000372const DeclContext *DeclContext::getParent() const {
373 if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000374 return SD->getDeclContext();
Argiris Kirtzidis5703f442008-11-19 17:36:39 +0000375 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
Steve Naroff52059382008-10-10 01:28:17 +0000376 return BD->getParentContext();
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000377 else
378 return NULL;
379}
Argiris Kirtzidis9cd599b2008-11-19 18:01:13 +0000380
381const DeclContext *DeclContext::getLexicalParent() const {
382 if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
383 return SD->getLexicalDeclContext();
Argiris Kirtzidisbc470282008-11-19 18:07:24 +0000384 return getParent();
Argiris Kirtzidis9cd599b2008-11-19 18:01:13 +0000385}
Douglas Gregor8acb7272008-12-11 16:49:14 +0000386
Douglas Gregor8acb7272008-12-11 16:49:14 +0000387// FIXME: We really want to use a DenseSet here to eliminate the
388// redundant storage of the declaration names, but (1) it doesn't give
389// us the ability to search based on DeclarationName, (2) we really
390// need something more like a DenseMultiSet, and (3) it's
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000391// implemented in terms of DenseMap anyway. However, this data
392// structure is really space-inefficient, so we'll have to do
393// something.
394typedef llvm::DenseMap<DeclarationName, std::vector<ScopedDecl*> >
395 StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000396
397DeclContext::~DeclContext() {
398 unsigned Size = LookupPtr.getInt();
399 if (Size == LookupIsMap) {
400 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
401 delete Map;
402 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000403 ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000404 delete [] Array;
405 }
406}
407
408void DeclContext::DestroyDecls(ASTContext &C) {
Steve Naroffb72e1972009-01-14 01:27:31 +0000409 for (decl_iterator D = decls_begin(); D != decls_end(); ) {
410 // FIXME: assert that this condition holds.
411 if ((*D)->getLexicalDeclContext() == this)
412 // Advance the cursor (via NextDeclInScope) *before* doing the Destroy.
413 (*D++)->Destroy(C);
414 else
415 ++D;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000416 }
417}
418
Douglas Gregord8028382009-01-05 19:45:36 +0000419bool DeclContext::isTransparentContext() const {
420 if (DeclKind == Decl::Enum)
421 return true; // FIXME: Check for C++0x scoped enums
422 else if (DeclKind == Decl::LinkageSpec)
423 return true;
424 else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord)
Douglas Gregor723d3332009-01-07 00:43:41 +0000425 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000426 else if (DeclKind == Decl::Namespace)
427 return false; // FIXME: Check for C++0x inline namespaces
428
429 return false;
430}
431
Steve Naroffab63fd62009-01-08 17:28:14 +0000432DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000433 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000434 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000435 case Decl::LinkageSpec:
436 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000437 // There is only one DeclContext for these entities.
438 return this;
439
440 case Decl::Namespace:
441 // The original namespace is our primary context.
442 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
443
444 case Decl::Enum:
Douglas Gregord8028382009-01-05 19:45:36 +0000445#if 0
446 // FIXME: See the comment for CXXRecord, below.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000447 // The declaration associated with the enumeration type is our
448 // primary context.
449 return Context.getTypeDeclType(static_cast<EnumDecl*>(this))
450 ->getAsEnumType()->getDecl();
Douglas Gregord8028382009-01-05 19:45:36 +0000451#else
452 return this;
453#endif
Douglas Gregor8acb7272008-12-11 16:49:14 +0000454
455 case Decl::Record:
456 case Decl::CXXRecord: {
457 // The declaration associated with the type is be our primary
458 // context.
459#if 0
460 // FIXME: This is what we expect to do. However, it doesn't work
461 // because ASTContext::setTagDefinition changes the result of
462 // Context.getTypeDeclType, meaning that our "primary" declaration
463 // of a RecordDecl/CXXRecordDecl will change, and we won't be able
464 // to find any values inserted into the earlier "primary"
465 // declaration. We need better tracking of redeclarations and
466 // definitions.
467 QualType Type = Context.getTypeDeclType(static_cast<RecordDecl*>(this));
468 return Type->getAsRecordType()->getDecl();
469#else
470 // FIXME: This hack will work for now, because the declaration we
471 // create when we're defining the record is the one we'll use as
472 // the definition later.
473 return this;
474#endif
475 }
476
477 case Decl::ObjCMethod:
478 return this;
479
480 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000481 case Decl::ObjCProtocol:
482 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000483 // FIXME: Can Objective-C interfaces be forward-declared?
484 return this;
485
Steve Naroffab63fd62009-01-08 17:28:14 +0000486 case Decl::ObjCImplementation:
487 case Decl::ObjCCategoryImpl:
488 return this;
489
Douglas Gregor8acb7272008-12-11 16:49:14 +0000490 default:
491 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
492 "Unknown DeclContext kind");
493 return this;
494 }
495}
496
497DeclContext *DeclContext::getNextContext() {
498 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000499 case Decl::TranslationUnit:
500 case Decl::Enum:
501 case Decl::Record:
502 case Decl::CXXRecord:
503 case Decl::ObjCMethod:
504 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000505 case Decl::ObjCCategory:
506 case Decl::ObjCProtocol:
507 case Decl::ObjCImplementation:
508 case Decl::ObjCCategoryImpl:
Douglas Gregord8028382009-01-05 19:45:36 +0000509 case Decl::LinkageSpec:
510 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000511 // There is only one DeclContext for these entities.
512 return 0;
513
514 case Decl::Namespace:
515 // Return the next namespace
516 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
517
518 default:
519 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
520 "Unknown DeclContext kind");
521 return 0;
522 }
523}
524
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000525void DeclContext::addDecl(ScopedDecl *D) {
Douglas Gregord394a272009-01-09 18:51:29 +0000526 assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
Douglas Gregord1675382009-01-09 19:42:16 +0000527 assert(!D->NextDeclInScope && D != LastDecl &&
528 "Decl already inserted into a DeclContext");
529
530 if (FirstDecl) {
531 LastDecl->NextDeclInScope = D;
532 LastDecl = D;
533 } else {
534 FirstDecl = LastDecl = D;
535 }
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000536 D->getDeclContext()->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000537}
538
Douglas Gregord8028382009-01-05 19:45:36 +0000539/// buildLookup - Build the lookup data structure with all of the
540/// declarations in DCtx (and any other contexts linked to it or
541/// transparent contexts nested within it).
Steve Naroffab63fd62009-01-08 17:28:14 +0000542void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000543 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000544 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
545 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000546 // Insert this declaration into the lookup structure
547 insertImpl(*D);
548
549 // If this declaration is itself a transparent declaration context,
550 // add its members (recursively).
551 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
552 if (InnerCtx->isTransparentContext())
Steve Naroffab63fd62009-01-08 17:28:14 +0000553 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000554 }
555 }
556}
557
Douglas Gregor8acb7272008-12-11 16:49:14 +0000558DeclContext::lookup_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000559DeclContext::lookup(DeclarationName Name) {
560 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000561 if (PrimaryContext != this)
Steve Naroffab63fd62009-01-08 17:28:14 +0000562 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000563
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000564 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000565 /// all of the linked DeclContexts (in declaration order!) and
566 /// inserting their values.
Douglas Gregord8028382009-01-05 19:45:36 +0000567 if (LookupPtr.getPointer() == 0)
Steve Naroffab63fd62009-01-08 17:28:14 +0000568 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000569
Douglas Gregor8acb7272008-12-11 16:49:14 +0000570 if (isLookupMap()) {
571 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
572 StoredDeclsMap::iterator Pos = Map->find(Name);
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000573 if (Pos != Map->end())
574 return lookup_result(&Pos->second.front(),
575 &Pos->second.front() + Pos->second.size());
576 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000577 }
578
579 // We have a small array. Look into it.
580 unsigned Size = LookupPtr.getInt();
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000581 ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
Douglas Gregor39677622008-12-11 20:41:00 +0000582 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000583 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000584 unsigned Last = Idx + 1;
585 while (Last != Size && Array[Last]->getDeclName() == Name)
586 ++Last;
587 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000588 }
589
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000590 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000591}
592
593DeclContext::lookup_const_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000594DeclContext::lookup(DeclarationName Name) const {
595 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000596}
597
Douglas Gregor38d38a02009-01-07 02:48:43 +0000598const DeclContext *DeclContext::getLookupContext() const {
599 const DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000600 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000601 while (Ctx->isTransparentContext())
602 Ctx = Ctx->getParent();
603 return Ctx;
604}
605
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000606void DeclContext::insert(ScopedDecl *D) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000607 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000608 if (PrimaryContext != this) {
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000609 PrimaryContext->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000610 return;
611 }
612
613 // If we already have a lookup data structure, perform the insertion
614 // into it. Otherwise, be lazy and don't build that structure until
615 // someone asks for it.
616 if (LookupPtr.getPointer())
617 insertImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000618
Douglas Gregord8028382009-01-05 19:45:36 +0000619 // If we are a transparent context, insert into our parent context,
620 // too. This operation is recursive.
621 if (isTransparentContext())
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000622 getParent()->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000623}
624
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000625void DeclContext::insertImpl(ScopedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000626 // Skip unnamed declarations.
627 if (!D->getDeclName())
628 return;
629
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000630 bool MayBeRedeclaration = true;
631
Douglas Gregor8acb7272008-12-11 16:49:14 +0000632 if (!isLookupMap()) {
633 unsigned Size = LookupPtr.getInt();
634
635 // The lookup data is stored as an array. Search through the array
636 // to find the insertion location.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000637 ScopedDecl **Array;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000638 if (Size == 0) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000639 Array = new ScopedDecl*[LookupIsMap - 1];
Douglas Gregor8acb7272008-12-11 16:49:14 +0000640 LookupPtr.setPointer(Array);
641 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000642 Array = static_cast<ScopedDecl **>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000643 }
644
645 // We always keep declarations of the same name next to each other
646 // in the array, so that it is easy to return multiple results
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000647 // from lookup().
648 unsigned FirstMatch;
649 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
650 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregor39677622008-12-11 20:41:00 +0000651 break;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000652
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000653 unsigned InsertPos = FirstMatch;
654 if (FirstMatch != Size) {
655 // We found another declaration with the same name. First
656 // determine whether this is a redeclaration of an existing
657 // declaration in this scope, in which case we will replace the
658 // existing declaration.
659 unsigned LastMatch = FirstMatch;
660 for (; LastMatch != Size; ++LastMatch) {
661 if (Array[LastMatch]->getDeclName() != D->getDeclName())
662 break;
663
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000664 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000665 // D is a redeclaration of an existing element in the
666 // array. Replace that element with D.
667 Array[LastMatch] = D;
668 return;
669 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000670 }
671
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000672 // [FirstMatch, LastMatch) contains the set of declarations that
673 // have the same name as this declaration. Determine where the
674 // declaration D will be inserted into this range.
675 if (D->getIdentifierNamespace() == Decl::IDNS_Tag)
676 InsertPos = LastMatch;
677 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
678 InsertPos = LastMatch - 1;
679 else
680 InsertPos = LastMatch;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000681 }
682
683 if (Size < LookupIsMap - 1) {
684 // The new declaration will fit in the array. Insert the new
685 // declaration at the position Match in the array.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000686 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000687 Array[Idx] = Array[Idx-1];
688
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000689 Array[InsertPos] = D;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000690 LookupPtr.setInt(Size + 1);
691 return;
692 }
693
694 // We've reached capacity in this array. Create a map and copy in
695 // all of the declarations that were stored in the array.
696 StoredDeclsMap *Map = new StoredDeclsMap(16);
697 LookupPtr.setPointer(Map);
698 LookupPtr.setInt(LookupIsMap);
Douglas Gregor39677622008-12-11 20:41:00 +0000699 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000700 insertImpl(Array[Idx]);
701 delete [] Array;
702
703 // Fall through to perform insertion into the map.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000704 MayBeRedeclaration = false;
705 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000706
707 // Insert this declaration into the map.
708 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
709 StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000710 if (Pos != Map->end()) {
711 if (MayBeRedeclaration) {
712 // Determine if this declaration is actually a redeclaration.
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000713 std::vector<ScopedDecl *>::iterator Redecl
714 = std::find_if(Pos->second.begin(), Pos->second.end(),
715 std::bind1st(std::mem_fun(&ScopedDecl::declarationReplaces),
716 D));
717 if (Redecl != Pos->second.end()) {
718 *Redecl = D;
719 return;
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000720 }
721 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000722
Douglas Gregor8acb7272008-12-11 16:49:14 +0000723 // Put this declaration into the appropriate slot.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000724 if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
725 Pos->second.push_back(D);
726 else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
727 ScopedDecl *TagD = Pos->second.back();
728 Pos->second.back() = D;
729 Pos->second.push_back(TagD);
730 } else
731 Pos->second.push_back(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000732 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000733 (*Map)[D->getDeclName()].push_back(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000734 }
735}