blob: da3b19df5b60784d9e56f1da5c263a8c95ad4c0a [file] [log] [blame]
Eli Friedman7dbab8a2008-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 Gregor8bd3c2e2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorb1fe2c92009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor91f84212008-12-11 16:49:14 +000022#include "clang/AST/Type.h"
Sebastian Redla7b98a72009-04-26 20:35:05 +000023#include "clang/AST/Stmt.h"
24#include "clang/AST/StmtCXX.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattnereae6cb62009-03-05 08:00:35 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000027#include <algorithm>
Chris Lattnerc25d8a72009-03-02 22:20:04 +000028#include <cstdio>
Douglas Gregor55297ac2008-12-23 00:26:44 +000029#include <vector>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Statistics
34//===----------------------------------------------------------------------===//
35
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000036#define DECL(Derived, Base) static int n##Derived##s = 0;
37#include "clang/AST/DeclNodes.def"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000038
39static bool StatSwitch = false;
40
Eli Friedman7dbab8a2008-06-07 16:52:53 +000041const char *Decl::getDeclKindName() const {
42 switch (DeclKind) {
Douglas Gregor8bd3c2e2009-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 Friedman7dbab8a2008-06-07 16:52:53 +000046 }
47}
48
Steve Naroff5faaef72009-01-20 19:53:53 +000049const char *DeclContext::getDeclKindName() const {
50 switch (DeclKind) {
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000051 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidisac152f12009-02-16 14:28:33 +000052#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000053#include "clang/AST/DeclNodes.def"
Steve Naroff5faaef72009-01-20 19:53:53 +000054 }
55}
56
Eli Friedman7dbab8a2008-06-07 16:52:53 +000057bool Decl::CollectingStats(bool Enable) {
58 if (Enable)
59 StatSwitch = true;
60 return StatSwitch;
61}
62
63void Decl::PrintStats() {
64 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump11289f42009-09-09 15:08:12 +000065
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000066 int totalDecls = 0;
67#define DECL(Derived, Base) totalDecls += n##Derived##s;
68#include "clang/AST/DeclNodes.def"
69 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump11289f42009-09-09 15:08:12 +000070
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000071 int totalBytes = 0;
72#define DECL(Derived, Base) \
73 if (n##Derived##s > 0) { \
74 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
75 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
76 n##Derived##s, (int)sizeof(Derived##Decl), \
77 (int)(n##Derived##s * sizeof(Derived##Decl))); \
78 }
79#include "clang/AST/DeclNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +000080
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000081 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman7dbab8a2008-06-07 16:52:53 +000082}
83
84void Decl::addDeclKind(Kind k) {
85 switch (k) {
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000086 default: assert(0 && "Declaration not in DeclNodes.def!");
87#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
88#include "clang/AST/DeclNodes.def"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000089 }
90}
91
Anders Carlssonaa73b912009-06-13 00:08:58 +000092bool Decl::isTemplateParameterPack() const {
93 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
94 return TTP->isParameterPack();
Mike Stump11289f42009-09-09 15:08:12 +000095
Anders Carlssonaa73b912009-06-13 00:08:58 +000096 return false;
97}
98
Douglas Gregorad3f2fc2009-06-25 22:08:12 +000099bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000100 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000101 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000102
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000103 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
104}
105
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000106//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000107// PrettyStackTraceDecl Implementation
108//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattnereae6cb62009-03-05 08:00:35 +0000110void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
111 SourceLocation TheLoc = Loc;
112 if (TheLoc.isInvalid() && TheDecl)
113 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattnereae6cb62009-03-05 08:00:35 +0000115 if (TheLoc.isValid()) {
116 TheLoc.print(OS, SM);
117 OS << ": ";
118 }
119
120 OS << Message;
121
122 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
123 OS << " '" << DN->getQualifiedNameAsString() << '\'';
124 OS << '\n';
125}
Mike Stump11289f42009-09-09 15:08:12 +0000126
Chris Lattnereae6cb62009-03-05 08:00:35 +0000127//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000128// Decl Implementation
129//===----------------------------------------------------------------------===//
130
Chris Lattner8e097192009-03-27 20:18:19 +0000131// Out-of-line virtual method providing a home for Decl.
132Decl::~Decl() {
133 if (isOutOfSemaDC())
134 delete getMultipleDC();
Mike Stump11289f42009-09-09 15:08:12 +0000135
Chris Lattner8e097192009-03-27 20:18:19 +0000136 assert(!HasAttrs && "attributes should have been freed by Destroy");
137}
138
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000139void Decl::setDeclContext(DeclContext *DC) {
140 if (isOutOfSemaDC())
141 delete getMultipleDC();
Mike Stump11289f42009-09-09 15:08:12 +0000142
Chris Lattnerb81eb052009-03-29 06:06:59 +0000143 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000144}
145
146void Decl::setLexicalDeclContext(DeclContext *DC) {
147 if (DC == getLexicalDeclContext())
148 return;
149
150 if (isInSemaDC()) {
151 MultipleDC *MDC = new MultipleDC();
152 MDC->SemanticDC = getDeclContext();
153 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000154 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000155 } else {
156 getMultipleDC()->LexicalDC = DC;
157 }
158}
159
John McCall4fa53422009-10-01 00:25:31 +0000160bool Decl::isInAnonymousNamespace() const {
161 const DeclContext *DC = getDeclContext();
162 do {
163 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
164 if (ND->isAnonymousNamespace())
165 return true;
166 } while ((DC = DC->getParent()));
167
168 return false;
169}
170
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000171TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000172 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
173 return TUD;
174
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000175 DeclContext *DC = getDeclContext();
176 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000177
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000178 while (!DC->isTranslationUnit()) {
179 DC = DC->getParent();
180 assert(DC && "This decl is not contained in a translation unit!");
181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000183 return cast<TranslationUnitDecl>(DC);
184}
185
186ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000187 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000188}
189
Chris Lattner8e097192009-03-27 20:18:19 +0000190unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
191 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000192 case Function:
193 case CXXMethod:
194 case CXXConstructor:
195 case CXXDestructor:
196 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000197 case OverloadedFunction:
198 case Typedef:
199 case EnumConstant:
200 case Var:
201 case ImplicitParam:
202 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000203 case NonTypeTemplateParm:
204 case ObjCMethod:
205 case ObjCContainer:
206 case ObjCCategory:
207 case ObjCInterface:
Chris Lattner8e097192009-03-27 20:18:19 +0000208 case ObjCProperty:
209 case ObjCCompatibleAlias:
210 return IDNS_Ordinary;
John McCalld1e9d832009-08-11 06:59:38 +0000211
John McCall3f746822009-11-17 05:59:44 +0000212 case UsingShadow:
213 return 0; // we'll actually overwrite this later
214
215 case UnresolvedUsing:
216 return IDNS_Tag | IDNS_Ordinary | IDNS_Using;
217
218 case Using:
219 return IDNS_Using;
220
Chris Lattner8e097192009-03-27 20:18:19 +0000221 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000222 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000223
Douglas Gregor79947a22009-04-24 00:11:27 +0000224 case ObjCImplementation:
225 return IDNS_ObjCImplementation;
226
227 case ObjCCategoryImpl:
228 return IDNS_ObjCCategoryImpl;
229
Chris Lattner8e097192009-03-27 20:18:19 +0000230 case Field:
231 case ObjCAtDefsField:
232 case ObjCIvar:
233 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattner8e097192009-03-27 20:18:19 +0000235 case Record:
236 case CXXRecord:
237 case Enum:
238 case TemplateTypeParm:
239 return IDNS_Tag;
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattner8e097192009-03-27 20:18:19 +0000241 case Namespace:
242 case Template:
243 case FunctionTemplate:
244 case ClassTemplate:
245 case TemplateTemplateParm:
Anders Carlssonf03bb512009-03-28 23:02:53 +0000246 case NamespaceAlias:
Chris Lattner8e097192009-03-27 20:18:19 +0000247 return IDNS_Tag | IDNS_Ordinary;
Mike Stump11289f42009-09-09 15:08:12 +0000248
Chris Lattner8e097192009-03-27 20:18:19 +0000249 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000250 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000251 case FriendTemplate:
Chris Lattner8e097192009-03-27 20:18:19 +0000252 case LinkageSpec:
253 case FileScopeAsm:
254 case StaticAssert:
255 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000256 case ObjCPropertyImpl:
257 case ObjCForwardProtocol:
258 case Block:
259 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000260
Chris Lattner8e097192009-03-27 20:18:19 +0000261 // Aren't looked up?
262 case UsingDirective:
263 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000264 case ClassTemplatePartialSpecialization:
Chris Lattner8e097192009-03-27 20:18:19 +0000265 return 0;
266 }
John McCall3f746822009-11-17 05:59:44 +0000267
268 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000269}
270
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000271void Decl::addAttr(Attr *NewAttr) {
272 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000273
274 NewAttr->setNext(ExistingAttr);
275 ExistingAttr = NewAttr;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000277 HasAttrs = true;
278}
279
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000280void Decl::invalidateAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000281 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000282
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000283 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000284 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000285}
286
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000287const Attr *Decl::getAttrsImpl() const {
Mike Stump11289f42009-09-09 15:08:12 +0000288 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000289 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000290}
291
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000292void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000293 bool HasLHSAttr = this->HasAttrs;
294 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000295
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000296 // Usually, neither decl has attrs, nothing to do.
297 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000299 // If 'this' has no attrs, swap the other way.
300 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000301 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000302
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000303 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000304
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000305 // Handle the case when both decls have attrs.
306 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000307 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000308 return;
309 }
Mike Stump11289f42009-09-09 15:08:12 +0000310
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000311 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000312 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
313 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000314 this->HasAttrs = false;
315 RHS->HasAttrs = true;
316}
317
318
Chris Lattner9a2d50e72009-03-04 06:05:19 +0000319void Decl::Destroy(ASTContext &C) {
320 // Free attributes for this decl.
321 if (HasAttrs) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000322 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000323 invalidateAttrs();
Chris Lattner9a2d50e72009-03-04 06:05:19 +0000324 HasAttrs = false;
325 }
Mike Stump11289f42009-09-09 15:08:12 +0000326
Douglas Gregor89100922009-01-13 19:47:12 +0000327#if 0
Douglas Gregor00447932009-01-20 04:25:11 +0000328 // FIXME: Once ownership is fully understood, we can enable this code
329 if (DeclContext *DC = dyn_cast<DeclContext>(this))
330 DC->decls_begin()->Destroy(C);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000331
Chris Lattnerfcd33a62009-03-28 06:04:26 +0000332 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000333 // within the loop, only the Destroy method for the first Decl
334 // will deallocate all of the Decls in a chain.
Mike Stump11289f42009-09-09 15:08:12 +0000335
Chris Lattnerfcd33a62009-03-28 06:04:26 +0000336 Decl* N = getNextDeclInContext();
Mike Stump11289f42009-09-09 15:08:12 +0000337
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000338 while (N) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +0000339 Decl* Tmp = N->getNextDeclInContext();
340 N->NextDeclInContext = 0;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000341 N->Destroy(C);
342 N = Tmp;
Mike Stump11289f42009-09-09 15:08:12 +0000343 }
Douglas Gregor89100922009-01-13 19:47:12 +0000344
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000345 this->~Decl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000346 C.Deallocate((void *)this);
Douglas Gregor00447932009-01-20 04:25:11 +0000347#endif
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000348}
349
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000350Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000351 Decl::Kind DK = D->getDeclKind();
352 switch(DK) {
353#define DECL_CONTEXT(Name) \
354 case Decl::Name: \
355 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
356#define DECL_CONTEXT_BASE(Name)
357#include "clang/AST/DeclNodes.def"
358 default:
359#define DECL_CONTEXT_BASE(Name) \
360 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
361 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
362#include "clang/AST/DeclNodes.def"
363 assert(false && "a decl that inherits DeclContext isn't handled");
364 return 0;
365 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000366}
367
368DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000369 Decl::Kind DK = D->getKind();
370 switch(DK) {
371#define DECL_CONTEXT(Name) \
372 case Decl::Name: \
373 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
374#define DECL_CONTEXT_BASE(Name)
375#include "clang/AST/DeclNodes.def"
376 default:
377#define DECL_CONTEXT_BASE(Name) \
378 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
379 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
380#include "clang/AST/DeclNodes.def"
381 assert(false && "a decl that inherits DeclContext isn't handled");
382 return 0;
383 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000384}
385
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000386CompoundStmt* Decl::getCompoundBody() const {
387 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redla7b98a72009-04-26 20:35:05 +0000388}
389
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000390SourceLocation Decl::getBodyRBrace() const {
391 Stmt *Body = getBody();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000392 if (!Body)
393 return SourceLocation();
394 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
395 return CS->getRBracLoc();
396 assert(isa<CXXTryStmt>(Body) &&
397 "Body can only be CompoundStmt or CXXTryStmt");
398 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
399}
400
Anders Carlssona28908d2009-03-25 23:38:06 +0000401#ifndef NDEBUG
402void Decl::CheckAccessDeclContext() const {
Anders Carlssonadf36b22009-08-29 20:47:47 +0000403 // If the decl is the toplevel translation unit or if we're not in a
404 // record decl context, we don't need to check anything.
405 if (isa<TranslationUnitDecl>(this) ||
406 !isa<CXXRecordDecl>(getDeclContext()))
407 return;
Mike Stump11289f42009-09-09 15:08:12 +0000408
409 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000410 "Access specifier is AS_none inside a record decl");
411}
412
413#endif
414
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000415//===----------------------------------------------------------------------===//
416// DeclContext Implementation
417//===----------------------------------------------------------------------===//
418
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000419bool DeclContext::classof(const Decl *D) {
420 switch (D->getKind()) {
421#define DECL_CONTEXT(Name) case Decl::Name:
422#define DECL_CONTEXT_BASE(Name)
423#include "clang/AST/DeclNodes.def"
424 return true;
425 default:
426#define DECL_CONTEXT_BASE(Name) \
427 if (D->getKind() >= Decl::Name##First && \
428 D->getKind() <= Decl::Name##Last) \
429 return true;
430#include "clang/AST/DeclNodes.def"
431 return false;
432 }
433}
434
Douglas Gregor91f84212008-12-11 16:49:14 +0000435DeclContext::~DeclContext() {
Douglas Gregor9615ec22009-04-09 17:29:08 +0000436 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor91f84212008-12-11 16:49:14 +0000437}
438
439void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000440 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00447932009-01-20 04:25:11 +0000441 (*D++)->Destroy(C);
Douglas Gregor91f84212008-12-11 16:49:14 +0000442}
443
Douglas Gregor7f737c02009-09-10 16:57:35 +0000444/// \brief Find the parent context of this context that will be
445/// used for unqualified name lookup.
446///
447/// Generally, the parent lookup context is the semantic context. However, for
448/// a friend function the parent lookup context is the lexical context, which
449/// is the class in which the friend is declared.
450DeclContext *DeclContext::getLookupParent() {
451 // FIXME: Find a better way to identify friends
452 if (isa<FunctionDecl>(this))
453 if (getParent()->getLookupContext()->isFileContext() &&
454 getLexicalParent()->getLookupContext()->isRecord())
455 return getLexicalParent();
456
457 return getParent();
458}
459
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000460bool DeclContext::isDependentContext() const {
461 if (isFileContext())
462 return false;
463
Douglas Gregor2373c592009-05-31 09:31:02 +0000464 if (isa<ClassTemplatePartialSpecializationDecl>(this))
465 return true;
466
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000467 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
468 if (Record->getDescribedClassTemplate())
469 return true;
470
471 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
472 if (Function->getDescribedFunctionTemplate())
473 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000474
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000475 return getParent() && getParent()->isDependentContext();
476}
477
Douglas Gregor07665a62009-01-05 19:45:36 +0000478bool DeclContext::isTransparentContext() const {
479 if (DeclKind == Decl::Enum)
480 return true; // FIXME: Check for C++0x scoped enums
481 else if (DeclKind == Decl::LinkageSpec)
482 return true;
Douglas Gregor8d092162009-02-26 00:02:51 +0000483 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000484 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor07665a62009-01-05 19:45:36 +0000485 else if (DeclKind == Decl::Namespace)
486 return false; // FIXME: Check for C++0x inline namespaces
487
488 return false;
489}
490
Douglas Gregore985a3b2009-08-27 06:03:53 +0000491bool DeclContext::Encloses(DeclContext *DC) {
492 if (getPrimaryContext() != this)
493 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000494
Douglas Gregore985a3b2009-08-27 06:03:53 +0000495 for (; DC; DC = DC->getParent())
496 if (DC->getPrimaryContext() == this)
497 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000498 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000499}
500
Steve Naroff35c62ae2009-01-08 17:28:14 +0000501DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000502 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000503 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000504 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000505 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000506 // There is only one DeclContext for these entities.
507 return this;
508
509 case Decl::Namespace:
510 // The original namespace is our primary context.
511 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
512
Douglas Gregor91f84212008-12-11 16:49:14 +0000513 case Decl::ObjCMethod:
514 return this;
515
516 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000517 case Decl::ObjCProtocol:
518 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000519 // FIXME: Can Objective-C interfaces be forward-declared?
520 return this;
521
Steve Naroff35c62ae2009-01-08 17:28:14 +0000522 case Decl::ObjCImplementation:
523 case Decl::ObjCCategoryImpl:
524 return this;
525
Douglas Gregor91f84212008-12-11 16:49:14 +0000526 default:
Douglas Gregor67a65642009-02-17 23:15:12 +0000527 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
528 // If this is a tag type that has a definition or is currently
529 // being defined, that definition is our primary context.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000530 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Mike Stump11289f42009-09-09 15:08:12 +0000531 if (TagT->isBeingDefined() ||
Douglas Gregor67a65642009-02-17 23:15:12 +0000532 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
533 return TagT->getDecl();
534 return this;
535 }
536
Douglas Gregor91f84212008-12-11 16:49:14 +0000537 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
538 "Unknown DeclContext kind");
539 return this;
540 }
541}
542
543DeclContext *DeclContext::getNextContext() {
544 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000545 case Decl::Namespace:
546 // Return the next namespace
547 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
548
549 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000550 return 0;
551 }
552}
553
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000554/// \brief Load the declarations within this lexical storage from an
555/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000556void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000557DeclContext::LoadLexicalDeclsFromExternalStorage() const {
558 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000559 assert(hasExternalLexicalStorage() && Source && "No external storage?");
560
Eli Friedmanf66a2e12009-04-27 23:43:36 +0000561 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000562 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000563 Decls))
564 return;
565
566 // There is no longer any lexical storage in this context
567 ExternalLexicalStorage = false;
568
569 if (Decls.empty())
570 return;
571
572 // Resolve all of the declaration IDs into declarations, building up
573 // a chain of declarations via the Decl::NextDeclInContext field.
574 Decl *FirstNewDecl = 0;
575 Decl *PrevDecl = 0;
576 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
577 Decl *D = Source->GetDecl(Decls[I]);
578 if (PrevDecl)
579 PrevDecl->NextDeclInContext = D;
580 else
581 FirstNewDecl = D;
582
583 PrevDecl = D;
584 }
585
586 // Splice the newly-read declarations into the beginning of the list
587 // of declarations.
588 PrevDecl->NextDeclInContext = FirstDecl;
589 FirstDecl = FirstNewDecl;
590 if (!LastDecl)
591 LastDecl = PrevDecl;
592}
593
Mike Stump11289f42009-09-09 15:08:12 +0000594void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000595DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000596 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000597 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000598 assert(hasExternalVisibleStorage() && Source && "No external storage?");
599
600 llvm::SmallVector<VisibleDeclaration, 64> Decls;
601 if (Source->ReadDeclsVisibleInContext(This, Decls))
602 return;
603
604 // There is no longer any visible storage in this context
605 ExternalVisibleStorage = false;
606
607 // Load the declaration IDs for all of the names visible in this
608 // context.
609 assert(!LookupPtr && "Have a lookup map before de-serialization?");
610 StoredDeclsMap *Map = new StoredDeclsMap;
611 LookupPtr = Map;
612 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
613 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
614 }
615}
616
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000617DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000618 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000619 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000620
621 // FIXME: Check whether we need to load some declarations from
622 // external storage.
Mike Stump11289f42009-09-09 15:08:12 +0000623 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000624}
625
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000626DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000627 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000628 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000629
Mike Stump11289f42009-09-09 15:08:12 +0000630 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000631}
632
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000633bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000634 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000635 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000636
637 return !FirstDecl;
638}
639
John McCalld1e9d832009-08-11 06:59:38 +0000640void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +0000641 assert(D->getLexicalDeclContext() == this &&
642 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +0000643 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +0000644 "Decl already inserted into a DeclContext");
645
646 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +0000647 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +0000648 LastDecl = D;
649 } else {
650 FirstDecl = LastDecl = D;
651 }
John McCalld1e9d832009-08-11 06:59:38 +0000652}
653
654void DeclContext::addDecl(Decl *D) {
655 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000656
657 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000658 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +0000659}
660
Douglas Gregor07665a62009-01-05 19:45:36 +0000661/// buildLookup - Build the lookup data structure with all of the
662/// declarations in DCtx (and any other contexts linked to it or
663/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000664void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000665 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +0000666 for (decl_iterator D = DCtx->decls_begin(),
667 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +0000668 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +0000669 // Insert this declaration into the lookup structure, but only
670 // if it's semantically in its decl context. During non-lazy
671 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000672 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +0000673 if (D->getDeclContext() == DCtx)
674 makeDeclVisibleInContextImpl(ND);
Douglas Gregor07665a62009-01-05 19:45:36 +0000675
676 // If this declaration is itself a transparent declaration context,
677 // add its members (recursively).
678 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
679 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000680 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +0000681 }
682 }
683}
684
Mike Stump11289f42009-09-09 15:08:12 +0000685DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000686DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +0000687 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000688 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000689 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000690
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000691 if (hasExternalVisibleStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000692 LoadVisibleDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000693
Douglas Gregor55297ac2008-12-23 00:26:44 +0000694 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +0000695 /// all of the linked DeclContexts (in declaration order!) and
696 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +0000697 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000698 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +0000699
Douglas Gregor9615ec22009-04-09 17:29:08 +0000700 if (!LookupPtr)
Chris Lattner24e24d52009-02-20 00:55:03 +0000701 return lookup_result(0, 0);
Douglas Gregor9615ec22009-04-09 17:29:08 +0000702 }
Douglas Gregor91f84212008-12-11 16:49:14 +0000703
Douglas Gregor9615ec22009-04-09 17:29:08 +0000704 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
705 StoredDeclsMap::iterator Pos = Map->find(Name);
706 if (Pos == Map->end())
707 return lookup_result(0, 0);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000708 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor91f84212008-12-11 16:49:14 +0000709}
710
Mike Stump11289f42009-09-09 15:08:12 +0000711DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000712DeclContext::lookup(DeclarationName Name) const {
713 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000714}
715
Chris Lattner17a1bfa2009-03-27 19:19:59 +0000716DeclContext *DeclContext::getLookupContext() {
717 DeclContext *Ctx = this;
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000718 // Skip through transparent contexts.
Douglas Gregor6ad0ef52009-01-06 23:51:29 +0000719 while (Ctx->isTransparentContext())
720 Ctx = Ctx->getParent();
721 return Ctx;
722}
723
Douglas Gregorf47b9112009-02-25 22:02:03 +0000724DeclContext *DeclContext::getEnclosingNamespaceContext() {
725 DeclContext *Ctx = this;
726 // Skip through non-namespace, non-translation-unit contexts.
727 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
728 Ctx = Ctx->getParent();
729 return Ctx->getPrimaryContext();
730}
731
John McCall759e32b2009-08-31 22:39:49 +0000732void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000733 // FIXME: This feels like a hack. Should DeclarationName support
734 // template-ids, or is there a better way to keep specializations
735 // from being visible?
736 if (isa<ClassTemplateSpecializationDecl>(D))
737 return;
738
Steve Naroff35c62ae2009-01-08 17:28:14 +0000739 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000740 if (PrimaryContext != this) {
John McCall759e32b2009-08-31 22:39:49 +0000741 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +0000742 return;
743 }
744
745 // If we already have a lookup data structure, perform the insertion
746 // into it. Otherwise, be lazy and don't build that structure until
747 // someone asks for it.
John McCall759e32b2009-08-31 22:39:49 +0000748 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000749 makeDeclVisibleInContextImpl(D);
Douglas Gregor07665a62009-01-05 19:45:36 +0000750
Douglas Gregor07665a62009-01-05 19:45:36 +0000751 // If we are a transparent context, insert into our parent context,
752 // too. This operation is recursive.
753 if (isTransparentContext())
John McCall759e32b2009-08-31 22:39:49 +0000754 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +0000755}
756
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000757void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000758 // Skip unnamed declarations.
759 if (!D->getDeclName())
760 return;
761
Douglas Gregor67a65642009-02-17 23:15:12 +0000762 // FIXME: This feels like a hack. Should DeclarationName support
763 // template-ids, or is there a better way to keep specializations
764 // from being visible?
765 if (isa<ClassTemplateSpecializationDecl>(D))
766 return;
767
Douglas Gregor9615ec22009-04-09 17:29:08 +0000768 if (!LookupPtr)
769 LookupPtr = new StoredDeclsMap;
Douglas Gregor91f84212008-12-11 16:49:14 +0000770
771 // Insert this declaration into the map.
Douglas Gregor9615ec22009-04-09 17:29:08 +0000772 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattnercaae7162009-02-20 01:44:05 +0000773 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
774 if (DeclNameEntries.isNull()) {
775 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +0000776 return;
Douglas Gregor91f84212008-12-11 16:49:14 +0000777 }
Chris Lattner24e24d52009-02-20 00:55:03 +0000778
Chris Lattner29578f32009-02-20 01:10:07 +0000779 // If it is possible that this is a redeclaration, check to see if there is
780 // already a decl for which declarationReplaces returns true. If there is
781 // one, just replace it and return.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000782 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D))
Chris Lattnercaae7162009-02-20 01:44:05 +0000783 return;
Mike Stump11289f42009-09-09 15:08:12 +0000784
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +0000785 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +0000786 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +0000787}
Douglas Gregor889ceb72009-02-03 19:21:40 +0000788
789/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
790/// this context.
Mike Stump11289f42009-09-09 15:08:12 +0000791DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000792DeclContext::getUsingDirectives() const {
793 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +0000794 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
795 reinterpret_cast<udir_iterator>(Result.second));
796}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000797
798void StoredDeclsList::materializeDecls(ASTContext &Context) {
799 if (isNull())
800 return;
801
802 switch ((DataKind)(Data & 0x03)) {
803 case DK_Decl:
804 case DK_Decl_Vector:
805 break;
806
807 case DK_DeclID: {
808 // Resolve this declaration ID to an actual declaration by
809 // querying the external AST source.
810 unsigned DeclID = Data >> 2;
811
812 ExternalASTSource *Source = Context.getExternalSource();
813 assert(Source && "No external AST source available!");
814
815 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
816 break;
817 }
818
819 case DK_ID_Vector: {
820 // We have a vector of declaration IDs. Resolve all of them to
821 // actual declarations.
822 VectorTy &Vector = *getAsVector();
823 ExternalASTSource *Source = Context.getExternalSource();
824 assert(Source && "No external AST source available!");
825
826 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
827 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
828
829 Data = (Data & ~0x03) | DK_Decl_Vector;
830 break;
831 }
832 }
833}