blob: 9db6ae1329e4ed28ecd7eb1aeaab7d3c6be13688 [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
Douglas Gregor42738572010-03-05 00:26:45 +000049void Decl::setInvalidDecl(bool Invalid) {
50 InvalidDecl = Invalid;
51 if (Invalid) {
52 // Defensive maneuver for ill-formed code: we're likely not to make it to
53 // a point where we set the access specifier, so default it to "public"
54 // to avoid triggering asserts elsewhere in the front end.
55 setAccess(AS_public);
56 }
57}
58
Steve Naroff0a473932009-01-20 19:53:53 +000059const char *DeclContext::getDeclKindName() const {
60 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000061 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000062#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000063#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000064 }
65}
66
Eli Friedman56d29372008-06-07 16:52:53 +000067bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000068 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000069 return StatSwitch;
70}
71
72void Decl::PrintStats() {
73 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor64650af2009-02-02 23:39:07 +000075 int totalDecls = 0;
76#define DECL(Derived, Base) totalDecls += n##Derived##s;
77#include "clang/AST/DeclNodes.def"
78 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor64650af2009-02-02 23:39:07 +000080 int totalBytes = 0;
81#define DECL(Derived, Base) \
82 if (n##Derived##s > 0) { \
83 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
84 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
85 n##Derived##s, (int)sizeof(Derived##Decl), \
86 (int)(n##Derived##s * sizeof(Derived##Decl))); \
87 }
88#include "clang/AST/DeclNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +000089
Douglas Gregor64650af2009-02-02 23:39:07 +000090 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000091}
92
93void Decl::addDeclKind(Kind k) {
94 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000095 default: assert(0 && "Declaration not in DeclNodes.def!");
96#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
97#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000098 }
99}
100
Anders Carlsson67e33202009-06-13 00:08:58 +0000101bool Decl::isTemplateParameterPack() const {
102 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
103 return TTP->isParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Anders Carlsson67e33202009-06-13 00:08:58 +0000105 return false;
106}
107
Douglas Gregore53060f2009-06-25 22:08:12 +0000108bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000109 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000110 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Douglas Gregore53060f2009-06-25 22:08:12 +0000112 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
113}
114
Douglas Gregor79c22782010-01-16 20:21:20 +0000115bool Decl::isDefinedOutsideFunctionOrMethod() const {
116 for (const DeclContext *DC = getDeclContext();
117 DC && !DC->isTranslationUnit();
118 DC = DC->getParent())
119 if (DC->isFunctionOrMethod())
120 return false;
121
122 return true;
123}
124
125
Eli Friedman56d29372008-06-07 16:52:53 +0000126//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000127// PrettyStackTraceDecl Implementation
128//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chris Lattner49f28ca2009-03-05 08:00:35 +0000130void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
131 SourceLocation TheLoc = Loc;
132 if (TheLoc.isInvalid() && TheDecl)
133 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Chris Lattner49f28ca2009-03-05 08:00:35 +0000135 if (TheLoc.isValid()) {
136 TheLoc.print(OS, SM);
137 OS << ": ";
138 }
139
140 OS << Message;
141
Daniel Dunbarc5236562009-11-21 09:05:59 +0000142 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000143 OS << " '" << DN->getQualifiedNameAsString() << '\'';
144 OS << '\n';
145}
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Chris Lattner49f28ca2009-03-05 08:00:35 +0000147//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000148// Decl Implementation
149//===----------------------------------------------------------------------===//
150
Chris Lattner769dbdf2009-03-27 20:18:19 +0000151// Out-of-line virtual method providing a home for Decl.
152Decl::~Decl() {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000153 assert(!HasAttrs && "attributes should have been freed by Destroy");
154}
155
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000156void Decl::setDeclContext(DeclContext *DC) {
157 if (isOutOfSemaDC())
158 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattneree219fd2009-03-29 06:06:59 +0000160 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000161}
162
163void Decl::setLexicalDeclContext(DeclContext *DC) {
164 if (DC == getLexicalDeclContext())
165 return;
166
167 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000168 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000169 MDC->SemanticDC = getDeclContext();
170 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000171 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000172 } else {
173 getMultipleDC()->LexicalDC = DC;
174 }
175}
176
John McCall9aeed322009-10-01 00:25:31 +0000177bool Decl::isInAnonymousNamespace() const {
178 const DeclContext *DC = getDeclContext();
179 do {
180 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
181 if (ND->isAnonymousNamespace())
182 return true;
183 } while ((DC = DC->getParent()));
184
185 return false;
186}
187
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000188TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000189 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
190 return TUD;
191
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000192 DeclContext *DC = getDeclContext();
193 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000195 while (!DC->isTranslationUnit()) {
196 DC = DC->getParent();
197 assert(DC && "This decl is not contained in a translation unit!");
198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000200 return cast<TranslationUnitDecl>(DC);
201}
202
203ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000204 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000205}
206
Tanya Lattner12ead492010-02-17 02:17:21 +0000207bool Decl::isUsed() const {
208 if (Used)
209 return true;
210
211 // Check for used attribute.
212 if (hasAttr<UsedAttr>())
213 return true;
214
215 // Check redeclarations for used attribute.
216 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
217 if (I->hasAttr<UsedAttr>() || I->Used)
218 return true;
219 }
220
221 return false;
222}
223
224
Chris Lattner769dbdf2009-03-27 20:18:19 +0000225unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
226 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000227 case Function:
228 case CXXMethod:
229 case CXXConstructor:
230 case CXXDestructor:
231 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000232 case Typedef:
233 case EnumConstant:
234 case Var:
235 case ImplicitParam:
236 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000237 case NonTypeTemplateParm:
238 case ObjCMethod:
239 case ObjCContainer:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000240 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000241 case ObjCProperty:
242 case ObjCCompatibleAlias:
243 return IDNS_Ordinary;
John McCall3f9a8a62009-08-11 06:59:38 +0000244
John McCall9488ea12009-11-17 05:59:44 +0000245 case UsingShadow:
246 return 0; // we'll actually overwrite this later
247
John McCall7ba107a2009-11-18 02:36:19 +0000248 case UnresolvedUsingValue:
249 case UnresolvedUsingTypename:
250 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000251
252 case Using:
253 return IDNS_Using;
254
Chris Lattner769dbdf2009-03-27 20:18:19 +0000255 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000256 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000258 case ObjCImplementation:
259 return IDNS_ObjCImplementation;
260
Fariborz Jahanian737061f2009-12-11 00:26:36 +0000261 case ObjCCategory:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000262 case ObjCCategoryImpl:
Fariborz Jahanian737061f2009-12-11 00:26:36 +0000263 return IDNS_ObjCCategoryName;
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000264
Chris Lattner769dbdf2009-03-27 20:18:19 +0000265 case Field:
266 case ObjCAtDefsField:
267 case ObjCIvar:
268 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattner769dbdf2009-03-27 20:18:19 +0000270 case Record:
271 case CXXRecord:
272 case Enum:
273 case TemplateTypeParm:
274 return IDNS_Tag;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner769dbdf2009-03-27 20:18:19 +0000276 case Namespace:
277 case Template:
278 case FunctionTemplate:
279 case ClassTemplate:
280 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000281 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000282 return IDNS_Tag | IDNS_Ordinary;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Chris Lattner769dbdf2009-03-27 20:18:19 +0000284 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000285 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000286 case FriendTemplate:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000287 case LinkageSpec:
288 case FileScopeAsm:
289 case StaticAssert:
290 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000291 case ObjCPropertyImpl:
292 case ObjCForwardProtocol:
293 case Block:
294 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000295
Chris Lattner769dbdf2009-03-27 20:18:19 +0000296 // Aren't looked up?
297 case UsingDirective:
298 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000299 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000300 return 0;
301 }
John McCall9488ea12009-11-17 05:59:44 +0000302
303 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000304}
305
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000306void Decl::addAttr(Attr *NewAttr) {
307 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000308
309 NewAttr->setNext(ExistingAttr);
310 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Eli Friedman56d29372008-06-07 16:52:53 +0000312 HasAttrs = true;
313}
314
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000315void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000316 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Eli Friedman56d29372008-06-07 16:52:53 +0000318 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000319 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000320}
321
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000322const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000323 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000324 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000325}
326
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000327void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000328 bool HasLHSAttr = this->HasAttrs;
329 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Eli Friedman56d29372008-06-07 16:52:53 +0000331 // Usually, neither decl has attrs, nothing to do.
332 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Eli Friedman56d29372008-06-07 16:52:53 +0000334 // If 'this' has no attrs, swap the other way.
335 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000336 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000338 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Eli Friedman56d29372008-06-07 16:52:53 +0000340 // Handle the case when both decls have attrs.
341 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000342 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000343 return;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Eli Friedman56d29372008-06-07 16:52:53 +0000346 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000347 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
348 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000349 this->HasAttrs = false;
350 RHS->HasAttrs = true;
351}
352
353
Chris Lattnercc581472009-03-04 06:05:19 +0000354void Decl::Destroy(ASTContext &C) {
355 // Free attributes for this decl.
356 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000357 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000358 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000359 HasAttrs = false;
360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000362#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000363 // FIXME: Once ownership is fully understood, we can enable this code
364 if (DeclContext *DC = dyn_cast<DeclContext>(this))
365 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000366
Chris Lattner244a67d2009-03-28 06:04:26 +0000367 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000368 // within the loop, only the Destroy method for the first Decl
369 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattner244a67d2009-03-28 06:04:26 +0000371 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000373 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000374 Decl* Tmp = N->getNextDeclInContext();
375 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000376 N->Destroy(C);
377 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000378 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000379
Ted Kremenek94a39002009-12-01 00:07:10 +0000380 if (isOutOfSemaDC())
381 delete (C) getMultipleDC();
382
Eli Friedman56d29372008-06-07 16:52:53 +0000383 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000384 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000385#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000386}
387
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000388Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000389 Decl::Kind DK = D->getDeclKind();
390 switch(DK) {
391#define DECL_CONTEXT(Name) \
392 case Decl::Name: \
393 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
394#define DECL_CONTEXT_BASE(Name)
395#include "clang/AST/DeclNodes.def"
396 default:
397#define DECL_CONTEXT_BASE(Name) \
398 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
399 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
400#include "clang/AST/DeclNodes.def"
401 assert(false && "a decl that inherits DeclContext isn't handled");
402 return 0;
403 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000404}
405
406DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000407 Decl::Kind DK = D->getKind();
408 switch(DK) {
409#define DECL_CONTEXT(Name) \
410 case Decl::Name: \
411 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
412#define DECL_CONTEXT_BASE(Name)
413#include "clang/AST/DeclNodes.def"
414 default:
415#define DECL_CONTEXT_BASE(Name) \
416 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
417 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
418#include "clang/AST/DeclNodes.def"
419 assert(false && "a decl that inherits DeclContext isn't handled");
420 return 0;
421 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000422}
423
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000424CompoundStmt* Decl::getCompoundBody() const {
425 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000426}
427
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000428SourceLocation Decl::getBodyRBrace() const {
429 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000430 if (!Body)
431 return SourceLocation();
432 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
433 return CS->getRBracLoc();
434 assert(isa<CXXTryStmt>(Body) &&
435 "Body can only be CompoundStmt or CXXTryStmt");
436 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
437}
438
Anders Carlsson1329c272009-03-25 23:38:06 +0000439#ifndef NDEBUG
440void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000441 // Suppress this check if any of the following hold:
442 // 1. this is the translation unit (and thus has no parent)
443 // 2. this is a template parameter (and thus doesn't belong to its context)
444 // 3. this is a ParmVarDecl (which can be in a record context during
445 // the brief period between its creation and the creation of the
446 // FunctionDecl)
447 // 4. the context is not a record
Anders Carlsson35eda442009-08-29 20:47:47 +0000448 if (isa<TranslationUnitDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000449 !isa<CXXRecordDecl>(getDeclContext()) ||
450 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000451 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
453 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000454 "Access specifier is AS_none inside a record decl");
455}
456
457#endif
458
Eli Friedman56d29372008-06-07 16:52:53 +0000459//===----------------------------------------------------------------------===//
460// DeclContext Implementation
461//===----------------------------------------------------------------------===//
462
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000463bool DeclContext::classof(const Decl *D) {
464 switch (D->getKind()) {
465#define DECL_CONTEXT(Name) case Decl::Name:
466#define DECL_CONTEXT_BASE(Name)
467#include "clang/AST/DeclNodes.def"
468 return true;
469 default:
470#define DECL_CONTEXT_BASE(Name) \
471 if (D->getKind() >= Decl::Name##First && \
472 D->getKind() <= Decl::Name##Last) \
473 return true;
474#include "clang/AST/DeclNodes.def"
475 return false;
476 }
477}
478
Douglas Gregor44b43212008-12-11 16:49:14 +0000479DeclContext::~DeclContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000480 // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
481 // ~DeclContext() is not guaranteed to be called when ASTContext uses
482 // a BumpPtrAllocator.
483 // delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000484}
485
486void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000487 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000488 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000489}
490
Douglas Gregore942bbe2009-09-10 16:57:35 +0000491/// \brief Find the parent context of this context that will be
492/// used for unqualified name lookup.
493///
494/// Generally, the parent lookup context is the semantic context. However, for
495/// a friend function the parent lookup context is the lexical context, which
496/// is the class in which the friend is declared.
497DeclContext *DeclContext::getLookupParent() {
498 // FIXME: Find a better way to identify friends
499 if (isa<FunctionDecl>(this))
500 if (getParent()->getLookupContext()->isFileContext() &&
501 getLexicalParent()->getLookupContext()->isRecord())
502 return getLexicalParent();
503
504 return getParent();
505}
506
Douglas Gregorbc221632009-05-28 16:34:51 +0000507bool DeclContext::isDependentContext() const {
508 if (isFileContext())
509 return false;
510
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000511 if (isa<ClassTemplatePartialSpecializationDecl>(this))
512 return true;
513
Douglas Gregorbc221632009-05-28 16:34:51 +0000514 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
515 if (Record->getDescribedClassTemplate())
516 return true;
517
518 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
519 if (Function->getDescribedFunctionTemplate())
520 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Douglas Gregorbc221632009-05-28 16:34:51 +0000522 return getParent() && getParent()->isDependentContext();
523}
524
Douglas Gregor074149e2009-01-05 19:45:36 +0000525bool DeclContext::isTransparentContext() const {
526 if (DeclKind == Decl::Enum)
527 return true; // FIXME: Check for C++0x scoped enums
528 else if (DeclKind == Decl::LinkageSpec)
529 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000530 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000531 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000532 else if (DeclKind == Decl::Namespace)
533 return false; // FIXME: Check for C++0x inline namespaces
534
535 return false;
536}
537
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000538bool DeclContext::Encloses(DeclContext *DC) {
539 if (getPrimaryContext() != this)
540 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000542 for (; DC; DC = DC->getParent())
543 if (DC->getPrimaryContext() == this)
544 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000545 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000546}
547
Steve Naroff0701bbb2009-01-08 17:28:14 +0000548DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000549 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000550 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000551 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000552 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000553 // There is only one DeclContext for these entities.
554 return this;
555
556 case Decl::Namespace:
557 // The original namespace is our primary context.
558 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
559
Douglas Gregor44b43212008-12-11 16:49:14 +0000560 case Decl::ObjCMethod:
561 return this;
562
563 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000564 case Decl::ObjCProtocol:
565 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000566 // FIXME: Can Objective-C interfaces be forward-declared?
567 return this;
568
Steve Naroff0701bbb2009-01-08 17:28:14 +0000569 case Decl::ObjCImplementation:
570 case Decl::ObjCCategoryImpl:
571 return this;
572
Douglas Gregor44b43212008-12-11 16:49:14 +0000573 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000574 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
575 // If this is a tag type that has a definition or is currently
576 // being defined, that definition is our primary context.
Ted Kremenek6217b802009-07-29 21:53:49 +0000577 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000578 if (TagT->isBeingDefined() ||
Douglas Gregorcc636682009-02-17 23:15:12 +0000579 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
580 return TagT->getDecl();
581 return this;
582 }
583
Douglas Gregor44b43212008-12-11 16:49:14 +0000584 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
585 "Unknown DeclContext kind");
586 return this;
587 }
588}
589
590DeclContext *DeclContext::getNextContext() {
591 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000592 case Decl::Namespace:
593 // Return the next namespace
594 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
595
596 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000597 return 0;
598 }
599}
600
Douglas Gregor2cf26342009-04-09 22:27:44 +0000601/// \brief Load the declarations within this lexical storage from an
602/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000603void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000604DeclContext::LoadLexicalDeclsFromExternalStorage() const {
605 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000606 assert(hasExternalLexicalStorage() && Source && "No external storage?");
607
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000608 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000609 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000610 Decls))
611 return;
612
613 // There is no longer any lexical storage in this context
614 ExternalLexicalStorage = false;
615
616 if (Decls.empty())
617 return;
618
619 // Resolve all of the declaration IDs into declarations, building up
620 // a chain of declarations via the Decl::NextDeclInContext field.
621 Decl *FirstNewDecl = 0;
622 Decl *PrevDecl = 0;
623 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
624 Decl *D = Source->GetDecl(Decls[I]);
625 if (PrevDecl)
626 PrevDecl->NextDeclInContext = D;
627 else
628 FirstNewDecl = D;
629
630 PrevDecl = D;
631 }
632
633 // Splice the newly-read declarations into the beginning of the list
634 // of declarations.
635 PrevDecl->NextDeclInContext = FirstDecl;
636 FirstDecl = FirstNewDecl;
637 if (!LastDecl)
638 LastDecl = PrevDecl;
639}
640
Mike Stump1eb44332009-09-09 15:08:12 +0000641void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000642DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000643 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000644 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000645 assert(hasExternalVisibleStorage() && Source && "No external storage?");
646
647 llvm::SmallVector<VisibleDeclaration, 64> Decls;
648 if (Source->ReadDeclsVisibleInContext(This, Decls))
649 return;
650
651 // There is no longer any visible storage in this context
652 ExternalVisibleStorage = false;
653
654 // Load the declaration IDs for all of the names visible in this
655 // context.
656 assert(!LookupPtr && "Have a lookup map before de-serialization?");
Ted Kremenek3478eb62010-02-11 07:12:28 +0000657 StoredDeclsMap *Map =
658 (StoredDeclsMap*) getParentASTContext().CreateStoredDeclsMap();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000659 LookupPtr = Map;
660 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
661 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
662 }
663}
664
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000665DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000666 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000667 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000668
669 // FIXME: Check whether we need to load some declarations from
670 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000671 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000672}
673
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000674DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000675 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000676 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000677
Mike Stump1eb44332009-09-09 15:08:12 +0000678 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000679}
680
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000681bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000682 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000683 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000684
685 return !FirstDecl;
686}
687
John McCall9f54ad42009-12-10 09:41:52 +0000688void DeclContext::removeDecl(Decl *D) {
689 assert(D->getLexicalDeclContext() == this &&
690 "decl being removed from non-lexical context");
691 assert((D->NextDeclInContext || D == LastDecl) &&
692 "decl is not in decls list");
693
694 // Remove D from the decl chain. This is O(n) but hopefully rare.
695 if (D == FirstDecl) {
696 if (D == LastDecl)
697 FirstDecl = LastDecl = 0;
698 else
699 FirstDecl = D->NextDeclInContext;
700 } else {
701 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
702 assert(I && "decl not found in linked list");
703 if (I->NextDeclInContext == D) {
704 I->NextDeclInContext = D->NextDeclInContext;
705 if (D == LastDecl) LastDecl = I;
706 break;
707 }
708 }
709 }
710
711 // Mark that D is no longer in the decl chain.
712 D->NextDeclInContext = 0;
713
714 // Remove D from the lookup table if necessary.
715 if (isa<NamedDecl>(D)) {
716 NamedDecl *ND = cast<NamedDecl>(D);
717
718 void *OpaqueMap = getPrimaryContext()->LookupPtr;
719 if (!OpaqueMap) return;
720
721 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(OpaqueMap);
722 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
723 assert(Pos != Map->end() && "no lookup entry for decl");
724 Pos->second.remove(ND);
725 }
726}
727
John McCall3f9a8a62009-08-11 06:59:38 +0000728void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000729 assert(D->getLexicalDeclContext() == this &&
730 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000731 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000732 "Decl already inserted into a DeclContext");
733
734 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000735 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000736 LastDecl = D;
737 } else {
738 FirstDecl = LastDecl = D;
739 }
John McCall3f9a8a62009-08-11 06:59:38 +0000740}
741
742void DeclContext::addDecl(Decl *D) {
743 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000744
745 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000746 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000747}
748
Douglas Gregor074149e2009-01-05 19:45:36 +0000749/// buildLookup - Build the lookup data structure with all of the
750/// declarations in DCtx (and any other contexts linked to it or
751/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000752void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000753 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000754 for (decl_iterator D = DCtx->decls_begin(),
755 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000756 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000757 // Insert this declaration into the lookup structure, but only
758 // if it's semantically in its decl context. During non-lazy
759 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000760 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000761 if (D->getDeclContext() == DCtx)
762 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000763
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000764 // Insert any forward-declared Objective-C interfaces into the lookup
765 // data structure.
766 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
767 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
768 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000769 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000770
Douglas Gregor074149e2009-01-05 19:45:36 +0000771 // If this declaration is itself a transparent declaration context,
772 // add its members (recursively).
773 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
774 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000775 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000776 }
777 }
778}
779
Mike Stump1eb44332009-09-09 15:08:12 +0000780DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000781DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000782 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000783 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000784 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000785
Douglas Gregor2cf26342009-04-09 22:27:44 +0000786 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000787 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000788
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000789 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000790 /// all of the linked DeclContexts (in declaration order!) and
791 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000792 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000793 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000794
Douglas Gregorc36c5402009-04-09 17:29:08 +0000795 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000796 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000797 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000798
Douglas Gregorc36c5402009-04-09 17:29:08 +0000799 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
800 StoredDeclsMap::iterator Pos = Map->find(Name);
801 if (Pos == Map->end())
802 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000803 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000804}
805
Mike Stump1eb44332009-09-09 15:08:12 +0000806DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000807DeclContext::lookup(DeclarationName Name) const {
808 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000809}
810
Chris Lattner0cf2b192009-03-27 19:19:59 +0000811DeclContext *DeclContext::getLookupContext() {
812 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000813 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000814 while (Ctx->isTransparentContext())
815 Ctx = Ctx->getParent();
816 return Ctx;
817}
818
Douglas Gregor88b70942009-02-25 22:02:03 +0000819DeclContext *DeclContext::getEnclosingNamespaceContext() {
820 DeclContext *Ctx = this;
821 // Skip through non-namespace, non-translation-unit contexts.
822 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
823 Ctx = Ctx->getParent();
824 return Ctx->getPrimaryContext();
825}
826
John McCallab88d972009-08-31 22:39:49 +0000827void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000828 // FIXME: This feels like a hack. Should DeclarationName support
829 // template-ids, or is there a better way to keep specializations
830 // from being visible?
831 if (isa<ClassTemplateSpecializationDecl>(D))
832 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000833 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
834 if (FD->isFunctionTemplateSpecialization())
835 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000836
Steve Naroff0701bbb2009-01-08 17:28:14 +0000837 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000838 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000839 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000840 return;
841 }
842
843 // If we already have a lookup data structure, perform the insertion
844 // into it. Otherwise, be lazy and don't build that structure until
845 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000846 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000847 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000848
Douglas Gregor074149e2009-01-05 19:45:36 +0000849 // If we are a transparent context, insert into our parent context,
850 // too. This operation is recursive.
851 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000852 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000853}
854
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000855void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000856 // Skip unnamed declarations.
857 if (!D->getDeclName())
858 return;
859
Douglas Gregorcc636682009-02-17 23:15:12 +0000860 // FIXME: This feels like a hack. Should DeclarationName support
861 // template-ids, or is there a better way to keep specializations
862 // from being visible?
863 if (isa<ClassTemplateSpecializationDecl>(D))
864 return;
865
Ted Kremenek3478eb62010-02-11 07:12:28 +0000866 ASTContext *C = 0;
867 if (!LookupPtr) {
868 C = &getParentASTContext();
869 LookupPtr = (StoredDeclsMap*) C->CreateStoredDeclsMap();
870 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000871
872 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000873 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000874 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
875 if (DeclNameEntries.isNull()) {
876 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000877 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000878 }
Chris Lattner91942502009-02-20 00:55:03 +0000879
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000880 // If it is possible that this is a redeclaration, check to see if there is
881 // already a decl for which declarationReplaces returns true. If there is
882 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +0000883 if (!C)
884 C = &getParentASTContext();
885
886 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000887 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000889 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000890 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000891}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000892
893/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
894/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000895DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000896DeclContext::getUsingDirectives() const {
897 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000898 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
899 reinterpret_cast<udir_iterator>(Result.second));
900}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000901
902void StoredDeclsList::materializeDecls(ASTContext &Context) {
903 if (isNull())
904 return;
905
906 switch ((DataKind)(Data & 0x03)) {
907 case DK_Decl:
908 case DK_Decl_Vector:
909 break;
910
911 case DK_DeclID: {
912 // Resolve this declaration ID to an actual declaration by
913 // querying the external AST source.
914 unsigned DeclID = Data >> 2;
915
916 ExternalASTSource *Source = Context.getExternalSource();
917 assert(Source && "No external AST source available!");
918
919 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
920 break;
921 }
922
923 case DK_ID_Vector: {
924 // We have a vector of declaration IDs. Resolve all of them to
925 // actual declarations.
926 VectorTy &Vector = *getAsVector();
927 ExternalASTSource *Source = Context.getExternalSource();
928 assert(Source && "No external AST source available!");
929
930 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
931 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
932
933 Data = (Data & ~0x03) | DK_Decl_Vector;
934 break;
935 }
936 }
937}
Ted Kremenek3478eb62010-02-11 07:12:28 +0000938
939//===----------------------------------------------------------------------===//
940// Creation and Destruction of StoredDeclsMaps. //
941//===----------------------------------------------------------------------===//
942
943void *ASTContext::CreateStoredDeclsMap() {
944 StoredDeclsMap *M = new StoredDeclsMap();
945 SDMs.push_back(M);
946 return M;
947}
948
949void ASTContext::ReleaseDeclContextMaps() {
950 for (std::vector<void*>::iterator I = SDMs.begin(), E = SDMs.end(); I!=E; ++I)
951 delete (StoredDeclsMap*) *I;
952}