blob: c557e615e78b90d7e67893ea81a1f15252a40a1d [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
Eli Friedman56d29372008-06-07 16:52:53 +000041const char *Decl::getDeclKindName() const {
42 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000043 default: assert(0 && "Declaration not in DeclNodes.def!");
44#define DECL(Derived, Base) case Derived: return #Derived;
45#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000046 }
47}
48
Steve Naroff0a473932009-01-20 19:53:53 +000049const char *DeclContext::getDeclKindName() const {
50 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000051 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000052#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000053#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000054 }
55}
56
Eli Friedman56d29372008-06-07 16:52:53 +000057bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000058 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000059 return StatSwitch;
60}
61
62void Decl::PrintStats() {
63 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000064
Douglas Gregor64650af2009-02-02 23:39:07 +000065 int totalDecls = 0;
66#define DECL(Derived, Base) totalDecls += n##Derived##s;
67#include "clang/AST/DeclNodes.def"
68 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000069
Douglas Gregor64650af2009-02-02 23:39:07 +000070 int totalBytes = 0;
71#define DECL(Derived, Base) \
72 if (n##Derived##s > 0) { \
73 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
74 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
75 n##Derived##s, (int)sizeof(Derived##Decl), \
76 (int)(n##Derived##s * sizeof(Derived##Decl))); \
77 }
78#include "clang/AST/DeclNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor64650af2009-02-02 23:39:07 +000080 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000081}
82
83void Decl::addDeclKind(Kind k) {
84 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000085 default: assert(0 && "Declaration not in DeclNodes.def!");
86#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
87#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000088 }
89}
90
Anders Carlsson67e33202009-06-13 00:08:58 +000091bool Decl::isTemplateParameterPack() const {
92 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
93 return TTP->isParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +000094
Anders Carlsson67e33202009-06-13 00:08:58 +000095 return false;
96}
97
Douglas Gregore53060f2009-06-25 22:08:12 +000098bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +000099 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000100 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Douglas Gregore53060f2009-06-25 22:08:12 +0000102 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
103}
104
Eli Friedman56d29372008-06-07 16:52:53 +0000105//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000106// PrettyStackTraceDecl Implementation
107//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner49f28ca2009-03-05 08:00:35 +0000109void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
110 SourceLocation TheLoc = Loc;
111 if (TheLoc.isInvalid() && TheDecl)
112 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattner49f28ca2009-03-05 08:00:35 +0000114 if (TheLoc.isValid()) {
115 TheLoc.print(OS, SM);
116 OS << ": ";
117 }
118
119 OS << Message;
120
Daniel Dunbarc5236562009-11-21 09:05:59 +0000121 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000122 OS << " '" << DN->getQualifiedNameAsString() << '\'';
123 OS << '\n';
124}
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chris Lattner49f28ca2009-03-05 08:00:35 +0000126//===----------------------------------------------------------------------===//
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() {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000132 assert(!HasAttrs && "attributes should have been freed by Destroy");
133}
134
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000135void Decl::setDeclContext(DeclContext *DC) {
136 if (isOutOfSemaDC())
137 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattneree219fd2009-03-29 06:06:59 +0000139 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000140}
141
142void Decl::setLexicalDeclContext(DeclContext *DC) {
143 if (DC == getLexicalDeclContext())
144 return;
145
146 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000147 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000148 MDC->SemanticDC = getDeclContext();
149 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000150 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000151 } else {
152 getMultipleDC()->LexicalDC = DC;
153 }
154}
155
John McCall9aeed322009-10-01 00:25:31 +0000156bool Decl::isInAnonymousNamespace() const {
157 const DeclContext *DC = getDeclContext();
158 do {
159 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
160 if (ND->isAnonymousNamespace())
161 return true;
162 } while ((DC = DC->getParent()));
163
164 return false;
165}
166
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000167TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000168 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
169 return TUD;
170
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000171 DeclContext *DC = getDeclContext();
172 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000174 while (!DC->isTranslationUnit()) {
175 DC = DC->getParent();
176 assert(DC && "This decl is not contained in a translation unit!");
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000179 return cast<TranslationUnitDecl>(DC);
180}
181
182ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000183 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000184}
185
Chris Lattner769dbdf2009-03-27 20:18:19 +0000186unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
187 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000188 case Function:
189 case CXXMethod:
190 case CXXConstructor:
191 case CXXDestructor:
192 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000193 case Typedef:
194 case EnumConstant:
195 case Var:
196 case ImplicitParam:
197 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000198 case NonTypeTemplateParm:
199 case ObjCMethod:
200 case ObjCContainer:
201 case ObjCCategory:
202 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000203 case ObjCProperty:
204 case ObjCCompatibleAlias:
205 return IDNS_Ordinary;
John McCall3f9a8a62009-08-11 06:59:38 +0000206
John McCall9488ea12009-11-17 05:59:44 +0000207 case UsingShadow:
208 return 0; // we'll actually overwrite this later
209
John McCall7ba107a2009-11-18 02:36:19 +0000210 case UnresolvedUsingValue:
211 case UnresolvedUsingTypename:
212 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000213
214 case Using:
215 return IDNS_Using;
216
Chris Lattner769dbdf2009-03-27 20:18:19 +0000217 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000218 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000220 case ObjCImplementation:
221 return IDNS_ObjCImplementation;
222
223 case ObjCCategoryImpl:
224 return IDNS_ObjCCategoryImpl;
225
Chris Lattner769dbdf2009-03-27 20:18:19 +0000226 case Field:
227 case ObjCAtDefsField:
228 case ObjCIvar:
229 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Chris Lattner769dbdf2009-03-27 20:18:19 +0000231 case Record:
232 case CXXRecord:
233 case Enum:
234 case TemplateTypeParm:
235 return IDNS_Tag;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattner769dbdf2009-03-27 20:18:19 +0000237 case Namespace:
238 case Template:
239 case FunctionTemplate:
240 case ClassTemplate:
241 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000242 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000243 return IDNS_Tag | IDNS_Ordinary;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattner769dbdf2009-03-27 20:18:19 +0000245 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000246 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000247 case FriendTemplate:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000248 case LinkageSpec:
249 case FileScopeAsm:
250 case StaticAssert:
251 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000252 case ObjCPropertyImpl:
253 case ObjCForwardProtocol:
254 case Block:
255 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000256
Chris Lattner769dbdf2009-03-27 20:18:19 +0000257 // Aren't looked up?
258 case UsingDirective:
259 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000260 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000261 return 0;
262 }
John McCall9488ea12009-11-17 05:59:44 +0000263
264 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000265}
266
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000267void Decl::addAttr(Attr *NewAttr) {
268 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000269
270 NewAttr->setNext(ExistingAttr);
271 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Eli Friedman56d29372008-06-07 16:52:53 +0000273 HasAttrs = true;
274}
275
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000276void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000277 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Eli Friedman56d29372008-06-07 16:52:53 +0000279 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000280 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000281}
282
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000283const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000284 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000285 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000286}
287
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000288void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000289 bool HasLHSAttr = this->HasAttrs;
290 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Eli Friedman56d29372008-06-07 16:52:53 +0000292 // Usually, neither decl has attrs, nothing to do.
293 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Eli Friedman56d29372008-06-07 16:52:53 +0000295 // If 'this' has no attrs, swap the other way.
296 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000297 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000299 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Eli Friedman56d29372008-06-07 16:52:53 +0000301 // Handle the case when both decls have attrs.
302 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000303 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000304 return;
305 }
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Eli Friedman56d29372008-06-07 16:52:53 +0000307 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000308 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
309 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000310 this->HasAttrs = false;
311 RHS->HasAttrs = true;
312}
313
314
Chris Lattnercc581472009-03-04 06:05:19 +0000315void Decl::Destroy(ASTContext &C) {
316 // Free attributes for this decl.
317 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000318 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000319 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000320 HasAttrs = false;
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000323#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000324 // FIXME: Once ownership is fully understood, we can enable this code
325 if (DeclContext *DC = dyn_cast<DeclContext>(this))
326 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000327
Chris Lattner244a67d2009-03-28 06:04:26 +0000328 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000329 // within the loop, only the Destroy method for the first Decl
330 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Chris Lattner244a67d2009-03-28 06:04:26 +0000332 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000334 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000335 Decl* Tmp = N->getNextDeclInContext();
336 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000337 N->Destroy(C);
338 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000339 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000340
Ted Kremenek94a39002009-12-01 00:07:10 +0000341 if (isOutOfSemaDC())
342 delete (C) getMultipleDC();
343
Eli Friedman56d29372008-06-07 16:52:53 +0000344 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000345 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000346#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000347}
348
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000349Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000350 Decl::Kind DK = D->getDeclKind();
351 switch(DK) {
352#define DECL_CONTEXT(Name) \
353 case Decl::Name: \
354 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
355#define DECL_CONTEXT_BASE(Name)
356#include "clang/AST/DeclNodes.def"
357 default:
358#define DECL_CONTEXT_BASE(Name) \
359 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
360 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
361#include "clang/AST/DeclNodes.def"
362 assert(false && "a decl that inherits DeclContext isn't handled");
363 return 0;
364 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000365}
366
367DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000368 Decl::Kind DK = D->getKind();
369 switch(DK) {
370#define DECL_CONTEXT(Name) \
371 case Decl::Name: \
372 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
373#define DECL_CONTEXT_BASE(Name)
374#include "clang/AST/DeclNodes.def"
375 default:
376#define DECL_CONTEXT_BASE(Name) \
377 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
378 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
379#include "clang/AST/DeclNodes.def"
380 assert(false && "a decl that inherits DeclContext isn't handled");
381 return 0;
382 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000383}
384
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000385CompoundStmt* Decl::getCompoundBody() const {
386 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000387}
388
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000389SourceLocation Decl::getBodyRBrace() const {
390 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000391 if (!Body)
392 return SourceLocation();
393 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
394 return CS->getRBracLoc();
395 assert(isa<CXXTryStmt>(Body) &&
396 "Body can only be CompoundStmt or CXXTryStmt");
397 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
398}
399
Anders Carlsson1329c272009-03-25 23:38:06 +0000400#ifndef NDEBUG
401void Decl::CheckAccessDeclContext() const {
Anders Carlsson35eda442009-08-29 20:47:47 +0000402 // If the decl is the toplevel translation unit or if we're not in a
403 // record decl context, we don't need to check anything.
404 if (isa<TranslationUnitDecl>(this) ||
405 !isa<CXXRecordDecl>(getDeclContext()))
406 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
408 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000409 "Access specifier is AS_none inside a record decl");
410}
411
412#endif
413
Eli Friedman56d29372008-06-07 16:52:53 +0000414//===----------------------------------------------------------------------===//
415// DeclContext Implementation
416//===----------------------------------------------------------------------===//
417
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000418bool DeclContext::classof(const Decl *D) {
419 switch (D->getKind()) {
420#define DECL_CONTEXT(Name) case Decl::Name:
421#define DECL_CONTEXT_BASE(Name)
422#include "clang/AST/DeclNodes.def"
423 return true;
424 default:
425#define DECL_CONTEXT_BASE(Name) \
426 if (D->getKind() >= Decl::Name##First && \
427 D->getKind() <= Decl::Name##Last) \
428 return true;
429#include "clang/AST/DeclNodes.def"
430 return false;
431 }
432}
433
Douglas Gregor44b43212008-12-11 16:49:14 +0000434DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000435 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000436}
437
438void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000439 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000440 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000441}
442
Douglas Gregore942bbe2009-09-10 16:57:35 +0000443/// \brief Find the parent context of this context that will be
444/// used for unqualified name lookup.
445///
446/// Generally, the parent lookup context is the semantic context. However, for
447/// a friend function the parent lookup context is the lexical context, which
448/// is the class in which the friend is declared.
449DeclContext *DeclContext::getLookupParent() {
450 // FIXME: Find a better way to identify friends
451 if (isa<FunctionDecl>(this))
452 if (getParent()->getLookupContext()->isFileContext() &&
453 getLexicalParent()->getLookupContext()->isRecord())
454 return getLexicalParent();
455
456 return getParent();
457}
458
Douglas Gregorbc221632009-05-28 16:34:51 +0000459bool DeclContext::isDependentContext() const {
460 if (isFileContext())
461 return false;
462
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000463 if (isa<ClassTemplatePartialSpecializationDecl>(this))
464 return true;
465
Douglas Gregorbc221632009-05-28 16:34:51 +0000466 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
467 if (Record->getDescribedClassTemplate())
468 return true;
469
470 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
471 if (Function->getDescribedFunctionTemplate())
472 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregorbc221632009-05-28 16:34:51 +0000474 return getParent() && getParent()->isDependentContext();
475}
476
Douglas Gregor074149e2009-01-05 19:45:36 +0000477bool DeclContext::isTransparentContext() const {
478 if (DeclKind == Decl::Enum)
479 return true; // FIXME: Check for C++0x scoped enums
480 else if (DeclKind == Decl::LinkageSpec)
481 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000482 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000483 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000484 else if (DeclKind == Decl::Namespace)
485 return false; // FIXME: Check for C++0x inline namespaces
486
487 return false;
488}
489
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000490bool DeclContext::Encloses(DeclContext *DC) {
491 if (getPrimaryContext() != this)
492 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000494 for (; DC; DC = DC->getParent())
495 if (DC->getPrimaryContext() == this)
496 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000497 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000498}
499
Steve Naroff0701bbb2009-01-08 17:28:14 +0000500DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000501 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000502 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000503 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000504 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000505 // There is only one DeclContext for these entities.
506 return this;
507
508 case Decl::Namespace:
509 // The original namespace is our primary context.
510 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
511
Douglas Gregor44b43212008-12-11 16:49:14 +0000512 case Decl::ObjCMethod:
513 return this;
514
515 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000516 case Decl::ObjCProtocol:
517 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000518 // FIXME: Can Objective-C interfaces be forward-declared?
519 return this;
520
Steve Naroff0701bbb2009-01-08 17:28:14 +0000521 case Decl::ObjCImplementation:
522 case Decl::ObjCCategoryImpl:
523 return this;
524
Douglas Gregor44b43212008-12-11 16:49:14 +0000525 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000526 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
527 // If this is a tag type that has a definition or is currently
528 // being defined, that definition is our primary context.
Ted Kremenek6217b802009-07-29 21:53:49 +0000529 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000530 if (TagT->isBeingDefined() ||
Douglas Gregorcc636682009-02-17 23:15:12 +0000531 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
532 return TagT->getDecl();
533 return this;
534 }
535
Douglas Gregor44b43212008-12-11 16:49:14 +0000536 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
537 "Unknown DeclContext kind");
538 return this;
539 }
540}
541
542DeclContext *DeclContext::getNextContext() {
543 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000544 case Decl::Namespace:
545 // Return the next namespace
546 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
547
548 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000549 return 0;
550 }
551}
552
Douglas Gregor2cf26342009-04-09 22:27:44 +0000553/// \brief Load the declarations within this lexical storage from an
554/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000555void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000556DeclContext::LoadLexicalDeclsFromExternalStorage() const {
557 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000558 assert(hasExternalLexicalStorage() && Source && "No external storage?");
559
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000560 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000561 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000562 Decls))
563 return;
564
565 // There is no longer any lexical storage in this context
566 ExternalLexicalStorage = false;
567
568 if (Decls.empty())
569 return;
570
571 // Resolve all of the declaration IDs into declarations, building up
572 // a chain of declarations via the Decl::NextDeclInContext field.
573 Decl *FirstNewDecl = 0;
574 Decl *PrevDecl = 0;
575 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
576 Decl *D = Source->GetDecl(Decls[I]);
577 if (PrevDecl)
578 PrevDecl->NextDeclInContext = D;
579 else
580 FirstNewDecl = D;
581
582 PrevDecl = D;
583 }
584
585 // Splice the newly-read declarations into the beginning of the list
586 // of declarations.
587 PrevDecl->NextDeclInContext = FirstDecl;
588 FirstDecl = FirstNewDecl;
589 if (!LastDecl)
590 LastDecl = PrevDecl;
591}
592
Mike Stump1eb44332009-09-09 15:08:12 +0000593void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000594DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000595 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000596 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000597 assert(hasExternalVisibleStorage() && Source && "No external storage?");
598
599 llvm::SmallVector<VisibleDeclaration, 64> Decls;
600 if (Source->ReadDeclsVisibleInContext(This, Decls))
601 return;
602
603 // There is no longer any visible storage in this context
604 ExternalVisibleStorage = false;
605
606 // Load the declaration IDs for all of the names visible in this
607 // context.
608 assert(!LookupPtr && "Have a lookup map before de-serialization?");
609 StoredDeclsMap *Map = new StoredDeclsMap;
610 LookupPtr = Map;
611 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
612 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
613 }
614}
615
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000616DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000617 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000618 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000619
620 // FIXME: Check whether we need to load some declarations from
621 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000622 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000623}
624
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000625DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000626 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000627 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000628
Mike Stump1eb44332009-09-09 15:08:12 +0000629 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000630}
631
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000632bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000633 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000634 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000635
636 return !FirstDecl;
637}
638
John McCall3f9a8a62009-08-11 06:59:38 +0000639void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000640 assert(D->getLexicalDeclContext() == this &&
641 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000642 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000643 "Decl already inserted into a DeclContext");
644
645 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000646 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000647 LastDecl = D;
648 } else {
649 FirstDecl = LastDecl = D;
650 }
John McCall3f9a8a62009-08-11 06:59:38 +0000651}
652
653void DeclContext::addDecl(Decl *D) {
654 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000655
656 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000657 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000658}
659
Douglas Gregor074149e2009-01-05 19:45:36 +0000660/// buildLookup - Build the lookup data structure with all of the
661/// declarations in DCtx (and any other contexts linked to it or
662/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000663void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000664 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000665 for (decl_iterator D = DCtx->decls_begin(),
666 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000667 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000668 // Insert this declaration into the lookup structure, but only
669 // if it's semantically in its decl context. During non-lazy
670 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000671 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000672 if (D->getDeclContext() == DCtx)
673 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000674
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000675 // Insert any forward-declared Objective-C interfaces into the lookup
676 // data structure.
677 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
678 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
679 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000680 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000681
Douglas Gregor074149e2009-01-05 19:45:36 +0000682 // If this declaration is itself a transparent declaration context,
683 // add its members (recursively).
684 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
685 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000686 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000687 }
688 }
689}
690
Mike Stump1eb44332009-09-09 15:08:12 +0000691DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000692DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000693 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000694 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000695 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000696
Douglas Gregor2cf26342009-04-09 22:27:44 +0000697 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000698 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000699
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000700 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000701 /// all of the linked DeclContexts (in declaration order!) and
702 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000703 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000704 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000705
Douglas Gregorc36c5402009-04-09 17:29:08 +0000706 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000707 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000708 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000709
Douglas Gregorc36c5402009-04-09 17:29:08 +0000710 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
711 StoredDeclsMap::iterator Pos = Map->find(Name);
712 if (Pos == Map->end())
713 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000714 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000715}
716
Mike Stump1eb44332009-09-09 15:08:12 +0000717DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000718DeclContext::lookup(DeclarationName Name) const {
719 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000720}
721
Chris Lattner0cf2b192009-03-27 19:19:59 +0000722DeclContext *DeclContext::getLookupContext() {
723 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000724 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000725 while (Ctx->isTransparentContext())
726 Ctx = Ctx->getParent();
727 return Ctx;
728}
729
Douglas Gregor88b70942009-02-25 22:02:03 +0000730DeclContext *DeclContext::getEnclosingNamespaceContext() {
731 DeclContext *Ctx = this;
732 // Skip through non-namespace, non-translation-unit contexts.
733 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
734 Ctx = Ctx->getParent();
735 return Ctx->getPrimaryContext();
736}
737
John McCallab88d972009-08-31 22:39:49 +0000738void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000739 // FIXME: This feels like a hack. Should DeclarationName support
740 // template-ids, or is there a better way to keep specializations
741 // from being visible?
742 if (isa<ClassTemplateSpecializationDecl>(D))
743 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000744 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
745 if (FD->isFunctionTemplateSpecialization())
746 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000747
Steve Naroff0701bbb2009-01-08 17:28:14 +0000748 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000749 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000750 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000751 return;
752 }
753
754 // If we already have a lookup data structure, perform the insertion
755 // into it. Otherwise, be lazy and don't build that structure until
756 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000757 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000758 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000759
Douglas Gregor074149e2009-01-05 19:45:36 +0000760 // If we are a transparent context, insert into our parent context,
761 // too. This operation is recursive.
762 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000763 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000764}
765
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000766void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000767 // Skip unnamed declarations.
768 if (!D->getDeclName())
769 return;
770
Douglas Gregorcc636682009-02-17 23:15:12 +0000771 // FIXME: This feels like a hack. Should DeclarationName support
772 // template-ids, or is there a better way to keep specializations
773 // from being visible?
774 if (isa<ClassTemplateSpecializationDecl>(D))
775 return;
776
Douglas Gregorc36c5402009-04-09 17:29:08 +0000777 if (!LookupPtr)
778 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000779
780 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000781 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000782 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
783 if (DeclNameEntries.isNull()) {
784 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000785 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000786 }
Chris Lattner91942502009-02-20 00:55:03 +0000787
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000788 // If it is possible that this is a redeclaration, check to see if there is
789 // already a decl for which declarationReplaces returns true. If there is
790 // one, just replace it and return.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000791 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D))
Chris Lattner67762a32009-02-20 01:44:05 +0000792 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000794 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000795 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000796}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000797
798/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
799/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000800DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000801DeclContext::getUsingDirectives() const {
802 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000803 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
804 reinterpret_cast<udir_iterator>(Result.second));
805}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000806
807void StoredDeclsList::materializeDecls(ASTContext &Context) {
808 if (isNull())
809 return;
810
811 switch ((DataKind)(Data & 0x03)) {
812 case DK_Decl:
813 case DK_Decl_Vector:
814 break;
815
816 case DK_DeclID: {
817 // Resolve this declaration ID to an actual declaration by
818 // querying the external AST source.
819 unsigned DeclID = Data >> 2;
820
821 ExternalASTSource *Source = Context.getExternalSource();
822 assert(Source && "No external AST source available!");
823
824 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
825 break;
826 }
827
828 case DK_ID_Vector: {
829 // We have a vector of declaration IDs. Resolve all of them to
830 // actual declarations.
831 VectorTy &Vector = *getAsVector();
832 ExternalASTSource *Source = Context.getExternalSource();
833 assert(Source && "No external AST source available!");
834
835 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
836 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
837
838 Data = (Data & ~0x03) | DK_Decl_Vector;
839 break;
840 }
841 }
842}