blob: 1aac7cfd598a6c29b82181f3970329a255e3967a [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"
John McCall92b7f702010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000022#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000023#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000024#include "clang/AST/Stmt.h"
25#include "clang/AST/StmtCXX.h"
Eli Friedman56d29372008-06-07 16:52:53 +000026#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000027#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000028#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000029#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000030#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000031using namespace clang;
32
33//===----------------------------------------------------------------------===//
34// Statistics
35//===----------------------------------------------------------------------===//
36
Douglas Gregor64650af2009-02-02 23:39:07 +000037#define DECL(Derived, Base) static int n##Derived##s = 0;
38#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000039
40static bool StatSwitch = false;
41
Eli Friedman56d29372008-06-07 16:52:53 +000042const char *Decl::getDeclKindName() const {
43 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000044 default: assert(0 && "Declaration not in DeclNodes.def!");
45#define DECL(Derived, Base) case Derived: return #Derived;
46#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000047 }
48}
49
Douglas Gregor42738572010-03-05 00:26:45 +000050void Decl::setInvalidDecl(bool Invalid) {
51 InvalidDecl = Invalid;
52 if (Invalid) {
53 // Defensive maneuver for ill-formed code: we're likely not to make it to
54 // a point where we set the access specifier, so default it to "public"
55 // to avoid triggering asserts elsewhere in the front end.
56 setAccess(AS_public);
57 }
58}
59
Steve Naroff0a473932009-01-20 19:53:53 +000060const char *DeclContext::getDeclKindName() const {
61 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000062 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000063#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000064#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000065 }
66}
67
Eli Friedman56d29372008-06-07 16:52:53 +000068bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000069 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000070 return StatSwitch;
71}
72
73void Decl::PrintStats() {
74 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor64650af2009-02-02 23:39:07 +000076 int totalDecls = 0;
77#define DECL(Derived, Base) totalDecls += n##Derived##s;
78#include "clang/AST/DeclNodes.def"
79 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Douglas Gregor64650af2009-02-02 23:39:07 +000081 int totalBytes = 0;
82#define DECL(Derived, Base) \
83 if (n##Derived##s > 0) { \
84 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
85 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
86 n##Derived##s, (int)sizeof(Derived##Decl), \
87 (int)(n##Derived##s * sizeof(Derived##Decl))); \
88 }
89#include "clang/AST/DeclNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor64650af2009-02-02 23:39:07 +000091 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000092}
93
94void Decl::addDeclKind(Kind k) {
95 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000096 default: assert(0 && "Declaration not in DeclNodes.def!");
97#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
98#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000099 }
100}
101
Anders Carlsson67e33202009-06-13 00:08:58 +0000102bool Decl::isTemplateParameterPack() const {
103 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
104 return TTP->isParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Anders Carlsson67e33202009-06-13 00:08:58 +0000106 return false;
107}
108
Douglas Gregore53060f2009-06-25 22:08:12 +0000109bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000110 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000111 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Douglas Gregore53060f2009-06-25 22:08:12 +0000113 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
114}
115
Douglas Gregor79c22782010-01-16 20:21:20 +0000116bool Decl::isDefinedOutsideFunctionOrMethod() const {
117 for (const DeclContext *DC = getDeclContext();
118 DC && !DC->isTranslationUnit();
119 DC = DC->getParent())
120 if (DC->isFunctionOrMethod())
121 return false;
122
123 return true;
124}
125
126
Eli Friedman56d29372008-06-07 16:52:53 +0000127//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000128// PrettyStackTraceDecl Implementation
129//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Chris Lattner49f28ca2009-03-05 08:00:35 +0000131void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
132 SourceLocation TheLoc = Loc;
133 if (TheLoc.isInvalid() && TheDecl)
134 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Chris Lattner49f28ca2009-03-05 08:00:35 +0000136 if (TheLoc.isValid()) {
137 TheLoc.print(OS, SM);
138 OS << ": ";
139 }
140
141 OS << Message;
142
Daniel Dunbarc5236562009-11-21 09:05:59 +0000143 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000144 OS << " '" << DN->getQualifiedNameAsString() << '\'';
145 OS << '\n';
146}
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattner49f28ca2009-03-05 08:00:35 +0000148//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000149// Decl Implementation
150//===----------------------------------------------------------------------===//
151
Chris Lattner769dbdf2009-03-27 20:18:19 +0000152// Out-of-line virtual method providing a home for Decl.
153Decl::~Decl() {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000154 assert(!HasAttrs && "attributes should have been freed by Destroy");
155}
156
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000157void Decl::setDeclContext(DeclContext *DC) {
158 if (isOutOfSemaDC())
159 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Chris Lattneree219fd2009-03-29 06:06:59 +0000161 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000162}
163
164void Decl::setLexicalDeclContext(DeclContext *DC) {
165 if (DC == getLexicalDeclContext())
166 return;
167
168 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000169 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000170 MDC->SemanticDC = getDeclContext();
171 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000172 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000173 } else {
174 getMultipleDC()->LexicalDC = DC;
175 }
176}
177
John McCall9aeed322009-10-01 00:25:31 +0000178bool Decl::isInAnonymousNamespace() const {
179 const DeclContext *DC = getDeclContext();
180 do {
181 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
182 if (ND->isAnonymousNamespace())
183 return true;
184 } while ((DC = DC->getParent()));
185
186 return false;
187}
188
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000189TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000190 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
191 return TUD;
192
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000193 DeclContext *DC = getDeclContext();
194 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000196 while (!DC->isTranslationUnit()) {
197 DC = DC->getParent();
198 assert(DC && "This decl is not contained in a translation unit!");
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000201 return cast<TranslationUnitDecl>(DC);
202}
203
204ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000205 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000206}
207
Tanya Lattner12ead492010-02-17 02:17:21 +0000208bool Decl::isUsed() const {
209 if (Used)
210 return true;
211
212 // Check for used attribute.
213 if (hasAttr<UsedAttr>())
214 return true;
215
216 // Check redeclarations for used attribute.
217 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
218 if (I->hasAttr<UsedAttr>() || I->Used)
219 return true;
220 }
221
222 return false;
223}
224
225
Chris Lattner769dbdf2009-03-27 20:18:19 +0000226unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
227 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000228 case Function:
229 case CXXMethod:
230 case CXXConstructor:
231 case CXXDestructor:
232 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000233 case Typedef:
234 case EnumConstant:
235 case Var:
236 case ImplicitParam:
237 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000238 case NonTypeTemplateParm:
239 case ObjCMethod:
240 case ObjCContainer:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000241 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000242 case ObjCProperty:
243 case ObjCCompatibleAlias:
244 return IDNS_Ordinary;
John McCall3f9a8a62009-08-11 06:59:38 +0000245
John McCall9488ea12009-11-17 05:59:44 +0000246 case UsingShadow:
247 return 0; // we'll actually overwrite this later
248
John McCall7ba107a2009-11-18 02:36:19 +0000249 case UnresolvedUsingValue:
250 case UnresolvedUsingTypename:
251 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000252
253 case Using:
254 return IDNS_Using;
255
Chris Lattner769dbdf2009-03-27 20:18:19 +0000256 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000257 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000259 case ObjCImplementation:
260 return IDNS_ObjCImplementation;
261
Fariborz Jahanian737061f2009-12-11 00:26:36 +0000262 case ObjCCategory:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000263 case ObjCCategoryImpl:
Fariborz Jahanian737061f2009-12-11 00:26:36 +0000264 return IDNS_ObjCCategoryName;
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000265
Chris Lattner769dbdf2009-03-27 20:18:19 +0000266 case Field:
267 case ObjCAtDefsField:
268 case ObjCIvar:
269 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner769dbdf2009-03-27 20:18:19 +0000271 case Record:
272 case CXXRecord:
273 case Enum:
274 case TemplateTypeParm:
275 return IDNS_Tag;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner769dbdf2009-03-27 20:18:19 +0000277 case Namespace:
278 case Template:
279 case FunctionTemplate:
280 case ClassTemplate:
281 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000282 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000283 return IDNS_Tag | IDNS_Ordinary;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattner769dbdf2009-03-27 20:18:19 +0000285 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000286 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000287 case FriendTemplate:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000288 case LinkageSpec:
289 case FileScopeAsm:
290 case StaticAssert:
291 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000292 case ObjCPropertyImpl:
293 case ObjCForwardProtocol:
294 case Block:
295 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000296
Chris Lattner769dbdf2009-03-27 20:18:19 +0000297 // Aren't looked up?
298 case UsingDirective:
299 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000300 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000301 return 0;
302 }
John McCall9488ea12009-11-17 05:59:44 +0000303
304 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000305}
306
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000307void Decl::addAttr(Attr *NewAttr) {
308 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000309
310 NewAttr->setNext(ExistingAttr);
311 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Eli Friedman56d29372008-06-07 16:52:53 +0000313 HasAttrs = true;
314}
315
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000316void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000317 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Eli Friedman56d29372008-06-07 16:52:53 +0000319 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000320 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000321}
322
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000323const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000324 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000325 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000326}
327
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000328void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000329 bool HasLHSAttr = this->HasAttrs;
330 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Eli Friedman56d29372008-06-07 16:52:53 +0000332 // Usually, neither decl has attrs, nothing to do.
333 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Eli Friedman56d29372008-06-07 16:52:53 +0000335 // If 'this' has no attrs, swap the other way.
336 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000337 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000339 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Eli Friedman56d29372008-06-07 16:52:53 +0000341 // Handle the case when both decls have attrs.
342 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000343 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000344 return;
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Eli Friedman56d29372008-06-07 16:52:53 +0000347 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000348 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
349 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000350 this->HasAttrs = false;
351 RHS->HasAttrs = true;
352}
353
354
Chris Lattnercc581472009-03-04 06:05:19 +0000355void Decl::Destroy(ASTContext &C) {
356 // Free attributes for this decl.
357 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000358 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000359 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000360 HasAttrs = false;
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000363#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000364 // FIXME: Once ownership is fully understood, we can enable this code
365 if (DeclContext *DC = dyn_cast<DeclContext>(this))
366 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000367
Chris Lattner244a67d2009-03-28 06:04:26 +0000368 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000369 // within the loop, only the Destroy method for the first Decl
370 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner244a67d2009-03-28 06:04:26 +0000372 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000374 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000375 Decl* Tmp = N->getNextDeclInContext();
376 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000377 N->Destroy(C);
378 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000379 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000380
Ted Kremenek94a39002009-12-01 00:07:10 +0000381 if (isOutOfSemaDC())
382 delete (C) getMultipleDC();
383
Eli Friedman56d29372008-06-07 16:52:53 +0000384 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000385 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000386#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000387}
388
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000389Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000390 Decl::Kind DK = D->getDeclKind();
391 switch(DK) {
392#define DECL_CONTEXT(Name) \
393 case Decl::Name: \
394 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
395#define DECL_CONTEXT_BASE(Name)
396#include "clang/AST/DeclNodes.def"
397 default:
398#define DECL_CONTEXT_BASE(Name) \
399 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
400 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
401#include "clang/AST/DeclNodes.def"
402 assert(false && "a decl that inherits DeclContext isn't handled");
403 return 0;
404 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000405}
406
407DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000408 Decl::Kind DK = D->getKind();
409 switch(DK) {
410#define DECL_CONTEXT(Name) \
411 case Decl::Name: \
412 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
413#define DECL_CONTEXT_BASE(Name)
414#include "clang/AST/DeclNodes.def"
415 default:
416#define DECL_CONTEXT_BASE(Name) \
417 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
418 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
419#include "clang/AST/DeclNodes.def"
420 assert(false && "a decl that inherits DeclContext isn't handled");
421 return 0;
422 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000423}
424
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000425CompoundStmt* Decl::getCompoundBody() const {
426 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000427}
428
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000429SourceLocation Decl::getBodyRBrace() const {
430 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000431 if (!Body)
432 return SourceLocation();
433 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
434 return CS->getRBracLoc();
435 assert(isa<CXXTryStmt>(Body) &&
436 "Body can only be CompoundStmt or CXXTryStmt");
437 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
438}
439
Anders Carlsson1329c272009-03-25 23:38:06 +0000440#ifndef NDEBUG
441void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000442 // Suppress this check if any of the following hold:
443 // 1. this is the translation unit (and thus has no parent)
444 // 2. this is a template parameter (and thus doesn't belong to its context)
445 // 3. this is a ParmVarDecl (which can be in a record context during
446 // the brief period between its creation and the creation of the
447 // FunctionDecl)
448 // 4. the context is not a record
Anders Carlsson35eda442009-08-29 20:47:47 +0000449 if (isa<TranslationUnitDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000450 !isa<CXXRecordDecl>(getDeclContext()) ||
451 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000452 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000453
454 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000455 "Access specifier is AS_none inside a record decl");
456}
457
458#endif
459
Eli Friedman56d29372008-06-07 16:52:53 +0000460//===----------------------------------------------------------------------===//
461// DeclContext Implementation
462//===----------------------------------------------------------------------===//
463
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000464bool DeclContext::classof(const Decl *D) {
465 switch (D->getKind()) {
466#define DECL_CONTEXT(Name) case Decl::Name:
467#define DECL_CONTEXT_BASE(Name)
468#include "clang/AST/DeclNodes.def"
469 return true;
470 default:
471#define DECL_CONTEXT_BASE(Name) \
472 if (D->getKind() >= Decl::Name##First && \
473 D->getKind() <= Decl::Name##Last) \
474 return true;
475#include "clang/AST/DeclNodes.def"
476 return false;
477 }
478}
479
Douglas Gregor44b43212008-12-11 16:49:14 +0000480DeclContext::~DeclContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000481 // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
482 // ~DeclContext() is not guaranteed to be called when ASTContext uses
483 // a BumpPtrAllocator.
484 // delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000485}
486
487void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000488 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000489 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000490}
491
Douglas Gregore942bbe2009-09-10 16:57:35 +0000492/// \brief Find the parent context of this context that will be
493/// used for unqualified name lookup.
494///
495/// Generally, the parent lookup context is the semantic context. However, for
496/// a friend function the parent lookup context is the lexical context, which
497/// is the class in which the friend is declared.
498DeclContext *DeclContext::getLookupParent() {
499 // FIXME: Find a better way to identify friends
500 if (isa<FunctionDecl>(this))
501 if (getParent()->getLookupContext()->isFileContext() &&
502 getLexicalParent()->getLookupContext()->isRecord())
503 return getLexicalParent();
504
505 return getParent();
506}
507
Douglas Gregorbc221632009-05-28 16:34:51 +0000508bool DeclContext::isDependentContext() const {
509 if (isFileContext())
510 return false;
511
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000512 if (isa<ClassTemplatePartialSpecializationDecl>(this))
513 return true;
514
Douglas Gregorbc221632009-05-28 16:34:51 +0000515 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
516 if (Record->getDescribedClassTemplate())
517 return true;
518
519 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
520 if (Function->getDescribedFunctionTemplate())
521 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Douglas Gregorbc221632009-05-28 16:34:51 +0000523 return getParent() && getParent()->isDependentContext();
524}
525
Douglas Gregor074149e2009-01-05 19:45:36 +0000526bool DeclContext::isTransparentContext() const {
527 if (DeclKind == Decl::Enum)
528 return true; // FIXME: Check for C++0x scoped enums
529 else if (DeclKind == Decl::LinkageSpec)
530 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000531 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000532 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000533 else if (DeclKind == Decl::Namespace)
534 return false; // FIXME: Check for C++0x inline namespaces
535
536 return false;
537}
538
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000539bool DeclContext::Encloses(DeclContext *DC) {
540 if (getPrimaryContext() != this)
541 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000543 for (; DC; DC = DC->getParent())
544 if (DC->getPrimaryContext() == this)
545 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000546 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000547}
548
Steve Naroff0701bbb2009-01-08 17:28:14 +0000549DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000550 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000551 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000552 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000553 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000554 // There is only one DeclContext for these entities.
555 return this;
556
557 case Decl::Namespace:
558 // The original namespace is our primary context.
559 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
560
Douglas Gregor44b43212008-12-11 16:49:14 +0000561 case Decl::ObjCMethod:
562 return this;
563
564 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000565 case Decl::ObjCProtocol:
566 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000567 // FIXME: Can Objective-C interfaces be forward-declared?
568 return this;
569
Steve Naroff0701bbb2009-01-08 17:28:14 +0000570 case Decl::ObjCImplementation:
571 case Decl::ObjCCategoryImpl:
572 return this;
573
Douglas Gregor44b43212008-12-11 16:49:14 +0000574 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000575 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
576 // If this is a tag type that has a definition or is currently
577 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000578 TagDecl *Tag = cast<TagDecl>(this);
579 assert(isa<TagType>(Tag->TypeForDecl) ||
580 isa<InjectedClassNameType>(Tag->TypeForDecl));
581
582 if (TagDecl *Def = Tag->getDefinition())
583 return Def;
584
585 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
586 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
587 if (TagTy->isBeingDefined())
588 // FIXME: is it necessarily being defined in the decl
589 // that owns the type?
590 return TagTy->getDecl();
591 }
592
593 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000594 }
595
Douglas Gregor44b43212008-12-11 16:49:14 +0000596 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
597 "Unknown DeclContext kind");
598 return this;
599 }
600}
601
602DeclContext *DeclContext::getNextContext() {
603 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000604 case Decl::Namespace:
605 // Return the next namespace
606 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
607
608 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000609 return 0;
610 }
611}
612
Douglas Gregor2cf26342009-04-09 22:27:44 +0000613/// \brief Load the declarations within this lexical storage from an
614/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000615void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000616DeclContext::LoadLexicalDeclsFromExternalStorage() const {
617 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000618 assert(hasExternalLexicalStorage() && Source && "No external storage?");
619
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000620 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000621 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000622 Decls))
623 return;
624
625 // There is no longer any lexical storage in this context
626 ExternalLexicalStorage = false;
627
628 if (Decls.empty())
629 return;
630
631 // Resolve all of the declaration IDs into declarations, building up
632 // a chain of declarations via the Decl::NextDeclInContext field.
633 Decl *FirstNewDecl = 0;
634 Decl *PrevDecl = 0;
635 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
636 Decl *D = Source->GetDecl(Decls[I]);
637 if (PrevDecl)
638 PrevDecl->NextDeclInContext = D;
639 else
640 FirstNewDecl = D;
641
642 PrevDecl = D;
643 }
644
645 // Splice the newly-read declarations into the beginning of the list
646 // of declarations.
647 PrevDecl->NextDeclInContext = FirstDecl;
648 FirstDecl = FirstNewDecl;
649 if (!LastDecl)
650 LastDecl = PrevDecl;
651}
652
Mike Stump1eb44332009-09-09 15:08:12 +0000653void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000654DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000655 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000656 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000657 assert(hasExternalVisibleStorage() && Source && "No external storage?");
658
659 llvm::SmallVector<VisibleDeclaration, 64> Decls;
660 if (Source->ReadDeclsVisibleInContext(This, Decls))
661 return;
662
663 // There is no longer any visible storage in this context
664 ExternalVisibleStorage = false;
665
666 // Load the declaration IDs for all of the names visible in this
667 // context.
668 assert(!LookupPtr && "Have a lookup map before de-serialization?");
Ted Kremenek3478eb62010-02-11 07:12:28 +0000669 StoredDeclsMap *Map =
670 (StoredDeclsMap*) getParentASTContext().CreateStoredDeclsMap();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000671 LookupPtr = Map;
672 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
673 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
674 }
675}
676
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000677DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000678 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000679 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000680
681 // FIXME: Check whether we need to load some declarations from
682 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000683 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000684}
685
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000686DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000687 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000688 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000689
Mike Stump1eb44332009-09-09 15:08:12 +0000690 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000691}
692
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000693bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000694 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000695 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000696
697 return !FirstDecl;
698}
699
John McCall9f54ad42009-12-10 09:41:52 +0000700void DeclContext::removeDecl(Decl *D) {
701 assert(D->getLexicalDeclContext() == this &&
702 "decl being removed from non-lexical context");
703 assert((D->NextDeclInContext || D == LastDecl) &&
704 "decl is not in decls list");
705
706 // Remove D from the decl chain. This is O(n) but hopefully rare.
707 if (D == FirstDecl) {
708 if (D == LastDecl)
709 FirstDecl = LastDecl = 0;
710 else
711 FirstDecl = D->NextDeclInContext;
712 } else {
713 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
714 assert(I && "decl not found in linked list");
715 if (I->NextDeclInContext == D) {
716 I->NextDeclInContext = D->NextDeclInContext;
717 if (D == LastDecl) LastDecl = I;
718 break;
719 }
720 }
721 }
722
723 // Mark that D is no longer in the decl chain.
724 D->NextDeclInContext = 0;
725
726 // Remove D from the lookup table if necessary.
727 if (isa<NamedDecl>(D)) {
728 NamedDecl *ND = cast<NamedDecl>(D);
729
730 void *OpaqueMap = getPrimaryContext()->LookupPtr;
731 if (!OpaqueMap) return;
732
733 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(OpaqueMap);
734 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
735 assert(Pos != Map->end() && "no lookup entry for decl");
736 Pos->second.remove(ND);
737 }
738}
739
John McCall3f9a8a62009-08-11 06:59:38 +0000740void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000741 assert(D->getLexicalDeclContext() == this &&
742 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000743 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000744 "Decl already inserted into a DeclContext");
745
746 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000747 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000748 LastDecl = D;
749 } else {
750 FirstDecl = LastDecl = D;
751 }
John McCall3f9a8a62009-08-11 06:59:38 +0000752}
753
754void DeclContext::addDecl(Decl *D) {
755 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000756
757 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000758 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000759}
760
Douglas Gregor074149e2009-01-05 19:45:36 +0000761/// buildLookup - Build the lookup data structure with all of the
762/// declarations in DCtx (and any other contexts linked to it or
763/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000764void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000765 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000766 for (decl_iterator D = DCtx->decls_begin(),
767 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000768 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000769 // Insert this declaration into the lookup structure, but only
770 // if it's semantically in its decl context. During non-lazy
771 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000772 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000773 if (D->getDeclContext() == DCtx)
774 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000775
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000776 // Insert any forward-declared Objective-C interfaces into the lookup
777 // data structure.
778 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
779 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
780 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000781 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000782
Douglas Gregor074149e2009-01-05 19:45:36 +0000783 // If this declaration is itself a transparent declaration context,
784 // add its members (recursively).
785 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
786 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000787 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000788 }
789 }
790}
791
Mike Stump1eb44332009-09-09 15:08:12 +0000792DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000793DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000794 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000795 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000796 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000797
Douglas Gregor2cf26342009-04-09 22:27:44 +0000798 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000799 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000800
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000801 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000802 /// all of the linked DeclContexts (in declaration order!) and
803 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000804 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000805 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000806
Douglas Gregorc36c5402009-04-09 17:29:08 +0000807 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000808 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000809 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000810
Douglas Gregorc36c5402009-04-09 17:29:08 +0000811 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
812 StoredDeclsMap::iterator Pos = Map->find(Name);
813 if (Pos == Map->end())
814 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000815 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000816}
817
Mike Stump1eb44332009-09-09 15:08:12 +0000818DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000819DeclContext::lookup(DeclarationName Name) const {
820 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000821}
822
Chris Lattner0cf2b192009-03-27 19:19:59 +0000823DeclContext *DeclContext::getLookupContext() {
824 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000825 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000826 while (Ctx->isTransparentContext())
827 Ctx = Ctx->getParent();
828 return Ctx;
829}
830
Douglas Gregor88b70942009-02-25 22:02:03 +0000831DeclContext *DeclContext::getEnclosingNamespaceContext() {
832 DeclContext *Ctx = this;
833 // Skip through non-namespace, non-translation-unit contexts.
834 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
835 Ctx = Ctx->getParent();
836 return Ctx->getPrimaryContext();
837}
838
John McCallab88d972009-08-31 22:39:49 +0000839void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000840 // FIXME: This feels like a hack. Should DeclarationName support
841 // template-ids, or is there a better way to keep specializations
842 // from being visible?
843 if (isa<ClassTemplateSpecializationDecl>(D))
844 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000845 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
846 if (FD->isFunctionTemplateSpecialization())
847 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000848
Steve Naroff0701bbb2009-01-08 17:28:14 +0000849 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000850 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000851 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000852 return;
853 }
854
855 // If we already have a lookup data structure, perform the insertion
856 // into it. Otherwise, be lazy and don't build that structure until
857 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000858 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000859 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000860
Douglas Gregor074149e2009-01-05 19:45:36 +0000861 // If we are a transparent context, insert into our parent context,
862 // too. This operation is recursive.
863 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000864 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000865}
866
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000867void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000868 // Skip unnamed declarations.
869 if (!D->getDeclName())
870 return;
871
Douglas Gregorcc636682009-02-17 23:15:12 +0000872 // FIXME: This feels like a hack. Should DeclarationName support
873 // template-ids, or is there a better way to keep specializations
874 // from being visible?
875 if (isa<ClassTemplateSpecializationDecl>(D))
876 return;
877
Ted Kremenek3478eb62010-02-11 07:12:28 +0000878 ASTContext *C = 0;
879 if (!LookupPtr) {
880 C = &getParentASTContext();
881 LookupPtr = (StoredDeclsMap*) C->CreateStoredDeclsMap();
882 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000883
884 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000885 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000886 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
887 if (DeclNameEntries.isNull()) {
888 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000889 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000890 }
Chris Lattner91942502009-02-20 00:55:03 +0000891
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000892 // If it is possible that this is a redeclaration, check to see if there is
893 // already a decl for which declarationReplaces returns true. If there is
894 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +0000895 if (!C)
896 C = &getParentASTContext();
897
898 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000899 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000901 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000902 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000903}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000904
905/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
906/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000907DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000908DeclContext::getUsingDirectives() const {
909 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000910 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
911 reinterpret_cast<udir_iterator>(Result.second));
912}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000913
914void StoredDeclsList::materializeDecls(ASTContext &Context) {
915 if (isNull())
916 return;
917
918 switch ((DataKind)(Data & 0x03)) {
919 case DK_Decl:
920 case DK_Decl_Vector:
921 break;
922
923 case DK_DeclID: {
924 // Resolve this declaration ID to an actual declaration by
925 // querying the external AST source.
926 unsigned DeclID = Data >> 2;
927
928 ExternalASTSource *Source = Context.getExternalSource();
929 assert(Source && "No external AST source available!");
930
931 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
932 break;
933 }
934
935 case DK_ID_Vector: {
936 // We have a vector of declaration IDs. Resolve all of them to
937 // actual declarations.
938 VectorTy &Vector = *getAsVector();
939 ExternalASTSource *Source = Context.getExternalSource();
940 assert(Source && "No external AST source available!");
941
942 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
943 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
944
945 Data = (Data & ~0x03) | DK_Decl_Vector;
946 break;
947 }
948 }
949}
Ted Kremenek3478eb62010-02-11 07:12:28 +0000950
951//===----------------------------------------------------------------------===//
952// Creation and Destruction of StoredDeclsMaps. //
953//===----------------------------------------------------------------------===//
954
955void *ASTContext::CreateStoredDeclsMap() {
956 StoredDeclsMap *M = new StoredDeclsMap();
957 SDMs.push_back(M);
958 return M;
959}
960
961void ASTContext::ReleaseDeclContextMaps() {
962 for (std::vector<void*>::iterator I = SDMs.begin(), E = SDMs.end(); I!=E; ++I)
963 delete (StoredDeclsMap*) *I;
964}