blob: a20786035a290fb3af100471f97c11b38fd6c4b9 [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"
Douglas Gregor469fc9a2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregord2b6edc2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor8acb7272008-12-11 16:49:14 +000022#include "clang/AST/Type.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000023#include "llvm/ADT/DenseMap.h"
Chris Lattnerc309ade2009-03-05 08:00:35 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregor6e71edc2008-12-23 21:05:05 +000025#include <algorithm>
Chris Lattner7d6220c2009-03-02 22:20:04 +000026#include <cstdio>
Douglas Gregorddfd9d52008-12-23 00:26:44 +000027#include <vector>
Eli Friedmana8a09ac2008-06-07 16:52:53 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// Statistics
32//===----------------------------------------------------------------------===//
33
Douglas Gregor469fc9a2009-02-02 23:39:07 +000034#define DECL(Derived, Base) static int n##Derived##s = 0;
35#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000036
37static bool StatSwitch = false;
38
39// This keeps track of all decl attributes. Since so few decls have attrs, we
40// keep them in a hash map instead of wasting space in the Decl class.
41typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
42
43static DeclAttrMapTy *DeclAttrs = 0;
44
45const char *Decl::getDeclKindName() const {
46 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000047 default: assert(0 && "Declaration not in DeclNodes.def!");
48#define DECL(Derived, Base) case Derived: return #Derived;
49#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000050 }
51}
52
Steve Naroffe5f128a2009-01-20 19:53:53 +000053const char *DeclContext::getDeclKindName() const {
54 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000055 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argiris Kirtzidis2d9b7612009-02-16 14:28:33 +000056#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor469fc9a2009-02-02 23:39:07 +000057#include "clang/AST/DeclNodes.def"
Steve Naroffe5f128a2009-01-20 19:53:53 +000058 }
59}
60
Eli Friedmana8a09ac2008-06-07 16:52:53 +000061bool Decl::CollectingStats(bool Enable) {
62 if (Enable)
63 StatSwitch = true;
64 return StatSwitch;
65}
66
67void Decl::PrintStats() {
68 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedmana8a09ac2008-06-07 16:52:53 +000069
Douglas Gregor469fc9a2009-02-02 23:39:07 +000070 int totalDecls = 0;
71#define DECL(Derived, Base) totalDecls += n##Derived##s;
72#include "clang/AST/DeclNodes.def"
73 fprintf(stderr, " %d decls total.\n", totalDecls);
74
75 int totalBytes = 0;
76#define DECL(Derived, Base) \
77 if (n##Derived##s > 0) { \
78 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
79 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
80 n##Derived##s, (int)sizeof(Derived##Decl), \
81 (int)(n##Derived##s * sizeof(Derived##Decl))); \
82 }
83#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000084
Douglas Gregor469fc9a2009-02-02 23:39:07 +000085 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedmana8a09ac2008-06-07 16:52:53 +000086}
87
88void Decl::addDeclKind(Kind k) {
89 switch (k) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000090 default: assert(0 && "Declaration not in DeclNodes.def!");
91#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
92#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000093 }
94}
95
96//===----------------------------------------------------------------------===//
Chris Lattnerc309ade2009-03-05 08:00:35 +000097// PrettyStackTraceDecl Implementation
98//===----------------------------------------------------------------------===//
99
100void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
101 SourceLocation TheLoc = Loc;
102 if (TheLoc.isInvalid() && TheDecl)
103 TheLoc = TheDecl->getLocation();
104
105 if (TheLoc.isValid()) {
106 TheLoc.print(OS, SM);
107 OS << ": ";
108 }
109
110 OS << Message;
111
112 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
113 OS << " '" << DN->getQualifiedNameAsString() << '\'';
114 OS << '\n';
115}
116
117//===----------------------------------------------------------------------===//
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000118// Decl Implementation
119//===----------------------------------------------------------------------===//
120
Chris Lattner8752fe02009-03-27 20:18:19 +0000121// Out-of-line virtual method providing a home for Decl.
122Decl::~Decl() {
123 if (isOutOfSemaDC())
124 delete getMultipleDC();
125
126 assert(!HasAttrs && "attributes should have been freed by Destroy");
127}
128
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000129void Decl::setDeclContext(DeclContext *DC) {
130 if (isOutOfSemaDC())
131 delete getMultipleDC();
132
Chris Lattner5ce4f6d2009-03-29 06:06:59 +0000133 DeclCtx = DC;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000134}
135
136void Decl::setLexicalDeclContext(DeclContext *DC) {
137 if (DC == getLexicalDeclContext())
138 return;
139
140 if (isInSemaDC()) {
141 MultipleDC *MDC = new MultipleDC();
142 MDC->SemanticDC = getDeclContext();
143 MDC->LexicalDC = DC;
Chris Lattner5ce4f6d2009-03-29 06:06:59 +0000144 DeclCtx = MDC;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000145 } else {
146 getMultipleDC()->LexicalDC = DC;
147 }
148}
149
Chris Lattner8752fe02009-03-27 20:18:19 +0000150unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
151 switch (DeclKind) {
152 default:
153 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
154 return IDNS_Ordinary;
155 assert(0 && "Unknown decl kind!");
156 case OverloadedFunction:
157 case Typedef:
158 case EnumConstant:
159 case Var:
160 case ImplicitParam:
161 case ParmVar:
162 case OriginalParmVar:
163 case NonTypeTemplateParm:
164 case ObjCMethod:
165 case ObjCContainer:
166 case ObjCCategory:
167 case ObjCInterface:
Chris Lattner8752fe02009-03-27 20:18:19 +0000168 case ObjCProperty:
169 case ObjCCompatibleAlias:
170 return IDNS_Ordinary;
171
172 case ObjCProtocol:
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000173 return IDNS_ObjCProtocol;
Chris Lattner8752fe02009-03-27 20:18:19 +0000174
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000175 case ObjCImplementation:
176 return IDNS_ObjCImplementation;
177
178 case ObjCCategoryImpl:
179 return IDNS_ObjCCategoryImpl;
180
Chris Lattner8752fe02009-03-27 20:18:19 +0000181 case Field:
182 case ObjCAtDefsField:
183 case ObjCIvar:
184 return IDNS_Member;
185
186 case Record:
187 case CXXRecord:
188 case Enum:
189 case TemplateTypeParm:
190 return IDNS_Tag;
191
192 case Namespace:
193 case Template:
194 case FunctionTemplate:
195 case ClassTemplate:
196 case TemplateTemplateParm:
Anders Carlsson1bf91fb2009-03-28 23:02:53 +0000197 case NamespaceAlias:
Chris Lattner8752fe02009-03-27 20:18:19 +0000198 return IDNS_Tag | IDNS_Ordinary;
199
200 // Never have names.
201 case LinkageSpec:
202 case FileScopeAsm:
203 case StaticAssert:
204 case ObjCClass:
Chris Lattner8752fe02009-03-27 20:18:19 +0000205 case ObjCPropertyImpl:
206 case ObjCForwardProtocol:
207 case Block:
208 case TranslationUnit:
Anders Carlssoncf4dc9e2009-04-24 06:02:55 +0000209 case CXXTempVar:
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000210
Chris Lattner8752fe02009-03-27 20:18:19 +0000211 // Aren't looked up?
212 case UsingDirective:
213 case ClassTemplateSpecialization:
214 return 0;
215 }
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000216}
217
218void Decl::addAttr(Attr *NewAttr) {
219 if (!DeclAttrs)
220 DeclAttrs = new DeclAttrMapTy();
221
222 Attr *&ExistingAttr = (*DeclAttrs)[this];
223
224 NewAttr->setNext(ExistingAttr);
225 ExistingAttr = NewAttr;
226
227 HasAttrs = true;
228}
229
230void Decl::invalidateAttrs() {
231 if (!HasAttrs) return;
232
233 HasAttrs = false;
234 (*DeclAttrs)[this] = 0;
235 DeclAttrs->erase(this);
236
237 if (DeclAttrs->empty()) {
238 delete DeclAttrs;
239 DeclAttrs = 0;
240 }
241}
242
Chris Lattner004a9bb2009-03-21 06:27:31 +0000243const Attr *Decl::getAttrsImpl() const {
244 assert(HasAttrs && "getAttrs() should verify this!");
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000245 return (*DeclAttrs)[this];
246}
247
248void Decl::swapAttrs(Decl *RHS) {
249 bool HasLHSAttr = this->HasAttrs;
250 bool HasRHSAttr = RHS->HasAttrs;
251
252 // Usually, neither decl has attrs, nothing to do.
253 if (!HasLHSAttr && !HasRHSAttr) return;
254
255 // If 'this' has no attrs, swap the other way.
256 if (!HasLHSAttr)
257 return RHS->swapAttrs(this);
258
259 // Handle the case when both decls have attrs.
260 if (HasRHSAttr) {
261 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
262 return;
263 }
264
265 // Otherwise, LHS has an attr and RHS doesn't.
266 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
267 (*DeclAttrs).erase(this);
268 this->HasAttrs = false;
269 RHS->HasAttrs = true;
270}
271
272
Chris Lattner0fc6d642009-03-04 06:05:19 +0000273void Decl::Destroy(ASTContext &C) {
274 // Free attributes for this decl.
275 if (HasAttrs) {
276 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
277 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
278
279 // release attributes.
280 it->second->Destroy(C);
281 invalidateAttrs();
282 HasAttrs = false;
283 }
284
Douglas Gregor8314d742009-01-13 19:47:12 +0000285#if 0
Douglas Gregorf4006be2009-01-20 04:25:11 +0000286 // FIXME: Once ownership is fully understood, we can enable this code
287 if (DeclContext *DC = dyn_cast<DeclContext>(this))
288 DC->decls_begin()->Destroy(C);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000289
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000290 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000291 // within the loop, only the Destroy method for the first Decl
292 // will deallocate all of the Decls in a chain.
293
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000294 Decl* N = getNextDeclInContext();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000295
296 while (N) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000297 Decl* Tmp = N->getNextDeclInContext();
298 N->NextDeclInContext = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000299 N->Destroy(C);
300 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000301 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000302
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000303 this->~Decl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000304 C.Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000305#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000306}
307
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000308Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000309 Decl::Kind DK = D->getDeclKind();
310 switch(DK) {
311#define DECL_CONTEXT(Name) \
312 case Decl::Name: \
313 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
314#define DECL_CONTEXT_BASE(Name)
315#include "clang/AST/DeclNodes.def"
316 default:
317#define DECL_CONTEXT_BASE(Name) \
318 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
319 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
320#include "clang/AST/DeclNodes.def"
321 assert(false && "a decl that inherits DeclContext isn't handled");
322 return 0;
323 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000324}
325
326DeclContext *Decl::castToDeclContext(const Decl *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000327 Decl::Kind DK = D->getKind();
328 switch(DK) {
329#define DECL_CONTEXT(Name) \
330 case Decl::Name: \
331 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
332#define DECL_CONTEXT_BASE(Name)
333#include "clang/AST/DeclNodes.def"
334 default:
335#define DECL_CONTEXT_BASE(Name) \
336 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
337 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
338#include "clang/AST/DeclNodes.def"
339 assert(false && "a decl that inherits DeclContext isn't handled");
340 return 0;
341 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000342}
343
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000344#ifndef NDEBUG
345void Decl::CheckAccessDeclContext() const {
Douglas Gregora470cea2009-04-07 20:58:25 +0000346 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
347 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000348 "Access specifier is AS_none inside a record decl");
349}
350
351#endif
352
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000353//===----------------------------------------------------------------------===//
354// DeclContext Implementation
355//===----------------------------------------------------------------------===//
356
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000357bool DeclContext::classof(const Decl *D) {
358 switch (D->getKind()) {
359#define DECL_CONTEXT(Name) case Decl::Name:
360#define DECL_CONTEXT_BASE(Name)
361#include "clang/AST/DeclNodes.def"
362 return true;
363 default:
364#define DECL_CONTEXT_BASE(Name) \
365 if (D->getKind() >= Decl::Name##First && \
366 D->getKind() <= Decl::Name##Last) \
367 return true;
368#include "clang/AST/DeclNodes.def"
369 return false;
370 }
371}
372
Douglas Gregor8acb7272008-12-11 16:49:14 +0000373DeclContext::~DeclContext() {
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000374 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000375}
376
377void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000378 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregorf4006be2009-01-20 04:25:11 +0000379 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000380}
381
Douglas Gregord8028382009-01-05 19:45:36 +0000382bool DeclContext::isTransparentContext() const {
383 if (DeclKind == Decl::Enum)
384 return true; // FIXME: Check for C++0x scoped enums
385 else if (DeclKind == Decl::LinkageSpec)
386 return true;
Douglas Gregor1db12932009-02-26 00:02:51 +0000387 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor723d3332009-01-07 00:43:41 +0000388 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000389 else if (DeclKind == Decl::Namespace)
390 return false; // FIXME: Check for C++0x inline namespaces
391
392 return false;
393}
394
Steve Naroffab63fd62009-01-08 17:28:14 +0000395DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000396 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000397 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000398 case Decl::LinkageSpec:
399 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000400 // There is only one DeclContext for these entities.
401 return this;
402
403 case Decl::Namespace:
404 // The original namespace is our primary context.
405 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
406
Douglas Gregor8acb7272008-12-11 16:49:14 +0000407 case Decl::ObjCMethod:
408 return this;
409
410 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000411 case Decl::ObjCProtocol:
412 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000413 // FIXME: Can Objective-C interfaces be forward-declared?
414 return this;
415
Steve Naroffab63fd62009-01-08 17:28:14 +0000416 case Decl::ObjCImplementation:
417 case Decl::ObjCCategoryImpl:
418 return this;
419
Douglas Gregor8acb7272008-12-11 16:49:14 +0000420 default:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000421 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
422 // If this is a tag type that has a definition or is currently
423 // being defined, that definition is our primary context.
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000424 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregora08b6c72009-02-17 23:15:12 +0000425 if (TagT->isBeingDefined() ||
426 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
427 return TagT->getDecl();
428 return this;
429 }
430
Douglas Gregor8acb7272008-12-11 16:49:14 +0000431 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
432 "Unknown DeclContext kind");
433 return this;
434 }
435}
436
437DeclContext *DeclContext::getNextContext() {
438 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000439 case Decl::Namespace:
440 // Return the next namespace
441 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
442
443 default:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000444 return 0;
445 }
446}
447
Douglas Gregorc34897d2009-04-09 22:27:44 +0000448/// \brief Load the declarations within this lexical storage from an
449/// external source.
450void
451DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
452 ExternalASTSource *Source = Context.getExternalSource();
453 assert(hasExternalLexicalStorage() && Source && "No external storage?");
454
455 llvm::SmallVector<unsigned, 64> Decls;
456 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
457 Decls))
458 return;
459
460 // There is no longer any lexical storage in this context
461 ExternalLexicalStorage = false;
462
463 if (Decls.empty())
464 return;
465
466 // Resolve all of the declaration IDs into declarations, building up
467 // a chain of declarations via the Decl::NextDeclInContext field.
468 Decl *FirstNewDecl = 0;
469 Decl *PrevDecl = 0;
470 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
471 Decl *D = Source->GetDecl(Decls[I]);
472 if (PrevDecl)
473 PrevDecl->NextDeclInContext = D;
474 else
475 FirstNewDecl = D;
476
477 PrevDecl = D;
478 }
479
480 // Splice the newly-read declarations into the beginning of the list
481 // of declarations.
482 PrevDecl->NextDeclInContext = FirstDecl;
483 FirstDecl = FirstNewDecl;
484 if (!LastDecl)
485 LastDecl = PrevDecl;
486}
487
488void
489DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
490 DeclContext *This = const_cast<DeclContext *>(this);
491 ExternalASTSource *Source = Context.getExternalSource();
492 assert(hasExternalVisibleStorage() && Source && "No external storage?");
493
494 llvm::SmallVector<VisibleDeclaration, 64> Decls;
495 if (Source->ReadDeclsVisibleInContext(This, Decls))
496 return;
497
498 // There is no longer any visible storage in this context
499 ExternalVisibleStorage = false;
500
501 // Load the declaration IDs for all of the names visible in this
502 // context.
503 assert(!LookupPtr && "Have a lookup map before de-serialization?");
504 StoredDeclsMap *Map = new StoredDeclsMap;
505 LookupPtr = Map;
506 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
507 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
508 }
509}
510
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000511DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000512 if (hasExternalLexicalStorage())
513 LoadLexicalDeclsFromExternalStorage(Context);
514
515 // FIXME: Check whether we need to load some declarations from
516 // external storage.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000517 return decl_iterator(FirstDecl);
518}
519
520DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000521 if (hasExternalLexicalStorage())
522 LoadLexicalDeclsFromExternalStorage(Context);
523
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000524 return decl_iterator();
525}
526
Douglas Gregorac8f2802009-04-10 17:25:41 +0000527bool DeclContext::decls_empty(ASTContext &Context) const {
528 if (hasExternalLexicalStorage())
529 LoadLexicalDeclsFromExternalStorage(Context);
530
531 return !FirstDecl;
532}
533
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000534void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner53602072009-02-20 00:56:18 +0000535 assert(D->getLexicalDeclContext() == this &&
536 "Decl inserted into wrong lexical context");
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000537 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregord1675382009-01-09 19:42:16 +0000538 "Decl already inserted into a DeclContext");
539
540 if (FirstDecl) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000541 LastDecl->NextDeclInContext = D;
Douglas Gregord1675382009-01-09 19:42:16 +0000542 LastDecl = D;
543 } else {
544 FirstDecl = LastDecl = D;
545 }
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000546
547 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000548 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000549}
550
Douglas Gregord8028382009-01-05 19:45:36 +0000551/// buildLookup - Build the lookup data structure with all of the
552/// declarations in DCtx (and any other contexts linked to it or
553/// transparent contexts nested within it).
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000554void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000555 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000556 for (decl_iterator D = DCtx->decls_begin(Context),
557 DEnd = DCtx->decls_end(Context);
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000558 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000559 // Insert this declaration into the lookup structure
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000560 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000561 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000562
563 // If this declaration is itself a transparent declaration context,
564 // add its members (recursively).
565 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
566 if (InnerCtx->isTransparentContext())
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000567 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000568 }
569 }
570}
571
Douglas Gregor8acb7272008-12-11 16:49:14 +0000572DeclContext::lookup_result
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000573DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000574 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000575 if (PrimaryContext != this)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000576 return PrimaryContext->lookup(Context, Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000577
Douglas Gregorc34897d2009-04-09 22:27:44 +0000578 if (hasExternalVisibleStorage())
579 LoadVisibleDeclsFromExternalStorage(Context);
580
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000581 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000582 /// all of the linked DeclContexts (in declaration order!) and
583 /// inserting their values.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000584 if (!LookupPtr) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000585 buildLookup(Context, this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000586
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000587 if (!LookupPtr)
Chris Lattner265f01d2009-02-20 00:55:03 +0000588 return lookup_result(0, 0);
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000589 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000590
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000591 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
592 StoredDeclsMap::iterator Pos = Map->find(Name);
593 if (Pos == Map->end())
594 return lookup_result(0, 0);
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000595 return Pos->second.getLookupResult(Context);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000596}
597
598DeclContext::lookup_const_result
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000599DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
600 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000601}
602
Chris Lattner9a502bd2009-03-27 19:19:59 +0000603DeclContext *DeclContext::getLookupContext() {
604 DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000605 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000606 while (Ctx->isTransparentContext())
607 Ctx = Ctx->getParent();
608 return Ctx;
609}
610
Douglas Gregor0d93f692009-02-25 22:02:03 +0000611DeclContext *DeclContext::getEnclosingNamespaceContext() {
612 DeclContext *Ctx = this;
613 // Skip through non-namespace, non-translation-unit contexts.
614 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
615 Ctx = Ctx->getParent();
616 return Ctx->getPrimaryContext();
617}
618
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000619void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000620 // FIXME: This feels like a hack. Should DeclarationName support
621 // template-ids, or is there a better way to keep specializations
622 // from being visible?
623 if (isa<ClassTemplateSpecializationDecl>(D))
624 return;
625
Steve Naroffab63fd62009-01-08 17:28:14 +0000626 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000627 if (PrimaryContext != this) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000628 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000629 return;
630 }
631
632 // If we already have a lookup data structure, perform the insertion
633 // into it. Otherwise, be lazy and don't build that structure until
634 // someone asks for it.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000635 if (LookupPtr)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000636 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregord8028382009-01-05 19:45:36 +0000637
Douglas Gregord8028382009-01-05 19:45:36 +0000638 // If we are a transparent context, insert into our parent context,
639 // too. This operation is recursive.
640 if (isTransparentContext())
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000641 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000642}
643
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000644void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
645 NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000646 // Skip unnamed declarations.
647 if (!D->getDeclName())
648 return;
649
Douglas Gregora08b6c72009-02-17 23:15:12 +0000650 // FIXME: This feels like a hack. Should DeclarationName support
651 // template-ids, or is there a better way to keep specializations
652 // from being visible?
653 if (isa<ClassTemplateSpecializationDecl>(D))
654 return;
655
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000656 if (!LookupPtr)
657 LookupPtr = new StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000658
659 // Insert this declaration into the map.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000660 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner77820d52009-02-20 01:44:05 +0000661 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
662 if (DeclNameEntries.isNull()) {
663 DeclNameEntries.setOnlyValue(D);
Chris Lattner6719b1f2009-02-19 07:00:44 +0000664 return;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000665 }
Chris Lattner265f01d2009-02-20 00:55:03 +0000666
Chris Lattnerba589d42009-02-20 01:10:07 +0000667 // If it is possible that this is a redeclaration, check to see if there is
668 // already a decl for which declarationReplaces returns true. If there is
669 // one, just replace it and return.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000670 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner77820d52009-02-20 01:44:05 +0000671 return;
Chris Lattner265f01d2009-02-20 00:55:03 +0000672
Chris Lattner6719b1f2009-02-19 07:00:44 +0000673 // Put this declaration into the appropriate slot.
Chris Lattner77820d52009-02-20 01:44:05 +0000674 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000675}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000676
677/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
678/// this context.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000679DeclContext::udir_iterator_range
680DeclContext::getUsingDirectives(ASTContext &Context) const {
681 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor7a7be652009-02-03 19:21:40 +0000682 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
683 reinterpret_cast<udir_iterator>(Result.second));
684}
Douglas Gregorc34897d2009-04-09 22:27:44 +0000685
686void StoredDeclsList::materializeDecls(ASTContext &Context) {
687 if (isNull())
688 return;
689
690 switch ((DataKind)(Data & 0x03)) {
691 case DK_Decl:
692 case DK_Decl_Vector:
693 break;
694
695 case DK_DeclID: {
696 // Resolve this declaration ID to an actual declaration by
697 // querying the external AST source.
698 unsigned DeclID = Data >> 2;
699
700 ExternalASTSource *Source = Context.getExternalSource();
701 assert(Source && "No external AST source available!");
702
703 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
704 break;
705 }
706
707 case DK_ID_Vector: {
708 // We have a vector of declaration IDs. Resolve all of them to
709 // actual declarations.
710 VectorTy &Vector = *getAsVector();
711 ExternalASTSource *Source = Context.getExternalSource();
712 assert(Source && "No external AST source available!");
713
714 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
715 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
716
717 Data = (Data & ~0x03) | DK_Decl_Vector;
718 break;
719 }
720 }
721}