blob: 47b7e7efb60e06dc12fc46705bff9d4e6aca0cbb [file] [log] [blame]
Eli Friedman56d29372008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Douglas Gregor64650af2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorc2ee10d2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000022#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000023#include "clang/AST/Stmt.h"
24#include "clang/AST/StmtCXX.h"
Eli Friedman56d29372008-06-07 16:52:53 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000027#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000028#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000029#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Statistics
34//===----------------------------------------------------------------------===//
35
Douglas Gregor64650af2009-02-02 23:39:07 +000036#define DECL(Derived, Base) static int n##Derived##s = 0;
37#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000038
39static bool StatSwitch = false;
40
Eli Friedman56d29372008-06-07 16:52:53 +000041const char *Decl::getDeclKindName() const {
42 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000043 default: assert(0 && "Declaration not in DeclNodes.def!");
44#define DECL(Derived, Base) case Derived: return #Derived;
45#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000046 }
47}
48
Steve Naroff0a473932009-01-20 19:53:53 +000049const char *DeclContext::getDeclKindName() const {
50 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000051 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000052#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000053#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000054 }
55}
56
Eli Friedman56d29372008-06-07 16:52:53 +000057bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000058 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000059 return StatSwitch;
60}
61
62void Decl::PrintStats() {
63 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000064
Douglas Gregor64650af2009-02-02 23:39:07 +000065 int totalDecls = 0;
66#define DECL(Derived, Base) totalDecls += n##Derived##s;
67#include "clang/AST/DeclNodes.def"
68 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000069
Douglas Gregor64650af2009-02-02 23:39:07 +000070 int totalBytes = 0;
71#define DECL(Derived, Base) \
72 if (n##Derived##s > 0) { \
73 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
74 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
75 n##Derived##s, (int)sizeof(Derived##Decl), \
76 (int)(n##Derived##s * sizeof(Derived##Decl))); \
77 }
78#include "clang/AST/DeclNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor64650af2009-02-02 23:39:07 +000080 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000081}
82
83void Decl::addDeclKind(Kind k) {
84 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000085 default: assert(0 && "Declaration not in DeclNodes.def!");
86#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
87#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000088 }
89}
90
Anders Carlsson67e33202009-06-13 00:08:58 +000091bool Decl::isTemplateParameterPack() const {
92 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
93 return TTP->isParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +000094
Anders Carlsson67e33202009-06-13 00:08:58 +000095 return false;
96}
97
Douglas Gregore53060f2009-06-25 22:08:12 +000098bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +000099 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000100 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Douglas Gregore53060f2009-06-25 22:08:12 +0000102 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
103}
104
Douglas Gregor79c22782010-01-16 20:21:20 +0000105bool Decl::isDefinedOutsideFunctionOrMethod() const {
106 for (const DeclContext *DC = getDeclContext();
107 DC && !DC->isTranslationUnit();
108 DC = DC->getParent())
109 if (DC->isFunctionOrMethod())
110 return false;
111
112 return true;
113}
114
115
Eli Friedman56d29372008-06-07 16:52:53 +0000116//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000117// PrettyStackTraceDecl Implementation
118//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner49f28ca2009-03-05 08:00:35 +0000120void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
121 SourceLocation TheLoc = Loc;
122 if (TheLoc.isInvalid() && TheDecl)
123 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner49f28ca2009-03-05 08:00:35 +0000125 if (TheLoc.isValid()) {
126 TheLoc.print(OS, SM);
127 OS << ": ";
128 }
129
130 OS << Message;
131
Daniel Dunbarc5236562009-11-21 09:05:59 +0000132 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000133 OS << " '" << DN->getQualifiedNameAsString() << '\'';
134 OS << '\n';
135}
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattner49f28ca2009-03-05 08:00:35 +0000137//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000138// Decl Implementation
139//===----------------------------------------------------------------------===//
140
Chris Lattner769dbdf2009-03-27 20:18:19 +0000141// Out-of-line virtual method providing a home for Decl.
142Decl::~Decl() {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000143 assert(!HasAttrs && "attributes should have been freed by Destroy");
144}
145
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000146void Decl::setDeclContext(DeclContext *DC) {
147 if (isOutOfSemaDC())
148 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Chris Lattneree219fd2009-03-29 06:06:59 +0000150 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000151}
152
153void Decl::setLexicalDeclContext(DeclContext *DC) {
154 if (DC == getLexicalDeclContext())
155 return;
156
157 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000158 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000159 MDC->SemanticDC = getDeclContext();
160 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000161 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000162 } else {
163 getMultipleDC()->LexicalDC = DC;
164 }
165}
166
John McCall9aeed322009-10-01 00:25:31 +0000167bool Decl::isInAnonymousNamespace() const {
168 const DeclContext *DC = getDeclContext();
169 do {
170 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
171 if (ND->isAnonymousNamespace())
172 return true;
173 } while ((DC = DC->getParent()));
174
175 return false;
176}
177
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000178TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000179 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
180 return TUD;
181
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000182 DeclContext *DC = getDeclContext();
183 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000185 while (!DC->isTranslationUnit()) {
186 DC = DC->getParent();
187 assert(DC && "This decl is not contained in a translation unit!");
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000190 return cast<TranslationUnitDecl>(DC);
191}
192
193ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000194 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000195}
196
Tanya Lattner12ead492010-02-17 02:17:21 +0000197bool Decl::isUsed() const {
198 if (Used)
199 return true;
200
201 // Check for used attribute.
202 if (hasAttr<UsedAttr>())
203 return true;
204
205 // Check redeclarations for used attribute.
206 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
207 if (I->hasAttr<UsedAttr>() || I->Used)
208 return true;
209 }
210
211 return false;
212}
213
214
Chris Lattner769dbdf2009-03-27 20:18:19 +0000215unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
216 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000217 case Function:
218 case CXXMethod:
219 case CXXConstructor:
220 case CXXDestructor:
221 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000222 case Typedef:
223 case EnumConstant:
224 case Var:
225 case ImplicitParam:
226 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000227 case NonTypeTemplateParm:
228 case ObjCMethod:
229 case ObjCContainer:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000230 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000231 case ObjCProperty:
232 case ObjCCompatibleAlias:
233 return IDNS_Ordinary;
John McCall3f9a8a62009-08-11 06:59:38 +0000234
John McCall9488ea12009-11-17 05:59:44 +0000235 case UsingShadow:
236 return 0; // we'll actually overwrite this later
237
John McCall7ba107a2009-11-18 02:36:19 +0000238 case UnresolvedUsingValue:
239 case UnresolvedUsingTypename:
240 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000241
242 case Using:
243 return IDNS_Using;
244
Chris Lattner769dbdf2009-03-27 20:18:19 +0000245 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000246 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000248 case ObjCImplementation:
249 return IDNS_ObjCImplementation;
250
Fariborz Jahanian737061f2009-12-11 00:26:36 +0000251 case ObjCCategory:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000252 case ObjCCategoryImpl:
Fariborz Jahanian737061f2009-12-11 00:26:36 +0000253 return IDNS_ObjCCategoryName;
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000254
Chris Lattner769dbdf2009-03-27 20:18:19 +0000255 case Field:
256 case ObjCAtDefsField:
257 case ObjCIvar:
258 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Chris Lattner769dbdf2009-03-27 20:18:19 +0000260 case Record:
261 case CXXRecord:
262 case Enum:
263 case TemplateTypeParm:
264 return IDNS_Tag;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattner769dbdf2009-03-27 20:18:19 +0000266 case Namespace:
267 case Template:
268 case FunctionTemplate:
269 case ClassTemplate:
270 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000271 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000272 return IDNS_Tag | IDNS_Ordinary;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner769dbdf2009-03-27 20:18:19 +0000274 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000275 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000276 case FriendTemplate:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000277 case LinkageSpec:
278 case FileScopeAsm:
279 case StaticAssert:
280 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000281 case ObjCPropertyImpl:
282 case ObjCForwardProtocol:
283 case Block:
284 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000285
Chris Lattner769dbdf2009-03-27 20:18:19 +0000286 // Aren't looked up?
287 case UsingDirective:
288 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000289 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000290 return 0;
291 }
John McCall9488ea12009-11-17 05:59:44 +0000292
293 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000294}
295
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000296void Decl::addAttr(Attr *NewAttr) {
297 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000298
299 NewAttr->setNext(ExistingAttr);
300 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Eli Friedman56d29372008-06-07 16:52:53 +0000302 HasAttrs = true;
303}
304
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000305void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000306 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Eli Friedman56d29372008-06-07 16:52:53 +0000308 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000309 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000310}
311
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000312const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000313 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000314 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000315}
316
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000317void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000318 bool HasLHSAttr = this->HasAttrs;
319 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Eli Friedman56d29372008-06-07 16:52:53 +0000321 // Usually, neither decl has attrs, nothing to do.
322 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Eli Friedman56d29372008-06-07 16:52:53 +0000324 // If 'this' has no attrs, swap the other way.
325 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000326 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000328 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Eli Friedman56d29372008-06-07 16:52:53 +0000330 // Handle the case when both decls have attrs.
331 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000332 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000333 return;
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Eli Friedman56d29372008-06-07 16:52:53 +0000336 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000337 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
338 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000339 this->HasAttrs = false;
340 RHS->HasAttrs = true;
341}
342
343
Chris Lattnercc581472009-03-04 06:05:19 +0000344void Decl::Destroy(ASTContext &C) {
345 // Free attributes for this decl.
346 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000347 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000348 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000349 HasAttrs = false;
350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000352#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000353 // FIXME: Once ownership is fully understood, we can enable this code
354 if (DeclContext *DC = dyn_cast<DeclContext>(this))
355 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000356
Chris Lattner244a67d2009-03-28 06:04:26 +0000357 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000358 // within the loop, only the Destroy method for the first Decl
359 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner244a67d2009-03-28 06:04:26 +0000361 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000363 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000364 Decl* Tmp = N->getNextDeclInContext();
365 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000366 N->Destroy(C);
367 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000368 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000369
Ted Kremenek94a39002009-12-01 00:07:10 +0000370 if (isOutOfSemaDC())
371 delete (C) getMultipleDC();
372
Eli Friedman56d29372008-06-07 16:52:53 +0000373 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000374 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000375#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000376}
377
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000378Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000379 Decl::Kind DK = D->getDeclKind();
380 switch(DK) {
381#define DECL_CONTEXT(Name) \
382 case Decl::Name: \
383 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
384#define DECL_CONTEXT_BASE(Name)
385#include "clang/AST/DeclNodes.def"
386 default:
387#define DECL_CONTEXT_BASE(Name) \
388 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
389 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
390#include "clang/AST/DeclNodes.def"
391 assert(false && "a decl that inherits DeclContext isn't handled");
392 return 0;
393 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000394}
395
396DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000397 Decl::Kind DK = D->getKind();
398 switch(DK) {
399#define DECL_CONTEXT(Name) \
400 case Decl::Name: \
401 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
402#define DECL_CONTEXT_BASE(Name)
403#include "clang/AST/DeclNodes.def"
404 default:
405#define DECL_CONTEXT_BASE(Name) \
406 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
407 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
408#include "clang/AST/DeclNodes.def"
409 assert(false && "a decl that inherits DeclContext isn't handled");
410 return 0;
411 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000412}
413
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000414CompoundStmt* Decl::getCompoundBody() const {
415 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000416}
417
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000418SourceLocation Decl::getBodyRBrace() const {
419 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000420 if (!Body)
421 return SourceLocation();
422 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
423 return CS->getRBracLoc();
424 assert(isa<CXXTryStmt>(Body) &&
425 "Body can only be CompoundStmt or CXXTryStmt");
426 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
427}
428
Anders Carlsson1329c272009-03-25 23:38:06 +0000429#ifndef NDEBUG
430void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000431 // Suppress this check if any of the following hold:
432 // 1. this is the translation unit (and thus has no parent)
433 // 2. this is a template parameter (and thus doesn't belong to its context)
434 // 3. this is a ParmVarDecl (which can be in a record context during
435 // the brief period between its creation and the creation of the
436 // FunctionDecl)
437 // 4. the context is not a record
Anders Carlsson35eda442009-08-29 20:47:47 +0000438 if (isa<TranslationUnitDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000439 !isa<CXXRecordDecl>(getDeclContext()) ||
440 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000441 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
443 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000444 "Access specifier is AS_none inside a record decl");
445}
446
447#endif
448
Eli Friedman56d29372008-06-07 16:52:53 +0000449//===----------------------------------------------------------------------===//
450// DeclContext Implementation
451//===----------------------------------------------------------------------===//
452
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000453bool DeclContext::classof(const Decl *D) {
454 switch (D->getKind()) {
455#define DECL_CONTEXT(Name) case Decl::Name:
456#define DECL_CONTEXT_BASE(Name)
457#include "clang/AST/DeclNodes.def"
458 return true;
459 default:
460#define DECL_CONTEXT_BASE(Name) \
461 if (D->getKind() >= Decl::Name##First && \
462 D->getKind() <= Decl::Name##Last) \
463 return true;
464#include "clang/AST/DeclNodes.def"
465 return false;
466 }
467}
468
Douglas Gregor44b43212008-12-11 16:49:14 +0000469DeclContext::~DeclContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000470 // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
471 // ~DeclContext() is not guaranteed to be called when ASTContext uses
472 // a BumpPtrAllocator.
473 // delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000474}
475
476void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000477 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000478 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000479}
480
Douglas Gregore942bbe2009-09-10 16:57:35 +0000481/// \brief Find the parent context of this context that will be
482/// used for unqualified name lookup.
483///
484/// Generally, the parent lookup context is the semantic context. However, for
485/// a friend function the parent lookup context is the lexical context, which
486/// is the class in which the friend is declared.
487DeclContext *DeclContext::getLookupParent() {
488 // FIXME: Find a better way to identify friends
489 if (isa<FunctionDecl>(this))
490 if (getParent()->getLookupContext()->isFileContext() &&
491 getLexicalParent()->getLookupContext()->isRecord())
492 return getLexicalParent();
493
494 return getParent();
495}
496
Douglas Gregorbc221632009-05-28 16:34:51 +0000497bool DeclContext::isDependentContext() const {
498 if (isFileContext())
499 return false;
500
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000501 if (isa<ClassTemplatePartialSpecializationDecl>(this))
502 return true;
503
Douglas Gregorbc221632009-05-28 16:34:51 +0000504 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
505 if (Record->getDescribedClassTemplate())
506 return true;
507
508 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
509 if (Function->getDescribedFunctionTemplate())
510 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregorbc221632009-05-28 16:34:51 +0000512 return getParent() && getParent()->isDependentContext();
513}
514
Douglas Gregor074149e2009-01-05 19:45:36 +0000515bool DeclContext::isTransparentContext() const {
516 if (DeclKind == Decl::Enum)
517 return true; // FIXME: Check for C++0x scoped enums
518 else if (DeclKind == Decl::LinkageSpec)
519 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000520 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000521 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000522 else if (DeclKind == Decl::Namespace)
523 return false; // FIXME: Check for C++0x inline namespaces
524
525 return false;
526}
527
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000528bool DeclContext::Encloses(DeclContext *DC) {
529 if (getPrimaryContext() != this)
530 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000532 for (; DC; DC = DC->getParent())
533 if (DC->getPrimaryContext() == this)
534 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000535 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000536}
537
Steve Naroff0701bbb2009-01-08 17:28:14 +0000538DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000539 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000540 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000541 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000542 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000543 // There is only one DeclContext for these entities.
544 return this;
545
546 case Decl::Namespace:
547 // The original namespace is our primary context.
548 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
549
Douglas Gregor44b43212008-12-11 16:49:14 +0000550 case Decl::ObjCMethod:
551 return this;
552
553 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000554 case Decl::ObjCProtocol:
555 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000556 // FIXME: Can Objective-C interfaces be forward-declared?
557 return this;
558
Steve Naroff0701bbb2009-01-08 17:28:14 +0000559 case Decl::ObjCImplementation:
560 case Decl::ObjCCategoryImpl:
561 return this;
562
Douglas Gregor44b43212008-12-11 16:49:14 +0000563 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000564 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
565 // If this is a tag type that has a definition or is currently
566 // being defined, that definition is our primary context.
Ted Kremenek6217b802009-07-29 21:53:49 +0000567 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000568 if (TagT->isBeingDefined() ||
Douglas Gregorcc636682009-02-17 23:15:12 +0000569 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
570 return TagT->getDecl();
571 return this;
572 }
573
Douglas Gregor44b43212008-12-11 16:49:14 +0000574 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
575 "Unknown DeclContext kind");
576 return this;
577 }
578}
579
580DeclContext *DeclContext::getNextContext() {
581 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000582 case Decl::Namespace:
583 // Return the next namespace
584 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
585
586 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000587 return 0;
588 }
589}
590
Douglas Gregor2cf26342009-04-09 22:27:44 +0000591/// \brief Load the declarations within this lexical storage from an
592/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000593void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000594DeclContext::LoadLexicalDeclsFromExternalStorage() const {
595 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000596 assert(hasExternalLexicalStorage() && Source && "No external storage?");
597
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000598 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000599 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000600 Decls))
601 return;
602
603 // There is no longer any lexical storage in this context
604 ExternalLexicalStorage = false;
605
606 if (Decls.empty())
607 return;
608
609 // Resolve all of the declaration IDs into declarations, building up
610 // a chain of declarations via the Decl::NextDeclInContext field.
611 Decl *FirstNewDecl = 0;
612 Decl *PrevDecl = 0;
613 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
614 Decl *D = Source->GetDecl(Decls[I]);
615 if (PrevDecl)
616 PrevDecl->NextDeclInContext = D;
617 else
618 FirstNewDecl = D;
619
620 PrevDecl = D;
621 }
622
623 // Splice the newly-read declarations into the beginning of the list
624 // of declarations.
625 PrevDecl->NextDeclInContext = FirstDecl;
626 FirstDecl = FirstNewDecl;
627 if (!LastDecl)
628 LastDecl = PrevDecl;
629}
630
Mike Stump1eb44332009-09-09 15:08:12 +0000631void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000632DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000633 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000634 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000635 assert(hasExternalVisibleStorage() && Source && "No external storage?");
636
637 llvm::SmallVector<VisibleDeclaration, 64> Decls;
638 if (Source->ReadDeclsVisibleInContext(This, Decls))
639 return;
640
641 // There is no longer any visible storage in this context
642 ExternalVisibleStorage = false;
643
644 // Load the declaration IDs for all of the names visible in this
645 // context.
646 assert(!LookupPtr && "Have a lookup map before de-serialization?");
Ted Kremenek3478eb62010-02-11 07:12:28 +0000647 StoredDeclsMap *Map =
648 (StoredDeclsMap*) getParentASTContext().CreateStoredDeclsMap();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000649 LookupPtr = Map;
650 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
651 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
652 }
653}
654
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000655DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000656 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000657 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000658
659 // FIXME: Check whether we need to load some declarations from
660 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000661 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000662}
663
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000664DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000665 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000666 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000667
Mike Stump1eb44332009-09-09 15:08:12 +0000668 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000669}
670
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000671bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000672 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000673 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000674
675 return !FirstDecl;
676}
677
John McCall9f54ad42009-12-10 09:41:52 +0000678void DeclContext::removeDecl(Decl *D) {
679 assert(D->getLexicalDeclContext() == this &&
680 "decl being removed from non-lexical context");
681 assert((D->NextDeclInContext || D == LastDecl) &&
682 "decl is not in decls list");
683
684 // Remove D from the decl chain. This is O(n) but hopefully rare.
685 if (D == FirstDecl) {
686 if (D == LastDecl)
687 FirstDecl = LastDecl = 0;
688 else
689 FirstDecl = D->NextDeclInContext;
690 } else {
691 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
692 assert(I && "decl not found in linked list");
693 if (I->NextDeclInContext == D) {
694 I->NextDeclInContext = D->NextDeclInContext;
695 if (D == LastDecl) LastDecl = I;
696 break;
697 }
698 }
699 }
700
701 // Mark that D is no longer in the decl chain.
702 D->NextDeclInContext = 0;
703
704 // Remove D from the lookup table if necessary.
705 if (isa<NamedDecl>(D)) {
706 NamedDecl *ND = cast<NamedDecl>(D);
707
708 void *OpaqueMap = getPrimaryContext()->LookupPtr;
709 if (!OpaqueMap) return;
710
711 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(OpaqueMap);
712 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
713 assert(Pos != Map->end() && "no lookup entry for decl");
714 Pos->second.remove(ND);
715 }
716}
717
John McCall3f9a8a62009-08-11 06:59:38 +0000718void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000719 assert(D->getLexicalDeclContext() == this &&
720 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000721 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000722 "Decl already inserted into a DeclContext");
723
724 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000725 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000726 LastDecl = D;
727 } else {
728 FirstDecl = LastDecl = D;
729 }
John McCall3f9a8a62009-08-11 06:59:38 +0000730}
731
732void DeclContext::addDecl(Decl *D) {
733 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000734
735 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000736 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000737}
738
Douglas Gregor074149e2009-01-05 19:45:36 +0000739/// buildLookup - Build the lookup data structure with all of the
740/// declarations in DCtx (and any other contexts linked to it or
741/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000742void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000743 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000744 for (decl_iterator D = DCtx->decls_begin(),
745 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000746 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000747 // Insert this declaration into the lookup structure, but only
748 // if it's semantically in its decl context. During non-lazy
749 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000750 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000751 if (D->getDeclContext() == DCtx)
752 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000753
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000754 // Insert any forward-declared Objective-C interfaces into the lookup
755 // data structure.
756 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
757 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
758 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000759 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000760
Douglas Gregor074149e2009-01-05 19:45:36 +0000761 // If this declaration is itself a transparent declaration context,
762 // add its members (recursively).
763 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
764 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000765 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000766 }
767 }
768}
769
Mike Stump1eb44332009-09-09 15:08:12 +0000770DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000771DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000772 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000773 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000774 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000775
Douglas Gregor2cf26342009-04-09 22:27:44 +0000776 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000777 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000778
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000779 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000780 /// all of the linked DeclContexts (in declaration order!) and
781 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000782 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000783 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000784
Douglas Gregorc36c5402009-04-09 17:29:08 +0000785 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000786 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000787 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000788
Douglas Gregorc36c5402009-04-09 17:29:08 +0000789 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
790 StoredDeclsMap::iterator Pos = Map->find(Name);
791 if (Pos == Map->end())
792 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000793 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000794}
795
Mike Stump1eb44332009-09-09 15:08:12 +0000796DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000797DeclContext::lookup(DeclarationName Name) const {
798 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000799}
800
Chris Lattner0cf2b192009-03-27 19:19:59 +0000801DeclContext *DeclContext::getLookupContext() {
802 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000803 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000804 while (Ctx->isTransparentContext())
805 Ctx = Ctx->getParent();
806 return Ctx;
807}
808
Douglas Gregor88b70942009-02-25 22:02:03 +0000809DeclContext *DeclContext::getEnclosingNamespaceContext() {
810 DeclContext *Ctx = this;
811 // Skip through non-namespace, non-translation-unit contexts.
812 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
813 Ctx = Ctx->getParent();
814 return Ctx->getPrimaryContext();
815}
816
John McCallab88d972009-08-31 22:39:49 +0000817void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000818 // FIXME: This feels like a hack. Should DeclarationName support
819 // template-ids, or is there a better way to keep specializations
820 // from being visible?
821 if (isa<ClassTemplateSpecializationDecl>(D))
822 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000823 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
824 if (FD->isFunctionTemplateSpecialization())
825 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000826
Steve Naroff0701bbb2009-01-08 17:28:14 +0000827 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000828 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000829 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000830 return;
831 }
832
833 // If we already have a lookup data structure, perform the insertion
834 // into it. Otherwise, be lazy and don't build that structure until
835 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000836 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000837 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000838
Douglas Gregor074149e2009-01-05 19:45:36 +0000839 // If we are a transparent context, insert into our parent context,
840 // too. This operation is recursive.
841 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000842 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000843}
844
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000845void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000846 // Skip unnamed declarations.
847 if (!D->getDeclName())
848 return;
849
Douglas Gregorcc636682009-02-17 23:15:12 +0000850 // FIXME: This feels like a hack. Should DeclarationName support
851 // template-ids, or is there a better way to keep specializations
852 // from being visible?
853 if (isa<ClassTemplateSpecializationDecl>(D))
854 return;
855
Ted Kremenek3478eb62010-02-11 07:12:28 +0000856 ASTContext *C = 0;
857 if (!LookupPtr) {
858 C = &getParentASTContext();
859 LookupPtr = (StoredDeclsMap*) C->CreateStoredDeclsMap();
860 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000861
862 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000863 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000864 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
865 if (DeclNameEntries.isNull()) {
866 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000867 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000868 }
Chris Lattner91942502009-02-20 00:55:03 +0000869
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000870 // If it is possible that this is a redeclaration, check to see if there is
871 // already a decl for which declarationReplaces returns true. If there is
872 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +0000873 if (!C)
874 C = &getParentASTContext();
875
876 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000877 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000879 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000880 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000881}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000882
883/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
884/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000885DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000886DeclContext::getUsingDirectives() const {
887 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000888 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
889 reinterpret_cast<udir_iterator>(Result.second));
890}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000891
892void StoredDeclsList::materializeDecls(ASTContext &Context) {
893 if (isNull())
894 return;
895
896 switch ((DataKind)(Data & 0x03)) {
897 case DK_Decl:
898 case DK_Decl_Vector:
899 break;
900
901 case DK_DeclID: {
902 // Resolve this declaration ID to an actual declaration by
903 // querying the external AST source.
904 unsigned DeclID = Data >> 2;
905
906 ExternalASTSource *Source = Context.getExternalSource();
907 assert(Source && "No external AST source available!");
908
909 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
910 break;
911 }
912
913 case DK_ID_Vector: {
914 // We have a vector of declaration IDs. Resolve all of them to
915 // actual declarations.
916 VectorTy &Vector = *getAsVector();
917 ExternalASTSource *Source = Context.getExternalSource();
918 assert(Source && "No external AST source available!");
919
920 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
921 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
922
923 Data = (Data & ~0x03) | DK_Decl_Vector;
924 break;
925 }
926 }
927}
Ted Kremenek3478eb62010-02-11 07:12:28 +0000928
929//===----------------------------------------------------------------------===//
930// Creation and Destruction of StoredDeclsMaps. //
931//===----------------------------------------------------------------------===//
932
933void *ASTContext::CreateStoredDeclsMap() {
934 StoredDeclsMap *M = new StoredDeclsMap();
935 SDMs.push_back(M);
936 return M;
937}
938
939void ASTContext::ReleaseDeclContextMaps() {
940 for (std::vector<void*>::iterator I = SDMs.begin(), E = SDMs.end(); I!=E; ++I)
941 delete (StoredDeclsMap*) *I;
942}