blob: 42a372632099c9d7aff630215e1bfc81f62ff7ce [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"
John McCall0c01d182010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Eli Friedman56d29372008-06-07 16:52:53 +000027#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000028#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000029#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000030#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000031#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Douglas Gregor64650af2009-02-02 23:39:07 +000038#define DECL(Derived, Base) static int n##Derived##s = 0;
39#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000040
41static bool StatSwitch = false;
42
Eli Friedman56d29372008-06-07 16:52:53 +000043const char *Decl::getDeclKindName() const {
44 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000045 default: assert(0 && "Declaration not in DeclNodes.def!");
46#define DECL(Derived, Base) case Derived: return #Derived;
47#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000048 }
49}
50
Douglas Gregor42738572010-03-05 00:26:45 +000051void Decl::setInvalidDecl(bool Invalid) {
52 InvalidDecl = Invalid;
53 if (Invalid) {
54 // Defensive maneuver for ill-formed code: we're likely not to make it to
55 // a point where we set the access specifier, so default it to "public"
56 // to avoid triggering asserts elsewhere in the front end.
57 setAccess(AS_public);
58 }
59}
60
Steve Naroff0a473932009-01-20 19:53:53 +000061const char *DeclContext::getDeclKindName() const {
62 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000063 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000064#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000065#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000066 }
67}
68
Eli Friedman56d29372008-06-07 16:52:53 +000069bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000070 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000071 return StatSwitch;
72}
73
74void Decl::PrintStats() {
75 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000076
Douglas Gregor64650af2009-02-02 23:39:07 +000077 int totalDecls = 0;
78#define DECL(Derived, Base) totalDecls += n##Derived##s;
79#include "clang/AST/DeclNodes.def"
80 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000081
Douglas Gregor64650af2009-02-02 23:39:07 +000082 int totalBytes = 0;
83#define DECL(Derived, Base) \
84 if (n##Derived##s > 0) { \
85 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
86 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
87 n##Derived##s, (int)sizeof(Derived##Decl), \
88 (int)(n##Derived##s * sizeof(Derived##Decl))); \
89 }
90#include "clang/AST/DeclNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +000091
Douglas Gregor64650af2009-02-02 23:39:07 +000092 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000093}
94
95void Decl::addDeclKind(Kind k) {
96 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000097 default: assert(0 && "Declaration not in DeclNodes.def!");
98#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
99#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +0000100 }
101}
102
Anders Carlsson67e33202009-06-13 00:08:58 +0000103bool Decl::isTemplateParameterPack() const {
104 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
105 return TTP->isParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Anders Carlsson67e33202009-06-13 00:08:58 +0000107 return false;
108}
109
Douglas Gregore53060f2009-06-25 22:08:12 +0000110bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000111 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000112 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Douglas Gregore53060f2009-06-25 22:08:12 +0000114 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
115}
116
Douglas Gregor79c22782010-01-16 20:21:20 +0000117bool Decl::isDefinedOutsideFunctionOrMethod() const {
118 for (const DeclContext *DC = getDeclContext();
119 DC && !DC->isTranslationUnit();
120 DC = DC->getParent())
121 if (DC->isFunctionOrMethod())
122 return false;
123
124 return true;
125}
126
127
Eli Friedman56d29372008-06-07 16:52:53 +0000128//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000129// PrettyStackTraceDecl Implementation
130//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattner49f28ca2009-03-05 08:00:35 +0000132void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
133 SourceLocation TheLoc = Loc;
134 if (TheLoc.isInvalid() && TheDecl)
135 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattner49f28ca2009-03-05 08:00:35 +0000137 if (TheLoc.isValid()) {
138 TheLoc.print(OS, SM);
139 OS << ": ";
140 }
141
142 OS << Message;
143
Daniel Dunbarc5236562009-11-21 09:05:59 +0000144 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000145 OS << " '" << DN->getQualifiedNameAsString() << '\'';
146 OS << '\n';
147}
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Chris Lattner49f28ca2009-03-05 08:00:35 +0000149//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000150// Decl Implementation
151//===----------------------------------------------------------------------===//
152
Chris Lattner769dbdf2009-03-27 20:18:19 +0000153// Out-of-line virtual method providing a home for Decl.
154Decl::~Decl() {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000155 assert(!HasAttrs && "attributes should have been freed by Destroy");
156}
157
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000158void Decl::setDeclContext(DeclContext *DC) {
159 if (isOutOfSemaDC())
160 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Chris Lattneree219fd2009-03-29 06:06:59 +0000162 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000163}
164
165void Decl::setLexicalDeclContext(DeclContext *DC) {
166 if (DC == getLexicalDeclContext())
167 return;
168
169 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000170 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000171 MDC->SemanticDC = getDeclContext();
172 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000173 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000174 } else {
175 getMultipleDC()->LexicalDC = DC;
176 }
177}
178
John McCall9aeed322009-10-01 00:25:31 +0000179bool Decl::isInAnonymousNamespace() const {
180 const DeclContext *DC = getDeclContext();
181 do {
182 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
183 if (ND->isAnonymousNamespace())
184 return true;
185 } while ((DC = DC->getParent()));
186
187 return false;
188}
189
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000190TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000191 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
192 return TUD;
193
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000194 DeclContext *DC = getDeclContext();
195 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000197 while (!DC->isTranslationUnit()) {
198 DC = DC->getParent();
199 assert(DC && "This decl is not contained in a translation unit!");
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000202 return cast<TranslationUnitDecl>(DC);
203}
204
205ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000206 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000207}
208
Tanya Lattner12ead492010-02-17 02:17:21 +0000209bool Decl::isUsed() const {
210 if (Used)
211 return true;
212
213 // Check for used attribute.
214 if (hasAttr<UsedAttr>())
215 return true;
216
217 // Check redeclarations for used attribute.
218 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
219 if (I->hasAttr<UsedAttr>() || I->Used)
220 return true;
221 }
222
223 return false;
224}
225
226
Chris Lattner769dbdf2009-03-27 20:18:19 +0000227unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
228 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000229 case Function:
230 case CXXMethod:
231 case CXXConstructor:
232 case CXXDestructor:
233 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000234 case EnumConstant:
235 case Var:
236 case ImplicitParam:
237 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000238 case NonTypeTemplateParm:
239 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000240 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000241 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000242
John McCall0d6b1642010-04-23 18:46:30 +0000243 case ObjCCompatibleAlias:
244 case ObjCInterface:
245 return IDNS_Ordinary | IDNS_Type;
246
247 case Typedef:
248 case UnresolvedUsingTypename:
249 case TemplateTypeParm:
250 return IDNS_Ordinary | IDNS_Type;
251
John McCall9488ea12009-11-17 05:59:44 +0000252 case UsingShadow:
253 return 0; // we'll actually overwrite this later
254
John McCall7ba107a2009-11-18 02:36:19 +0000255 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000256 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000257
258 case Using:
259 return IDNS_Using;
260
Chris Lattner769dbdf2009-03-27 20:18:19 +0000261 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000262 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattner769dbdf2009-03-27 20:18:19 +0000264 case Field:
265 case ObjCAtDefsField:
266 case ObjCIvar:
267 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattner769dbdf2009-03-27 20:18:19 +0000269 case Record:
270 case CXXRecord:
271 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000272 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner769dbdf2009-03-27 20:18:19 +0000274 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000275 case NamespaceAlias:
276 return IDNS_Namespace;
277
Chris Lattner769dbdf2009-03-27 20:18:19 +0000278 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000279 return IDNS_Ordinary;
280
Chris Lattner769dbdf2009-03-27 20:18:19 +0000281 case ClassTemplate:
282 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000283 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
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 case UsingDirective:
298 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000299 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000300 case ObjCImplementation:
301 case ObjCCategory:
302 case ObjCCategoryImpl:
303 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000304 return 0;
305 }
John McCall9488ea12009-11-17 05:59:44 +0000306
307 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000308}
309
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000310void Decl::addAttr(Attr *NewAttr) {
311 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000312
313 NewAttr->setNext(ExistingAttr);
314 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Eli Friedman56d29372008-06-07 16:52:53 +0000316 HasAttrs = true;
317}
318
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000319void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000320 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Eli Friedman56d29372008-06-07 16:52:53 +0000322 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000323 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000324}
325
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000326const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000327 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000328 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000329}
330
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000331void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000332 bool HasLHSAttr = this->HasAttrs;
333 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Eli Friedman56d29372008-06-07 16:52:53 +0000335 // Usually, neither decl has attrs, nothing to do.
336 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Eli Friedman56d29372008-06-07 16:52:53 +0000338 // If 'this' has no attrs, swap the other way.
339 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000340 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000342 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Eli Friedman56d29372008-06-07 16:52:53 +0000344 // Handle the case when both decls have attrs.
345 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000346 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000347 return;
348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Eli Friedman56d29372008-06-07 16:52:53 +0000350 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000351 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
352 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000353 this->HasAttrs = false;
354 RHS->HasAttrs = true;
355}
356
357
Chris Lattnercc581472009-03-04 06:05:19 +0000358void Decl::Destroy(ASTContext &C) {
359 // Free attributes for this decl.
360 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000361 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000362 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000363 HasAttrs = false;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000366#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000367 // FIXME: Once ownership is fully understood, we can enable this code
368 if (DeclContext *DC = dyn_cast<DeclContext>(this))
369 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000370
Chris Lattner244a67d2009-03-28 06:04:26 +0000371 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000372 // within the loop, only the Destroy method for the first Decl
373 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner244a67d2009-03-28 06:04:26 +0000375 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000377 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000378 Decl* Tmp = N->getNextDeclInContext();
379 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000380 N->Destroy(C);
381 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000382 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000383
Ted Kremenek94a39002009-12-01 00:07:10 +0000384 if (isOutOfSemaDC())
385 delete (C) getMultipleDC();
386
Eli Friedman56d29372008-06-07 16:52:53 +0000387 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000388 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000389#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000390}
391
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000392Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000393 Decl::Kind DK = D->getDeclKind();
394 switch(DK) {
395#define DECL_CONTEXT(Name) \
396 case Decl::Name: \
397 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
398#define DECL_CONTEXT_BASE(Name)
399#include "clang/AST/DeclNodes.def"
400 default:
401#define DECL_CONTEXT_BASE(Name) \
402 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
403 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
404#include "clang/AST/DeclNodes.def"
405 assert(false && "a decl that inherits DeclContext isn't handled");
406 return 0;
407 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000408}
409
410DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000411 Decl::Kind DK = D->getKind();
412 switch(DK) {
413#define DECL_CONTEXT(Name) \
414 case Decl::Name: \
415 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
416#define DECL_CONTEXT_BASE(Name)
417#include "clang/AST/DeclNodes.def"
418 default:
419#define DECL_CONTEXT_BASE(Name) \
420 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
421 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
422#include "clang/AST/DeclNodes.def"
423 assert(false && "a decl that inherits DeclContext isn't handled");
424 return 0;
425 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000426}
427
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000428CompoundStmt* Decl::getCompoundBody() const {
429 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000430}
431
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000432SourceLocation Decl::getBodyRBrace() const {
433 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000434 if (!Body)
435 return SourceLocation();
436 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
437 return CS->getRBracLoc();
438 assert(isa<CXXTryStmt>(Body) &&
439 "Body can only be CompoundStmt or CXXTryStmt");
440 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
441}
442
Anders Carlsson1329c272009-03-25 23:38:06 +0000443#ifndef NDEBUG
444void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000445 // Suppress this check if any of the following hold:
446 // 1. this is the translation unit (and thus has no parent)
447 // 2. this is a template parameter (and thus doesn't belong to its context)
448 // 3. this is a ParmVarDecl (which can be in a record context during
449 // the brief period between its creation and the creation of the
450 // FunctionDecl)
451 // 4. the context is not a record
Anders Carlsson35eda442009-08-29 20:47:47 +0000452 if (isa<TranslationUnitDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000453 !isa<CXXRecordDecl>(getDeclContext()) ||
454 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000455 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
457 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000458 "Access specifier is AS_none inside a record decl");
459}
460
461#endif
462
Eli Friedman56d29372008-06-07 16:52:53 +0000463//===----------------------------------------------------------------------===//
464// DeclContext Implementation
465//===----------------------------------------------------------------------===//
466
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000467bool DeclContext::classof(const Decl *D) {
468 switch (D->getKind()) {
469#define DECL_CONTEXT(Name) case Decl::Name:
470#define DECL_CONTEXT_BASE(Name)
471#include "clang/AST/DeclNodes.def"
472 return true;
473 default:
474#define DECL_CONTEXT_BASE(Name) \
475 if (D->getKind() >= Decl::Name##First && \
476 D->getKind() <= Decl::Name##Last) \
477 return true;
478#include "clang/AST/DeclNodes.def"
479 return false;
480 }
481}
482
Douglas Gregor44b43212008-12-11 16:49:14 +0000483DeclContext::~DeclContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000484 // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
485 // ~DeclContext() is not guaranteed to be called when ASTContext uses
486 // a BumpPtrAllocator.
John McCall0c01d182010-03-24 05:22:00 +0000487 // delete LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000488}
489
490void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000491 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000492 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000493}
494
Douglas Gregore942bbe2009-09-10 16:57:35 +0000495/// \brief Find the parent context of this context that will be
496/// used for unqualified name lookup.
497///
498/// Generally, the parent lookup context is the semantic context. However, for
499/// a friend function the parent lookup context is the lexical context, which
500/// is the class in which the friend is declared.
501DeclContext *DeclContext::getLookupParent() {
502 // FIXME: Find a better way to identify friends
503 if (isa<FunctionDecl>(this))
504 if (getParent()->getLookupContext()->isFileContext() &&
505 getLexicalParent()->getLookupContext()->isRecord())
506 return getLexicalParent();
507
508 return getParent();
509}
510
Douglas Gregorbc221632009-05-28 16:34:51 +0000511bool DeclContext::isDependentContext() const {
512 if (isFileContext())
513 return false;
514
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000515 if (isa<ClassTemplatePartialSpecializationDecl>(this))
516 return true;
517
Douglas Gregorbc221632009-05-28 16:34:51 +0000518 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
519 if (Record->getDescribedClassTemplate())
520 return true;
521
John McCall0c01d182010-03-24 05:22:00 +0000522 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000523 if (Function->getDescribedFunctionTemplate())
524 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000525
John McCall0c01d182010-03-24 05:22:00 +0000526 // Friend function declarations are dependent if their *lexical*
527 // context is dependent.
528 if (cast<Decl>(this)->getFriendObjectKind())
529 return getLexicalParent()->isDependentContext();
530 }
531
Douglas Gregorbc221632009-05-28 16:34:51 +0000532 return getParent() && getParent()->isDependentContext();
533}
534
Douglas Gregor074149e2009-01-05 19:45:36 +0000535bool DeclContext::isTransparentContext() const {
536 if (DeclKind == Decl::Enum)
537 return true; // FIXME: Check for C++0x scoped enums
538 else if (DeclKind == Decl::LinkageSpec)
539 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000540 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000541 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000542 else if (DeclKind == Decl::Namespace)
543 return false; // FIXME: Check for C++0x inline namespaces
544
545 return false;
546}
547
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000548bool DeclContext::Encloses(DeclContext *DC) {
549 if (getPrimaryContext() != this)
550 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000552 for (; DC; DC = DC->getParent())
553 if (DC->getPrimaryContext() == this)
554 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000555 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000556}
557
Steve Naroff0701bbb2009-01-08 17:28:14 +0000558DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000559 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000560 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000561 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000562 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000563 // There is only one DeclContext for these entities.
564 return this;
565
566 case Decl::Namespace:
567 // The original namespace is our primary context.
568 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
569
Douglas Gregor44b43212008-12-11 16:49:14 +0000570 case Decl::ObjCMethod:
571 return this;
572
573 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000574 case Decl::ObjCProtocol:
575 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000576 // FIXME: Can Objective-C interfaces be forward-declared?
577 return this;
578
Steve Naroff0701bbb2009-01-08 17:28:14 +0000579 case Decl::ObjCImplementation:
580 case Decl::ObjCCategoryImpl:
581 return this;
582
Douglas Gregor44b43212008-12-11 16:49:14 +0000583 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000584 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
585 // If this is a tag type that has a definition or is currently
586 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000587 TagDecl *Tag = cast<TagDecl>(this);
588 assert(isa<TagType>(Tag->TypeForDecl) ||
589 isa<InjectedClassNameType>(Tag->TypeForDecl));
590
591 if (TagDecl *Def = Tag->getDefinition())
592 return Def;
593
594 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
595 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
596 if (TagTy->isBeingDefined())
597 // FIXME: is it necessarily being defined in the decl
598 // that owns the type?
599 return TagTy->getDecl();
600 }
601
602 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000603 }
604
Douglas Gregor44b43212008-12-11 16:49:14 +0000605 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
606 "Unknown DeclContext kind");
607 return this;
608 }
609}
610
611DeclContext *DeclContext::getNextContext() {
612 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000613 case Decl::Namespace:
614 // Return the next namespace
615 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
616
617 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000618 return 0;
619 }
620}
621
Douglas Gregor2cf26342009-04-09 22:27:44 +0000622/// \brief Load the declarations within this lexical storage from an
623/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000624void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000625DeclContext::LoadLexicalDeclsFromExternalStorage() const {
626 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000627 assert(hasExternalLexicalStorage() && Source && "No external storage?");
628
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000629 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000630 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000631 Decls))
632 return;
633
634 // There is no longer any lexical storage in this context
635 ExternalLexicalStorage = false;
636
637 if (Decls.empty())
638 return;
639
640 // Resolve all of the declaration IDs into declarations, building up
641 // a chain of declarations via the Decl::NextDeclInContext field.
642 Decl *FirstNewDecl = 0;
643 Decl *PrevDecl = 0;
644 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
645 Decl *D = Source->GetDecl(Decls[I]);
646 if (PrevDecl)
647 PrevDecl->NextDeclInContext = D;
648 else
649 FirstNewDecl = D;
650
651 PrevDecl = D;
652 }
653
654 // Splice the newly-read declarations into the beginning of the list
655 // of declarations.
656 PrevDecl->NextDeclInContext = FirstDecl;
657 FirstDecl = FirstNewDecl;
658 if (!LastDecl)
659 LastDecl = PrevDecl;
660}
661
Mike Stump1eb44332009-09-09 15:08:12 +0000662void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000663DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000664 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000665 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000666 assert(hasExternalVisibleStorage() && Source && "No external storage?");
667
668 llvm::SmallVector<VisibleDeclaration, 64> Decls;
669 if (Source->ReadDeclsVisibleInContext(This, Decls))
670 return;
671
672 // There is no longer any visible storage in this context
673 ExternalVisibleStorage = false;
674
675 // Load the declaration IDs for all of the names visible in this
676 // context.
677 assert(!LookupPtr && "Have a lookup map before de-serialization?");
John McCall0c01d182010-03-24 05:22:00 +0000678 StoredDeclsMap *Map = CreateStoredDeclsMap(getParentASTContext());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000679 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
680 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
681 }
682}
683
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000684DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000685 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000686 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000687
688 // FIXME: Check whether we need to load some declarations from
689 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000690 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000691}
692
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000693DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000694 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000695 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000696
Mike Stump1eb44332009-09-09 15:08:12 +0000697 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000698}
699
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000700bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000701 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000702 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000703
704 return !FirstDecl;
705}
706
John McCall9f54ad42009-12-10 09:41:52 +0000707void DeclContext::removeDecl(Decl *D) {
708 assert(D->getLexicalDeclContext() == this &&
709 "decl being removed from non-lexical context");
710 assert((D->NextDeclInContext || D == LastDecl) &&
711 "decl is not in decls list");
712
713 // Remove D from the decl chain. This is O(n) but hopefully rare.
714 if (D == FirstDecl) {
715 if (D == LastDecl)
716 FirstDecl = LastDecl = 0;
717 else
718 FirstDecl = D->NextDeclInContext;
719 } else {
720 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
721 assert(I && "decl not found in linked list");
722 if (I->NextDeclInContext == D) {
723 I->NextDeclInContext = D->NextDeclInContext;
724 if (D == LastDecl) LastDecl = I;
725 break;
726 }
727 }
728 }
729
730 // Mark that D is no longer in the decl chain.
731 D->NextDeclInContext = 0;
732
733 // Remove D from the lookup table if necessary.
734 if (isa<NamedDecl>(D)) {
735 NamedDecl *ND = cast<NamedDecl>(D);
736
John McCall0c01d182010-03-24 05:22:00 +0000737 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
738 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000739
John McCall9f54ad42009-12-10 09:41:52 +0000740 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
741 assert(Pos != Map->end() && "no lookup entry for decl");
742 Pos->second.remove(ND);
743 }
744}
745
John McCall3f9a8a62009-08-11 06:59:38 +0000746void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000747 assert(D->getLexicalDeclContext() == this &&
748 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000749 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000750 "Decl already inserted into a DeclContext");
751
752 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000753 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000754 LastDecl = D;
755 } else {
756 FirstDecl = LastDecl = D;
757 }
John McCall3f9a8a62009-08-11 06:59:38 +0000758}
759
760void DeclContext::addDecl(Decl *D) {
761 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000762
763 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000764 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000765}
766
Douglas Gregor074149e2009-01-05 19:45:36 +0000767/// buildLookup - Build the lookup data structure with all of the
768/// declarations in DCtx (and any other contexts linked to it or
769/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000770void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000771 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000772 for (decl_iterator D = DCtx->decls_begin(),
773 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000774 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000775 // Insert this declaration into the lookup structure, but only
776 // if it's semantically in its decl context. During non-lazy
777 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000778 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000779 if (D->getDeclContext() == DCtx)
780 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000781
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000782 // Insert any forward-declared Objective-C interfaces into the lookup
783 // data structure.
784 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
785 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
786 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000787 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000788
Douglas Gregor074149e2009-01-05 19:45:36 +0000789 // If this declaration is itself a transparent declaration context,
790 // add its members (recursively).
791 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
792 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000793 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000794 }
795 }
796}
797
Mike Stump1eb44332009-09-09 15:08:12 +0000798DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000799DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000800 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000801 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000802 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000803
Douglas Gregor2cf26342009-04-09 22:27:44 +0000804 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000805 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000806
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000807 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000808 /// all of the linked DeclContexts (in declaration order!) and
809 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000810 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000811 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000812
Douglas Gregorc36c5402009-04-09 17:29:08 +0000813 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000814 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000815 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000816
John McCall0c01d182010-03-24 05:22:00 +0000817 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
818 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000819 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000820 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000821}
822
Mike Stump1eb44332009-09-09 15:08:12 +0000823DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000824DeclContext::lookup(DeclarationName Name) const {
825 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000826}
827
Chris Lattner0cf2b192009-03-27 19:19:59 +0000828DeclContext *DeclContext::getLookupContext() {
829 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000830 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000831 while (Ctx->isTransparentContext())
832 Ctx = Ctx->getParent();
833 return Ctx;
834}
835
Douglas Gregor88b70942009-02-25 22:02:03 +0000836DeclContext *DeclContext::getEnclosingNamespaceContext() {
837 DeclContext *Ctx = this;
838 // Skip through non-namespace, non-translation-unit contexts.
839 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
840 Ctx = Ctx->getParent();
841 return Ctx->getPrimaryContext();
842}
843
John McCallab88d972009-08-31 22:39:49 +0000844void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000845 // FIXME: This feels like a hack. Should DeclarationName support
846 // template-ids, or is there a better way to keep specializations
847 // from being visible?
848 if (isa<ClassTemplateSpecializationDecl>(D))
849 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000850 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
851 if (FD->isFunctionTemplateSpecialization())
852 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000853
Steve Naroff0701bbb2009-01-08 17:28:14 +0000854 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000855 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000856 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000857 return;
858 }
859
860 // If we already have a lookup data structure, perform the insertion
861 // into it. Otherwise, be lazy and don't build that structure until
862 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000863 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000864 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000865
Douglas Gregor074149e2009-01-05 19:45:36 +0000866 // If we are a transparent context, insert into our parent context,
867 // too. This operation is recursive.
868 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000869 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000870}
871
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000872void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000873 // Skip unnamed declarations.
874 if (!D->getDeclName())
875 return;
876
Douglas Gregorcc636682009-02-17 23:15:12 +0000877 // FIXME: This feels like a hack. Should DeclarationName support
878 // template-ids, or is there a better way to keep specializations
879 // from being visible?
880 if (isa<ClassTemplateSpecializationDecl>(D))
881 return;
882
Ted Kremenek3478eb62010-02-11 07:12:28 +0000883 ASTContext *C = 0;
884 if (!LookupPtr) {
885 C = &getParentASTContext();
John McCall0c01d182010-03-24 05:22:00 +0000886 CreateStoredDeclsMap(*C);
Ted Kremenek3478eb62010-02-11 07:12:28 +0000887 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000888
889 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000890 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000891 if (DeclNameEntries.isNull()) {
892 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000893 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000894 }
Chris Lattner91942502009-02-20 00:55:03 +0000895
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000896 // If it is possible that this is a redeclaration, check to see if there is
897 // already a decl for which declarationReplaces returns true. If there is
898 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +0000899 if (!C)
900 C = &getParentASTContext();
901
902 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000903 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000905 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000906 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000907}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000908
909/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
910/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000911DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000912DeclContext::getUsingDirectives() const {
913 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000914 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
915 reinterpret_cast<udir_iterator>(Result.second));
916}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000917
918void StoredDeclsList::materializeDecls(ASTContext &Context) {
919 if (isNull())
920 return;
921
922 switch ((DataKind)(Data & 0x03)) {
923 case DK_Decl:
924 case DK_Decl_Vector:
925 break;
926
927 case DK_DeclID: {
928 // Resolve this declaration ID to an actual declaration by
929 // querying the external AST source.
930 unsigned DeclID = Data >> 2;
931
932 ExternalASTSource *Source = Context.getExternalSource();
933 assert(Source && "No external AST source available!");
934
935 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
936 break;
937 }
938
939 case DK_ID_Vector: {
940 // We have a vector of declaration IDs. Resolve all of them to
941 // actual declarations.
942 VectorTy &Vector = *getAsVector();
943 ExternalASTSource *Source = Context.getExternalSource();
944 assert(Source && "No external AST source available!");
945
946 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
947 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
948
949 Data = (Data & ~0x03) | DK_Decl_Vector;
950 break;
951 }
952 }
953}
Ted Kremenek3478eb62010-02-11 07:12:28 +0000954
955//===----------------------------------------------------------------------===//
956// Creation and Destruction of StoredDeclsMaps. //
957//===----------------------------------------------------------------------===//
958
John McCall0c01d182010-03-24 05:22:00 +0000959StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
960 assert(!LookupPtr && "context already has a decls map");
961 assert(getPrimaryContext() == this &&
962 "creating decls map on non-primary context");
963
964 StoredDeclsMap *M;
965 bool Dependent = isDependentContext();
966 if (Dependent)
967 M = new DependentStoredDeclsMap();
968 else
969 M = new StoredDeclsMap();
970 M->Previous = C.LastSDM;
971 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
972 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +0000973 return M;
974}
975
976void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +0000977 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
978 // pointer because the subclass doesn't add anything that needs to
979 // be deleted.
980
981 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
982}
983
984void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
985 while (Map) {
986 // Advance the iteration before we invalidate memory.
987 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
988
989 if (Dependent)
990 delete static_cast<DependentStoredDeclsMap*>(Map);
991 else
992 delete Map;
993
994 Map = Next.getPointer();
995 Dependent = Next.getInt();
996 }
997}
998
John McCall0c01d182010-03-24 05:22:00 +0000999DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1000 DeclContext *Parent,
1001 const PartialDiagnostic &PDiag) {
1002 assert(Parent->isDependentContext()
1003 && "cannot iterate dependent diagnostics of non-dependent context");
1004 Parent = Parent->getPrimaryContext();
1005 if (!Parent->LookupPtr)
1006 Parent->CreateStoredDeclsMap(C);
1007
1008 DependentStoredDeclsMap *Map
1009 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1010
Douglas Gregorb8365182010-03-29 23:56:53 +00001011 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001012 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001013 PartialDiagnostic::Storage *DiagStorage = 0;
1014 if (PDiag.hasStorage())
1015 DiagStorage = new (C) PartialDiagnostic::Storage;
1016
1017 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001018
1019 // TODO: Maybe we shouldn't reverse the order during insertion.
1020 DD->NextDiagnostic = Map->FirstDiagnostic;
1021 Map->FirstDiagnostic = DD;
1022
1023 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001024}