blob: 016f7b1e041b6d2416b042a7196fb3996c29aa48 [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
Steve Naroff0a473932009-01-20 19:53:53 +000095const char *DeclContext::getDeclKindName() const {
96 switch (DeclKind) {
97 default: assert(0 && "Unknown decl kind!");
98 case Decl::TranslationUnit: return "TranslationUnit";
99 case Decl::Namespace: return "Namespace";
100 case Decl::OverloadedFunction: return "OverloadedFunction";
101 case Decl::Typedef: return "Typedef";
102 case Decl::Function: return "Function";
103 case Decl::Var: return "Var";
104 case Decl::ParmVar: return "ParmVar";
105 case Decl::OriginalParmVar: return "OriginalParmVar";
106 case Decl::EnumConstant: return "EnumConstant";
107 case Decl::ObjCIvar: return "ObjCIvar";
108 case Decl::ObjCInterface: return "ObjCInterface";
109 case Decl::ObjCImplementation: return "ObjCImplementation";
110 case Decl::ObjCClass: return "ObjCClass";
111 case Decl::ObjCMethod: return "ObjCMethod";
112 case Decl::ObjCProtocol: return "ObjCProtocol";
113 case Decl::ObjCProperty: return "ObjCProperty";
114 case Decl::ObjCPropertyImpl: return "ObjCPropertyImpl";
115 case Decl::ObjCForwardProtocol: return "ObjCForwardProtocol";
116 case Decl::Record: return "Record";
117 case Decl::CXXRecord: return "CXXRecord";
118 case Decl::Enum: return "Enum";
119 case Decl::Block: return "Block";
120 case Decl::Field: return "Field";
121 }
122}
123
Eli Friedman56d29372008-06-07 16:52:53 +0000124bool Decl::CollectingStats(bool Enable) {
125 if (Enable)
126 StatSwitch = true;
127 return StatSwitch;
128}
129
130void Decl::PrintStats() {
131 fprintf(stderr, "*** Decl Stats:\n");
132 fprintf(stderr, " %d decls total.\n",
Fariborz Jahanian4f5420d2008-12-20 21:06:28 +0000133 int(nFuncs+nVars+nParmVars+nOriginalParmVars+nFieldDecls+nSUC+nCXXSUC+
Eli Friedman56d29372008-06-07 16:52:53 +0000134 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
135 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000136 nAtDefsFieldDecls+nNamespaces+nOverFuncs));
Eli Friedman56d29372008-06-07 16:52:53 +0000137 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
138 nNamespaces, (int)sizeof(NamespaceDecl),
139 int(nNamespaces*sizeof(NamespaceDecl)));
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000140 fprintf(stderr, " %d overloaded function decls, %d each (%d bytes)\n",
141 nOverFuncs, (int)sizeof(OverloadedFunctionDecl),
142 int(nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedman56d29372008-06-07 16:52:53 +0000143 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
144 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
145 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
146 nVars, (int)sizeof(VarDecl),
147 int(nVars*sizeof(VarDecl)));
148 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
149 nParmVars, (int)sizeof(ParmVarDecl),
150 int(nParmVars*sizeof(ParmVarDecl)));
Fariborz Jahanian4f5420d2008-12-20 21:06:28 +0000151 fprintf(stderr, " %d original parameter variable decls, %d each (%d bytes)\n",
152 nOriginalParmVars, (int)sizeof(ParmVarWithOriginalTypeDecl),
153 int(nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)));
Eli Friedman56d29372008-06-07 16:52:53 +0000154 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
155 nFieldDecls, (int)sizeof(FieldDecl),
156 int(nFieldDecls*sizeof(FieldDecl)));
Ted Kremenek01e67792008-08-20 03:26:33 +0000157 fprintf(stderr, " %d @defs generated field decls, %d each (%d bytes)\n",
158 nAtDefsFieldDecls, (int)sizeof(ObjCAtDefsFieldDecl),
159 int(nAtDefsFieldDecls*sizeof(ObjCAtDefsFieldDecl)));
Eli Friedman56d29372008-06-07 16:52:53 +0000160 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
161 nSUC, (int)sizeof(RecordDecl),
162 int(nSUC*sizeof(RecordDecl)));
Argyrios Kyrtzidis55d71f92008-08-10 01:47:31 +0000163 fprintf(stderr, " %d C++ struct/union/class decls, %d each (%d bytes)\n",
164 nCXXSUC, (int)sizeof(CXXRecordDecl),
165 int(nCXXSUC*sizeof(CXXRecordDecl)));
Eli Friedman56d29372008-06-07 16:52:53 +0000166 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
167 nEnumDecls, (int)sizeof(EnumDecl),
168 int(nEnumDecls*sizeof(EnumDecl)));
169 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
170 nEnumConst, (int)sizeof(EnumConstantDecl),
171 int(nEnumConst*sizeof(EnumConstantDecl)));
172 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
173 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
174 // Objective-C decls...
175 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
176 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
177 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
178 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
179 nIvarDecls, (int)sizeof(ObjCIvarDecl),
180 int(nIvarDecls*sizeof(ObjCIvarDecl)));
181 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
182 nClassDecls, (int)sizeof(ObjCClassDecl),
183 int(nClassDecls*sizeof(ObjCClassDecl)));
184 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
185 nMethodDecls, (int)sizeof(ObjCMethodDecl),
186 int(nMethodDecls*sizeof(ObjCMethodDecl)));
187 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
188 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
189 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
190 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
191 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
192 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
193 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
194 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
195 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
196
197 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
198 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
199 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
200
201 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
202 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
203 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
204
205 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
206 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
207 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
208
209 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
210 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
211 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
212
213 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
214 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
215 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
216
217 fprintf(stderr, "Total bytes = %d\n",
218 int(nFuncs*sizeof(FunctionDecl)+
219 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Fariborz Jahanian4f5420d2008-12-20 21:06:28 +0000220 nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)+
Eli Friedman56d29372008-06-07 16:52:53 +0000221 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
Douglas Gregor44b43212008-12-11 16:49:14 +0000222 nCXXSUC*sizeof(CXXRecordDecl)+
Eli Friedman56d29372008-06-07 16:52:53 +0000223 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
224 nTypedef*sizeof(TypedefDecl)+
225 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
226 nIvarDecls*sizeof(ObjCIvarDecl)+
227 nClassDecls*sizeof(ObjCClassDecl)+
228 nMethodDecls*sizeof(ObjCMethodDecl)+
229 nProtocolDecls*sizeof(ObjCProtocolDecl)+
230 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
231 nCategoryDecls*sizeof(ObjCCategoryDecl)+
232 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
233 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
234 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
235 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
236 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
237 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
238 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000239 nNamespaces*sizeof(NamespaceDecl)+
240 nOverFuncs*sizeof(OverloadedFunctionDecl)));
Eli Friedman56d29372008-06-07 16:52:53 +0000241
242}
243
244void Decl::addDeclKind(Kind k) {
245 switch (k) {
246 case Namespace: nNamespaces++; break;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000247 case OverloadedFunction: nOverFuncs++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000248 case Typedef: nTypedef++; break;
249 case Function: nFuncs++; break;
250 case Var: nVars++; break;
251 case ParmVar: nParmVars++; break;
Fariborz Jahanian4f5420d2008-12-20 21:06:28 +0000252 case OriginalParmVar: nOriginalParmVars++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000253 case EnumConstant: nEnumConst++; break;
254 case Field: nFieldDecls++; break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000255 case Record: nSUC++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000256 case Enum: nEnumDecls++; break;
Steve Naroff09c47192009-01-09 15:36:25 +0000257 case ObjCContainer: break; // is abstract...no need to account for.
Eli Friedman56d29372008-06-07 16:52:53 +0000258 case ObjCInterface: nInterfaceDecls++; break;
259 case ObjCClass: nClassDecls++; break;
260 case ObjCMethod: nMethodDecls++; break;
261 case ObjCProtocol: nProtocolDecls++; break;
262 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
263 case ObjCCategory: nCategoryDecls++; break;
264 case ObjCIvar: nIvarDecls++; break;
Ted Kremenek01e67792008-08-20 03:26:33 +0000265 case ObjCAtDefsField: nAtDefsFieldDecls++; break;
Eli Friedman56d29372008-06-07 16:52:53 +0000266 case ObjCImplementation: nObjCImplementationDecls++; break;
267 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
268 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
269 case ObjCProperty: nObjCPropertyDecl++; break;
270 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
271 case LinkageSpec: nLinkageSpecDecl++; break;
272 case FileScopeAsm: nFileScopeAsmDecl++; break;
Steve Naroff56ee6892008-10-08 17:01:13 +0000273 case Block: nBlockDecls++; break;
Chris Lattner41110242008-06-17 18:05:57 +0000274 case ImplicitParam:
Eli Friedman56d29372008-06-07 16:52:53 +0000275 case TranslationUnit: break;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000276
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000277 case CXXRecord: nCXXSUC++; break;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000278 // FIXME: Statistics for C++ decls.
Douglas Gregor72c3f312008-12-05 18:15:24 +0000279 case TemplateTypeParm:
280 case NonTypeTemplateParm:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000281 case CXXMethod:
Douglas Gregorb48fe382008-10-31 09:07:45 +0000282 case CXXConstructor:
Douglas Gregor42a552f2008-11-05 20:51:48 +0000283 case CXXDestructor:
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000284 case CXXConversion:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000285 case CXXClassVar:
286 break;
Eli Friedman56d29372008-06-07 16:52:53 +0000287 }
288}
289
290//===----------------------------------------------------------------------===//
291// Decl Implementation
292//===----------------------------------------------------------------------===//
293
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000294void Decl::setDeclContext(DeclContext *DC) {
295 if (isOutOfSemaDC())
296 delete getMultipleDC();
297
298 DeclCtx = reinterpret_cast<uintptr_t>(DC);
299}
300
301void Decl::setLexicalDeclContext(DeclContext *DC) {
302 if (DC == getLexicalDeclContext())
303 return;
304
305 if (isInSemaDC()) {
306 MultipleDC *MDC = new MultipleDC();
307 MDC->SemanticDC = getDeclContext();
308 MDC->LexicalDC = DC;
309 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
310 } else {
311 getMultipleDC()->LexicalDC = DC;
312 }
313}
314
Eli Friedman56d29372008-06-07 16:52:53 +0000315// Out-of-line virtual method providing a home for Decl.
316Decl::~Decl() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000317 if (isOutOfSemaDC())
318 delete getMultipleDC();
319
Eli Friedman56d29372008-06-07 16:52:53 +0000320 if (!HasAttrs)
321 return;
322
323 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
324 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
325
326 // release attributes.
327 delete it->second;
328 invalidateAttrs();
329}
330
331void Decl::addAttr(Attr *NewAttr) {
332 if (!DeclAttrs)
333 DeclAttrs = new DeclAttrMapTy();
334
335 Attr *&ExistingAttr = (*DeclAttrs)[this];
336
337 NewAttr->setNext(ExistingAttr);
338 ExistingAttr = NewAttr;
339
340 HasAttrs = true;
341}
342
343void Decl::invalidateAttrs() {
344 if (!HasAttrs) return;
345
346 HasAttrs = false;
347 (*DeclAttrs)[this] = 0;
348 DeclAttrs->erase(this);
349
350 if (DeclAttrs->empty()) {
351 delete DeclAttrs;
352 DeclAttrs = 0;
353 }
354}
355
356const Attr *Decl::getAttrs() const {
357 if (!HasAttrs)
358 return 0;
359
360 return (*DeclAttrs)[this];
361}
362
363void Decl::swapAttrs(Decl *RHS) {
364 bool HasLHSAttr = this->HasAttrs;
365 bool HasRHSAttr = RHS->HasAttrs;
366
367 // Usually, neither decl has attrs, nothing to do.
368 if (!HasLHSAttr && !HasRHSAttr) return;
369
370 // If 'this' has no attrs, swap the other way.
371 if (!HasLHSAttr)
372 return RHS->swapAttrs(this);
373
374 // Handle the case when both decls have attrs.
375 if (HasRHSAttr) {
376 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
377 return;
378 }
379
380 // Otherwise, LHS has an attr and RHS doesn't.
381 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
382 (*DeclAttrs).erase(this);
383 this->HasAttrs = false;
384 RHS->HasAttrs = true;
385}
386
387
388void Decl::Destroy(ASTContext& C) {
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000389#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000390 // FIXME: Once ownership is fully understood, we can enable this code
391 if (DeclContext *DC = dyn_cast<DeclContext>(this))
392 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000393
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000394 // Observe the unrolled recursion. By setting N->NextDeclInScope = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000395 // within the loop, only the Destroy method for the first Decl
396 // will deallocate all of the Decls in a chain.
397
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000398 Decl* N = NextDeclInScope;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000399
400 while (N) {
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000401 Decl* Tmp = N->NextDeclInScope;
402 N->NextDeclInScope = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000403 N->Destroy(C);
404 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000405 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000406
Eli Friedman56d29372008-06-07 16:52:53 +0000407 this->~Decl();
408 C.getAllocator().Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000409#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000410}
411
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000412Decl *Decl::castFromDeclContext (const DeclContext *D) {
413 return DeclContext::CastTo<Decl>(D);
414}
415
416DeclContext *Decl::castToDeclContext(const Decl *D) {
417 return DeclContext::CastTo<DeclContext>(D);
418}
419
Eli Friedman56d29372008-06-07 16:52:53 +0000420//===----------------------------------------------------------------------===//
421// DeclContext Implementation
422//===----------------------------------------------------------------------===//
423
Argyrios Kyrtzidis20bc6762008-11-19 17:36:39 +0000424const DeclContext *DeclContext::getParent() const {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000425 if (const Decl *D = dyn_cast<Decl>(this))
426 return D->getDeclContext();
427
428 return NULL;
Eli Friedman56d29372008-06-07 16:52:53 +0000429}
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000430
431const DeclContext *DeclContext::getLexicalParent() const {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000432 if (const Decl *D = dyn_cast<Decl>(this))
433 return D->getLexicalDeclContext();
434
Argyrios Kyrtzidis051c13a2008-11-19 18:07:24 +0000435 return getParent();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000436}
Douglas Gregor44b43212008-12-11 16:49:14 +0000437
Douglas Gregor44b43212008-12-11 16:49:14 +0000438// FIXME: We really want to use a DenseSet here to eliminate the
439// redundant storage of the declaration names, but (1) it doesn't give
440// us the ability to search based on DeclarationName, (2) we really
441// need something more like a DenseMultiSet, and (3) it's
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000442// implemented in terms of DenseMap anyway. However, this data
443// structure is really space-inefficient, so we'll have to do
444// something.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000445typedef llvm::DenseMap<DeclarationName, std::vector<NamedDecl*> >
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000446 StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000447
448DeclContext::~DeclContext() {
449 unsigned Size = LookupPtr.getInt();
450 if (Size == LookupIsMap) {
451 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
452 delete Map;
453 } else {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000454 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor44b43212008-12-11 16:49:14 +0000455 delete [] Array;
456 }
457}
458
459void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000460 for (decl_iterator D = decls_begin(); D != decls_end(); )
461 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000462}
463
Douglas Gregor074149e2009-01-05 19:45:36 +0000464bool DeclContext::isTransparentContext() const {
465 if (DeclKind == Decl::Enum)
466 return true; // FIXME: Check for C++0x scoped enums
467 else if (DeclKind == Decl::LinkageSpec)
468 return true;
469 else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000470 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000471 else if (DeclKind == Decl::Namespace)
472 return false; // FIXME: Check for C++0x inline namespaces
473
474 return false;
475}
476
Steve Naroff0701bbb2009-01-08 17:28:14 +0000477DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000478 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000479 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000480 case Decl::LinkageSpec:
481 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000482 // There is only one DeclContext for these entities.
483 return this;
484
485 case Decl::Namespace:
486 // The original namespace is our primary context.
487 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
488
489 case Decl::Enum:
Douglas Gregor44b43212008-12-11 16:49:14 +0000490 case Decl::Record:
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000491 case Decl::CXXRecord:
492 // If this is a tag type that has a definition or is currently
493 // being defined, that definition is our primary context.
494 if (TagType *TagT = cast_or_null<TagType>(cast<TagDecl>(this)->TypeForDecl))
495 if (TagT->isBeingDefined() ||
496 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
497 return TagT->getDecl();
Douglas Gregor44b43212008-12-11 16:49:14 +0000498 return this;
Douglas Gregor44b43212008-12-11 16:49:14 +0000499
500 case Decl::ObjCMethod:
501 return this;
502
503 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000504 case Decl::ObjCProtocol:
505 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000506 // FIXME: Can Objective-C interfaces be forward-declared?
507 return this;
508
Steve Naroff0701bbb2009-01-08 17:28:14 +0000509 case Decl::ObjCImplementation:
510 case Decl::ObjCCategoryImpl:
511 return this;
512
Douglas Gregor44b43212008-12-11 16:49:14 +0000513 default:
514 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
515 "Unknown DeclContext kind");
516 return this;
517 }
518}
519
520DeclContext *DeclContext::getNextContext() {
521 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000522 case Decl::TranslationUnit:
523 case Decl::Enum:
524 case Decl::Record:
525 case Decl::CXXRecord:
526 case Decl::ObjCMethod:
527 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000528 case Decl::ObjCCategory:
529 case Decl::ObjCProtocol:
530 case Decl::ObjCImplementation:
531 case Decl::ObjCCategoryImpl:
Douglas Gregor074149e2009-01-05 19:45:36 +0000532 case Decl::LinkageSpec:
533 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000534 // There is only one DeclContext for these entities.
535 return 0;
536
537 case Decl::Namespace:
538 // Return the next namespace
539 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
540
541 default:
542 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
543 "Unknown DeclContext kind");
544 return 0;
545 }
546}
547
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000548void DeclContext::addDecl(Decl *D) {
Douglas Gregora8cc8ce2009-01-09 18:51:29 +0000549 assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000550 assert(!D->NextDeclInScope && D != LastDecl &&
551 "Decl already inserted into a DeclContext");
552
553 if (FirstDecl) {
554 LastDecl->NextDeclInScope = D;
555 LastDecl = D;
556 } else {
557 FirstDecl = LastDecl = D;
558 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000559
560 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor40f4e692009-01-20 16:54:50 +0000561 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000562}
563
Douglas Gregor074149e2009-01-05 19:45:36 +0000564/// buildLookup - Build the lookup data structure with all of the
565/// declarations in DCtx (and any other contexts linked to it or
566/// transparent contexts nested within it).
Steve Naroff0701bbb2009-01-08 17:28:14 +0000567void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000568 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000569 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
570 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000571 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000572 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor40f4e692009-01-20 16:54:50 +0000573 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000574
575 // If this declaration is itself a transparent declaration context,
576 // add its members (recursively).
577 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
578 if (InnerCtx->isTransparentContext())
Steve Naroff0701bbb2009-01-08 17:28:14 +0000579 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000580 }
581 }
582}
583
Douglas Gregor44b43212008-12-11 16:49:14 +0000584DeclContext::lookup_result
Steve Naroff0701bbb2009-01-08 17:28:14 +0000585DeclContext::lookup(DeclarationName Name) {
586 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000587 if (PrimaryContext != this)
Steve Naroff0701bbb2009-01-08 17:28:14 +0000588 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000589
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000590 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000591 /// all of the linked DeclContexts (in declaration order!) and
592 /// inserting their values.
Douglas Gregor074149e2009-01-05 19:45:36 +0000593 if (LookupPtr.getPointer() == 0)
Steve Naroff0701bbb2009-01-08 17:28:14 +0000594 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000595
Douglas Gregor44b43212008-12-11 16:49:14 +0000596 if (isLookupMap()) {
597 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
598 StoredDeclsMap::iterator Pos = Map->find(Name);
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000599 if (Pos != Map->end())
600 return lookup_result(&Pos->second.front(),
601 &Pos->second.front() + Pos->second.size());
602 return lookup_result(0, 0);
Douglas Gregor44b43212008-12-11 16:49:14 +0000603 }
604
605 // We have a small array. Look into it.
606 unsigned Size = LookupPtr.getInt();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000607 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregore267ff32008-12-11 20:41:00 +0000608 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000609 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000610 unsigned Last = Idx + 1;
611 while (Last != Size && Array[Last]->getDeclName() == Name)
612 ++Last;
613 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor44b43212008-12-11 16:49:14 +0000614 }
615
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000616 return lookup_result(0, 0);
Douglas Gregor44b43212008-12-11 16:49:14 +0000617}
618
619DeclContext::lookup_const_result
Steve Naroff0701bbb2009-01-08 17:28:14 +0000620DeclContext::lookup(DeclarationName Name) const {
621 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000622}
623
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000624const DeclContext *DeclContext::getLookupContext() const {
625 const DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000626 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000627 while (Ctx->isTransparentContext())
628 Ctx = Ctx->getParent();
629 return Ctx;
630}
631
Douglas Gregor40f4e692009-01-20 16:54:50 +0000632void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000633 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000634 if (PrimaryContext != this) {
Douglas Gregor40f4e692009-01-20 16:54:50 +0000635 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000636 return;
637 }
638
639 // If we already have a lookup data structure, perform the insertion
640 // into it. Otherwise, be lazy and don't build that structure until
641 // someone asks for it.
642 if (LookupPtr.getPointer())
Douglas Gregor40f4e692009-01-20 16:54:50 +0000643 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000644
Douglas Gregor074149e2009-01-05 19:45:36 +0000645 // If we are a transparent context, insert into our parent context,
646 // too. This operation is recursive.
647 if (isTransparentContext())
Douglas Gregor40f4e692009-01-20 16:54:50 +0000648 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000649}
650
Douglas Gregor40f4e692009-01-20 16:54:50 +0000651void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000652 // Skip unnamed declarations.
653 if (!D->getDeclName())
654 return;
655
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000656 bool MayBeRedeclaration = true;
657
Douglas Gregor44b43212008-12-11 16:49:14 +0000658 if (!isLookupMap()) {
659 unsigned Size = LookupPtr.getInt();
660
661 // The lookup data is stored as an array. Search through the array
662 // to find the insertion location.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000663 NamedDecl **Array;
Douglas Gregor44b43212008-12-11 16:49:14 +0000664 if (Size == 0) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000665 Array = new NamedDecl*[LookupIsMap - 1];
Douglas Gregor44b43212008-12-11 16:49:14 +0000666 LookupPtr.setPointer(Array);
667 } else {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000668 Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
Douglas Gregor44b43212008-12-11 16:49:14 +0000669 }
670
671 // We always keep declarations of the same name next to each other
672 // in the array, so that it is easy to return multiple results
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000673 // from lookup().
674 unsigned FirstMatch;
675 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
676 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregore267ff32008-12-11 20:41:00 +0000677 break;
Douglas Gregor44b43212008-12-11 16:49:14 +0000678
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000679 unsigned InsertPos = FirstMatch;
680 if (FirstMatch != Size) {
681 // We found another declaration with the same name. First
682 // determine whether this is a redeclaration of an existing
683 // declaration in this scope, in which case we will replace the
684 // existing declaration.
685 unsigned LastMatch = FirstMatch;
686 for (; LastMatch != Size; ++LastMatch) {
687 if (Array[LastMatch]->getDeclName() != D->getDeclName())
688 break;
689
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000690 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000691 // D is a redeclaration of an existing element in the
692 // array. Replace that element with D.
693 Array[LastMatch] = D;
694 return;
695 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000696 }
697
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000698 // [FirstMatch, LastMatch) contains the set of declarations that
699 // have the same name as this declaration. Determine where the
700 // declaration D will be inserted into this range.
701 if (D->getIdentifierNamespace() == Decl::IDNS_Tag)
702 InsertPos = LastMatch;
703 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
704 InsertPos = LastMatch - 1;
705 else
706 InsertPos = LastMatch;
Douglas Gregor44b43212008-12-11 16:49:14 +0000707 }
708
709 if (Size < LookupIsMap - 1) {
710 // The new declaration will fit in the array. Insert the new
711 // declaration at the position Match in the array.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000712 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000713 Array[Idx] = Array[Idx-1];
714
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000715 Array[InsertPos] = D;
Douglas Gregor44b43212008-12-11 16:49:14 +0000716 LookupPtr.setInt(Size + 1);
717 return;
718 }
719
720 // We've reached capacity in this array. Create a map and copy in
721 // all of the declarations that were stored in the array.
722 StoredDeclsMap *Map = new StoredDeclsMap(16);
723 LookupPtr.setPointer(Map);
724 LookupPtr.setInt(LookupIsMap);
Douglas Gregore267ff32008-12-11 20:41:00 +0000725 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor40f4e692009-01-20 16:54:50 +0000726 makeDeclVisibleInContextImpl(Array[Idx]);
Douglas Gregor44b43212008-12-11 16:49:14 +0000727 delete [] Array;
728
729 // Fall through to perform insertion into the map.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000730 MayBeRedeclaration = false;
731 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000732
733 // Insert this declaration into the map.
734 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
735 StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000736 if (Pos != Map->end()) {
737 if (MayBeRedeclaration) {
738 // Determine if this declaration is actually a redeclaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000739 std::vector<NamedDecl *>::iterator Redecl
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000740 = std::find_if(Pos->second.begin(), Pos->second.end(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000741 std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000742 D));
743 if (Redecl != Pos->second.end()) {
744 *Redecl = D;
745 return;
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000746 }
747 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000748
Douglas Gregor44b43212008-12-11 16:49:14 +0000749 // Put this declaration into the appropriate slot.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000750 if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
751 Pos->second.push_back(D);
752 else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000753 NamedDecl *TagD = Pos->second.back();
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000754 Pos->second.back() = D;
755 Pos->second.push_back(TagD);
756 } else
757 Pos->second.push_back(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000758 } else {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000759 (*Map)[D->getDeclName()].push_back(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000760 }
761}