blob: a39a506de0ae1a6c4dfc14ccf8c7effbdddc01b6 [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
Anders Carlsson67e33202009-06-13 00:08:58 +000098bool Decl::isTemplateParameterPack() const {
99 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
100 return TTP->isParameterPack();
101
102 return false;
103}
104
Eli Friedman56d29372008-06-07 16:52:53 +0000105//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000106// PrettyStackTraceDecl Implementation
107//===----------------------------------------------------------------------===//
108
109void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
110 SourceLocation TheLoc = Loc;
111 if (TheLoc.isInvalid() && TheDecl)
112 TheLoc = TheDecl->getLocation();
113
114 if (TheLoc.isValid()) {
115 TheLoc.print(OS, SM);
116 OS << ": ";
117 }
118
119 OS << Message;
120
121 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
122 OS << " '" << DN->getQualifiedNameAsString() << '\'';
123 OS << '\n';
124}
125
126//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000127// Decl Implementation
128//===----------------------------------------------------------------------===//
129
Chris Lattner769dbdf2009-03-27 20:18:19 +0000130// Out-of-line virtual method providing a home for Decl.
131Decl::~Decl() {
132 if (isOutOfSemaDC())
133 delete getMultipleDC();
134
135 assert(!HasAttrs && "attributes should have been freed by Destroy");
136}
137
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000138void Decl::setDeclContext(DeclContext *DC) {
139 if (isOutOfSemaDC())
140 delete getMultipleDC();
141
Chris Lattneree219fd2009-03-29 06:06:59 +0000142 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000143}
144
145void Decl::setLexicalDeclContext(DeclContext *DC) {
146 if (DC == getLexicalDeclContext())
147 return;
148
149 if (isInSemaDC()) {
150 MultipleDC *MDC = new MultipleDC();
151 MDC->SemanticDC = getDeclContext();
152 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000153 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000154 } else {
155 getMultipleDC()->LexicalDC = DC;
156 }
157}
158
Chris Lattner769dbdf2009-03-27 20:18:19 +0000159unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
160 switch (DeclKind) {
161 default:
162 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
163 return IDNS_Ordinary;
164 assert(0 && "Unknown decl kind!");
165 case OverloadedFunction:
166 case Typedef:
167 case EnumConstant:
168 case Var:
169 case ImplicitParam:
170 case ParmVar:
171 case OriginalParmVar:
172 case NonTypeTemplateParm:
173 case ObjCMethod:
174 case ObjCContainer:
175 case ObjCCategory:
176 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000177 case ObjCProperty:
178 case ObjCCompatibleAlias:
179 return IDNS_Ordinary;
180
181 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000182 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000183
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000184 case ObjCImplementation:
185 return IDNS_ObjCImplementation;
186
187 case ObjCCategoryImpl:
188 return IDNS_ObjCCategoryImpl;
189
Chris Lattner769dbdf2009-03-27 20:18:19 +0000190 case Field:
191 case ObjCAtDefsField:
192 case ObjCIvar:
193 return IDNS_Member;
194
195 case Record:
196 case CXXRecord:
197 case Enum:
198 case TemplateTypeParm:
199 return IDNS_Tag;
200
201 case Namespace:
202 case Template:
203 case FunctionTemplate:
204 case ClassTemplate:
205 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000206 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000207 return IDNS_Tag | IDNS_Ordinary;
208
209 // Never have names.
210 case LinkageSpec:
211 case FileScopeAsm:
212 case StaticAssert:
213 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000214 case ObjCPropertyImpl:
215 case ObjCForwardProtocol:
216 case Block:
217 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000218
Chris Lattner769dbdf2009-03-27 20:18:19 +0000219 // Aren't looked up?
220 case UsingDirective:
221 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000222 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000223 return 0;
224 }
Eli Friedman56d29372008-06-07 16:52:53 +0000225}
226
227void Decl::addAttr(Attr *NewAttr) {
228 if (!DeclAttrs)
229 DeclAttrs = new DeclAttrMapTy();
230
231 Attr *&ExistingAttr = (*DeclAttrs)[this];
232
233 NewAttr->setNext(ExistingAttr);
234 ExistingAttr = NewAttr;
235
236 HasAttrs = true;
237}
238
239void Decl::invalidateAttrs() {
240 if (!HasAttrs) return;
241
242 HasAttrs = false;
243 (*DeclAttrs)[this] = 0;
244 DeclAttrs->erase(this);
245
246 if (DeclAttrs->empty()) {
247 delete DeclAttrs;
248 DeclAttrs = 0;
249 }
250}
251
Chris Lattner81abbdd2009-03-21 06:27:31 +0000252const Attr *Decl::getAttrsImpl() const {
253 assert(HasAttrs && "getAttrs() should verify this!");
Eli Friedman56d29372008-06-07 16:52:53 +0000254 return (*DeclAttrs)[this];
255}
256
257void Decl::swapAttrs(Decl *RHS) {
258 bool HasLHSAttr = this->HasAttrs;
259 bool HasRHSAttr = RHS->HasAttrs;
260
261 // Usually, neither decl has attrs, nothing to do.
262 if (!HasLHSAttr && !HasRHSAttr) return;
263
264 // If 'this' has no attrs, swap the other way.
265 if (!HasLHSAttr)
266 return RHS->swapAttrs(this);
267
268 // Handle the case when both decls have attrs.
269 if (HasRHSAttr) {
270 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
271 return;
272 }
273
274 // Otherwise, LHS has an attr and RHS doesn't.
275 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
276 (*DeclAttrs).erase(this);
277 this->HasAttrs = false;
278 RHS->HasAttrs = true;
279}
280
281
Chris Lattnercc581472009-03-04 06:05:19 +0000282void Decl::Destroy(ASTContext &C) {
283 // Free attributes for this decl.
284 if (HasAttrs) {
285 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
286 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
287
288 // release attributes.
289 it->second->Destroy(C);
290 invalidateAttrs();
291 HasAttrs = false;
292 }
293
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000294#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000295 // FIXME: Once ownership is fully understood, we can enable this code
296 if (DeclContext *DC = dyn_cast<DeclContext>(this))
297 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000298
Chris Lattner244a67d2009-03-28 06:04:26 +0000299 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000300 // within the loop, only the Destroy method for the first Decl
301 // will deallocate all of the Decls in a chain.
302
Chris Lattner244a67d2009-03-28 06:04:26 +0000303 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000304
305 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000306 Decl* Tmp = N->getNextDeclInContext();
307 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000308 N->Destroy(C);
309 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000310 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000311
Eli Friedman56d29372008-06-07 16:52:53 +0000312 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000313 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000314#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000315}
316
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000317Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000318 Decl::Kind DK = D->getDeclKind();
319 switch(DK) {
320#define DECL_CONTEXT(Name) \
321 case Decl::Name: \
322 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
323#define DECL_CONTEXT_BASE(Name)
324#include "clang/AST/DeclNodes.def"
325 default:
326#define DECL_CONTEXT_BASE(Name) \
327 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
328 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
329#include "clang/AST/DeclNodes.def"
330 assert(false && "a decl that inherits DeclContext isn't handled");
331 return 0;
332 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000333}
334
335DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000336 Decl::Kind DK = D->getKind();
337 switch(DK) {
338#define DECL_CONTEXT(Name) \
339 case Decl::Name: \
340 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
341#define DECL_CONTEXT_BASE(Name)
342#include "clang/AST/DeclNodes.def"
343 default:
344#define DECL_CONTEXT_BASE(Name) \
345 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
346 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
347#include "clang/AST/DeclNodes.def"
348 assert(false && "a decl that inherits DeclContext isn't handled");
349 return 0;
350 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000351}
352
Sebastian Redld3a413d2009-04-26 20:35:05 +0000353CompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
354 return dyn_cast_or_null<CompoundStmt>(getBody(Context));
355}
356
357SourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
358 Stmt *Body = getBody(Context);
359 if (!Body)
360 return SourceLocation();
361 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
362 return CS->getRBracLoc();
363 assert(isa<CXXTryStmt>(Body) &&
364 "Body can only be CompoundStmt or CXXTryStmt");
365 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
366}
367
Anders Carlsson1329c272009-03-25 23:38:06 +0000368#ifndef NDEBUG
369void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000370 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
371 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000372 "Access specifier is AS_none inside a record decl");
373}
374
375#endif
376
Eli Friedman56d29372008-06-07 16:52:53 +0000377//===----------------------------------------------------------------------===//
378// DeclContext Implementation
379//===----------------------------------------------------------------------===//
380
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000381bool DeclContext::classof(const Decl *D) {
382 switch (D->getKind()) {
383#define DECL_CONTEXT(Name) case Decl::Name:
384#define DECL_CONTEXT_BASE(Name)
385#include "clang/AST/DeclNodes.def"
386 return true;
387 default:
388#define DECL_CONTEXT_BASE(Name) \
389 if (D->getKind() >= Decl::Name##First && \
390 D->getKind() <= Decl::Name##Last) \
391 return true;
392#include "clang/AST/DeclNodes.def"
393 return false;
394 }
395}
396
Douglas Gregor44b43212008-12-11 16:49:14 +0000397DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000398 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000399}
400
401void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000402 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000403 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000404}
405
Douglas Gregorbc221632009-05-28 16:34:51 +0000406bool DeclContext::isDependentContext() const {
407 if (isFileContext())
408 return false;
409
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000410 if (isa<ClassTemplatePartialSpecializationDecl>(this))
411 return true;
412
Douglas Gregorbc221632009-05-28 16:34:51 +0000413 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
414 if (Record->getDescribedClassTemplate())
415 return true;
416
417 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
418 if (Function->getDescribedFunctionTemplate())
419 return true;
420
421 return getParent() && getParent()->isDependentContext();
422}
423
Douglas Gregor074149e2009-01-05 19:45:36 +0000424bool DeclContext::isTransparentContext() const {
425 if (DeclKind == Decl::Enum)
426 return true; // FIXME: Check for C++0x scoped enums
427 else if (DeclKind == Decl::LinkageSpec)
428 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000429 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000430 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000431 else if (DeclKind == Decl::Namespace)
432 return false; // FIXME: Check for C++0x inline namespaces
433
434 return false;
435}
436
Steve Naroff0701bbb2009-01-08 17:28:14 +0000437DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000438 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000439 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000440 case Decl::LinkageSpec:
441 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000442 // There is only one DeclContext for these entities.
443 return this;
444
445 case Decl::Namespace:
446 // The original namespace is our primary context.
447 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
448
Douglas Gregor44b43212008-12-11 16:49:14 +0000449 case Decl::ObjCMethod:
450 return this;
451
452 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000453 case Decl::ObjCProtocol:
454 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000455 // FIXME: Can Objective-C interfaces be forward-declared?
456 return this;
457
Steve Naroff0701bbb2009-01-08 17:28:14 +0000458 case Decl::ObjCImplementation:
459 case Decl::ObjCCategoryImpl:
460 return this;
461
Douglas Gregor44b43212008-12-11 16:49:14 +0000462 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000463 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
464 // If this is a tag type that has a definition or is currently
465 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000466 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000467 if (TagT->isBeingDefined() ||
468 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
469 return TagT->getDecl();
470 return this;
471 }
472
Douglas Gregor44b43212008-12-11 16:49:14 +0000473 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
474 "Unknown DeclContext kind");
475 return this;
476 }
477}
478
479DeclContext *DeclContext::getNextContext() {
480 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000481 case Decl::Namespace:
482 // Return the next namespace
483 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
484
485 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000486 return 0;
487 }
488}
489
Douglas Gregor2cf26342009-04-09 22:27:44 +0000490/// \brief Load the declarations within this lexical storage from an
491/// external source.
492void
493DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
494 ExternalASTSource *Source = Context.getExternalSource();
495 assert(hasExternalLexicalStorage() && Source && "No external storage?");
496
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000497 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000498 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
499 Decls))
500 return;
501
502 // There is no longer any lexical storage in this context
503 ExternalLexicalStorage = false;
504
505 if (Decls.empty())
506 return;
507
508 // Resolve all of the declaration IDs into declarations, building up
509 // a chain of declarations via the Decl::NextDeclInContext field.
510 Decl *FirstNewDecl = 0;
511 Decl *PrevDecl = 0;
512 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
513 Decl *D = Source->GetDecl(Decls[I]);
514 if (PrevDecl)
515 PrevDecl->NextDeclInContext = D;
516 else
517 FirstNewDecl = D;
518
519 PrevDecl = D;
520 }
521
522 // Splice the newly-read declarations into the beginning of the list
523 // of declarations.
524 PrevDecl->NextDeclInContext = FirstDecl;
525 FirstDecl = FirstNewDecl;
526 if (!LastDecl)
527 LastDecl = PrevDecl;
528}
529
530void
531DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
532 DeclContext *This = const_cast<DeclContext *>(this);
533 ExternalASTSource *Source = Context.getExternalSource();
534 assert(hasExternalVisibleStorage() && Source && "No external storage?");
535
536 llvm::SmallVector<VisibleDeclaration, 64> Decls;
537 if (Source->ReadDeclsVisibleInContext(This, Decls))
538 return;
539
540 // There is no longer any visible storage in this context
541 ExternalVisibleStorage = false;
542
543 // Load the declaration IDs for all of the names visible in this
544 // context.
545 assert(!LookupPtr && "Have a lookup map before de-serialization?");
546 StoredDeclsMap *Map = new StoredDeclsMap;
547 LookupPtr = Map;
548 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
549 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
550 }
551}
552
Douglas Gregor6ab35242009-04-09 21:40:53 +0000553DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000554 if (hasExternalLexicalStorage())
555 LoadLexicalDeclsFromExternalStorage(Context);
556
557 // FIXME: Check whether we need to load some declarations from
558 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000559 return decl_iterator(FirstDecl);
560}
561
562DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000563 if (hasExternalLexicalStorage())
564 LoadLexicalDeclsFromExternalStorage(Context);
565
Douglas Gregor6ab35242009-04-09 21:40:53 +0000566 return decl_iterator();
567}
568
Douglas Gregor8038d512009-04-10 17:25:41 +0000569bool DeclContext::decls_empty(ASTContext &Context) const {
570 if (hasExternalLexicalStorage())
571 LoadLexicalDeclsFromExternalStorage(Context);
572
573 return !FirstDecl;
574}
575
Douglas Gregor6ab35242009-04-09 21:40:53 +0000576void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000577 assert(D->getLexicalDeclContext() == this &&
578 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000579 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000580 "Decl already inserted into a DeclContext");
581
582 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000583 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000584 LastDecl = D;
585 } else {
586 FirstDecl = LastDecl = D;
587 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000588
589 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000590 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000591}
592
Douglas Gregor074149e2009-01-05 19:45:36 +0000593/// buildLookup - Build the lookup data structure with all of the
594/// declarations in DCtx (and any other contexts linked to it or
595/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000596void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000597 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000598 for (decl_iterator D = DCtx->decls_begin(Context),
599 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000600 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000601 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000602 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000603 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000604
605 // If this declaration is itself a transparent declaration context,
606 // add its members (recursively).
607 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
608 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000609 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000610 }
611 }
612}
613
Douglas Gregor44b43212008-12-11 16:49:14 +0000614DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000615DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000616 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000617 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000618 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000619
Douglas Gregor2cf26342009-04-09 22:27:44 +0000620 if (hasExternalVisibleStorage())
621 LoadVisibleDeclsFromExternalStorage(Context);
622
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000623 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000624 /// all of the linked DeclContexts (in declaration order!) and
625 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000626 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000627 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000628
Douglas Gregorc36c5402009-04-09 17:29:08 +0000629 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000630 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000631 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000632
Douglas Gregorc36c5402009-04-09 17:29:08 +0000633 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
634 StoredDeclsMap::iterator Pos = Map->find(Name);
635 if (Pos == Map->end())
636 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000637 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000638}
639
640DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000641DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
642 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000643}
644
Chris Lattner0cf2b192009-03-27 19:19:59 +0000645DeclContext *DeclContext::getLookupContext() {
646 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000647 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000648 while (Ctx->isTransparentContext())
649 Ctx = Ctx->getParent();
650 return Ctx;
651}
652
Douglas Gregor88b70942009-02-25 22:02:03 +0000653DeclContext *DeclContext::getEnclosingNamespaceContext() {
654 DeclContext *Ctx = this;
655 // Skip through non-namespace, non-translation-unit contexts.
656 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
657 Ctx = Ctx->getParent();
658 return Ctx->getPrimaryContext();
659}
660
Douglas Gregor6ab35242009-04-09 21:40:53 +0000661void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000662 // FIXME: This feels like a hack. Should DeclarationName support
663 // template-ids, or is there a better way to keep specializations
664 // from being visible?
665 if (isa<ClassTemplateSpecializationDecl>(D))
666 return;
667
Steve Naroff0701bbb2009-01-08 17:28:14 +0000668 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000669 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000670 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000671 return;
672 }
673
674 // If we already have a lookup data structure, perform the insertion
675 // into it. Otherwise, be lazy and don't build that structure until
676 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000677 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000678 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000679
Douglas Gregor074149e2009-01-05 19:45:36 +0000680 // If we are a transparent context, insert into our parent context,
681 // too. This operation is recursive.
682 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000683 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000684}
685
Douglas Gregor6ab35242009-04-09 21:40:53 +0000686void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
687 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000688 // Skip unnamed declarations.
689 if (!D->getDeclName())
690 return;
691
Douglas Gregorcc636682009-02-17 23:15:12 +0000692 // FIXME: This feels like a hack. Should DeclarationName support
693 // template-ids, or is there a better way to keep specializations
694 // from being visible?
695 if (isa<ClassTemplateSpecializationDecl>(D))
696 return;
697
Douglas Gregorc36c5402009-04-09 17:29:08 +0000698 if (!LookupPtr)
699 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000700
701 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000702 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000703 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
704 if (DeclNameEntries.isNull()) {
705 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000706 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000707 }
Chris Lattner91942502009-02-20 00:55:03 +0000708
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000709 // If it is possible that this is a redeclaration, check to see if there is
710 // already a decl for which declarationReplaces returns true. If there is
711 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000712 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000713 return;
Chris Lattner91942502009-02-20 00:55:03 +0000714
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000715 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000716 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000717}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000718
719/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
720/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000721DeclContext::udir_iterator_range
722DeclContext::getUsingDirectives(ASTContext &Context) const {
723 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000724 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
725 reinterpret_cast<udir_iterator>(Result.second));
726}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000727
728void StoredDeclsList::materializeDecls(ASTContext &Context) {
729 if (isNull())
730 return;
731
732 switch ((DataKind)(Data & 0x03)) {
733 case DK_Decl:
734 case DK_Decl_Vector:
735 break;
736
737 case DK_DeclID: {
738 // Resolve this declaration ID to an actual declaration by
739 // querying the external AST source.
740 unsigned DeclID = Data >> 2;
741
742 ExternalASTSource *Source = Context.getExternalSource();
743 assert(Source && "No external AST source available!");
744
745 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
746 break;
747 }
748
749 case DK_ID_Vector: {
750 // We have a vector of declaration IDs. Resolve all of them to
751 // actual declarations.
752 VectorTy &Vector = *getAsVector();
753 ExternalASTSource *Source = Context.getExternalSource();
754 assert(Source && "No external AST source available!");
755
756 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
757 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
758
759 Data = (Data & ~0x03) | DK_Decl_Vector;
760 break;
761 }
762 }
763}