blob: fd7de715db7ff99002cbeb92c60d495f0814acdd [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"
Douglas Gregor64650af2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorc2ee10d2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000022#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000023#include "clang/AST/Stmt.h"
24#include "clang/AST/StmtCXX.h"
Eli Friedman56d29372008-06-07 16:52:53 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000027#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000028#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000029#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Statistics
34//===----------------------------------------------------------------------===//
35
Douglas Gregor64650af2009-02-02 23:39:07 +000036#define DECL(Derived, Base) static int n##Derived##s = 0;
37#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000038
39static bool StatSwitch = false;
40
41// This keeps track of all decl attributes. Since so few decls have attrs, we
42// keep them in a hash map instead of wasting space in the Decl class.
43typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
44
45static DeclAttrMapTy *DeclAttrs = 0;
46
47const char *Decl::getDeclKindName() const {
48 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000049 default: assert(0 && "Declaration not in DeclNodes.def!");
50#define DECL(Derived, Base) case Derived: return #Derived;
51#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000052 }
53}
54
Steve Naroff0a473932009-01-20 19:53:53 +000055const char *DeclContext::getDeclKindName() const {
56 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000057 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000058#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000059#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000060 }
61}
62
Eli Friedman56d29372008-06-07 16:52:53 +000063bool Decl::CollectingStats(bool Enable) {
64 if (Enable)
65 StatSwitch = true;
66 return StatSwitch;
67}
68
69void Decl::PrintStats() {
70 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedman56d29372008-06-07 16:52:53 +000071
Douglas Gregor64650af2009-02-02 23:39:07 +000072 int totalDecls = 0;
73#define DECL(Derived, Base) totalDecls += n##Derived##s;
74#include "clang/AST/DeclNodes.def"
75 fprintf(stderr, " %d decls total.\n", totalDecls);
76
77 int totalBytes = 0;
78#define DECL(Derived, Base) \
79 if (n##Derived##s > 0) { \
80 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
81 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
82 n##Derived##s, (int)sizeof(Derived##Decl), \
83 (int)(n##Derived##s * sizeof(Derived##Decl))); \
84 }
85#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000086
Douglas Gregor64650af2009-02-02 23:39:07 +000087 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000088}
89
90void Decl::addDeclKind(Kind k) {
91 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000092 default: assert(0 && "Declaration not in DeclNodes.def!");
93#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
94#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000095 }
96}
97
98//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +000099// PrettyStackTraceDecl Implementation
100//===----------------------------------------------------------------------===//
101
102void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
103 SourceLocation TheLoc = Loc;
104 if (TheLoc.isInvalid() && TheDecl)
105 TheLoc = TheDecl->getLocation();
106
107 if (TheLoc.isValid()) {
108 TheLoc.print(OS, SM);
109 OS << ": ";
110 }
111
112 OS << Message;
113
114 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
115 OS << " '" << DN->getQualifiedNameAsString() << '\'';
116 OS << '\n';
117}
118
119//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000120// Decl Implementation
121//===----------------------------------------------------------------------===//
122
Chris Lattner769dbdf2009-03-27 20:18:19 +0000123// Out-of-line virtual method providing a home for Decl.
124Decl::~Decl() {
125 if (isOutOfSemaDC())
126 delete getMultipleDC();
127
128 assert(!HasAttrs && "attributes should have been freed by Destroy");
129}
130
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000131void Decl::setDeclContext(DeclContext *DC) {
132 if (isOutOfSemaDC())
133 delete getMultipleDC();
134
Chris Lattneree219fd2009-03-29 06:06:59 +0000135 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000136}
137
138void Decl::setLexicalDeclContext(DeclContext *DC) {
139 if (DC == getLexicalDeclContext())
140 return;
141
142 if (isInSemaDC()) {
143 MultipleDC *MDC = new MultipleDC();
144 MDC->SemanticDC = getDeclContext();
145 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000146 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000147 } else {
148 getMultipleDC()->LexicalDC = DC;
149 }
150}
151
Chris Lattner769dbdf2009-03-27 20:18:19 +0000152unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
153 switch (DeclKind) {
154 default:
155 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
156 return IDNS_Ordinary;
157 assert(0 && "Unknown decl kind!");
158 case OverloadedFunction:
159 case Typedef:
160 case EnumConstant:
161 case Var:
162 case ImplicitParam:
163 case ParmVar:
164 case OriginalParmVar:
165 case NonTypeTemplateParm:
166 case ObjCMethod:
167 case ObjCContainer:
168 case ObjCCategory:
169 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000170 case ObjCProperty:
171 case ObjCCompatibleAlias:
172 return IDNS_Ordinary;
173
174 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000175 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000176
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000177 case ObjCImplementation:
178 return IDNS_ObjCImplementation;
179
180 case ObjCCategoryImpl:
181 return IDNS_ObjCCategoryImpl;
182
Chris Lattner769dbdf2009-03-27 20:18:19 +0000183 case Field:
184 case ObjCAtDefsField:
185 case ObjCIvar:
186 return IDNS_Member;
187
188 case Record:
189 case CXXRecord:
190 case Enum:
191 case TemplateTypeParm:
192 return IDNS_Tag;
193
194 case Namespace:
195 case Template:
196 case FunctionTemplate:
197 case ClassTemplate:
198 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000199 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000200 return IDNS_Tag | IDNS_Ordinary;
201
202 // Never have names.
203 case LinkageSpec:
204 case FileScopeAsm:
205 case StaticAssert:
206 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000207 case ObjCPropertyImpl:
208 case ObjCForwardProtocol:
209 case Block:
210 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000211
Chris Lattner769dbdf2009-03-27 20:18:19 +0000212 // Aren't looked up?
213 case UsingDirective:
214 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000215 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000216 return 0;
217 }
Eli Friedman56d29372008-06-07 16:52:53 +0000218}
219
220void Decl::addAttr(Attr *NewAttr) {
221 if (!DeclAttrs)
222 DeclAttrs = new DeclAttrMapTy();
223
224 Attr *&ExistingAttr = (*DeclAttrs)[this];
225
226 NewAttr->setNext(ExistingAttr);
227 ExistingAttr = NewAttr;
228
229 HasAttrs = true;
230}
231
232void Decl::invalidateAttrs() {
233 if (!HasAttrs) return;
234
235 HasAttrs = false;
236 (*DeclAttrs)[this] = 0;
237 DeclAttrs->erase(this);
238
239 if (DeclAttrs->empty()) {
240 delete DeclAttrs;
241 DeclAttrs = 0;
242 }
243}
244
Chris Lattner81abbdd2009-03-21 06:27:31 +0000245const Attr *Decl::getAttrsImpl() const {
246 assert(HasAttrs && "getAttrs() should verify this!");
Eli Friedman56d29372008-06-07 16:52:53 +0000247 return (*DeclAttrs)[this];
248}
249
250void Decl::swapAttrs(Decl *RHS) {
251 bool HasLHSAttr = this->HasAttrs;
252 bool HasRHSAttr = RHS->HasAttrs;
253
254 // Usually, neither decl has attrs, nothing to do.
255 if (!HasLHSAttr && !HasRHSAttr) return;
256
257 // If 'this' has no attrs, swap the other way.
258 if (!HasLHSAttr)
259 return RHS->swapAttrs(this);
260
261 // Handle the case when both decls have attrs.
262 if (HasRHSAttr) {
263 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
264 return;
265 }
266
267 // Otherwise, LHS has an attr and RHS doesn't.
268 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
269 (*DeclAttrs).erase(this);
270 this->HasAttrs = false;
271 RHS->HasAttrs = true;
272}
273
274
Chris Lattnercc581472009-03-04 06:05:19 +0000275void Decl::Destroy(ASTContext &C) {
276 // Free attributes for this decl.
277 if (HasAttrs) {
278 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
279 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
280
281 // release attributes.
282 it->second->Destroy(C);
283 invalidateAttrs();
284 HasAttrs = false;
285 }
286
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000287#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000288 // FIXME: Once ownership is fully understood, we can enable this code
289 if (DeclContext *DC = dyn_cast<DeclContext>(this))
290 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000291
Chris Lattner244a67d2009-03-28 06:04:26 +0000292 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000293 // within the loop, only the Destroy method for the first Decl
294 // will deallocate all of the Decls in a chain.
295
Chris Lattner244a67d2009-03-28 06:04:26 +0000296 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000297
298 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000299 Decl* Tmp = N->getNextDeclInContext();
300 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000301 N->Destroy(C);
302 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000303 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000304
Eli Friedman56d29372008-06-07 16:52:53 +0000305 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000306 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000307#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000308}
309
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000310Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000311 Decl::Kind DK = D->getDeclKind();
312 switch(DK) {
313#define DECL_CONTEXT(Name) \
314 case Decl::Name: \
315 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
316#define DECL_CONTEXT_BASE(Name)
317#include "clang/AST/DeclNodes.def"
318 default:
319#define DECL_CONTEXT_BASE(Name) \
320 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
321 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
322#include "clang/AST/DeclNodes.def"
323 assert(false && "a decl that inherits DeclContext isn't handled");
324 return 0;
325 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000326}
327
328DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000329 Decl::Kind DK = D->getKind();
330 switch(DK) {
331#define DECL_CONTEXT(Name) \
332 case Decl::Name: \
333 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
334#define DECL_CONTEXT_BASE(Name)
335#include "clang/AST/DeclNodes.def"
336 default:
337#define DECL_CONTEXT_BASE(Name) \
338 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
339 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
340#include "clang/AST/DeclNodes.def"
341 assert(false && "a decl that inherits DeclContext isn't handled");
342 return 0;
343 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000344}
345
Sebastian Redld3a413d2009-04-26 20:35:05 +0000346CompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
347 return dyn_cast_or_null<CompoundStmt>(getBody(Context));
348}
349
350SourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
351 Stmt *Body = getBody(Context);
352 if (!Body)
353 return SourceLocation();
354 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
355 return CS->getRBracLoc();
356 assert(isa<CXXTryStmt>(Body) &&
357 "Body can only be CompoundStmt or CXXTryStmt");
358 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
359}
360
Anders Carlsson1329c272009-03-25 23:38:06 +0000361#ifndef NDEBUG
362void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000363 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
364 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000365 "Access specifier is AS_none inside a record decl");
366}
367
368#endif
369
Eli Friedman56d29372008-06-07 16:52:53 +0000370//===----------------------------------------------------------------------===//
371// DeclContext Implementation
372//===----------------------------------------------------------------------===//
373
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000374bool DeclContext::classof(const Decl *D) {
375 switch (D->getKind()) {
376#define DECL_CONTEXT(Name) case Decl::Name:
377#define DECL_CONTEXT_BASE(Name)
378#include "clang/AST/DeclNodes.def"
379 return true;
380 default:
381#define DECL_CONTEXT_BASE(Name) \
382 if (D->getKind() >= Decl::Name##First && \
383 D->getKind() <= Decl::Name##Last) \
384 return true;
385#include "clang/AST/DeclNodes.def"
386 return false;
387 }
388}
389
Douglas Gregor44b43212008-12-11 16:49:14 +0000390DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000391 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000392}
393
394void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000395 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000396 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000397}
398
Douglas Gregorbc221632009-05-28 16:34:51 +0000399bool DeclContext::isDependentContext() const {
400 if (isFileContext())
401 return false;
402
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000403 if (isa<ClassTemplatePartialSpecializationDecl>(this))
404 return true;
405
Douglas Gregorbc221632009-05-28 16:34:51 +0000406 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
407 if (Record->getDescribedClassTemplate())
408 return true;
409
410 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
411 if (Function->getDescribedFunctionTemplate())
412 return true;
413
414 return getParent() && getParent()->isDependentContext();
415}
416
Douglas Gregor074149e2009-01-05 19:45:36 +0000417bool DeclContext::isTransparentContext() const {
418 if (DeclKind == Decl::Enum)
419 return true; // FIXME: Check for C++0x scoped enums
420 else if (DeclKind == Decl::LinkageSpec)
421 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000422 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000423 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000424 else if (DeclKind == Decl::Namespace)
425 return false; // FIXME: Check for C++0x inline namespaces
426
427 return false;
428}
429
Steve Naroff0701bbb2009-01-08 17:28:14 +0000430DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000431 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000432 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000433 case Decl::LinkageSpec:
434 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000435 // There is only one DeclContext for these entities.
436 return this;
437
438 case Decl::Namespace:
439 // The original namespace is our primary context.
440 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
441
Douglas Gregor44b43212008-12-11 16:49:14 +0000442 case Decl::ObjCMethod:
443 return this;
444
445 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000446 case Decl::ObjCProtocol:
447 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000448 // FIXME: Can Objective-C interfaces be forward-declared?
449 return this;
450
Steve Naroff0701bbb2009-01-08 17:28:14 +0000451 case Decl::ObjCImplementation:
452 case Decl::ObjCCategoryImpl:
453 return this;
454
Douglas Gregor44b43212008-12-11 16:49:14 +0000455 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000456 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
457 // If this is a tag type that has a definition or is currently
458 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000459 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000460 if (TagT->isBeingDefined() ||
461 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
462 return TagT->getDecl();
463 return this;
464 }
465
Douglas Gregor44b43212008-12-11 16:49:14 +0000466 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
467 "Unknown DeclContext kind");
468 return this;
469 }
470}
471
472DeclContext *DeclContext::getNextContext() {
473 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000474 case Decl::Namespace:
475 // Return the next namespace
476 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
477
478 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000479 return 0;
480 }
481}
482
Douglas Gregor2cf26342009-04-09 22:27:44 +0000483/// \brief Load the declarations within this lexical storage from an
484/// external source.
485void
486DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
487 ExternalASTSource *Source = Context.getExternalSource();
488 assert(hasExternalLexicalStorage() && Source && "No external storage?");
489
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000490 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000491 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
492 Decls))
493 return;
494
495 // There is no longer any lexical storage in this context
496 ExternalLexicalStorage = false;
497
498 if (Decls.empty())
499 return;
500
501 // Resolve all of the declaration IDs into declarations, building up
502 // a chain of declarations via the Decl::NextDeclInContext field.
503 Decl *FirstNewDecl = 0;
504 Decl *PrevDecl = 0;
505 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
506 Decl *D = Source->GetDecl(Decls[I]);
507 if (PrevDecl)
508 PrevDecl->NextDeclInContext = D;
509 else
510 FirstNewDecl = D;
511
512 PrevDecl = D;
513 }
514
515 // Splice the newly-read declarations into the beginning of the list
516 // of declarations.
517 PrevDecl->NextDeclInContext = FirstDecl;
518 FirstDecl = FirstNewDecl;
519 if (!LastDecl)
520 LastDecl = PrevDecl;
521}
522
523void
524DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
525 DeclContext *This = const_cast<DeclContext *>(this);
526 ExternalASTSource *Source = Context.getExternalSource();
527 assert(hasExternalVisibleStorage() && Source && "No external storage?");
528
529 llvm::SmallVector<VisibleDeclaration, 64> Decls;
530 if (Source->ReadDeclsVisibleInContext(This, Decls))
531 return;
532
533 // There is no longer any visible storage in this context
534 ExternalVisibleStorage = false;
535
536 // Load the declaration IDs for all of the names visible in this
537 // context.
538 assert(!LookupPtr && "Have a lookup map before de-serialization?");
539 StoredDeclsMap *Map = new StoredDeclsMap;
540 LookupPtr = Map;
541 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
542 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
543 }
544}
545
Douglas Gregor6ab35242009-04-09 21:40:53 +0000546DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000547 if (hasExternalLexicalStorage())
548 LoadLexicalDeclsFromExternalStorage(Context);
549
550 // FIXME: Check whether we need to load some declarations from
551 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000552 return decl_iterator(FirstDecl);
553}
554
555DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000556 if (hasExternalLexicalStorage())
557 LoadLexicalDeclsFromExternalStorage(Context);
558
Douglas Gregor6ab35242009-04-09 21:40:53 +0000559 return decl_iterator();
560}
561
Douglas Gregor8038d512009-04-10 17:25:41 +0000562bool DeclContext::decls_empty(ASTContext &Context) const {
563 if (hasExternalLexicalStorage())
564 LoadLexicalDeclsFromExternalStorage(Context);
565
566 return !FirstDecl;
567}
568
Douglas Gregor6ab35242009-04-09 21:40:53 +0000569void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000570 assert(D->getLexicalDeclContext() == this &&
571 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000572 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000573 "Decl already inserted into a DeclContext");
574
575 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000576 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000577 LastDecl = D;
578 } else {
579 FirstDecl = LastDecl = D;
580 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000581
582 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000583 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000584}
585
Douglas Gregor074149e2009-01-05 19:45:36 +0000586/// buildLookup - Build the lookup data structure with all of the
587/// declarations in DCtx (and any other contexts linked to it or
588/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000589void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000590 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000591 for (decl_iterator D = DCtx->decls_begin(Context),
592 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000593 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000594 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000595 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000596 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000597
598 // If this declaration is itself a transparent declaration context,
599 // add its members (recursively).
600 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
601 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000602 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000603 }
604 }
605}
606
Douglas Gregor44b43212008-12-11 16:49:14 +0000607DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000608DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000609 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000610 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000611 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000612
Douglas Gregor2cf26342009-04-09 22:27:44 +0000613 if (hasExternalVisibleStorage())
614 LoadVisibleDeclsFromExternalStorage(Context);
615
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000616 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000617 /// all of the linked DeclContexts (in declaration order!) and
618 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000619 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000620 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000621
Douglas Gregorc36c5402009-04-09 17:29:08 +0000622 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000623 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000624 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000625
Douglas Gregorc36c5402009-04-09 17:29:08 +0000626 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
627 StoredDeclsMap::iterator Pos = Map->find(Name);
628 if (Pos == Map->end())
629 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000630 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000631}
632
633DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000634DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
635 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000636}
637
Chris Lattner0cf2b192009-03-27 19:19:59 +0000638DeclContext *DeclContext::getLookupContext() {
639 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000640 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000641 while (Ctx->isTransparentContext())
642 Ctx = Ctx->getParent();
643 return Ctx;
644}
645
Douglas Gregor88b70942009-02-25 22:02:03 +0000646DeclContext *DeclContext::getEnclosingNamespaceContext() {
647 DeclContext *Ctx = this;
648 // Skip through non-namespace, non-translation-unit contexts.
649 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
650 Ctx = Ctx->getParent();
651 return Ctx->getPrimaryContext();
652}
653
Douglas Gregor6ab35242009-04-09 21:40:53 +0000654void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000655 // FIXME: This feels like a hack. Should DeclarationName support
656 // template-ids, or is there a better way to keep specializations
657 // from being visible?
658 if (isa<ClassTemplateSpecializationDecl>(D))
659 return;
660
Steve Naroff0701bbb2009-01-08 17:28:14 +0000661 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000662 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000663 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000664 return;
665 }
666
667 // If we already have a lookup data structure, perform the insertion
668 // into it. Otherwise, be lazy and don't build that structure until
669 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000670 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000671 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000672
Douglas Gregor074149e2009-01-05 19:45:36 +0000673 // If we are a transparent context, insert into our parent context,
674 // too. This operation is recursive.
675 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000676 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000677}
678
Douglas Gregor6ab35242009-04-09 21:40:53 +0000679void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
680 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000681 // Skip unnamed declarations.
682 if (!D->getDeclName())
683 return;
684
Douglas Gregorcc636682009-02-17 23:15:12 +0000685 // FIXME: This feels like a hack. Should DeclarationName support
686 // template-ids, or is there a better way to keep specializations
687 // from being visible?
688 if (isa<ClassTemplateSpecializationDecl>(D))
689 return;
690
Douglas Gregorc36c5402009-04-09 17:29:08 +0000691 if (!LookupPtr)
692 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000693
694 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000695 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000696 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
697 if (DeclNameEntries.isNull()) {
698 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000699 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000700 }
Chris Lattner91942502009-02-20 00:55:03 +0000701
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000702 // If it is possible that this is a redeclaration, check to see if there is
703 // already a decl for which declarationReplaces returns true. If there is
704 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000705 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000706 return;
Chris Lattner91942502009-02-20 00:55:03 +0000707
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000708 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000709 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000710}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000711
712/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
713/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000714DeclContext::udir_iterator_range
715DeclContext::getUsingDirectives(ASTContext &Context) const {
716 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000717 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
718 reinterpret_cast<udir_iterator>(Result.second));
719}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000720
721void StoredDeclsList::materializeDecls(ASTContext &Context) {
722 if (isNull())
723 return;
724
725 switch ((DataKind)(Data & 0x03)) {
726 case DK_Decl:
727 case DK_Decl_Vector:
728 break;
729
730 case DK_DeclID: {
731 // Resolve this declaration ID to an actual declaration by
732 // querying the external AST source.
733 unsigned DeclID = Data >> 2;
734
735 ExternalASTSource *Source = Context.getExternalSource();
736 assert(Source && "No external AST source available!");
737
738 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
739 break;
740 }
741
742 case DK_ID_Vector: {
743 // We have a vector of declaration IDs. Resolve all of them to
744 // actual declarations.
745 VectorTy &Vector = *getAsVector();
746 ExternalASTSource *Source = Context.getExternalSource();
747 assert(Source && "No external AST source available!");
748
749 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
750 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
751
752 Data = (Data & ~0x03) | DK_Decl_Vector;
753 break;
754 }
755 }
756}