blob: a9495343e8872cb6f8df26ea27da79fbbc59b497 [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.
John McCall3cb0ebd2010-03-10 03:28:59 +0000577 TagDecl *Tag = cast<TagDecl>(this);
578 assert(isa<TagType>(Tag->TypeForDecl) ||
579 isa<InjectedClassNameType>(Tag->TypeForDecl));
580
581 if (TagDecl *Def = Tag->getDefinition())
582 return Def;
583
584 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
585 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
586 if (TagTy->isBeingDefined())
587 // FIXME: is it necessarily being defined in the decl
588 // that owns the type?
589 return TagTy->getDecl();
590 }
591
592 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000593 }
594
Douglas Gregor44b43212008-12-11 16:49:14 +0000595 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
596 "Unknown DeclContext kind");
597 return this;
598 }
599}
600
601DeclContext *DeclContext::getNextContext() {
602 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000603 case Decl::Namespace:
604 // Return the next namespace
605 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
606
607 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000608 return 0;
609 }
610}
611
Douglas Gregor2cf26342009-04-09 22:27:44 +0000612/// \brief Load the declarations within this lexical storage from an
613/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000614void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000615DeclContext::LoadLexicalDeclsFromExternalStorage() const {
616 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000617 assert(hasExternalLexicalStorage() && Source && "No external storage?");
618
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000619 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000620 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000621 Decls))
622 return;
623
624 // There is no longer any lexical storage in this context
625 ExternalLexicalStorage = false;
626
627 if (Decls.empty())
628 return;
629
630 // Resolve all of the declaration IDs into declarations, building up
631 // a chain of declarations via the Decl::NextDeclInContext field.
632 Decl *FirstNewDecl = 0;
633 Decl *PrevDecl = 0;
634 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
635 Decl *D = Source->GetDecl(Decls[I]);
636 if (PrevDecl)
637 PrevDecl->NextDeclInContext = D;
638 else
639 FirstNewDecl = D;
640
641 PrevDecl = D;
642 }
643
644 // Splice the newly-read declarations into the beginning of the list
645 // of declarations.
646 PrevDecl->NextDeclInContext = FirstDecl;
647 FirstDecl = FirstNewDecl;
648 if (!LastDecl)
649 LastDecl = PrevDecl;
650}
651
Mike Stump1eb44332009-09-09 15:08:12 +0000652void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000653DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000654 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000655 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000656 assert(hasExternalVisibleStorage() && Source && "No external storage?");
657
658 llvm::SmallVector<VisibleDeclaration, 64> Decls;
659 if (Source->ReadDeclsVisibleInContext(This, Decls))
660 return;
661
662 // There is no longer any visible storage in this context
663 ExternalVisibleStorage = false;
664
665 // Load the declaration IDs for all of the names visible in this
666 // context.
667 assert(!LookupPtr && "Have a lookup map before de-serialization?");
Ted Kremenek3478eb62010-02-11 07:12:28 +0000668 StoredDeclsMap *Map =
669 (StoredDeclsMap*) getParentASTContext().CreateStoredDeclsMap();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000670 LookupPtr = Map;
671 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
672 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
673 }
674}
675
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000676DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000677 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000678 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000679
680 // FIXME: Check whether we need to load some declarations from
681 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000682 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000683}
684
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000685DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000686 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000687 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000688
Mike Stump1eb44332009-09-09 15:08:12 +0000689 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000690}
691
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000692bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000693 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000694 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000695
696 return !FirstDecl;
697}
698
John McCall9f54ad42009-12-10 09:41:52 +0000699void DeclContext::removeDecl(Decl *D) {
700 assert(D->getLexicalDeclContext() == this &&
701 "decl being removed from non-lexical context");
702 assert((D->NextDeclInContext || D == LastDecl) &&
703 "decl is not in decls list");
704
705 // Remove D from the decl chain. This is O(n) but hopefully rare.
706 if (D == FirstDecl) {
707 if (D == LastDecl)
708 FirstDecl = LastDecl = 0;
709 else
710 FirstDecl = D->NextDeclInContext;
711 } else {
712 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
713 assert(I && "decl not found in linked list");
714 if (I->NextDeclInContext == D) {
715 I->NextDeclInContext = D->NextDeclInContext;
716 if (D == LastDecl) LastDecl = I;
717 break;
718 }
719 }
720 }
721
722 // Mark that D is no longer in the decl chain.
723 D->NextDeclInContext = 0;
724
725 // Remove D from the lookup table if necessary.
726 if (isa<NamedDecl>(D)) {
727 NamedDecl *ND = cast<NamedDecl>(D);
728
729 void *OpaqueMap = getPrimaryContext()->LookupPtr;
730 if (!OpaqueMap) return;
731
732 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(OpaqueMap);
733 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
734 assert(Pos != Map->end() && "no lookup entry for decl");
735 Pos->second.remove(ND);
736 }
737}
738
John McCall3f9a8a62009-08-11 06:59:38 +0000739void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000740 assert(D->getLexicalDeclContext() == this &&
741 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000742 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000743 "Decl already inserted into a DeclContext");
744
745 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000746 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000747 LastDecl = D;
748 } else {
749 FirstDecl = LastDecl = D;
750 }
John McCall3f9a8a62009-08-11 06:59:38 +0000751}
752
753void DeclContext::addDecl(Decl *D) {
754 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000755
756 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000757 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000758}
759
Douglas Gregor074149e2009-01-05 19:45:36 +0000760/// buildLookup - Build the lookup data structure with all of the
761/// declarations in DCtx (and any other contexts linked to it or
762/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000763void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000764 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000765 for (decl_iterator D = DCtx->decls_begin(),
766 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000767 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000768 // Insert this declaration into the lookup structure, but only
769 // if it's semantically in its decl context. During non-lazy
770 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000771 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000772 if (D->getDeclContext() == DCtx)
773 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000774
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000775 // Insert any forward-declared Objective-C interfaces into the lookup
776 // data structure.
777 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
778 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
779 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000780 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000781
Douglas Gregor074149e2009-01-05 19:45:36 +0000782 // If this declaration is itself a transparent declaration context,
783 // add its members (recursively).
784 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
785 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000786 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000787 }
788 }
789}
790
Mike Stump1eb44332009-09-09 15:08:12 +0000791DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000792DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000793 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000794 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000795 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000796
Douglas Gregor2cf26342009-04-09 22:27:44 +0000797 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000798 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000799
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000800 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000801 /// all of the linked DeclContexts (in declaration order!) and
802 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000803 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000804 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000805
Douglas Gregorc36c5402009-04-09 17:29:08 +0000806 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000807 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000808 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000809
Douglas Gregorc36c5402009-04-09 17:29:08 +0000810 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
811 StoredDeclsMap::iterator Pos = Map->find(Name);
812 if (Pos == Map->end())
813 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000814 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000815}
816
Mike Stump1eb44332009-09-09 15:08:12 +0000817DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000818DeclContext::lookup(DeclarationName Name) const {
819 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000820}
821
Chris Lattner0cf2b192009-03-27 19:19:59 +0000822DeclContext *DeclContext::getLookupContext() {
823 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000824 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000825 while (Ctx->isTransparentContext())
826 Ctx = Ctx->getParent();
827 return Ctx;
828}
829
Douglas Gregor88b70942009-02-25 22:02:03 +0000830DeclContext *DeclContext::getEnclosingNamespaceContext() {
831 DeclContext *Ctx = this;
832 // Skip through non-namespace, non-translation-unit contexts.
833 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
834 Ctx = Ctx->getParent();
835 return Ctx->getPrimaryContext();
836}
837
John McCallab88d972009-08-31 22:39:49 +0000838void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000839 // FIXME: This feels like a hack. Should DeclarationName support
840 // template-ids, or is there a better way to keep specializations
841 // from being visible?
842 if (isa<ClassTemplateSpecializationDecl>(D))
843 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000844 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
845 if (FD->isFunctionTemplateSpecialization())
846 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000847
Steve Naroff0701bbb2009-01-08 17:28:14 +0000848 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000849 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000850 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000851 return;
852 }
853
854 // If we already have a lookup data structure, perform the insertion
855 // into it. Otherwise, be lazy and don't build that structure until
856 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000857 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000858 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000859
Douglas Gregor074149e2009-01-05 19:45:36 +0000860 // If we are a transparent context, insert into our parent context,
861 // too. This operation is recursive.
862 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000863 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000864}
865
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000866void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000867 // Skip unnamed declarations.
868 if (!D->getDeclName())
869 return;
870
Douglas Gregorcc636682009-02-17 23:15:12 +0000871 // FIXME: This feels like a hack. Should DeclarationName support
872 // template-ids, or is there a better way to keep specializations
873 // from being visible?
874 if (isa<ClassTemplateSpecializationDecl>(D))
875 return;
876
Ted Kremenek3478eb62010-02-11 07:12:28 +0000877 ASTContext *C = 0;
878 if (!LookupPtr) {
879 C = &getParentASTContext();
880 LookupPtr = (StoredDeclsMap*) C->CreateStoredDeclsMap();
881 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000882
883 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000884 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000885 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
886 if (DeclNameEntries.isNull()) {
887 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000888 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000889 }
Chris Lattner91942502009-02-20 00:55:03 +0000890
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000891 // If it is possible that this is a redeclaration, check to see if there is
892 // already a decl for which declarationReplaces returns true. If there is
893 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +0000894 if (!C)
895 C = &getParentASTContext();
896
897 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000898 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000900 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000901 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000902}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000903
904/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
905/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000906DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000907DeclContext::getUsingDirectives() const {
908 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000909 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
910 reinterpret_cast<udir_iterator>(Result.second));
911}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000912
913void StoredDeclsList::materializeDecls(ASTContext &Context) {
914 if (isNull())
915 return;
916
917 switch ((DataKind)(Data & 0x03)) {
918 case DK_Decl:
919 case DK_Decl_Vector:
920 break;
921
922 case DK_DeclID: {
923 // Resolve this declaration ID to an actual declaration by
924 // querying the external AST source.
925 unsigned DeclID = Data >> 2;
926
927 ExternalASTSource *Source = Context.getExternalSource();
928 assert(Source && "No external AST source available!");
929
930 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
931 break;
932 }
933
934 case DK_ID_Vector: {
935 // We have a vector of declaration IDs. Resolve all of them to
936 // actual declarations.
937 VectorTy &Vector = *getAsVector();
938 ExternalASTSource *Source = Context.getExternalSource();
939 assert(Source && "No external AST source available!");
940
941 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
942 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
943
944 Data = (Data & ~0x03) | DK_Decl_Vector;
945 break;
946 }
947 }
948}
Ted Kremenek3478eb62010-02-11 07:12:28 +0000949
950//===----------------------------------------------------------------------===//
951// Creation and Destruction of StoredDeclsMaps. //
952//===----------------------------------------------------------------------===//
953
954void *ASTContext::CreateStoredDeclsMap() {
955 StoredDeclsMap *M = new StoredDeclsMap();
956 SDMs.push_back(M);
957 return M;
958}
959
960void ASTContext::ReleaseDeclContextMaps() {
961 for (std::vector<void*>::iterator I = SDMs.begin(), E = SDMs.end(); I!=E; ++I)
962 delete (StoredDeclsMap*) *I;
963}