blob: 49890144a956c1e9991d6c5b0ec5965c60ba9a81 [file] [log] [blame]
Eli Friedman56d29372008-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 Dunbare91593e2008-08-11 04:54:23 +000015#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000016#include "clang/AST/DeclCXX.h"
Eli Friedman56d29372008-06-07 16:52:53 +000017#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000018#include "clang/AST/Type.h"
Eli Friedman56d29372008-06-07 16:52:53 +000019#include "llvm/ADT/DenseMap.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000020#include <algorithm>
21#include <functional>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000022#include <vector>
Eli Friedman56d29372008-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 Jahanian4f5420d2008-12-20 21:06:28 +000033static unsigned nOriginalParmVars = 0;
Eli Friedman56d29372008-06-07 16:52:53 +000034static unsigned nSUC = 0;
Argyrios Kyrtzidis55d71f92008-08-10 01:47:31 +000035static unsigned nCXXSUC = 0;
Eli Friedman56d29372008-06-07 16:52:53 +000036static unsigned nEnumConst = 0;
37static unsigned nEnumDecls = 0;
38static unsigned nNamespaces = 0;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000039static unsigned nOverFuncs = 0;
Eli Friedman56d29372008-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 Kremenek01e67792008-08-20 03:26:33 +000049static unsigned nAtDefsFieldDecls = 0;
Eli Friedman56d29372008-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 Naroff56ee6892008-10-08 17:01:13 +000057static unsigned nBlockDecls = 0;
Eli Friedman56d29372008-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 Gregor8e9bebd2008-10-21 16:13:35 +000071 case OverloadedFunction: return "OverloadedFunction";
Eli Friedman56d29372008-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 Jahanian4f5420d2008-12-20 21:06:28 +000076 case OriginalParmVar: return "OriginalParmVar";
Eli Friedman56d29372008-06-07 16:52:53 +000077 case EnumConstant: return "EnumConstant";
78 case ObjCIvar: return "ObjCIvar";
79 case ObjCInterface: return "ObjCInterface";
Steve Naroffd40910b2008-12-01 20:33:01 +000080 case ObjCImplementation: return "ObjCImplementation";
Eli Friedman56d29372008-06-07 16:52:53 +000081 case ObjCClass: return "ObjCClass";
82 case ObjCMethod: return "ObjCMethod";
83 case ObjCProtocol: return "ObjCProtocol";
Steve Naroffd40910b2008-12-01 20:33:01 +000084 case ObjCProperty: return "ObjCProperty";
85 case ObjCPropertyImpl: return "ObjCPropertyImpl";
Eli Friedman56d29372008-06-07 16:52:53 +000086 case ObjCForwardProtocol: return "ObjCForwardProtocol";
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000087 case Record: return "Record";
88 case CXXRecord: return "CXXRecord";
Eli Friedman56d29372008-06-07 16:52:53 +000089 case Enum: return "Enum";
Steve Naroff56ee6892008-10-08 17:01:13 +000090 case Block: return "Block";
Eli Friedman56d29372008-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 Jahanian4f5420d2008-12-20 21:06:28 +0000103 int(nFuncs+nVars+nParmVars+nOriginalParmVars+nFieldDecls+nSUC+nCXXSUC+
Eli Friedman56d29372008-06-07 16:52:53 +0000104 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
105 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000106 nAtDefsFieldDecls+nNamespaces+nOverFuncs));
Eli Friedman56d29372008-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 Gregor8e9bebd2008-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 Friedman56d29372008-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 Jahanian4f5420d2008-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 Friedman56d29372008-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 Kremenek01e67792008-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 Friedman56d29372008-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)));
Argyrios Kyrtzidis55d71f92008-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 Friedman56d29372008-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 Jahanian4f5420d2008-12-20 21:06:28 +0000190 nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)+
Eli Friedman56d29372008-06-07 16:52:53 +0000191 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Douglas Gregor44b43212008-12-11 16:49:14 +0000192 nCXXSUC*sizeof(CXXRecordDecl)+
Eli Friedman56d29372008-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 Gregor8e9bebd2008-10-21 16:13:35 +0000209 nNamespaces*sizeof(NamespaceDecl)+
210 nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedman56d29372008-06-07 16:52:53 +0000211
212}
213
214void Decl::addDeclKind(Kind k) {
215 switch (k) {
216 case Namespace: nNamespaces++; break;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000217 case OverloadedFunction: nOverFuncs++; break;
Eli Friedman56d29372008-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 Jahanian4f5420d2008-12-20 21:06:28 +0000222 case OriginalParmVar: nOriginalParmVars++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000223 case EnumConstant: nEnumConst++; break;
224 case Field: nFieldDecls++; break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000225 case Record: nSUC++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000226 case Enum: nEnumDecls++; break;
227 case ObjCInterface: nInterfaceDecls++; break;
228 case ObjCClass: nClassDecls++; break;
229 case ObjCMethod: nMethodDecls++; break;
230 case ObjCProtocol: nProtocolDecls++; break;
231 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
232 case ObjCCategory: nCategoryDecls++; break;
233 case ObjCIvar: nIvarDecls++; break;
Ted Kremenek01e67792008-08-20 03:26:33 +0000234 case ObjCAtDefsField: nAtDefsFieldDecls++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000235 case ObjCImplementation: nObjCImplementationDecls++; break;
236 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
237 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
238 case ObjCProperty: nObjCPropertyDecl++; break;
239 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
240 case LinkageSpec: nLinkageSpecDecl++; break;
241 case FileScopeAsm: nFileScopeAsmDecl++; break;
Steve Naroff56ee6892008-10-08 17:01:13 +0000242 case Block: nBlockDecls++; break;
Chris Lattner41110242008-06-17 18:05:57 +0000243 case ImplicitParam:
Eli Friedman56d29372008-06-07 16:52:53 +0000244 case TranslationUnit: break;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000245
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000246 case CXXRecord: nCXXSUC++; break;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000247 // FIXME: Statistics for C++ decls.
Douglas Gregor72c3f312008-12-05 18:15:24 +0000248 case TemplateTypeParm:
249 case NonTypeTemplateParm:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000250 case CXXMethod:
Douglas Gregorb48fe382008-10-31 09:07:45 +0000251 case CXXConstructor:
Douglas Gregor42a552f2008-11-05 20:51:48 +0000252 case CXXDestructor:
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000253 case CXXConversion:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000254 case CXXClassVar:
255 break;
Eli Friedman56d29372008-06-07 16:52:53 +0000256 }
257}
258
259//===----------------------------------------------------------------------===//
260// Decl Implementation
261//===----------------------------------------------------------------------===//
262
263// Out-of-line virtual method providing a home for Decl.
264Decl::~Decl() {
265 if (!HasAttrs)
266 return;
267
268 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
269 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
270
271 // release attributes.
272 delete it->second;
273 invalidateAttrs();
274}
275
276void Decl::addAttr(Attr *NewAttr) {
277 if (!DeclAttrs)
278 DeclAttrs = new DeclAttrMapTy();
279
280 Attr *&ExistingAttr = (*DeclAttrs)[this];
281
282 NewAttr->setNext(ExistingAttr);
283 ExistingAttr = NewAttr;
284
285 HasAttrs = true;
286}
287
288void Decl::invalidateAttrs() {
289 if (!HasAttrs) return;
290
291 HasAttrs = false;
292 (*DeclAttrs)[this] = 0;
293 DeclAttrs->erase(this);
294
295 if (DeclAttrs->empty()) {
296 delete DeclAttrs;
297 DeclAttrs = 0;
298 }
299}
300
301const Attr *Decl::getAttrs() const {
302 if (!HasAttrs)
303 return 0;
304
305 return (*DeclAttrs)[this];
306}
307
308void Decl::swapAttrs(Decl *RHS) {
309 bool HasLHSAttr = this->HasAttrs;
310 bool HasRHSAttr = RHS->HasAttrs;
311
312 // Usually, neither decl has attrs, nothing to do.
313 if (!HasLHSAttr && !HasRHSAttr) return;
314
315 // If 'this' has no attrs, swap the other way.
316 if (!HasLHSAttr)
317 return RHS->swapAttrs(this);
318
319 // Handle the case when both decls have attrs.
320 if (HasRHSAttr) {
321 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
322 return;
323 }
324
325 // Otherwise, LHS has an attr and RHS doesn't.
326 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
327 (*DeclAttrs).erase(this);
328 this->HasAttrs = false;
329 RHS->HasAttrs = true;
330}
331
332
333void Decl::Destroy(ASTContext& C) {
Eli Friedman56d29372008-06-07 16:52:53 +0000334 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
335
336 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
337 // within the loop, only the Destroy method for the first ScopedDecl
338 // will deallocate all of the ScopedDecls in a chain.
339
340 ScopedDecl* N = SD->getNextDeclarator();
341
342 while (N) {
343 ScopedDecl* Tmp = N->getNextDeclarator();
344 N->NextDeclarator = 0x0;
345 N->Destroy(C);
346 N = Tmp;
347 }
348 }
349
350 this->~Decl();
351 C.getAllocator().Deallocate((void *)this);
352}
353
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000354Decl *Decl::castFromDeclContext (const DeclContext *D) {
355 return DeclContext::CastTo<Decl>(D);
356}
357
358DeclContext *Decl::castToDeclContext(const Decl *D) {
359 return DeclContext::CastTo<DeclContext>(D);
360}
361
Eli Friedman56d29372008-06-07 16:52:53 +0000362//===----------------------------------------------------------------------===//
363// DeclContext Implementation
364//===----------------------------------------------------------------------===//
365
Argyrios Kyrtzidis20bc6762008-11-19 17:36:39 +0000366const DeclContext *DeclContext::getParent() const {
367 if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Eli Friedman56d29372008-06-07 16:52:53 +0000368 return SD->getDeclContext();
Argyrios Kyrtzidis20bc6762008-11-19 17:36:39 +0000369 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
Steve Naroff090276f2008-10-10 01:28:17 +0000370 return BD->getParentContext();
Eli Friedman56d29372008-06-07 16:52:53 +0000371 else
372 return NULL;
373}
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000374
375const DeclContext *DeclContext::getLexicalParent() const {
376 if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
377 return SD->getLexicalDeclContext();
Argyrios Kyrtzidis051c13a2008-11-19 18:07:24 +0000378 return getParent();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000379}
Douglas Gregor44b43212008-12-11 16:49:14 +0000380
Douglas Gregor44b43212008-12-11 16:49:14 +0000381// FIXME: We really want to use a DenseSet here to eliminate the
382// redundant storage of the declaration names, but (1) it doesn't give
383// us the ability to search based on DeclarationName, (2) we really
384// need something more like a DenseMultiSet, and (3) it's
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000385// implemented in terms of DenseMap anyway. However, this data
386// structure is really space-inefficient, so we'll have to do
387// something.
388typedef llvm::DenseMap<DeclarationName, std::vector<ScopedDecl*> >
389 StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000390
391DeclContext::~DeclContext() {
392 unsigned Size = LookupPtr.getInt();
393 if (Size == LookupIsMap) {
394 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
395 delete Map;
396 } else {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000397 ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
Douglas Gregor44b43212008-12-11 16:49:14 +0000398 delete [] Array;
399 }
400}
401
402void DeclContext::DestroyDecls(ASTContext &C) {
403 for (decl_iterator D = Decls.begin(); D != Decls.end(); ++D) {
404 if ((*D)->getLexicalDeclContext() == this)
405 (*D)->Destroy(C);
406 }
407}
408
Douglas Gregor074149e2009-01-05 19:45:36 +0000409bool DeclContext::isTransparentContext() const {
410 if (DeclKind == Decl::Enum)
411 return true; // FIXME: Check for C++0x scoped enums
412 else if (DeclKind == Decl::LinkageSpec)
413 return true;
414 else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000415 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000416 else if (DeclKind == Decl::Namespace)
417 return false; // FIXME: Check for C++0x inline namespaces
418
419 return false;
420}
421
Steve Naroff0701bbb2009-01-08 17:28:14 +0000422DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000423 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000424 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000425 case Decl::LinkageSpec:
426 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000427 // There is only one DeclContext for these entities.
428 return this;
429
430 case Decl::Namespace:
431 // The original namespace is our primary context.
432 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
433
434 case Decl::Enum:
Douglas Gregor074149e2009-01-05 19:45:36 +0000435#if 0
436 // FIXME: See the comment for CXXRecord, below.
Douglas Gregor44b43212008-12-11 16:49:14 +0000437 // The declaration associated with the enumeration type is our
438 // primary context.
439 return Context.getTypeDeclType(static_cast<EnumDecl*>(this))
440 ->getAsEnumType()->getDecl();
Douglas Gregor074149e2009-01-05 19:45:36 +0000441#else
442 return this;
443#endif
Douglas Gregor44b43212008-12-11 16:49:14 +0000444
445 case Decl::Record:
446 case Decl::CXXRecord: {
447 // The declaration associated with the type is be our primary
448 // context.
449#if 0
450 // FIXME: This is what we expect to do. However, it doesn't work
451 // because ASTContext::setTagDefinition changes the result of
452 // Context.getTypeDeclType, meaning that our "primary" declaration
453 // of a RecordDecl/CXXRecordDecl will change, and we won't be able
454 // to find any values inserted into the earlier "primary"
455 // declaration. We need better tracking of redeclarations and
456 // definitions.
457 QualType Type = Context.getTypeDeclType(static_cast<RecordDecl*>(this));
458 return Type->getAsRecordType()->getDecl();
459#else
460 // FIXME: This hack will work for now, because the declaration we
461 // create when we're defining the record is the one we'll use as
462 // the definition later.
463 return this;
464#endif
465 }
466
467 case Decl::ObjCMethod:
468 return this;
469
470 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000471 case Decl::ObjCProtocol:
472 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000473 // FIXME: Can Objective-C interfaces be forward-declared?
474 return this;
475
Steve Naroff0701bbb2009-01-08 17:28:14 +0000476 case Decl::ObjCImplementation:
477 case Decl::ObjCCategoryImpl:
478 return this;
479
Douglas Gregor44b43212008-12-11 16:49:14 +0000480 default:
481 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
482 "Unknown DeclContext kind");
483 return this;
484 }
485}
486
487DeclContext *DeclContext::getNextContext() {
488 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000489 case Decl::TranslationUnit:
490 case Decl::Enum:
491 case Decl::Record:
492 case Decl::CXXRecord:
493 case Decl::ObjCMethod:
494 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000495 case Decl::ObjCCategory:
496 case Decl::ObjCProtocol:
497 case Decl::ObjCImplementation:
498 case Decl::ObjCCategoryImpl:
Douglas Gregor074149e2009-01-05 19:45:36 +0000499 case Decl::LinkageSpec:
500 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000501 // There is only one DeclContext for these entities.
502 return 0;
503
504 case Decl::Namespace:
505 // Return the next namespace
506 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
507
508 default:
509 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
510 "Unknown DeclContext kind");
511 return 0;
512 }
513}
514
515void DeclContext::addDecl(ASTContext &Context, ScopedDecl *D, bool AllowLookup) {
516 Decls.push_back(D);
517 if (AllowLookup)
518 D->getDeclContext()->insert(Context, D);
519}
520
Douglas Gregor074149e2009-01-05 19:45:36 +0000521/// buildLookup - Build the lookup data structure with all of the
522/// declarations in DCtx (and any other contexts linked to it or
523/// transparent contexts nested within it).
Steve Naroff0701bbb2009-01-08 17:28:14 +0000524void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000525 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000526 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
527 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000528 // Insert this declaration into the lookup structure
529 insertImpl(*D);
530
531 // If this declaration is itself a transparent declaration context,
532 // add its members (recursively).
533 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
534 if (InnerCtx->isTransparentContext())
Steve Naroff0701bbb2009-01-08 17:28:14 +0000535 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000536 }
537 }
538}
539
Douglas Gregor44b43212008-12-11 16:49:14 +0000540DeclContext::lookup_result
Steve Naroff0701bbb2009-01-08 17:28:14 +0000541DeclContext::lookup(DeclarationName Name) {
542 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000543 if (PrimaryContext != this)
Steve Naroff0701bbb2009-01-08 17:28:14 +0000544 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000545
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000546 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000547 /// all of the linked DeclContexts (in declaration order!) and
548 /// inserting their values.
Douglas Gregor074149e2009-01-05 19:45:36 +0000549 if (LookupPtr.getPointer() == 0)
Steve Naroff0701bbb2009-01-08 17:28:14 +0000550 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000551
Douglas Gregor44b43212008-12-11 16:49:14 +0000552 if (isLookupMap()) {
553 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
554 StoredDeclsMap::iterator Pos = Map->find(Name);
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000555 if (Pos != Map->end())
556 return lookup_result(&Pos->second.front(),
557 &Pos->second.front() + Pos->second.size());
558 return lookup_result(0, 0);
Douglas Gregor44b43212008-12-11 16:49:14 +0000559 }
560
561 // We have a small array. Look into it.
562 unsigned Size = LookupPtr.getInt();
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000563 ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
Douglas Gregore267ff32008-12-11 20:41:00 +0000564 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000565 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000566 unsigned Last = Idx + 1;
567 while (Last != Size && Array[Last]->getDeclName() == Name)
568 ++Last;
569 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor44b43212008-12-11 16:49:14 +0000570 }
571
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000572 return lookup_result(0, 0);
Douglas Gregor44b43212008-12-11 16:49:14 +0000573}
574
575DeclContext::lookup_const_result
Steve Naroff0701bbb2009-01-08 17:28:14 +0000576DeclContext::lookup(DeclarationName Name) const {
577 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000578}
579
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000580const DeclContext *DeclContext::getLookupContext() const {
581 const DeclContext *Ctx = this;
Douglas Gregorce356072009-01-06 23:51:29 +0000582 while (Ctx->isTransparentContext())
583 Ctx = Ctx->getParent();
584 return Ctx;
585}
586
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000587void DeclContext::insert(ASTContext &Context, ScopedDecl *D) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000588 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000589 if (PrimaryContext != this) {
590 PrimaryContext->insert(Context, D);
591 return;
592 }
593
594 // If we already have a lookup data structure, perform the insertion
595 // into it. Otherwise, be lazy and don't build that structure until
596 // someone asks for it.
597 if (LookupPtr.getPointer())
598 insertImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000599
600
601 // If we are a transparent context, insert into our parent context,
602 // too. This operation is recursive.
603 if (isTransparentContext())
604 getParent()->insert(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000605}
606
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000607void DeclContext::insertImpl(ScopedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000608 // Skip unnamed declarations.
609 if (!D->getDeclName())
610 return;
611
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000612 bool MayBeRedeclaration = true;
613
Douglas Gregor44b43212008-12-11 16:49:14 +0000614 if (!isLookupMap()) {
615 unsigned Size = LookupPtr.getInt();
616
617 // The lookup data is stored as an array. Search through the array
618 // to find the insertion location.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000619 ScopedDecl **Array;
Douglas Gregor44b43212008-12-11 16:49:14 +0000620 if (Size == 0) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000621 Array = new ScopedDecl*[LookupIsMap - 1];
Douglas Gregor44b43212008-12-11 16:49:14 +0000622 LookupPtr.setPointer(Array);
623 } else {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000624 Array = static_cast<ScopedDecl **>(LookupPtr.getPointer());
Douglas Gregor44b43212008-12-11 16:49:14 +0000625 }
626
627 // We always keep declarations of the same name next to each other
628 // in the array, so that it is easy to return multiple results
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000629 // from lookup().
630 unsigned FirstMatch;
631 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
632 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregore267ff32008-12-11 20:41:00 +0000633 break;
Douglas Gregor44b43212008-12-11 16:49:14 +0000634
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000635 unsigned InsertPos = FirstMatch;
636 if (FirstMatch != Size) {
637 // We found another declaration with the same name. First
638 // determine whether this is a redeclaration of an existing
639 // declaration in this scope, in which case we will replace the
640 // existing declaration.
641 unsigned LastMatch = FirstMatch;
642 for (; LastMatch != Size; ++LastMatch) {
643 if (Array[LastMatch]->getDeclName() != D->getDeclName())
644 break;
645
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000646 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000647 // D is a redeclaration of an existing element in the
648 // array. Replace that element with D.
649 Array[LastMatch] = D;
650 return;
651 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000652 }
653
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000654 // [FirstMatch, LastMatch) contains the set of declarations that
655 // have the same name as this declaration. Determine where the
656 // declaration D will be inserted into this range.
657 if (D->getIdentifierNamespace() == Decl::IDNS_Tag)
658 InsertPos = LastMatch;
659 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
660 InsertPos = LastMatch - 1;
661 else
662 InsertPos = LastMatch;
Douglas Gregor44b43212008-12-11 16:49:14 +0000663 }
664
665 if (Size < LookupIsMap - 1) {
666 // The new declaration will fit in the array. Insert the new
667 // declaration at the position Match in the array.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000668 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000669 Array[Idx] = Array[Idx-1];
670
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000671 Array[InsertPos] = D;
Douglas Gregor44b43212008-12-11 16:49:14 +0000672 LookupPtr.setInt(Size + 1);
673 return;
674 }
675
676 // We've reached capacity in this array. Create a map and copy in
677 // all of the declarations that were stored in the array.
678 StoredDeclsMap *Map = new StoredDeclsMap(16);
679 LookupPtr.setPointer(Map);
680 LookupPtr.setInt(LookupIsMap);
Douglas Gregore267ff32008-12-11 20:41:00 +0000681 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000682 insertImpl(Array[Idx]);
683 delete [] Array;
684
685 // Fall through to perform insertion into the map.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000686 MayBeRedeclaration = false;
687 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000688
689 // Insert this declaration into the map.
690 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
691 StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000692 if (Pos != Map->end()) {
693 if (MayBeRedeclaration) {
694 // Determine if this declaration is actually a redeclaration.
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000695 std::vector<ScopedDecl *>::iterator Redecl
696 = std::find_if(Pos->second.begin(), Pos->second.end(),
697 std::bind1st(std::mem_fun(&ScopedDecl::declarationReplaces),
698 D));
699 if (Redecl != Pos->second.end()) {
700 *Redecl = D;
701 return;
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000702 }
703 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000704
Douglas Gregor44b43212008-12-11 16:49:14 +0000705 // Put this declaration into the appropriate slot.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000706 if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
707 Pos->second.push_back(D);
708 else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
709 ScopedDecl *TagD = Pos->second.back();
710 Pos->second.back() = D;
711 Pos->second.push_back(TagD);
712 } else
713 Pos->second.push_back(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000714 } else {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000715 (*Map)[D->getDeclName()].push_back(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000716 }
717}