blob: 28656873ab0596c858b4b3c4668db315909899bf [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 Gregor8acb7272008-12-11 16:49:14 +0000445 case Decl::Record:
Douglas Gregor98b27542009-01-17 00:42:38 +0000446 case Decl::CXXRecord:
447 // If this is a tag type that has a definition or is currently
448 // being defined, that definition is our primary context.
449 if (TagType *TagT = cast_or_null<TagType>(cast<TagDecl>(this)->TypeForDecl))
450 if (TagT->isBeingDefined() ||
451 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
452 return TagT->getDecl();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000453 return this;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000454
455 case Decl::ObjCMethod:
456 return this;
457
458 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000459 case Decl::ObjCProtocol:
460 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000461 // FIXME: Can Objective-C interfaces be forward-declared?
462 return this;
463
Steve Naroffab63fd62009-01-08 17:28:14 +0000464 case Decl::ObjCImplementation:
465 case Decl::ObjCCategoryImpl:
466 return this;
467
Douglas Gregor8acb7272008-12-11 16:49:14 +0000468 default:
469 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
470 "Unknown DeclContext kind");
471 return this;
472 }
473}
474
475DeclContext *DeclContext::getNextContext() {
476 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000477 case Decl::TranslationUnit:
478 case Decl::Enum:
479 case Decl::Record:
480 case Decl::CXXRecord:
481 case Decl::ObjCMethod:
482 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000483 case Decl::ObjCCategory:
484 case Decl::ObjCProtocol:
485 case Decl::ObjCImplementation:
486 case Decl::ObjCCategoryImpl:
Douglas Gregord8028382009-01-05 19:45:36 +0000487 case Decl::LinkageSpec:
488 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000489 // There is only one DeclContext for these entities.
490 return 0;
491
492 case Decl::Namespace:
493 // Return the next namespace
494 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
495
496 default:
497 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
498 "Unknown DeclContext kind");
499 return 0;
500 }
501}
502
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000503void DeclContext::addDecl(ScopedDecl *D) {
Douglas Gregord394a272009-01-09 18:51:29 +0000504 assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
Douglas Gregord1675382009-01-09 19:42:16 +0000505 assert(!D->NextDeclInScope && D != LastDecl &&
506 "Decl already inserted into a DeclContext");
507
508 if (FirstDecl) {
509 LastDecl->NextDeclInScope = D;
510 LastDecl = D;
511 } else {
512 FirstDecl = LastDecl = D;
513 }
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000514 D->getDeclContext()->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000515}
516
Douglas Gregord8028382009-01-05 19:45:36 +0000517/// buildLookup - Build the lookup data structure with all of the
518/// declarations in DCtx (and any other contexts linked to it or
519/// transparent contexts nested within it).
Steve Naroffab63fd62009-01-08 17:28:14 +0000520void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000521 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000522 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
523 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000524 // Insert this declaration into the lookup structure
525 insertImpl(*D);
526
527 // If this declaration is itself a transparent declaration context,
528 // add its members (recursively).
529 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
530 if (InnerCtx->isTransparentContext())
Steve Naroffab63fd62009-01-08 17:28:14 +0000531 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000532 }
533 }
534}
535
Douglas Gregor8acb7272008-12-11 16:49:14 +0000536DeclContext::lookup_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000537DeclContext::lookup(DeclarationName Name) {
538 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000539 if (PrimaryContext != this)
Steve Naroffab63fd62009-01-08 17:28:14 +0000540 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000541
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000542 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000543 /// all of the linked DeclContexts (in declaration order!) and
544 /// inserting their values.
Douglas Gregord8028382009-01-05 19:45:36 +0000545 if (LookupPtr.getPointer() == 0)
Steve Naroffab63fd62009-01-08 17:28:14 +0000546 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000547
Douglas Gregor8acb7272008-12-11 16:49:14 +0000548 if (isLookupMap()) {
549 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
550 StoredDeclsMap::iterator Pos = Map->find(Name);
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000551 if (Pos != Map->end())
552 return lookup_result(&Pos->second.front(),
553 &Pos->second.front() + Pos->second.size());
554 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000555 }
556
557 // We have a small array. Look into it.
558 unsigned Size = LookupPtr.getInt();
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000559 ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
Douglas Gregor39677622008-12-11 20:41:00 +0000560 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000561 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000562 unsigned Last = Idx + 1;
563 while (Last != Size && Array[Last]->getDeclName() == Name)
564 ++Last;
565 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000566 }
567
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000568 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000569}
570
571DeclContext::lookup_const_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000572DeclContext::lookup(DeclarationName Name) const {
573 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000574}
575
Douglas Gregor38d38a02009-01-07 02:48:43 +0000576const DeclContext *DeclContext::getLookupContext() const {
577 const DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000578 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000579 while (Ctx->isTransparentContext())
580 Ctx = Ctx->getParent();
581 return Ctx;
582}
583
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000584void DeclContext::insert(ScopedDecl *D) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000585 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000586 if (PrimaryContext != this) {
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000587 PrimaryContext->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000588 return;
589 }
590
591 // If we already have a lookup data structure, perform the insertion
592 // into it. Otherwise, be lazy and don't build that structure until
593 // someone asks for it.
594 if (LookupPtr.getPointer())
595 insertImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000596
Douglas Gregord8028382009-01-05 19:45:36 +0000597 // If we are a transparent context, insert into our parent context,
598 // too. This operation is recursive.
599 if (isTransparentContext())
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000600 getParent()->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000601}
602
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000603void DeclContext::insertImpl(ScopedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000604 // Skip unnamed declarations.
605 if (!D->getDeclName())
606 return;
607
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000608 bool MayBeRedeclaration = true;
609
Douglas Gregor8acb7272008-12-11 16:49:14 +0000610 if (!isLookupMap()) {
611 unsigned Size = LookupPtr.getInt();
612
613 // The lookup data is stored as an array. Search through the array
614 // to find the insertion location.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000615 ScopedDecl **Array;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000616 if (Size == 0) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000617 Array = new ScopedDecl*[LookupIsMap - 1];
Douglas Gregor8acb7272008-12-11 16:49:14 +0000618 LookupPtr.setPointer(Array);
619 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000620 Array = static_cast<ScopedDecl **>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000621 }
622
623 // We always keep declarations of the same name next to each other
624 // in the array, so that it is easy to return multiple results
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000625 // from lookup().
626 unsigned FirstMatch;
627 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
628 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregor39677622008-12-11 20:41:00 +0000629 break;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000630
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000631 unsigned InsertPos = FirstMatch;
632 if (FirstMatch != Size) {
633 // We found another declaration with the same name. First
634 // determine whether this is a redeclaration of an existing
635 // declaration in this scope, in which case we will replace the
636 // existing declaration.
637 unsigned LastMatch = FirstMatch;
638 for (; LastMatch != Size; ++LastMatch) {
639 if (Array[LastMatch]->getDeclName() != D->getDeclName())
640 break;
641
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000642 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000643 // D is a redeclaration of an existing element in the
644 // array. Replace that element with D.
645 Array[LastMatch] = D;
646 return;
647 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000648 }
649
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000650 // [FirstMatch, LastMatch) contains the set of declarations that
651 // have the same name as this declaration. Determine where the
652 // declaration D will be inserted into this range.
653 if (D->getIdentifierNamespace() == Decl::IDNS_Tag)
654 InsertPos = LastMatch;
655 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
656 InsertPos = LastMatch - 1;
657 else
658 InsertPos = LastMatch;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000659 }
660
661 if (Size < LookupIsMap - 1) {
662 // The new declaration will fit in the array. Insert the new
663 // declaration at the position Match in the array.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000664 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000665 Array[Idx] = Array[Idx-1];
666
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000667 Array[InsertPos] = D;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000668 LookupPtr.setInt(Size + 1);
669 return;
670 }
671
672 // We've reached capacity in this array. Create a map and copy in
673 // all of the declarations that were stored in the array.
674 StoredDeclsMap *Map = new StoredDeclsMap(16);
675 LookupPtr.setPointer(Map);
676 LookupPtr.setInt(LookupIsMap);
Douglas Gregor39677622008-12-11 20:41:00 +0000677 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000678 insertImpl(Array[Idx]);
679 delete [] Array;
680
681 // Fall through to perform insertion into the map.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000682 MayBeRedeclaration = false;
683 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000684
685 // Insert this declaration into the map.
686 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
687 StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000688 if (Pos != Map->end()) {
689 if (MayBeRedeclaration) {
690 // Determine if this declaration is actually a redeclaration.
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000691 std::vector<ScopedDecl *>::iterator Redecl
692 = std::find_if(Pos->second.begin(), Pos->second.end(),
693 std::bind1st(std::mem_fun(&ScopedDecl::declarationReplaces),
694 D));
695 if (Redecl != Pos->second.end()) {
696 *Redecl = D;
697 return;
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000698 }
699 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000700
Douglas Gregor8acb7272008-12-11 16:49:14 +0000701 // Put this declaration into the appropriate slot.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000702 if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
703 Pos->second.push_back(D);
704 else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
705 ScopedDecl *TagD = Pos->second.back();
706 Pos->second.back() = D;
707 Pos->second.push_back(TagD);
708 } else
709 Pos->second.push_back(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000710 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000711 (*Map)[D->getDeclName()].push_back(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000712 }
713}