blob: 5526be091f43544463796b05ca92b47c2239ebe3 [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
Douglas Gregoraf8ad2b2009-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 Friedmana8a09ac2008-06-07 16:52:53 +0000286// Out-of-line virtual method providing a home for Decl.
287Decl::~Decl() {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000288 if (isOutOfSemaDC())
289 delete getMultipleDC();
290
Eli Friedmana8a09ac2008-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 Gregor8314d742009-01-13 19:47:12 +0000360#if 0
Douglas Gregorf4006be2009-01-20 04:25:11 +0000361 // FIXME: Once ownership is fully understood, we can enable this code
362 if (DeclContext *DC = dyn_cast<DeclContext>(this))
363 DC->decls_begin()->Destroy(C);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000364
Douglas Gregorf4006be2009-01-20 04:25:11 +0000365 // Observe the unrolled recursion. By setting N->NextDeclInScope = 0x0
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000366 // within the loop, only the Destroy method for the first Decl
367 // will deallocate all of the Decls in a chain.
368
Douglas Gregorf4006be2009-01-20 04:25:11 +0000369 Decl* N = NextDeclInScope;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000370
371 while (N) {
Douglas Gregorf4006be2009-01-20 04:25:11 +0000372 Decl* Tmp = N->NextDeclInScope;
373 N->NextDeclInScope = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000374 N->Destroy(C);
375 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000376 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000377
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000378 this->~Decl();
379 C.getAllocator().Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000380#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000381}
382
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000383Decl *Decl::castFromDeclContext (const DeclContext *D) {
384 return DeclContext::CastTo<Decl>(D);
385}
386
387DeclContext *Decl::castToDeclContext(const Decl *D) {
388 return DeclContext::CastTo<DeclContext>(D);
389}
390
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000391//===----------------------------------------------------------------------===//
392// DeclContext Implementation
393//===----------------------------------------------------------------------===//
394
Argiris Kirtzidis5703f442008-11-19 17:36:39 +0000395const DeclContext *DeclContext::getParent() const {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000396 if (const Decl *D = dyn_cast<Decl>(this))
397 return D->getDeclContext();
398
399 return NULL;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000400}
Argiris Kirtzidis9cd599b2008-11-19 18:01:13 +0000401
402const DeclContext *DeclContext::getLexicalParent() const {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000403 if (const Decl *D = dyn_cast<Decl>(this))
404 return D->getLexicalDeclContext();
405
Argiris Kirtzidisbc470282008-11-19 18:07:24 +0000406 return getParent();
Argiris Kirtzidis9cd599b2008-11-19 18:01:13 +0000407}
Douglas Gregor8acb7272008-12-11 16:49:14 +0000408
Douglas Gregor8acb7272008-12-11 16:49:14 +0000409// FIXME: We really want to use a DenseSet here to eliminate the
410// redundant storage of the declaration names, but (1) it doesn't give
411// us the ability to search based on DeclarationName, (2) we really
412// need something more like a DenseMultiSet, and (3) it's
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000413// implemented in terms of DenseMap anyway. However, this data
414// structure is really space-inefficient, so we'll have to do
415// something.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000416typedef llvm::DenseMap<DeclarationName, std::vector<NamedDecl*> >
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000417 StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000418
419DeclContext::~DeclContext() {
420 unsigned Size = LookupPtr.getInt();
421 if (Size == LookupIsMap) {
422 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
423 delete Map;
424 } else {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000425 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000426 delete [] Array;
427 }
428}
429
430void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregorf4006be2009-01-20 04:25:11 +0000431 for (decl_iterator D = decls_begin(); D != decls_end(); )
432 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000433}
434
Douglas Gregord8028382009-01-05 19:45:36 +0000435bool DeclContext::isTransparentContext() const {
436 if (DeclKind == Decl::Enum)
437 return true; // FIXME: Check for C++0x scoped enums
438 else if (DeclKind == Decl::LinkageSpec)
439 return true;
440 else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord)
Douglas Gregor723d3332009-01-07 00:43:41 +0000441 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000442 else if (DeclKind == Decl::Namespace)
443 return false; // FIXME: Check for C++0x inline namespaces
444
445 return false;
446}
447
Steve Naroffab63fd62009-01-08 17:28:14 +0000448DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000449 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000450 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000451 case Decl::LinkageSpec:
452 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000453 // There is only one DeclContext for these entities.
454 return this;
455
456 case Decl::Namespace:
457 // The original namespace is our primary context.
458 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
459
460 case Decl::Enum:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000461 case Decl::Record:
Douglas Gregor98b27542009-01-17 00:42:38 +0000462 case Decl::CXXRecord:
463 // If this is a tag type that has a definition or is currently
464 // being defined, that definition is our primary context.
465 if (TagType *TagT = cast_or_null<TagType>(cast<TagDecl>(this)->TypeForDecl))
466 if (TagT->isBeingDefined() ||
467 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
468 return TagT->getDecl();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000469 return this;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000470
471 case Decl::ObjCMethod:
472 return this;
473
474 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000475 case Decl::ObjCProtocol:
476 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000477 // FIXME: Can Objective-C interfaces be forward-declared?
478 return this;
479
Steve Naroffab63fd62009-01-08 17:28:14 +0000480 case Decl::ObjCImplementation:
481 case Decl::ObjCCategoryImpl:
482 return this;
483
Douglas Gregor8acb7272008-12-11 16:49:14 +0000484 default:
485 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
486 "Unknown DeclContext kind");
487 return this;
488 }
489}
490
491DeclContext *DeclContext::getNextContext() {
492 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000493 case Decl::TranslationUnit:
494 case Decl::Enum:
495 case Decl::Record:
496 case Decl::CXXRecord:
497 case Decl::ObjCMethod:
498 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000499 case Decl::ObjCCategory:
500 case Decl::ObjCProtocol:
501 case Decl::ObjCImplementation:
502 case Decl::ObjCCategoryImpl:
Douglas Gregord8028382009-01-05 19:45:36 +0000503 case Decl::LinkageSpec:
504 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000505 // There is only one DeclContext for these entities.
506 return 0;
507
508 case Decl::Namespace:
509 // Return the next namespace
510 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
511
512 default:
513 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
514 "Unknown DeclContext kind");
515 return 0;
516 }
517}
518
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000519void DeclContext::addDecl(Decl *D) {
Douglas Gregord394a272009-01-09 18:51:29 +0000520 assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
Douglas Gregord1675382009-01-09 19:42:16 +0000521 assert(!D->NextDeclInScope && D != LastDecl &&
522 "Decl already inserted into a DeclContext");
523
524 if (FirstDecl) {
525 LastDecl->NextDeclInScope = D;
526 LastDecl = D;
527 } else {
528 FirstDecl = LastDecl = D;
529 }
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000530
531 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
532 ND->getDeclContext()->insert(ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000533}
534
Douglas Gregord8028382009-01-05 19:45:36 +0000535/// buildLookup - Build the lookup data structure with all of the
536/// declarations in DCtx (and any other contexts linked to it or
537/// transparent contexts nested within it).
Steve Naroffab63fd62009-01-08 17:28:14 +0000538void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000539 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000540 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
541 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000542 // Insert this declaration into the lookup structure
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000543 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
544 insertImpl(ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000545
546 // If this declaration is itself a transparent declaration context,
547 // add its members (recursively).
548 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
549 if (InnerCtx->isTransparentContext())
Steve Naroffab63fd62009-01-08 17:28:14 +0000550 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000551 }
552 }
553}
554
Douglas Gregor8acb7272008-12-11 16:49:14 +0000555DeclContext::lookup_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000556DeclContext::lookup(DeclarationName Name) {
557 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000558 if (PrimaryContext != this)
Steve Naroffab63fd62009-01-08 17:28:14 +0000559 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000560
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000561 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000562 /// all of the linked DeclContexts (in declaration order!) and
563 /// inserting their values.
Douglas Gregord8028382009-01-05 19:45:36 +0000564 if (LookupPtr.getPointer() == 0)
Steve Naroffab63fd62009-01-08 17:28:14 +0000565 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000566
Douglas Gregor8acb7272008-12-11 16:49:14 +0000567 if (isLookupMap()) {
568 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
569 StoredDeclsMap::iterator Pos = Map->find(Name);
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000570 if (Pos != Map->end())
571 return lookup_result(&Pos->second.front(),
572 &Pos->second.front() + Pos->second.size());
573 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000574 }
575
576 // We have a small array. Look into it.
577 unsigned Size = LookupPtr.getInt();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000578 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor39677622008-12-11 20:41:00 +0000579 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000580 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000581 unsigned Last = Idx + 1;
582 while (Last != Size && Array[Last]->getDeclName() == Name)
583 ++Last;
584 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000585 }
586
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000587 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000588}
589
590DeclContext::lookup_const_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000591DeclContext::lookup(DeclarationName Name) const {
592 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000593}
594
Douglas Gregor38d38a02009-01-07 02:48:43 +0000595const DeclContext *DeclContext::getLookupContext() const {
596 const DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000597 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000598 while (Ctx->isTransparentContext())
599 Ctx = Ctx->getParent();
600 return Ctx;
601}
602
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000603void DeclContext::insert(NamedDecl *D) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000604 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000605 if (PrimaryContext != this) {
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000606 PrimaryContext->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000607 return;
608 }
609
610 // If we already have a lookup data structure, perform the insertion
611 // into it. Otherwise, be lazy and don't build that structure until
612 // someone asks for it.
613 if (LookupPtr.getPointer())
614 insertImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000615
Douglas Gregord8028382009-01-05 19:45:36 +0000616 // If we are a transparent context, insert into our parent context,
617 // too. This operation is recursive.
618 if (isTransparentContext())
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000619 getParent()->insert(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000620}
621
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000622void DeclContext::insertImpl(NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000623 // Skip unnamed declarations.
624 if (!D->getDeclName())
625 return;
626
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000627 bool MayBeRedeclaration = true;
628
Douglas Gregor8acb7272008-12-11 16:49:14 +0000629 if (!isLookupMap()) {
630 unsigned Size = LookupPtr.getInt();
631
632 // The lookup data is stored as an array. Search through the array
633 // to find the insertion location.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000634 NamedDecl **Array;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000635 if (Size == 0) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000636 Array = new NamedDecl*[LookupIsMap - 1];
Douglas Gregor8acb7272008-12-11 16:49:14 +0000637 LookupPtr.setPointer(Array);
638 } else {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000639 Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000640 }
641
642 // We always keep declarations of the same name next to each other
643 // in the array, so that it is easy to return multiple results
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000644 // from lookup().
645 unsigned FirstMatch;
646 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
647 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregor39677622008-12-11 20:41:00 +0000648 break;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000649
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000650 unsigned InsertPos = FirstMatch;
651 if (FirstMatch != Size) {
652 // We found another declaration with the same name. First
653 // determine whether this is a redeclaration of an existing
654 // declaration in this scope, in which case we will replace the
655 // existing declaration.
656 unsigned LastMatch = FirstMatch;
657 for (; LastMatch != Size; ++LastMatch) {
658 if (Array[LastMatch]->getDeclName() != D->getDeclName())
659 break;
660
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000661 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000662 // D is a redeclaration of an existing element in the
663 // array. Replace that element with D.
664 Array[LastMatch] = D;
665 return;
666 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000667 }
668
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000669 // [FirstMatch, LastMatch) contains the set of declarations that
670 // have the same name as this declaration. Determine where the
671 // declaration D will be inserted into this range.
672 if (D->getIdentifierNamespace() == Decl::IDNS_Tag)
673 InsertPos = LastMatch;
674 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
675 InsertPos = LastMatch - 1;
676 else
677 InsertPos = LastMatch;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000678 }
679
680 if (Size < LookupIsMap - 1) {
681 // The new declaration will fit in the array. Insert the new
682 // declaration at the position Match in the array.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000683 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000684 Array[Idx] = Array[Idx-1];
685
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000686 Array[InsertPos] = D;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000687 LookupPtr.setInt(Size + 1);
688 return;
689 }
690
691 // We've reached capacity in this array. Create a map and copy in
692 // all of the declarations that were stored in the array.
693 StoredDeclsMap *Map = new StoredDeclsMap(16);
694 LookupPtr.setPointer(Map);
695 LookupPtr.setInt(LookupIsMap);
Douglas Gregor39677622008-12-11 20:41:00 +0000696 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000697 insertImpl(Array[Idx]);
698 delete [] Array;
699
700 // Fall through to perform insertion into the map.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000701 MayBeRedeclaration = false;
702 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000703
704 // Insert this declaration into the map.
705 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
706 StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000707 if (Pos != Map->end()) {
708 if (MayBeRedeclaration) {
709 // Determine if this declaration is actually a redeclaration.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000710 std::vector<NamedDecl *>::iterator Redecl
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000711 = std::find_if(Pos->second.begin(), Pos->second.end(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000712 std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000713 D));
714 if (Redecl != Pos->second.end()) {
715 *Redecl = D;
716 return;
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000717 }
718 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000719
Douglas Gregor8acb7272008-12-11 16:49:14 +0000720 // Put this declaration into the appropriate slot.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000721 if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
722 Pos->second.push_back(D);
723 else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000724 NamedDecl *TagD = Pos->second.back();
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000725 Pos->second.back() = D;
726 Pos->second.push_back(TagD);
727 } else
728 Pos->second.push_back(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000729 } else {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000730 (*Map)[D->getDeclName()].push_back(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000731 }
732}