blob: 70792c4efdf1b9a4b6ce0a581f9beeaca94fa28b [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";
Steve Naroff4c92fea2009-01-14 01:27:31 +000091 case Field: return "Field";
Eli Friedman56d29372008-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 Jahanian4f5420d2008-12-20 21:06:28 +0000104 int(nFuncs+nVars+nParmVars+nOriginalParmVars+nFieldDecls+nSUC+nCXXSUC+
Eli Friedman56d29372008-06-07 16:52:53 +0000105 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
106 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000107 nAtDefsFieldDecls+nNamespaces+nOverFuncs));
Eli Friedman56d29372008-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 Gregor8e9bebd2008-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 Friedman56d29372008-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 Jahanian4f5420d2008-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 Friedman56d29372008-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 Kremenek01e67792008-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 Friedman56d29372008-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)));
Argyrios Kyrtzidis55d71f92008-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 Friedman56d29372008-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 Jahanian4f5420d2008-12-20 21:06:28 +0000191 nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)+
Eli Friedman56d29372008-06-07 16:52:53 +0000192 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Douglas Gregor44b43212008-12-11 16:49:14 +0000193 nCXXSUC*sizeof(CXXRecordDecl)+
Eli Friedman56d29372008-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 Gregor8e9bebd2008-10-21 16:13:35 +0000210 nNamespaces*sizeof(NamespaceDecl)+
211 nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedman56d29372008-06-07 16:52:53 +0000212
213}
214
215void Decl::addDeclKind(Kind k) {
216 switch (k) {
217 case Namespace: nNamespaces++; break;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000218 case OverloadedFunction: nOverFuncs++; break;
Eli Friedman56d29372008-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 Jahanian4f5420d2008-12-20 21:06:28 +0000223 case OriginalParmVar: nOriginalParmVars++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000224 case EnumConstant: nEnumConst++; break;
225 case Field: nFieldDecls++; break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000226 case Record: nSUC++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000227 case Enum: nEnumDecls++; break;
Steve Naroff09c47192009-01-09 15:36:25 +0000228 case ObjCContainer: break; // is abstract...no need to account for.
Eli Friedman56d29372008-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 Kremenek01e67792008-08-20 03:26:33 +0000236 case ObjCAtDefsField: nAtDefsFieldDecls++; break;
Eli Friedman56d29372008-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 Naroff56ee6892008-10-08 17:01:13 +0000244 case Block: nBlockDecls++; break;
Chris Lattner41110242008-06-17 18:05:57 +0000245 case ImplicitParam:
Eli Friedman56d29372008-06-07 16:52:53 +0000246 case TranslationUnit: break;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000247
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000248 case CXXRecord: nCXXSUC++; break;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000249 // FIXME: Statistics for C++ decls.
Douglas Gregor72c3f312008-12-05 18:15:24 +0000250 case TemplateTypeParm:
251 case NonTypeTemplateParm:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000252 case CXXMethod:
Douglas Gregorb48fe382008-10-31 09:07:45 +0000253 case CXXConstructor:
Douglas Gregor42a552f2008-11-05 20:51:48 +0000254 case CXXDestructor:
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000255 case CXXConversion:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000256 case CXXClassVar:
257 break;
Eli Friedman56d29372008-06-07 16:52:53 +0000258 }
259}
260
261//===----------------------------------------------------------------------===//
262// Decl Implementation
263//===----------------------------------------------------------------------===//
264
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000265void Decl::setDeclContext(DeclContext *DC) {
266 if (isOutOfSemaDC())
267 delete getMultipleDC();
268
269 DeclCtx = reinterpret_cast<uintptr_t>(DC);
270}
271
272void Decl::setLexicalDeclContext(DeclContext *DC) {
273 if (DC == getLexicalDeclContext())
274 return;
275
276 if (isInSemaDC()) {
277 MultipleDC *MDC = new MultipleDC();
278 MDC->SemanticDC = getDeclContext();
279 MDC->LexicalDC = DC;
280 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
281 } else {
282 getMultipleDC()->LexicalDC = DC;
283 }
284}
285
Eli Friedman56d29372008-06-07 16:52:53 +0000286// Out-of-line virtual method providing a home for Decl.
287Decl::~Decl() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000288 if (isOutOfSemaDC())
289 delete getMultipleDC();
290
Eli Friedman56d29372008-06-07 16:52:53 +0000291 if (!HasAttrs)
292 return;
293
294 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
295 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
296
297 // release attributes.
298 delete it->second;
299 invalidateAttrs();
300}
301
302void Decl::addAttr(Attr *NewAttr) {
303 if (!DeclAttrs)
304 DeclAttrs = new DeclAttrMapTy();
305
306 Attr *&ExistingAttr = (*DeclAttrs)[this];
307
308 NewAttr->setNext(ExistingAttr);
309 ExistingAttr = NewAttr;
310
311 HasAttrs = true;
312}
313
314void Decl::invalidateAttrs() {
315 if (!HasAttrs) return;
316
317 HasAttrs = false;
318 (*DeclAttrs)[this] = 0;
319 DeclAttrs->erase(this);
320
321 if (DeclAttrs->empty()) {
322 delete DeclAttrs;
323 DeclAttrs = 0;
324 }
325}
326
327const Attr *Decl::getAttrs() const {
328 if (!HasAttrs)
329 return 0;
330
331 return (*DeclAttrs)[this];
332}
333
334void Decl::swapAttrs(Decl *RHS) {
335 bool HasLHSAttr = this->HasAttrs;
336 bool HasRHSAttr = RHS->HasAttrs;
337
338 // Usually, neither decl has attrs, nothing to do.
339 if (!HasLHSAttr && !HasRHSAttr) return;
340
341 // If 'this' has no attrs, swap the other way.
342 if (!HasLHSAttr)
343 return RHS->swapAttrs(this);
344
345 // Handle the case when both decls have attrs.
346 if (HasRHSAttr) {
347 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
348 return;
349 }
350
351 // Otherwise, LHS has an attr and RHS doesn't.
352 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
353 (*DeclAttrs).erase(this);
354 this->HasAttrs = false;
355 RHS->HasAttrs = true;
356}
357
358
359void Decl::Destroy(ASTContext& C) {
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000360#if 0
361 // FIXME: This causes double-destroys in some cases, so it is
362 // disabled at the moment.
Eli Friedman56d29372008-06-07 16:52:53 +0000363
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000364 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
365 // within the loop, only the Destroy method for the first Decl
366 // will deallocate all of the Decls in a chain.
367
368 Decl* N = SD->getNextDeclarator();
369
370 while (N) {
371 Decl* Tmp = N->getNextDeclarator();
372 N->NextDeclarator = 0x0;
373 N->Destroy(C);
374 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000375 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000376#endif
377
Eli Friedman56d29372008-06-07 16:52:53 +0000378 this->~Decl();
379 C.getAllocator().Deallocate((void *)this);
380}
381
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000382Decl *Decl::castFromDeclContext (const DeclContext *D) {
383 return DeclContext::CastTo<Decl>(D);
384}
385
386DeclContext *Decl::castToDeclContext(const Decl *D) {
387 return DeclContext::CastTo<DeclContext>(D);
388}
389
Eli Friedman56d29372008-06-07 16:52:53 +0000390//===----------------------------------------------------------------------===//
391// DeclContext Implementation
392//===----------------------------------------------------------------------===//
393
Argyrios Kyrtzidis20bc6762008-11-19 17:36:39 +0000394const DeclContext *DeclContext::getParent() const {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000395 if (const Decl *D = dyn_cast<Decl>(this))
396 return D->getDeclContext();
397
398 return NULL;
Eli Friedman56d29372008-06-07 16:52:53 +0000399}
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000400
401const DeclContext *DeclContext::getLexicalParent() const {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000402 if (const Decl *D = dyn_cast<Decl>(this))
403 return D->getLexicalDeclContext();
404
Argyrios Kyrtzidis051c13a2008-11-19 18:07:24 +0000405 return getParent();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000406}
Douglas Gregor44b43212008-12-11 16:49:14 +0000407
Douglas Gregor44b43212008-12-11 16:49:14 +0000408// FIXME: We really want to use a DenseSet here to eliminate the
409// redundant storage of the declaration names, but (1) it doesn't give
410// us the ability to search based on DeclarationName, (2) we really
411// need something more like a DenseMultiSet, and (3) it's
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000412// implemented in terms of DenseMap anyway. However, this data
413// structure is really space-inefficient, so we'll have to do
414// something.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000415typedef llvm::DenseMap<DeclarationName, std::vector<NamedDecl*> >
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000416 StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000417
418DeclContext::~DeclContext() {
419 unsigned Size = LookupPtr.getInt();
420 if (Size == LookupIsMap) {
421 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
422 delete Map;
423 } else {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000424 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor44b43212008-12-11 16:49:14 +0000425 delete [] Array;
426 }
427}
428
429void DeclContext::DestroyDecls(ASTContext &C) {
Steve Naroff4c92fea2009-01-14 01:27:31 +0000430 for (decl_iterator D = decls_begin(); D != decls_end(); ) {
431 // FIXME: assert that this condition holds.
432 if ((*D)->getLexicalDeclContext() == this)
433 // Advance the cursor (via NextDeclInScope) *before* doing the Destroy.
434 (*D++)->Destroy(C);
435 else
436 ++D;
Douglas Gregor44b43212008-12-11 16:49:14 +0000437 }
438}
439
Douglas Gregor074149e2009-01-05 19:45:36 +0000440bool DeclContext::isTransparentContext() const {
441 if (DeclKind == Decl::Enum)
442 return true; // FIXME: Check for C++0x scoped enums
443 else if (DeclKind == Decl::LinkageSpec)
444 return true;
445 else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000446 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000447 else if (DeclKind == Decl::Namespace)
448 return false; // FIXME: Check for C++0x inline namespaces
449
450 return false;
451}
452
Steve Naroff0701bbb2009-01-08 17:28:14 +0000453DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000454 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000455 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000456 case Decl::LinkageSpec:
457 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000458 // There is only one DeclContext for these entities.
459 return this;
460
461 case Decl::Namespace:
462 // The original namespace is our primary context.
463 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
464
465 case Decl::Enum:
Douglas Gregor44b43212008-12-11 16:49:14 +0000466 case Decl::Record:
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000467 case Decl::CXXRecord:
468 // If this is a tag type that has a definition or is currently
469 // being defined, that definition is our primary context.
470 if (TagType *TagT = cast_or_null<TagType>(cast<TagDecl>(this)->TypeForDecl))
471 if (TagT->isBeingDefined() ||
472 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
473 return TagT->getDecl();
Douglas Gregor44b43212008-12-11 16:49:14 +0000474 return this;
Douglas Gregor44b43212008-12-11 16:49:14 +0000475
476 case Decl::ObjCMethod:
477 return this;
478
479 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000480 case Decl::ObjCProtocol:
481 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000482 // FIXME: Can Objective-C interfaces be forward-declared?
483 return this;
484
Steve Naroff0701bbb2009-01-08 17:28:14 +0000485 case Decl::ObjCImplementation:
486 case Decl::ObjCCategoryImpl:
487 return this;
488
Douglas Gregor44b43212008-12-11 16:49:14 +0000489 default:
490 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
491 "Unknown DeclContext kind");
492 return this;
493 }
494}
495
496DeclContext *DeclContext::getNextContext() {
497 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000498 case Decl::TranslationUnit:
499 case Decl::Enum:
500 case Decl::Record:
501 case Decl::CXXRecord:
502 case Decl::ObjCMethod:
503 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000504 case Decl::ObjCCategory:
505 case Decl::ObjCProtocol:
506 case Decl::ObjCImplementation:
507 case Decl::ObjCCategoryImpl:
Douglas Gregor074149e2009-01-05 19:45:36 +0000508 case Decl::LinkageSpec:
509 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000510 // There is only one DeclContext for these entities.
511 return 0;
512
513 case Decl::Namespace:
514 // Return the next namespace
515 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
516
517 default:
518 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
519 "Unknown DeclContext kind");
520 return 0;
521 }
522}
523
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000524void DeclContext::addDecl(Decl *D) {
Douglas Gregora8cc8ce2009-01-09 18:51:29 +0000525 assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000526 assert(!D->NextDeclInScope && D != LastDecl &&
527 "Decl already inserted into a DeclContext");
528
529 if (FirstDecl) {
530 LastDecl->NextDeclInScope = D;
531 LastDecl = D;
532 } else {
533 FirstDecl = LastDecl = D;
534 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000535
536 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
537 ND->getDeclContext()->insert(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000538}
539
Douglas Gregor074149e2009-01-05 19:45:36 +0000540/// buildLookup - Build the lookup data structure with all of the
541/// declarations in DCtx (and any other contexts linked to it or
542/// transparent contexts nested within it).
Steve Naroff0701bbb2009-01-08 17:28:14 +0000543void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000544 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000545 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
546 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000547 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000548 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
549 insertImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000550
551 // If this declaration is itself a transparent declaration context,
552 // add its members (recursively).
553 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
554 if (InnerCtx->isTransparentContext())
Steve Naroff0701bbb2009-01-08 17:28:14 +0000555 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000556 }
557 }
558}
559
Douglas Gregor44b43212008-12-11 16:49:14 +0000560DeclContext::lookup_result
Steve Naroff0701bbb2009-01-08 17:28:14 +0000561DeclContext::lookup(DeclarationName Name) {
562 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000563 if (PrimaryContext != this)
Steve Naroff0701bbb2009-01-08 17:28:14 +0000564 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000565
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000566 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000567 /// all of the linked DeclContexts (in declaration order!) and
568 /// inserting their values.
Douglas Gregor074149e2009-01-05 19:45:36 +0000569 if (LookupPtr.getPointer() == 0)
Steve Naroff0701bbb2009-01-08 17:28:14 +0000570 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000571
Douglas Gregor44b43212008-12-11 16:49:14 +0000572 if (isLookupMap()) {
573 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
574 StoredDeclsMap::iterator Pos = Map->find(Name);
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000575 if (Pos != Map->end())
576 return lookup_result(&Pos->second.front(),
577 &Pos->second.front() + Pos->second.size());
578 return lookup_result(0, 0);
Douglas Gregor44b43212008-12-11 16:49:14 +0000579 }
580
581 // We have a small array. Look into it.
582 unsigned Size = LookupPtr.getInt();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000583 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregore267ff32008-12-11 20:41:00 +0000584 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000585 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000586 unsigned Last = Idx + 1;
587 while (Last != Size && Array[Last]->getDeclName() == Name)
588 ++Last;
589 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor44b43212008-12-11 16:49:14 +0000590 }
591
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000592 return lookup_result(0, 0);
Douglas Gregor44b43212008-12-11 16:49:14 +0000593}
594
595DeclContext::lookup_const_result
Steve Naroff0701bbb2009-01-08 17:28:14 +0000596DeclContext::lookup(DeclarationName Name) const {
597 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000598}
599
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000600const DeclContext *DeclContext::getLookupContext() const {
601 const DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000602 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000603 while (Ctx->isTransparentContext())
604 Ctx = Ctx->getParent();
605 return Ctx;
606}
607
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000608void DeclContext::insert(NamedDecl *D) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000609 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000610 if (PrimaryContext != this) {
Douglas Gregor482b77d2009-01-12 23:27:07 +0000611 PrimaryContext->insert(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000612 return;
613 }
614
615 // If we already have a lookup data structure, perform the insertion
616 // into it. Otherwise, be lazy and don't build that structure until
617 // someone asks for it.
618 if (LookupPtr.getPointer())
619 insertImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000620
Douglas Gregor074149e2009-01-05 19:45:36 +0000621 // If we are a transparent context, insert into our parent context,
622 // too. This operation is recursive.
623 if (isTransparentContext())
Douglas Gregor482b77d2009-01-12 23:27:07 +0000624 getParent()->insert(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000625}
626
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000627void DeclContext::insertImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000628 // Skip unnamed declarations.
629 if (!D->getDeclName())
630 return;
631
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000632 bool MayBeRedeclaration = true;
633
Douglas Gregor44b43212008-12-11 16:49:14 +0000634 if (!isLookupMap()) {
635 unsigned Size = LookupPtr.getInt();
636
637 // The lookup data is stored as an array. Search through the array
638 // to find the insertion location.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000639 NamedDecl **Array;
Douglas Gregor44b43212008-12-11 16:49:14 +0000640 if (Size == 0) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000641 Array = new NamedDecl*[LookupIsMap - 1];
Douglas Gregor44b43212008-12-11 16:49:14 +0000642 LookupPtr.setPointer(Array);
643 } else {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000644 Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
Douglas Gregor44b43212008-12-11 16:49:14 +0000645 }
646
647 // We always keep declarations of the same name next to each other
648 // in the array, so that it is easy to return multiple results
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000649 // from lookup().
650 unsigned FirstMatch;
651 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
652 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregore267ff32008-12-11 20:41:00 +0000653 break;
Douglas Gregor44b43212008-12-11 16:49:14 +0000654
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000655 unsigned InsertPos = FirstMatch;
656 if (FirstMatch != Size) {
657 // We found another declaration with the same name. First
658 // determine whether this is a redeclaration of an existing
659 // declaration in this scope, in which case we will replace the
660 // existing declaration.
661 unsigned LastMatch = FirstMatch;
662 for (; LastMatch != Size; ++LastMatch) {
663 if (Array[LastMatch]->getDeclName() != D->getDeclName())
664 break;
665
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000666 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000667 // D is a redeclaration of an existing element in the
668 // array. Replace that element with D.
669 Array[LastMatch] = D;
670 return;
671 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000672 }
673
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000674 // [FirstMatch, LastMatch) contains the set of declarations that
675 // have the same name as this declaration. Determine where the
676 // declaration D will be inserted into this range.
677 if (D->getIdentifierNamespace() == Decl::IDNS_Tag)
678 InsertPos = LastMatch;
679 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
680 InsertPos = LastMatch - 1;
681 else
682 InsertPos = LastMatch;
Douglas Gregor44b43212008-12-11 16:49:14 +0000683 }
684
685 if (Size < LookupIsMap - 1) {
686 // The new declaration will fit in the array. Insert the new
687 // declaration at the position Match in the array.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000688 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000689 Array[Idx] = Array[Idx-1];
690
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000691 Array[InsertPos] = D;
Douglas Gregor44b43212008-12-11 16:49:14 +0000692 LookupPtr.setInt(Size + 1);
693 return;
694 }
695
696 // We've reached capacity in this array. Create a map and copy in
697 // all of the declarations that were stored in the array.
698 StoredDeclsMap *Map = new StoredDeclsMap(16);
699 LookupPtr.setPointer(Map);
700 LookupPtr.setInt(LookupIsMap);
Douglas Gregore267ff32008-12-11 20:41:00 +0000701 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000702 insertImpl(Array[Idx]);
703 delete [] Array;
704
705 // Fall through to perform insertion into the map.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000706 MayBeRedeclaration = false;
707 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000708
709 // Insert this declaration into the map.
710 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
711 StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000712 if (Pos != Map->end()) {
713 if (MayBeRedeclaration) {
714 // Determine if this declaration is actually a redeclaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000715 std::vector<NamedDecl *>::iterator Redecl
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000716 = std::find_if(Pos->second.begin(), Pos->second.end(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000717 std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000718 D));
719 if (Redecl != Pos->second.end()) {
720 *Redecl = D;
721 return;
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000722 }
723 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000724
Douglas Gregor44b43212008-12-11 16:49:14 +0000725 // Put this declaration into the appropriate slot.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000726 if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
727 Pos->second.push_back(D);
728 else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000729 NamedDecl *TagD = Pos->second.back();
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000730 Pos->second.back() = D;
731 Pos->second.push_back(TagD);
732 } else
733 Pos->second.push_back(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000734 } else {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000735 (*Map)[D->getDeclName()].push_back(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000736 }
737}