blob: 95b749bfbbd84784f7f8079529a048cd13dbb281 [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
Douglas Gregor79c22782010-01-16 20:21:20 +0000105bool Decl::isDefinedOutsideFunctionOrMethod() const {
106 for (const DeclContext *DC = getDeclContext();
107 DC && !DC->isTranslationUnit();
108 DC = DC->getParent())
109 if (DC->isFunctionOrMethod())
110 return false;
111
112 return true;
113}
114
115
Eli Friedman56d29372008-06-07 16:52:53 +0000116//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000117// PrettyStackTraceDecl Implementation
118//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner49f28ca2009-03-05 08:00:35 +0000120void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
121 SourceLocation TheLoc = Loc;
122 if (TheLoc.isInvalid() && TheDecl)
123 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner49f28ca2009-03-05 08:00:35 +0000125 if (TheLoc.isValid()) {
126 TheLoc.print(OS, SM);
127 OS << ": ";
128 }
129
130 OS << Message;
131
Daniel Dunbarc5236562009-11-21 09:05:59 +0000132 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000133 OS << " '" << DN->getQualifiedNameAsString() << '\'';
134 OS << '\n';
135}
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattner49f28ca2009-03-05 08:00:35 +0000137//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000138// Decl Implementation
139//===----------------------------------------------------------------------===//
140
Chris Lattner769dbdf2009-03-27 20:18:19 +0000141// Out-of-line virtual method providing a home for Decl.
142Decl::~Decl() {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000143 assert(!HasAttrs && "attributes should have been freed by Destroy");
144}
145
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000146void Decl::setDeclContext(DeclContext *DC) {
147 if (isOutOfSemaDC())
148 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Chris Lattneree219fd2009-03-29 06:06:59 +0000150 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000151}
152
153void Decl::setLexicalDeclContext(DeclContext *DC) {
154 if (DC == getLexicalDeclContext())
155 return;
156
157 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000158 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000159 MDC->SemanticDC = getDeclContext();
160 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000161 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000162 } else {
163 getMultipleDC()->LexicalDC = DC;
164 }
165}
166
John McCall9aeed322009-10-01 00:25:31 +0000167bool Decl::isInAnonymousNamespace() const {
168 const DeclContext *DC = getDeclContext();
169 do {
170 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
171 if (ND->isAnonymousNamespace())
172 return true;
173 } while ((DC = DC->getParent()));
174
175 return false;
176}
177
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000178TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000179 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
180 return TUD;
181
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000182 DeclContext *DC = getDeclContext();
183 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000185 while (!DC->isTranslationUnit()) {
186 DC = DC->getParent();
187 assert(DC && "This decl is not contained in a translation unit!");
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000190 return cast<TranslationUnitDecl>(DC);
191}
192
193ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000194 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000195}
196
Chris Lattner769dbdf2009-03-27 20:18:19 +0000197unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
198 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000199 case Function:
200 case CXXMethod:
201 case CXXConstructor:
202 case CXXDestructor:
203 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000204 case Typedef:
205 case EnumConstant:
206 case Var:
207 case ImplicitParam:
208 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000209 case NonTypeTemplateParm:
210 case ObjCMethod:
211 case ObjCContainer:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000212 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000213 case ObjCProperty:
214 case ObjCCompatibleAlias:
215 return IDNS_Ordinary;
John McCall3f9a8a62009-08-11 06:59:38 +0000216
John McCall9488ea12009-11-17 05:59:44 +0000217 case UsingShadow:
218 return 0; // we'll actually overwrite this later
219
John McCall7ba107a2009-11-18 02:36:19 +0000220 case UnresolvedUsingValue:
221 case UnresolvedUsingTypename:
222 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000223
224 case Using:
225 return IDNS_Using;
226
Chris Lattner769dbdf2009-03-27 20:18:19 +0000227 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000228 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000230 case ObjCImplementation:
231 return IDNS_ObjCImplementation;
232
Fariborz Jahanian737061f2009-12-11 00:26:36 +0000233 case ObjCCategory:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000234 case ObjCCategoryImpl:
Fariborz Jahanian737061f2009-12-11 00:26:36 +0000235 return IDNS_ObjCCategoryName;
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000236
Chris Lattner769dbdf2009-03-27 20:18:19 +0000237 case Field:
238 case ObjCAtDefsField:
239 case ObjCIvar:
240 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner769dbdf2009-03-27 20:18:19 +0000242 case Record:
243 case CXXRecord:
244 case Enum:
245 case TemplateTypeParm:
246 return IDNS_Tag;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Chris Lattner769dbdf2009-03-27 20:18:19 +0000248 case Namespace:
249 case Template:
250 case FunctionTemplate:
251 case ClassTemplate:
252 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000253 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000254 return IDNS_Tag | IDNS_Ordinary;
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattner769dbdf2009-03-27 20:18:19 +0000256 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000257 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000258 case FriendTemplate:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000259 case LinkageSpec:
260 case FileScopeAsm:
261 case StaticAssert:
262 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000263 case ObjCPropertyImpl:
264 case ObjCForwardProtocol:
265 case Block:
266 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000267
Chris Lattner769dbdf2009-03-27 20:18:19 +0000268 // Aren't looked up?
269 case UsingDirective:
270 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000271 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000272 return 0;
273 }
John McCall9488ea12009-11-17 05:59:44 +0000274
275 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000276}
277
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000278void Decl::addAttr(Attr *NewAttr) {
279 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000280
281 NewAttr->setNext(ExistingAttr);
282 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Eli Friedman56d29372008-06-07 16:52:53 +0000284 HasAttrs = true;
285}
286
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000287void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000288 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Eli Friedman56d29372008-06-07 16:52:53 +0000290 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000291 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000292}
293
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000294const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000295 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000296 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000297}
298
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000299void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000300 bool HasLHSAttr = this->HasAttrs;
301 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Eli Friedman56d29372008-06-07 16:52:53 +0000303 // Usually, neither decl has attrs, nothing to do.
304 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Eli Friedman56d29372008-06-07 16:52:53 +0000306 // If 'this' has no attrs, swap the other way.
307 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000308 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000310 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Eli Friedman56d29372008-06-07 16:52:53 +0000312 // Handle the case when both decls have attrs.
313 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000314 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000315 return;
316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Eli Friedman56d29372008-06-07 16:52:53 +0000318 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000319 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
320 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000321 this->HasAttrs = false;
322 RHS->HasAttrs = true;
323}
324
325
Chris Lattnercc581472009-03-04 06:05:19 +0000326void Decl::Destroy(ASTContext &C) {
327 // Free attributes for this decl.
328 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000329 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000330 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000331 HasAttrs = false;
332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000334#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000335 // FIXME: Once ownership is fully understood, we can enable this code
336 if (DeclContext *DC = dyn_cast<DeclContext>(this))
337 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000338
Chris Lattner244a67d2009-03-28 06:04:26 +0000339 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000340 // within the loop, only the Destroy method for the first Decl
341 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Chris Lattner244a67d2009-03-28 06:04:26 +0000343 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000345 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000346 Decl* Tmp = N->getNextDeclInContext();
347 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000348 N->Destroy(C);
349 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000350 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000351
Ted Kremenek94a39002009-12-01 00:07:10 +0000352 if (isOutOfSemaDC())
353 delete (C) getMultipleDC();
354
Eli Friedman56d29372008-06-07 16:52:53 +0000355 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000356 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000357#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000358}
359
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000360Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000361 Decl::Kind DK = D->getDeclKind();
362 switch(DK) {
363#define DECL_CONTEXT(Name) \
364 case Decl::Name: \
365 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
366#define DECL_CONTEXT_BASE(Name)
367#include "clang/AST/DeclNodes.def"
368 default:
369#define DECL_CONTEXT_BASE(Name) \
370 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
371 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
372#include "clang/AST/DeclNodes.def"
373 assert(false && "a decl that inherits DeclContext isn't handled");
374 return 0;
375 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000376}
377
378DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000379 Decl::Kind DK = D->getKind();
380 switch(DK) {
381#define DECL_CONTEXT(Name) \
382 case Decl::Name: \
383 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
384#define DECL_CONTEXT_BASE(Name)
385#include "clang/AST/DeclNodes.def"
386 default:
387#define DECL_CONTEXT_BASE(Name) \
388 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
389 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
390#include "clang/AST/DeclNodes.def"
391 assert(false && "a decl that inherits DeclContext isn't handled");
392 return 0;
393 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000394}
395
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000396CompoundStmt* Decl::getCompoundBody() const {
397 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000398}
399
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000400SourceLocation Decl::getBodyRBrace() const {
401 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000402 if (!Body)
403 return SourceLocation();
404 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
405 return CS->getRBracLoc();
406 assert(isa<CXXTryStmt>(Body) &&
407 "Body can only be CompoundStmt or CXXTryStmt");
408 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
409}
410
Anders Carlsson1329c272009-03-25 23:38:06 +0000411#ifndef NDEBUG
412void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000413 // Suppress this check if any of the following hold:
414 // 1. this is the translation unit (and thus has no parent)
415 // 2. this is a template parameter (and thus doesn't belong to its context)
416 // 3. this is a ParmVarDecl (which can be in a record context during
417 // the brief period between its creation and the creation of the
418 // FunctionDecl)
419 // 4. the context is not a record
Anders Carlsson35eda442009-08-29 20:47:47 +0000420 if (isa<TranslationUnitDecl>(this) ||
John McCall46460a62010-01-20 21:53:11 +0000421 isTemplateParameter() ||
422 isa<ParmVarDecl>(this) ||
Anders Carlsson35eda442009-08-29 20:47:47 +0000423 !isa<CXXRecordDecl>(getDeclContext()))
424 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
426 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000427 "Access specifier is AS_none inside a record decl");
428}
429
430#endif
431
Eli Friedman56d29372008-06-07 16:52:53 +0000432//===----------------------------------------------------------------------===//
433// DeclContext Implementation
434//===----------------------------------------------------------------------===//
435
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000436bool DeclContext::classof(const Decl *D) {
437 switch (D->getKind()) {
438#define DECL_CONTEXT(Name) case Decl::Name:
439#define DECL_CONTEXT_BASE(Name)
440#include "clang/AST/DeclNodes.def"
441 return true;
442 default:
443#define DECL_CONTEXT_BASE(Name) \
444 if (D->getKind() >= Decl::Name##First && \
445 D->getKind() <= Decl::Name##Last) \
446 return true;
447#include "clang/AST/DeclNodes.def"
448 return false;
449 }
450}
451
Douglas Gregor44b43212008-12-11 16:49:14 +0000452DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000453 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000454}
455
456void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000457 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000458 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000459}
460
Douglas Gregore942bbe2009-09-10 16:57:35 +0000461/// \brief Find the parent context of this context that will be
462/// used for unqualified name lookup.
463///
464/// Generally, the parent lookup context is the semantic context. However, for
465/// a friend function the parent lookup context is the lexical context, which
466/// is the class in which the friend is declared.
467DeclContext *DeclContext::getLookupParent() {
468 // FIXME: Find a better way to identify friends
469 if (isa<FunctionDecl>(this))
470 if (getParent()->getLookupContext()->isFileContext() &&
471 getLexicalParent()->getLookupContext()->isRecord())
472 return getLexicalParent();
473
474 return getParent();
475}
476
Douglas Gregorbc221632009-05-28 16:34:51 +0000477bool DeclContext::isDependentContext() const {
478 if (isFileContext())
479 return false;
480
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000481 if (isa<ClassTemplatePartialSpecializationDecl>(this))
482 return true;
483
Douglas Gregorbc221632009-05-28 16:34:51 +0000484 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
485 if (Record->getDescribedClassTemplate())
486 return true;
487
488 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
489 if (Function->getDescribedFunctionTemplate())
490 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Douglas Gregorbc221632009-05-28 16:34:51 +0000492 return getParent() && getParent()->isDependentContext();
493}
494
Douglas Gregor074149e2009-01-05 19:45:36 +0000495bool DeclContext::isTransparentContext() const {
496 if (DeclKind == Decl::Enum)
497 return true; // FIXME: Check for C++0x scoped enums
498 else if (DeclKind == Decl::LinkageSpec)
499 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000500 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000501 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000502 else if (DeclKind == Decl::Namespace)
503 return false; // FIXME: Check for C++0x inline namespaces
504
505 return false;
506}
507
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000508bool DeclContext::Encloses(DeclContext *DC) {
509 if (getPrimaryContext() != this)
510 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000512 for (; DC; DC = DC->getParent())
513 if (DC->getPrimaryContext() == this)
514 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000515 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000516}
517
Steve Naroff0701bbb2009-01-08 17:28:14 +0000518DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000519 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000520 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000521 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000522 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000523 // There is only one DeclContext for these entities.
524 return this;
525
526 case Decl::Namespace:
527 // The original namespace is our primary context.
528 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
529
Douglas Gregor44b43212008-12-11 16:49:14 +0000530 case Decl::ObjCMethod:
531 return this;
532
533 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000534 case Decl::ObjCProtocol:
535 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000536 // FIXME: Can Objective-C interfaces be forward-declared?
537 return this;
538
Steve Naroff0701bbb2009-01-08 17:28:14 +0000539 case Decl::ObjCImplementation:
540 case Decl::ObjCCategoryImpl:
541 return this;
542
Douglas Gregor44b43212008-12-11 16:49:14 +0000543 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000544 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
545 // If this is a tag type that has a definition or is currently
546 // being defined, that definition is our primary context.
Ted Kremenek6217b802009-07-29 21:53:49 +0000547 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000548 if (TagT->isBeingDefined() ||
Douglas Gregorcc636682009-02-17 23:15:12 +0000549 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
550 return TagT->getDecl();
551 return this;
552 }
553
Douglas Gregor44b43212008-12-11 16:49:14 +0000554 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
555 "Unknown DeclContext kind");
556 return this;
557 }
558}
559
560DeclContext *DeclContext::getNextContext() {
561 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000562 case Decl::Namespace:
563 // Return the next namespace
564 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
565
566 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000567 return 0;
568 }
569}
570
Douglas Gregor2cf26342009-04-09 22:27:44 +0000571/// \brief Load the declarations within this lexical storage from an
572/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000573void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000574DeclContext::LoadLexicalDeclsFromExternalStorage() const {
575 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000576 assert(hasExternalLexicalStorage() && Source && "No external storage?");
577
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000578 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000579 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000580 Decls))
581 return;
582
583 // There is no longer any lexical storage in this context
584 ExternalLexicalStorage = false;
585
586 if (Decls.empty())
587 return;
588
589 // Resolve all of the declaration IDs into declarations, building up
590 // a chain of declarations via the Decl::NextDeclInContext field.
591 Decl *FirstNewDecl = 0;
592 Decl *PrevDecl = 0;
593 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
594 Decl *D = Source->GetDecl(Decls[I]);
595 if (PrevDecl)
596 PrevDecl->NextDeclInContext = D;
597 else
598 FirstNewDecl = D;
599
600 PrevDecl = D;
601 }
602
603 // Splice the newly-read declarations into the beginning of the list
604 // of declarations.
605 PrevDecl->NextDeclInContext = FirstDecl;
606 FirstDecl = FirstNewDecl;
607 if (!LastDecl)
608 LastDecl = PrevDecl;
609}
610
Mike Stump1eb44332009-09-09 15:08:12 +0000611void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000612DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000613 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000614 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000615 assert(hasExternalVisibleStorage() && Source && "No external storage?");
616
617 llvm::SmallVector<VisibleDeclaration, 64> Decls;
618 if (Source->ReadDeclsVisibleInContext(This, Decls))
619 return;
620
621 // There is no longer any visible storage in this context
622 ExternalVisibleStorage = false;
623
624 // Load the declaration IDs for all of the names visible in this
625 // context.
626 assert(!LookupPtr && "Have a lookup map before de-serialization?");
627 StoredDeclsMap *Map = new StoredDeclsMap;
628 LookupPtr = Map;
629 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
630 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
631 }
632}
633
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000634DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000635 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000636 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000637
638 // FIXME: Check whether we need to load some declarations from
639 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000640 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000641}
642
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000643DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000644 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000645 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000646
Mike Stump1eb44332009-09-09 15:08:12 +0000647 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000648}
649
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000650bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000651 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000652 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000653
654 return !FirstDecl;
655}
656
John McCall9f54ad42009-12-10 09:41:52 +0000657void DeclContext::removeDecl(Decl *D) {
658 assert(D->getLexicalDeclContext() == this &&
659 "decl being removed from non-lexical context");
660 assert((D->NextDeclInContext || D == LastDecl) &&
661 "decl is not in decls list");
662
663 // Remove D from the decl chain. This is O(n) but hopefully rare.
664 if (D == FirstDecl) {
665 if (D == LastDecl)
666 FirstDecl = LastDecl = 0;
667 else
668 FirstDecl = D->NextDeclInContext;
669 } else {
670 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
671 assert(I && "decl not found in linked list");
672 if (I->NextDeclInContext == D) {
673 I->NextDeclInContext = D->NextDeclInContext;
674 if (D == LastDecl) LastDecl = I;
675 break;
676 }
677 }
678 }
679
680 // Mark that D is no longer in the decl chain.
681 D->NextDeclInContext = 0;
682
683 // Remove D from the lookup table if necessary.
684 if (isa<NamedDecl>(D)) {
685 NamedDecl *ND = cast<NamedDecl>(D);
686
687 void *OpaqueMap = getPrimaryContext()->LookupPtr;
688 if (!OpaqueMap) return;
689
690 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(OpaqueMap);
691 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
692 assert(Pos != Map->end() && "no lookup entry for decl");
693 Pos->second.remove(ND);
694 }
695}
696
John McCall3f9a8a62009-08-11 06:59:38 +0000697void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000698 assert(D->getLexicalDeclContext() == this &&
699 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000700 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000701 "Decl already inserted into a DeclContext");
702
703 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000704 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000705 LastDecl = D;
706 } else {
707 FirstDecl = LastDecl = D;
708 }
John McCall3f9a8a62009-08-11 06:59:38 +0000709}
710
711void DeclContext::addDecl(Decl *D) {
712 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000713
714 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000715 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000716}
717
Douglas Gregor074149e2009-01-05 19:45:36 +0000718/// buildLookup - Build the lookup data structure with all of the
719/// declarations in DCtx (and any other contexts linked to it or
720/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000721void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000722 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000723 for (decl_iterator D = DCtx->decls_begin(),
724 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000725 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000726 // Insert this declaration into the lookup structure, but only
727 // if it's semantically in its decl context. During non-lazy
728 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000729 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000730 if (D->getDeclContext() == DCtx)
731 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000732
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000733 // Insert any forward-declared Objective-C interfaces into the lookup
734 // data structure.
735 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
736 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
737 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000738 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000739
Douglas Gregor074149e2009-01-05 19:45:36 +0000740 // If this declaration is itself a transparent declaration context,
741 // add its members (recursively).
742 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
743 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000744 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000745 }
746 }
747}
748
Mike Stump1eb44332009-09-09 15:08:12 +0000749DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000750DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000751 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000752 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000753 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000754
Douglas Gregor2cf26342009-04-09 22:27:44 +0000755 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000756 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000757
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000758 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000759 /// all of the linked DeclContexts (in declaration order!) and
760 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000761 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000762 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000763
Douglas Gregorc36c5402009-04-09 17:29:08 +0000764 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000765 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000766 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000767
Douglas Gregorc36c5402009-04-09 17:29:08 +0000768 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
769 StoredDeclsMap::iterator Pos = Map->find(Name);
770 if (Pos == Map->end())
771 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000772 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000773}
774
Mike Stump1eb44332009-09-09 15:08:12 +0000775DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000776DeclContext::lookup(DeclarationName Name) const {
777 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000778}
779
Chris Lattner0cf2b192009-03-27 19:19:59 +0000780DeclContext *DeclContext::getLookupContext() {
781 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000782 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000783 while (Ctx->isTransparentContext())
784 Ctx = Ctx->getParent();
785 return Ctx;
786}
787
Douglas Gregor88b70942009-02-25 22:02:03 +0000788DeclContext *DeclContext::getEnclosingNamespaceContext() {
789 DeclContext *Ctx = this;
790 // Skip through non-namespace, non-translation-unit contexts.
791 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
792 Ctx = Ctx->getParent();
793 return Ctx->getPrimaryContext();
794}
795
John McCallab88d972009-08-31 22:39:49 +0000796void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000797 // FIXME: This feels like a hack. Should DeclarationName support
798 // template-ids, or is there a better way to keep specializations
799 // from being visible?
800 if (isa<ClassTemplateSpecializationDecl>(D))
801 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000802 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
803 if (FD->isFunctionTemplateSpecialization())
804 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000805
Steve Naroff0701bbb2009-01-08 17:28:14 +0000806 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000807 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000808 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000809 return;
810 }
811
812 // If we already have a lookup data structure, perform the insertion
813 // into it. Otherwise, be lazy and don't build that structure until
814 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000815 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000817
Douglas Gregor074149e2009-01-05 19:45:36 +0000818 // If we are a transparent context, insert into our parent context,
819 // too. This operation is recursive.
820 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000821 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000822}
823
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000824void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000825 // Skip unnamed declarations.
826 if (!D->getDeclName())
827 return;
828
Douglas Gregorcc636682009-02-17 23:15:12 +0000829 // FIXME: This feels like a hack. Should DeclarationName support
830 // template-ids, or is there a better way to keep specializations
831 // from being visible?
832 if (isa<ClassTemplateSpecializationDecl>(D))
833 return;
834
Douglas Gregorc36c5402009-04-09 17:29:08 +0000835 if (!LookupPtr)
836 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000837
838 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000839 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000840 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
841 if (DeclNameEntries.isNull()) {
842 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000843 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000844 }
Chris Lattner91942502009-02-20 00:55:03 +0000845
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000846 // If it is possible that this is a redeclaration, check to see if there is
847 // already a decl for which declarationReplaces returns true. If there is
848 // one, just replace it and return.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000849 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D))
Chris Lattner67762a32009-02-20 01:44:05 +0000850 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000852 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000853 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000854}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000855
856/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
857/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000858DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000859DeclContext::getUsingDirectives() const {
860 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000861 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
862 reinterpret_cast<udir_iterator>(Result.second));
863}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000864
865void StoredDeclsList::materializeDecls(ASTContext &Context) {
866 if (isNull())
867 return;
868
869 switch ((DataKind)(Data & 0x03)) {
870 case DK_Decl:
871 case DK_Decl_Vector:
872 break;
873
874 case DK_DeclID: {
875 // Resolve this declaration ID to an actual declaration by
876 // querying the external AST source.
877 unsigned DeclID = Data >> 2;
878
879 ExternalASTSource *Source = Context.getExternalSource();
880 assert(Source && "No external AST source available!");
881
882 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
883 break;
884 }
885
886 case DK_ID_Vector: {
887 // We have a vector of declaration IDs. Resolve all of them to
888 // actual declarations.
889 VectorTy &Vector = *getAsVector();
890 ExternalASTSource *Source = Context.getExternalSource();
891 assert(Source && "No external AST source available!");
892
893 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
894 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
895
896 Data = (Data & ~0x03) | DK_Decl_Vector;
897 break;
898 }
899 }
900}