blob: 61d22b9d7075ae8cf6c02204905d798985aaa3e7 [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:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000234 case Typedef:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000235 case EnumConstant:
236 case Var:
237 case ImplicitParam:
238 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000239 case NonTypeTemplateParm:
240 case ObjCMethod:
John McCalld04efc92010-04-23 02:41:41 +0000241 case ObjCInterface:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000242 case ObjCProperty:
243 case ObjCCompatibleAlias:
244 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000245
John McCall9488ea12009-11-17 05:59:44 +0000246 case UsingShadow:
247 return 0; // we'll actually overwrite this later
248
John McCall7ba107a2009-11-18 02:36:19 +0000249 case UnresolvedUsingValue:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000250 case UnresolvedUsingTypename:
John McCall7ba107a2009-11-18 02:36:19 +0000251 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000252
253 case Using:
254 return IDNS_Using;
255
Chris Lattner769dbdf2009-03-27 20:18:19 +0000256 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000257 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattner769dbdf2009-03-27 20:18:19 +0000259 case Field:
260 case ObjCAtDefsField:
261 case ObjCIvar:
262 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattner769dbdf2009-03-27 20:18:19 +0000264 case Record:
265 case CXXRecord:
266 case Enum:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000267 case TemplateTypeParm:
268 return IDNS_Tag;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattner769dbdf2009-03-27 20:18:19 +0000270 case Namespace:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000271 case FunctionTemplate:
272 case ClassTemplate:
273 case TemplateTemplateParm:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000274 case NamespaceAlias:
275 return IDNS_Tag | IDNS_Ordinary;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner769dbdf2009-03-27 20:18:19 +0000277 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000278 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000279 case FriendTemplate:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000280 case LinkageSpec:
281 case FileScopeAsm:
282 case StaticAssert:
283 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000284 case ObjCPropertyImpl:
285 case ObjCForwardProtocol:
286 case Block:
287 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000288
Chris Lattner769dbdf2009-03-27 20:18:19 +0000289 case UsingDirective:
290 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000291 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000292 case ObjCImplementation:
293 case ObjCCategory:
294 case ObjCCategoryImpl:
295 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000296 return 0;
297 }
John McCall9488ea12009-11-17 05:59:44 +0000298
299 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000300}
301
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000302void Decl::addAttr(Attr *NewAttr) {
303 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000304
305 NewAttr->setNext(ExistingAttr);
306 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Eli Friedman56d29372008-06-07 16:52:53 +0000308 HasAttrs = true;
309}
310
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000311void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000312 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Eli Friedman56d29372008-06-07 16:52:53 +0000314 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000315 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000316}
317
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000318const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000319 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000320 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000321}
322
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000323void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000324 bool HasLHSAttr = this->HasAttrs;
325 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Eli Friedman56d29372008-06-07 16:52:53 +0000327 // Usually, neither decl has attrs, nothing to do.
328 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Eli Friedman56d29372008-06-07 16:52:53 +0000330 // If 'this' has no attrs, swap the other way.
331 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000332 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000334 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Eli Friedman56d29372008-06-07 16:52:53 +0000336 // Handle the case when both decls have attrs.
337 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000338 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000339 return;
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Eli Friedman56d29372008-06-07 16:52:53 +0000342 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000343 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
344 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000345 this->HasAttrs = false;
346 RHS->HasAttrs = true;
347}
348
349
Chris Lattnercc581472009-03-04 06:05:19 +0000350void Decl::Destroy(ASTContext &C) {
351 // Free attributes for this decl.
352 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000353 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000354 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000355 HasAttrs = false;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000358#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000359 // FIXME: Once ownership is fully understood, we can enable this code
360 if (DeclContext *DC = dyn_cast<DeclContext>(this))
361 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000362
Chris Lattner244a67d2009-03-28 06:04:26 +0000363 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000364 // within the loop, only the Destroy method for the first Decl
365 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Chris Lattner244a67d2009-03-28 06:04:26 +0000367 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000369 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000370 Decl* Tmp = N->getNextDeclInContext();
371 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000372 N->Destroy(C);
373 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000374 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000375
Ted Kremenek94a39002009-12-01 00:07:10 +0000376 if (isOutOfSemaDC())
377 delete (C) getMultipleDC();
378
Eli Friedman56d29372008-06-07 16:52:53 +0000379 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000380 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000381#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000382}
383
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000384Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000385 Decl::Kind DK = D->getDeclKind();
386 switch(DK) {
387#define DECL_CONTEXT(Name) \
388 case Decl::Name: \
389 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
390#define DECL_CONTEXT_BASE(Name)
391#include "clang/AST/DeclNodes.def"
392 default:
393#define DECL_CONTEXT_BASE(Name) \
394 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
395 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
396#include "clang/AST/DeclNodes.def"
397 assert(false && "a decl that inherits DeclContext isn't handled");
398 return 0;
399 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000400}
401
402DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000403 Decl::Kind DK = D->getKind();
404 switch(DK) {
405#define DECL_CONTEXT(Name) \
406 case Decl::Name: \
407 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
408#define DECL_CONTEXT_BASE(Name)
409#include "clang/AST/DeclNodes.def"
410 default:
411#define DECL_CONTEXT_BASE(Name) \
412 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
413 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
414#include "clang/AST/DeclNodes.def"
415 assert(false && "a decl that inherits DeclContext isn't handled");
416 return 0;
417 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000418}
419
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000420CompoundStmt* Decl::getCompoundBody() const {
421 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000422}
423
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000424SourceLocation Decl::getBodyRBrace() const {
425 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000426 if (!Body)
427 return SourceLocation();
428 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
429 return CS->getRBracLoc();
430 assert(isa<CXXTryStmt>(Body) &&
431 "Body can only be CompoundStmt or CXXTryStmt");
432 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
433}
434
Anders Carlsson1329c272009-03-25 23:38:06 +0000435#ifndef NDEBUG
436void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000437 // Suppress this check if any of the following hold:
438 // 1. this is the translation unit (and thus has no parent)
439 // 2. this is a template parameter (and thus doesn't belong to its context)
440 // 3. this is a ParmVarDecl (which can be in a record context during
441 // the brief period between its creation and the creation of the
442 // FunctionDecl)
443 // 4. the context is not a record
Anders Carlsson35eda442009-08-29 20:47:47 +0000444 if (isa<TranslationUnitDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000445 !isa<CXXRecordDecl>(getDeclContext()) ||
446 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000447 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
449 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000450 "Access specifier is AS_none inside a record decl");
451}
452
453#endif
454
Eli Friedman56d29372008-06-07 16:52:53 +0000455//===----------------------------------------------------------------------===//
456// DeclContext Implementation
457//===----------------------------------------------------------------------===//
458
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000459bool DeclContext::classof(const Decl *D) {
460 switch (D->getKind()) {
461#define DECL_CONTEXT(Name) case Decl::Name:
462#define DECL_CONTEXT_BASE(Name)
463#include "clang/AST/DeclNodes.def"
464 return true;
465 default:
466#define DECL_CONTEXT_BASE(Name) \
467 if (D->getKind() >= Decl::Name##First && \
468 D->getKind() <= Decl::Name##Last) \
469 return true;
470#include "clang/AST/DeclNodes.def"
471 return false;
472 }
473}
474
Douglas Gregor44b43212008-12-11 16:49:14 +0000475DeclContext::~DeclContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000476 // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
477 // ~DeclContext() is not guaranteed to be called when ASTContext uses
478 // a BumpPtrAllocator.
John McCall0c01d182010-03-24 05:22:00 +0000479 // delete LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000480}
481
482void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000483 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000484 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000485}
486
Douglas Gregore942bbe2009-09-10 16:57:35 +0000487/// \brief Find the parent context of this context that will be
488/// used for unqualified name lookup.
489///
490/// Generally, the parent lookup context is the semantic context. However, for
491/// a friend function the parent lookup context is the lexical context, which
492/// is the class in which the friend is declared.
493DeclContext *DeclContext::getLookupParent() {
494 // FIXME: Find a better way to identify friends
495 if (isa<FunctionDecl>(this))
496 if (getParent()->getLookupContext()->isFileContext() &&
497 getLexicalParent()->getLookupContext()->isRecord())
498 return getLexicalParent();
499
500 return getParent();
501}
502
Douglas Gregorbc221632009-05-28 16:34:51 +0000503bool DeclContext::isDependentContext() const {
504 if (isFileContext())
505 return false;
506
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000507 if (isa<ClassTemplatePartialSpecializationDecl>(this))
508 return true;
509
Douglas Gregorbc221632009-05-28 16:34:51 +0000510 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
511 if (Record->getDescribedClassTemplate())
512 return true;
513
John McCall0c01d182010-03-24 05:22:00 +0000514 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000515 if (Function->getDescribedFunctionTemplate())
516 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000517
John McCall0c01d182010-03-24 05:22:00 +0000518 // Friend function declarations are dependent if their *lexical*
519 // context is dependent.
520 if (cast<Decl>(this)->getFriendObjectKind())
521 return getLexicalParent()->isDependentContext();
522 }
523
Douglas Gregorbc221632009-05-28 16:34:51 +0000524 return getParent() && getParent()->isDependentContext();
525}
526
Douglas Gregor074149e2009-01-05 19:45:36 +0000527bool DeclContext::isTransparentContext() const {
528 if (DeclKind == Decl::Enum)
529 return true; // FIXME: Check for C++0x scoped enums
530 else if (DeclKind == Decl::LinkageSpec)
531 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000532 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000533 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000534 else if (DeclKind == Decl::Namespace)
535 return false; // FIXME: Check for C++0x inline namespaces
536
537 return false;
538}
539
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000540bool DeclContext::Encloses(DeclContext *DC) {
541 if (getPrimaryContext() != this)
542 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000544 for (; DC; DC = DC->getParent())
545 if (DC->getPrimaryContext() == this)
546 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000547 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000548}
549
Steve Naroff0701bbb2009-01-08 17:28:14 +0000550DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000551 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000552 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000553 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000554 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000555 // There is only one DeclContext for these entities.
556 return this;
557
558 case Decl::Namespace:
559 // The original namespace is our primary context.
560 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
561
Douglas Gregor44b43212008-12-11 16:49:14 +0000562 case Decl::ObjCMethod:
563 return this;
564
565 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000566 case Decl::ObjCProtocol:
567 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000568 // FIXME: Can Objective-C interfaces be forward-declared?
569 return this;
570
Steve Naroff0701bbb2009-01-08 17:28:14 +0000571 case Decl::ObjCImplementation:
572 case Decl::ObjCCategoryImpl:
573 return this;
574
Douglas Gregor44b43212008-12-11 16:49:14 +0000575 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000576 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
577 // If this is a tag type that has a definition or is currently
578 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000579 TagDecl *Tag = cast<TagDecl>(this);
580 assert(isa<TagType>(Tag->TypeForDecl) ||
581 isa<InjectedClassNameType>(Tag->TypeForDecl));
582
583 if (TagDecl *Def = Tag->getDefinition())
584 return Def;
585
586 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
587 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
588 if (TagTy->isBeingDefined())
589 // FIXME: is it necessarily being defined in the decl
590 // that owns the type?
591 return TagTy->getDecl();
592 }
593
594 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000595 }
596
Douglas Gregor44b43212008-12-11 16:49:14 +0000597 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
598 "Unknown DeclContext kind");
599 return this;
600 }
601}
602
603DeclContext *DeclContext::getNextContext() {
604 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000605 case Decl::Namespace:
606 // Return the next namespace
607 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
608
609 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000610 return 0;
611 }
612}
613
Douglas Gregor2cf26342009-04-09 22:27:44 +0000614/// \brief Load the declarations within this lexical storage from an
615/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000616void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000617DeclContext::LoadLexicalDeclsFromExternalStorage() const {
618 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000619 assert(hasExternalLexicalStorage() && Source && "No external storage?");
620
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000621 llvm::SmallVector<uint32_t, 64> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000622 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000623 Decls))
624 return;
625
626 // There is no longer any lexical storage in this context
627 ExternalLexicalStorage = false;
628
629 if (Decls.empty())
630 return;
631
632 // Resolve all of the declaration IDs into declarations, building up
633 // a chain of declarations via the Decl::NextDeclInContext field.
634 Decl *FirstNewDecl = 0;
635 Decl *PrevDecl = 0;
636 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
637 Decl *D = Source->GetDecl(Decls[I]);
638 if (PrevDecl)
639 PrevDecl->NextDeclInContext = D;
640 else
641 FirstNewDecl = D;
642
643 PrevDecl = D;
644 }
645
646 // Splice the newly-read declarations into the beginning of the list
647 // of declarations.
648 PrevDecl->NextDeclInContext = FirstDecl;
649 FirstDecl = FirstNewDecl;
650 if (!LastDecl)
651 LastDecl = PrevDecl;
652}
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000655DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000656 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000657 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000658 assert(hasExternalVisibleStorage() && Source && "No external storage?");
659
660 llvm::SmallVector<VisibleDeclaration, 64> Decls;
661 if (Source->ReadDeclsVisibleInContext(This, Decls))
662 return;
663
664 // There is no longer any visible storage in this context
665 ExternalVisibleStorage = false;
666
667 // Load the declaration IDs for all of the names visible in this
668 // context.
669 assert(!LookupPtr && "Have a lookup map before de-serialization?");
John McCall0c01d182010-03-24 05:22:00 +0000670 StoredDeclsMap *Map = CreateStoredDeclsMap(getParentASTContext());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000671 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
672 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
673 }
674}
675
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000676DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000677 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000678 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000679
680 // FIXME: Check whether we need to load some declarations from
681 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000682 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000683}
684
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000685DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000686 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000687 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000688
Mike Stump1eb44332009-09-09 15:08:12 +0000689 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000690}
691
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000692bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000693 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000694 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000695
696 return !FirstDecl;
697}
698
John McCall9f54ad42009-12-10 09:41:52 +0000699void DeclContext::removeDecl(Decl *D) {
700 assert(D->getLexicalDeclContext() == this &&
701 "decl being removed from non-lexical context");
702 assert((D->NextDeclInContext || D == LastDecl) &&
703 "decl is not in decls list");
704
705 // Remove D from the decl chain. This is O(n) but hopefully rare.
706 if (D == FirstDecl) {
707 if (D == LastDecl)
708 FirstDecl = LastDecl = 0;
709 else
710 FirstDecl = D->NextDeclInContext;
711 } else {
712 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
713 assert(I && "decl not found in linked list");
714 if (I->NextDeclInContext == D) {
715 I->NextDeclInContext = D->NextDeclInContext;
716 if (D == LastDecl) LastDecl = I;
717 break;
718 }
719 }
720 }
721
722 // Mark that D is no longer in the decl chain.
723 D->NextDeclInContext = 0;
724
725 // Remove D from the lookup table if necessary.
726 if (isa<NamedDecl>(D)) {
727 NamedDecl *ND = cast<NamedDecl>(D);
728
John McCall0c01d182010-03-24 05:22:00 +0000729 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
730 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000731
John McCall9f54ad42009-12-10 09:41:52 +0000732 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
733 assert(Pos != Map->end() && "no lookup entry for decl");
734 Pos->second.remove(ND);
735 }
736}
737
John McCall3f9a8a62009-08-11 06:59:38 +0000738void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000739 assert(D->getLexicalDeclContext() == this &&
740 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000741 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000742 "Decl already inserted into a DeclContext");
743
744 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000745 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000746 LastDecl = D;
747 } else {
748 FirstDecl = LastDecl = D;
749 }
John McCall3f9a8a62009-08-11 06:59:38 +0000750}
751
752void DeclContext::addDecl(Decl *D) {
753 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000754
755 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000756 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000757}
758
Douglas Gregor074149e2009-01-05 19:45:36 +0000759/// buildLookup - Build the lookup data structure with all of the
760/// declarations in DCtx (and any other contexts linked to it or
761/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000762void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000763 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000764 for (decl_iterator D = DCtx->decls_begin(),
765 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000766 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000767 // Insert this declaration into the lookup structure, but only
768 // if it's semantically in its decl context. During non-lazy
769 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000770 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000771 if (D->getDeclContext() == DCtx)
772 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000773
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000774 // Insert any forward-declared Objective-C interfaces into the lookup
775 // data structure.
776 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
777 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
778 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000779 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000780
Douglas Gregor074149e2009-01-05 19:45:36 +0000781 // If this declaration is itself a transparent declaration context,
782 // add its members (recursively).
783 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
784 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000785 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000786 }
787 }
788}
789
Mike Stump1eb44332009-09-09 15:08:12 +0000790DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000791DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000792 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000793 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000794 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000795
Douglas Gregor2cf26342009-04-09 22:27:44 +0000796 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000797 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000798
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000799 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000800 /// all of the linked DeclContexts (in declaration order!) and
801 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000802 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000803 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000804
Douglas Gregorc36c5402009-04-09 17:29:08 +0000805 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000806 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000807 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000808
John McCall0c01d182010-03-24 05:22:00 +0000809 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
810 if (Pos == LookupPtr->end())
Douglas Gregorc36c5402009-04-09 17:29:08 +0000811 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000812 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000813}
814
Mike Stump1eb44332009-09-09 15:08:12 +0000815DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816DeclContext::lookup(DeclarationName Name) const {
817 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000818}
819
Chris Lattner0cf2b192009-03-27 19:19:59 +0000820DeclContext *DeclContext::getLookupContext() {
821 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000822 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000823 while (Ctx->isTransparentContext())
824 Ctx = Ctx->getParent();
825 return Ctx;
826}
827
Douglas Gregor88b70942009-02-25 22:02:03 +0000828DeclContext *DeclContext::getEnclosingNamespaceContext() {
829 DeclContext *Ctx = this;
830 // Skip through non-namespace, non-translation-unit contexts.
831 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
832 Ctx = Ctx->getParent();
833 return Ctx->getPrimaryContext();
834}
835
John McCallab88d972009-08-31 22:39:49 +0000836void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000837 // FIXME: This feels like a hack. Should DeclarationName support
838 // template-ids, or is there a better way to keep specializations
839 // from being visible?
840 if (isa<ClassTemplateSpecializationDecl>(D))
841 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000842 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
843 if (FD->isFunctionTemplateSpecialization())
844 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000845
Steve Naroff0701bbb2009-01-08 17:28:14 +0000846 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000847 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000848 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000849 return;
850 }
851
852 // If we already have a lookup data structure, perform the insertion
853 // into it. Otherwise, be lazy and don't build that structure until
854 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000855 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000856 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000857
Douglas Gregor074149e2009-01-05 19:45:36 +0000858 // If we are a transparent context, insert into our parent context,
859 // too. This operation is recursive.
860 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000861 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000862}
863
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000864void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000865 // Skip unnamed declarations.
866 if (!D->getDeclName())
867 return;
868
Douglas Gregorcc636682009-02-17 23:15:12 +0000869 // FIXME: This feels like a hack. Should DeclarationName support
870 // template-ids, or is there a better way to keep specializations
871 // from being visible?
872 if (isa<ClassTemplateSpecializationDecl>(D))
873 return;
874
Ted Kremenek3478eb62010-02-11 07:12:28 +0000875 ASTContext *C = 0;
876 if (!LookupPtr) {
877 C = &getParentASTContext();
John McCall0c01d182010-03-24 05:22:00 +0000878 CreateStoredDeclsMap(*C);
Ted Kremenek3478eb62010-02-11 07:12:28 +0000879 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000880
881 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000882 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000883 if (DeclNameEntries.isNull()) {
884 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000885 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000886 }
Chris Lattner91942502009-02-20 00:55:03 +0000887
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000888 // If it is possible that this is a redeclaration, check to see if there is
889 // already a decl for which declarationReplaces returns true. If there is
890 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +0000891 if (!C)
892 C = &getParentASTContext();
893
894 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000895 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000897 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000898 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000899}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000900
901/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
902/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000903DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000904DeclContext::getUsingDirectives() const {
905 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000906 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
907 reinterpret_cast<udir_iterator>(Result.second));
908}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000909
910void StoredDeclsList::materializeDecls(ASTContext &Context) {
911 if (isNull())
912 return;
913
914 switch ((DataKind)(Data & 0x03)) {
915 case DK_Decl:
916 case DK_Decl_Vector:
917 break;
918
919 case DK_DeclID: {
920 // Resolve this declaration ID to an actual declaration by
921 // querying the external AST source.
922 unsigned DeclID = Data >> 2;
923
924 ExternalASTSource *Source = Context.getExternalSource();
925 assert(Source && "No external AST source available!");
926
927 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
928 break;
929 }
930
931 case DK_ID_Vector: {
932 // We have a vector of declaration IDs. Resolve all of them to
933 // actual declarations.
934 VectorTy &Vector = *getAsVector();
935 ExternalASTSource *Source = Context.getExternalSource();
936 assert(Source && "No external AST source available!");
937
938 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
939 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
940
941 Data = (Data & ~0x03) | DK_Decl_Vector;
942 break;
943 }
944 }
945}
Ted Kremenek3478eb62010-02-11 07:12:28 +0000946
947//===----------------------------------------------------------------------===//
948// Creation and Destruction of StoredDeclsMaps. //
949//===----------------------------------------------------------------------===//
950
John McCall0c01d182010-03-24 05:22:00 +0000951StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
952 assert(!LookupPtr && "context already has a decls map");
953 assert(getPrimaryContext() == this &&
954 "creating decls map on non-primary context");
955
956 StoredDeclsMap *M;
957 bool Dependent = isDependentContext();
958 if (Dependent)
959 M = new DependentStoredDeclsMap();
960 else
961 M = new StoredDeclsMap();
962 M->Previous = C.LastSDM;
963 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
964 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +0000965 return M;
966}
967
968void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +0000969 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
970 // pointer because the subclass doesn't add anything that needs to
971 // be deleted.
972
973 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
974}
975
976void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
977 while (Map) {
978 // Advance the iteration before we invalidate memory.
979 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
980
981 if (Dependent)
982 delete static_cast<DependentStoredDeclsMap*>(Map);
983 else
984 delete Map;
985
986 Map = Next.getPointer();
987 Dependent = Next.getInt();
988 }
989}
990
John McCall0c01d182010-03-24 05:22:00 +0000991DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
992 DeclContext *Parent,
993 const PartialDiagnostic &PDiag) {
994 assert(Parent->isDependentContext()
995 && "cannot iterate dependent diagnostics of non-dependent context");
996 Parent = Parent->getPrimaryContext();
997 if (!Parent->LookupPtr)
998 Parent->CreateStoredDeclsMap(C);
999
1000 DependentStoredDeclsMap *Map
1001 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1002
Douglas Gregorb8365182010-03-29 23:56:53 +00001003 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001004 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001005 PartialDiagnostic::Storage *DiagStorage = 0;
1006 if (PDiag.hasStorage())
1007 DiagStorage = new (C) PartialDiagnostic::Storage;
1008
1009 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001010
1011 // TODO: Maybe we shouldn't reverse the order during insertion.
1012 DD->NextDiagnostic = Map->FirstDiagnostic;
1013 Map->FirstDiagnostic = DD;
1014
1015 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001016}