blob: 80b422e75388413fcebfeb3584355120da80367c [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"
Sebastian Redlbc9ef252009-04-26 20:35:05 +000023#include "clang/AST/Stmt.h"
24#include "clang/AST/StmtCXX.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattnerc309ade2009-03-05 08:00:35 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor6e71edc2008-12-23 21:05:05 +000027#include <algorithm>
Chris Lattner7d6220c2009-03-02 22:20:04 +000028#include <cstdio>
Douglas Gregorddfd9d52008-12-23 00:26:44 +000029#include <vector>
Eli Friedmana8a09ac2008-06-07 16:52:53 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Statistics
34//===----------------------------------------------------------------------===//
35
Douglas Gregor469fc9a2009-02-02 23:39:07 +000036#define DECL(Derived, Base) static int n##Derived##s = 0;
37#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-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 Gregor469fc9a2009-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 Friedmana8a09ac2008-06-07 16:52:53 +000052 }
53}
54
Steve Naroffe5f128a2009-01-20 19:53:53 +000055const char *DeclContext::getDeclKindName() const {
56 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000057 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argiris Kirtzidis2d9b7612009-02-16 14:28:33 +000058#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor469fc9a2009-02-02 23:39:07 +000059#include "clang/AST/DeclNodes.def"
Steve Naroffe5f128a2009-01-20 19:53:53 +000060 }
61}
62
Eli Friedmana8a09ac2008-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 Friedmana8a09ac2008-06-07 16:52:53 +000071
Douglas Gregor469fc9a2009-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 Friedmana8a09ac2008-06-07 16:52:53 +000086
Douglas Gregor469fc9a2009-02-02 23:39:07 +000087 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedmana8a09ac2008-06-07 16:52:53 +000088}
89
90void Decl::addDeclKind(Kind k) {
91 switch (k) {
Douglas Gregor469fc9a2009-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 Friedmana8a09ac2008-06-07 16:52:53 +000095 }
96}
97
98//===----------------------------------------------------------------------===//
Chris Lattnerc309ade2009-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 Friedmana8a09ac2008-06-07 16:52:53 +0000120// Decl Implementation
121//===----------------------------------------------------------------------===//
122
Chris Lattner8752fe02009-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 Gregoraf8ad2b2009-01-20 01:17:11 +0000131void Decl::setDeclContext(DeclContext *DC) {
132 if (isOutOfSemaDC())
133 delete getMultipleDC();
134
Chris Lattner5ce4f6d2009-03-29 06:06:59 +0000135 DeclCtx = DC;
Douglas Gregoraf8ad2b2009-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 Lattner5ce4f6d2009-03-29 06:06:59 +0000146 DeclCtx = MDC;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000147 } else {
148 getMultipleDC()->LexicalDC = DC;
149 }
150}
151
Chris Lattner8752fe02009-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 Lattner8752fe02009-03-27 20:18:19 +0000170 case ObjCProperty:
171 case ObjCCompatibleAlias:
172 return IDNS_Ordinary;
173
174 case ObjCProtocol:
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000175 return IDNS_ObjCProtocol;
Chris Lattner8752fe02009-03-27 20:18:19 +0000176
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000177 case ObjCImplementation:
178 return IDNS_ObjCImplementation;
179
180 case ObjCCategoryImpl:
181 return IDNS_ObjCCategoryImpl;
182
Chris Lattner8752fe02009-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 Carlsson1bf91fb2009-03-28 23:02:53 +0000199 case NamespaceAlias:
Chris Lattner8752fe02009-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 Lattner8752fe02009-03-27 20:18:19 +0000207 case ObjCPropertyImpl:
208 case ObjCForwardProtocol:
209 case Block:
210 case TranslationUnit:
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000211
Chris Lattner8752fe02009-03-27 20:18:19 +0000212 // Aren't looked up?
213 case UsingDirective:
214 case ClassTemplateSpecialization:
215 return 0;
216 }
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000217}
218
219void Decl::addAttr(Attr *NewAttr) {
220 if (!DeclAttrs)
221 DeclAttrs = new DeclAttrMapTy();
222
223 Attr *&ExistingAttr = (*DeclAttrs)[this];
224
225 NewAttr->setNext(ExistingAttr);
226 ExistingAttr = NewAttr;
227
228 HasAttrs = true;
229}
230
231void Decl::invalidateAttrs() {
232 if (!HasAttrs) return;
233
234 HasAttrs = false;
235 (*DeclAttrs)[this] = 0;
236 DeclAttrs->erase(this);
237
238 if (DeclAttrs->empty()) {
239 delete DeclAttrs;
240 DeclAttrs = 0;
241 }
242}
243
Chris Lattner004a9bb2009-03-21 06:27:31 +0000244const Attr *Decl::getAttrsImpl() const {
245 assert(HasAttrs && "getAttrs() should verify this!");
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000246 return (*DeclAttrs)[this];
247}
248
249void Decl::swapAttrs(Decl *RHS) {
250 bool HasLHSAttr = this->HasAttrs;
251 bool HasRHSAttr = RHS->HasAttrs;
252
253 // Usually, neither decl has attrs, nothing to do.
254 if (!HasLHSAttr && !HasRHSAttr) return;
255
256 // If 'this' has no attrs, swap the other way.
257 if (!HasLHSAttr)
258 return RHS->swapAttrs(this);
259
260 // Handle the case when both decls have attrs.
261 if (HasRHSAttr) {
262 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
263 return;
264 }
265
266 // Otherwise, LHS has an attr and RHS doesn't.
267 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
268 (*DeclAttrs).erase(this);
269 this->HasAttrs = false;
270 RHS->HasAttrs = true;
271}
272
273
Chris Lattner0fc6d642009-03-04 06:05:19 +0000274void Decl::Destroy(ASTContext &C) {
275 // Free attributes for this decl.
276 if (HasAttrs) {
277 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
278 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
279
280 // release attributes.
281 it->second->Destroy(C);
282 invalidateAttrs();
283 HasAttrs = false;
284 }
285
Douglas Gregor8314d742009-01-13 19:47:12 +0000286#if 0
Douglas Gregorf4006be2009-01-20 04:25:11 +0000287 // FIXME: Once ownership is fully understood, we can enable this code
288 if (DeclContext *DC = dyn_cast<DeclContext>(this))
289 DC->decls_begin()->Destroy(C);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000290
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000291 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000292 // within the loop, only the Destroy method for the first Decl
293 // will deallocate all of the Decls in a chain.
294
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000295 Decl* N = getNextDeclInContext();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000296
297 while (N) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000298 Decl* Tmp = N->getNextDeclInContext();
299 N->NextDeclInContext = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000300 N->Destroy(C);
301 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000302 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000303
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000304 this->~Decl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000305 C.Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000306#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000307}
308
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000309Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000310 Decl::Kind DK = D->getDeclKind();
311 switch(DK) {
312#define DECL_CONTEXT(Name) \
313 case Decl::Name: \
314 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
315#define DECL_CONTEXT_BASE(Name)
316#include "clang/AST/DeclNodes.def"
317 default:
318#define DECL_CONTEXT_BASE(Name) \
319 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
320 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
321#include "clang/AST/DeclNodes.def"
322 assert(false && "a decl that inherits DeclContext isn't handled");
323 return 0;
324 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000325}
326
327DeclContext *Decl::castToDeclContext(const Decl *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000328 Decl::Kind DK = D->getKind();
329 switch(DK) {
330#define DECL_CONTEXT(Name) \
331 case Decl::Name: \
332 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
333#define DECL_CONTEXT_BASE(Name)
334#include "clang/AST/DeclNodes.def"
335 default:
336#define DECL_CONTEXT_BASE(Name) \
337 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
338 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
339#include "clang/AST/DeclNodes.def"
340 assert(false && "a decl that inherits DeclContext isn't handled");
341 return 0;
342 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000343}
344
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000345CompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
346 return dyn_cast_or_null<CompoundStmt>(getBody(Context));
347}
348
349SourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
350 Stmt *Body = getBody(Context);
351 if (!Body)
352 return SourceLocation();
353 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
354 return CS->getRBracLoc();
355 assert(isa<CXXTryStmt>(Body) &&
356 "Body can only be CompoundStmt or CXXTryStmt");
357 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
358}
359
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000360#ifndef NDEBUG
361void Decl::CheckAccessDeclContext() const {
Douglas Gregora470cea2009-04-07 20:58:25 +0000362 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
363 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000364 "Access specifier is AS_none inside a record decl");
365}
366
367#endif
368
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000369//===----------------------------------------------------------------------===//
370// DeclContext Implementation
371//===----------------------------------------------------------------------===//
372
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000373bool DeclContext::classof(const Decl *D) {
374 switch (D->getKind()) {
375#define DECL_CONTEXT(Name) case Decl::Name:
376#define DECL_CONTEXT_BASE(Name)
377#include "clang/AST/DeclNodes.def"
378 return true;
379 default:
380#define DECL_CONTEXT_BASE(Name) \
381 if (D->getKind() >= Decl::Name##First && \
382 D->getKind() <= Decl::Name##Last) \
383 return true;
384#include "clang/AST/DeclNodes.def"
385 return false;
386 }
387}
388
Douglas Gregor8acb7272008-12-11 16:49:14 +0000389DeclContext::~DeclContext() {
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000390 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000391}
392
393void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000394 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregorf4006be2009-01-20 04:25:11 +0000395 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000396}
397
Douglas Gregor1a6b88e2009-05-28 16:34:51 +0000398bool DeclContext::isDependentContext() const {
399 if (isFileContext())
400 return false;
401
402 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
403 if (Record->getDescribedClassTemplate())
404 return true;
405
406 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
407 if (Function->getDescribedFunctionTemplate())
408 return true;
409
410 return getParent() && getParent()->isDependentContext();
411}
412
Douglas Gregord8028382009-01-05 19:45:36 +0000413bool DeclContext::isTransparentContext() const {
414 if (DeclKind == Decl::Enum)
415 return true; // FIXME: Check for C++0x scoped enums
416 else if (DeclKind == Decl::LinkageSpec)
417 return true;
Douglas Gregor1db12932009-02-26 00:02:51 +0000418 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor723d3332009-01-07 00:43:41 +0000419 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000420 else if (DeclKind == Decl::Namespace)
421 return false; // FIXME: Check for C++0x inline namespaces
422
423 return false;
424}
425
Steve Naroffab63fd62009-01-08 17:28:14 +0000426DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000427 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000428 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000429 case Decl::LinkageSpec:
430 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000431 // There is only one DeclContext for these entities.
432 return this;
433
434 case Decl::Namespace:
435 // The original namespace is our primary context.
436 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
437
Douglas Gregor8acb7272008-12-11 16:49:14 +0000438 case Decl::ObjCMethod:
439 return this;
440
441 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000442 case Decl::ObjCProtocol:
443 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000444 // FIXME: Can Objective-C interfaces be forward-declared?
445 return this;
446
Steve Naroffab63fd62009-01-08 17:28:14 +0000447 case Decl::ObjCImplementation:
448 case Decl::ObjCCategoryImpl:
449 return this;
450
Douglas Gregor8acb7272008-12-11 16:49:14 +0000451 default:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000452 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
453 // If this is a tag type that has a definition or is currently
454 // being defined, that definition is our primary context.
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000455 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregora08b6c72009-02-17 23:15:12 +0000456 if (TagT->isBeingDefined() ||
457 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
458 return TagT->getDecl();
459 return this;
460 }
461
Douglas Gregor8acb7272008-12-11 16:49:14 +0000462 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
463 "Unknown DeclContext kind");
464 return this;
465 }
466}
467
468DeclContext *DeclContext::getNextContext() {
469 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000470 case Decl::Namespace:
471 // Return the next namespace
472 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
473
474 default:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000475 return 0;
476 }
477}
478
Douglas Gregorc34897d2009-04-09 22:27:44 +0000479/// \brief Load the declarations within this lexical storage from an
480/// external source.
481void
482DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
483 ExternalASTSource *Source = Context.getExternalSource();
484 assert(hasExternalLexicalStorage() && Source && "No external storage?");
485
Eli Friedmancdf59742009-04-27 23:43:36 +0000486 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000487 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
488 Decls))
489 return;
490
491 // There is no longer any lexical storage in this context
492 ExternalLexicalStorage = false;
493
494 if (Decls.empty())
495 return;
496
497 // Resolve all of the declaration IDs into declarations, building up
498 // a chain of declarations via the Decl::NextDeclInContext field.
499 Decl *FirstNewDecl = 0;
500 Decl *PrevDecl = 0;
501 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
502 Decl *D = Source->GetDecl(Decls[I]);
503 if (PrevDecl)
504 PrevDecl->NextDeclInContext = D;
505 else
506 FirstNewDecl = D;
507
508 PrevDecl = D;
509 }
510
511 // Splice the newly-read declarations into the beginning of the list
512 // of declarations.
513 PrevDecl->NextDeclInContext = FirstDecl;
514 FirstDecl = FirstNewDecl;
515 if (!LastDecl)
516 LastDecl = PrevDecl;
517}
518
519void
520DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
521 DeclContext *This = const_cast<DeclContext *>(this);
522 ExternalASTSource *Source = Context.getExternalSource();
523 assert(hasExternalVisibleStorage() && Source && "No external storage?");
524
525 llvm::SmallVector<VisibleDeclaration, 64> Decls;
526 if (Source->ReadDeclsVisibleInContext(This, Decls))
527 return;
528
529 // There is no longer any visible storage in this context
530 ExternalVisibleStorage = false;
531
532 // Load the declaration IDs for all of the names visible in this
533 // context.
534 assert(!LookupPtr && "Have a lookup map before de-serialization?");
535 StoredDeclsMap *Map = new StoredDeclsMap;
536 LookupPtr = Map;
537 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
538 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
539 }
540}
541
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000542DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000543 if (hasExternalLexicalStorage())
544 LoadLexicalDeclsFromExternalStorage(Context);
545
546 // FIXME: Check whether we need to load some declarations from
547 // external storage.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000548 return decl_iterator(FirstDecl);
549}
550
551DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000552 if (hasExternalLexicalStorage())
553 LoadLexicalDeclsFromExternalStorage(Context);
554
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000555 return decl_iterator();
556}
557
Douglas Gregorac8f2802009-04-10 17:25:41 +0000558bool DeclContext::decls_empty(ASTContext &Context) const {
559 if (hasExternalLexicalStorage())
560 LoadLexicalDeclsFromExternalStorage(Context);
561
562 return !FirstDecl;
563}
564
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000565void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner53602072009-02-20 00:56:18 +0000566 assert(D->getLexicalDeclContext() == this &&
567 "Decl inserted into wrong lexical context");
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000568 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregord1675382009-01-09 19:42:16 +0000569 "Decl already inserted into a DeclContext");
570
571 if (FirstDecl) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000572 LastDecl->NextDeclInContext = D;
Douglas Gregord1675382009-01-09 19:42:16 +0000573 LastDecl = D;
574 } else {
575 FirstDecl = LastDecl = D;
576 }
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000577
578 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000579 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000580}
581
Douglas Gregord8028382009-01-05 19:45:36 +0000582/// buildLookup - Build the lookup data structure with all of the
583/// declarations in DCtx (and any other contexts linked to it or
584/// transparent contexts nested within it).
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000585void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000586 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000587 for (decl_iterator D = DCtx->decls_begin(Context),
588 DEnd = DCtx->decls_end(Context);
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000589 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000590 // Insert this declaration into the lookup structure
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000591 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000592 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000593
594 // If this declaration is itself a transparent declaration context,
595 // add its members (recursively).
596 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
597 if (InnerCtx->isTransparentContext())
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000598 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000599 }
600 }
601}
602
Douglas Gregor8acb7272008-12-11 16:49:14 +0000603DeclContext::lookup_result
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000604DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000605 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000606 if (PrimaryContext != this)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000607 return PrimaryContext->lookup(Context, Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000608
Douglas Gregorc34897d2009-04-09 22:27:44 +0000609 if (hasExternalVisibleStorage())
610 LoadVisibleDeclsFromExternalStorage(Context);
611
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000612 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000613 /// all of the linked DeclContexts (in declaration order!) and
614 /// inserting their values.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000615 if (!LookupPtr) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000616 buildLookup(Context, this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000617
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000618 if (!LookupPtr)
Chris Lattner265f01d2009-02-20 00:55:03 +0000619 return lookup_result(0, 0);
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000620 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000621
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000622 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
623 StoredDeclsMap::iterator Pos = Map->find(Name);
624 if (Pos == Map->end())
625 return lookup_result(0, 0);
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000626 return Pos->second.getLookupResult(Context);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000627}
628
629DeclContext::lookup_const_result
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000630DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
631 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000632}
633
Chris Lattner9a502bd2009-03-27 19:19:59 +0000634DeclContext *DeclContext::getLookupContext() {
635 DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000636 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000637 while (Ctx->isTransparentContext())
638 Ctx = Ctx->getParent();
639 return Ctx;
640}
641
Douglas Gregor0d93f692009-02-25 22:02:03 +0000642DeclContext *DeclContext::getEnclosingNamespaceContext() {
643 DeclContext *Ctx = this;
644 // Skip through non-namespace, non-translation-unit contexts.
645 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
646 Ctx = Ctx->getParent();
647 return Ctx->getPrimaryContext();
648}
649
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000650void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000651 // FIXME: This feels like a hack. Should DeclarationName support
652 // template-ids, or is there a better way to keep specializations
653 // from being visible?
654 if (isa<ClassTemplateSpecializationDecl>(D))
655 return;
656
Steve Naroffab63fd62009-01-08 17:28:14 +0000657 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000658 if (PrimaryContext != this) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000659 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000660 return;
661 }
662
663 // If we already have a lookup data structure, perform the insertion
664 // into it. Otherwise, be lazy and don't build that structure until
665 // someone asks for it.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000666 if (LookupPtr)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000667 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregord8028382009-01-05 19:45:36 +0000668
Douglas Gregord8028382009-01-05 19:45:36 +0000669 // If we are a transparent context, insert into our parent context,
670 // too. This operation is recursive.
671 if (isTransparentContext())
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000672 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000673}
674
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000675void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
676 NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000677 // Skip unnamed declarations.
678 if (!D->getDeclName())
679 return;
680
Douglas Gregora08b6c72009-02-17 23:15:12 +0000681 // FIXME: This feels like a hack. Should DeclarationName support
682 // template-ids, or is there a better way to keep specializations
683 // from being visible?
684 if (isa<ClassTemplateSpecializationDecl>(D))
685 return;
686
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000687 if (!LookupPtr)
688 LookupPtr = new StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000689
690 // Insert this declaration into the map.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000691 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner77820d52009-02-20 01:44:05 +0000692 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
693 if (DeclNameEntries.isNull()) {
694 DeclNameEntries.setOnlyValue(D);
Chris Lattner6719b1f2009-02-19 07:00:44 +0000695 return;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000696 }
Chris Lattner265f01d2009-02-20 00:55:03 +0000697
Chris Lattnerba589d42009-02-20 01:10:07 +0000698 // If it is possible that this is a redeclaration, check to see if there is
699 // already a decl for which declarationReplaces returns true. If there is
700 // one, just replace it and return.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000701 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner77820d52009-02-20 01:44:05 +0000702 return;
Chris Lattner265f01d2009-02-20 00:55:03 +0000703
Chris Lattner6719b1f2009-02-19 07:00:44 +0000704 // Put this declaration into the appropriate slot.
Chris Lattner77820d52009-02-20 01:44:05 +0000705 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000706}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000707
708/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
709/// this context.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000710DeclContext::udir_iterator_range
711DeclContext::getUsingDirectives(ASTContext &Context) const {
712 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor7a7be652009-02-03 19:21:40 +0000713 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
714 reinterpret_cast<udir_iterator>(Result.second));
715}
Douglas Gregorc34897d2009-04-09 22:27:44 +0000716
717void StoredDeclsList::materializeDecls(ASTContext &Context) {
718 if (isNull())
719 return;
720
721 switch ((DataKind)(Data & 0x03)) {
722 case DK_Decl:
723 case DK_Decl_Vector:
724 break;
725
726 case DK_DeclID: {
727 // Resolve this declaration ID to an actual declaration by
728 // querying the external AST source.
729 unsigned DeclID = Data >> 2;
730
731 ExternalASTSource *Source = Context.getExternalSource();
732 assert(Source && "No external AST source available!");
733
734 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
735 break;
736 }
737
738 case DK_ID_Vector: {
739 // We have a vector of declaration IDs. Resolve all of them to
740 // actual declarations.
741 VectorTy &Vector = *getAsVector();
742 ExternalASTSource *Source = Context.getExternalSource();
743 assert(Source && "No external AST source available!");
744
745 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
746 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
747
748 Data = (Data & ~0x03) | DK_Decl_Vector;
749 break;
750 }
751 }
752}