blob: a73d17a5ca2eefb199c0bd2ca2593db06c0f6df8 [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
Sean Hunt9a555912010-05-30 07:21:58 +000038#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
39#define ABSTRACT_DECL(DECL)
40#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Eli Friedman56d29372008-06-07 16:52:53 +000044const char *Decl::getDeclKindName() const {
45 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000046 default: assert(0 && "Declaration not in DeclNodes.inc!");
47#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
48#define ABSTRACT_DECL(DECL)
49#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000050 }
51}
52
Douglas Gregor42738572010-03-05 00:26:45 +000053void Decl::setInvalidDecl(bool Invalid) {
54 InvalidDecl = Invalid;
55 if (Invalid) {
56 // Defensive maneuver for ill-formed code: we're likely not to make it to
57 // a point where we set the access specifier, so default it to "public"
58 // to avoid triggering asserts elsewhere in the front end.
59 setAccess(AS_public);
60 }
61}
62
Steve Naroff0a473932009-01-20 19:53:53 +000063const char *DeclContext::getDeclKindName() const {
64 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000065 default: assert(0 && "Declaration context not in DeclNodes.inc!");
66#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
67#define ABSTRACT_DECL(DECL)
68#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000069 }
70}
71
Eli Friedman56d29372008-06-07 16:52:53 +000072bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000073 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000074 return StatSwitch;
75}
76
77void Decl::PrintStats() {
78 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor64650af2009-02-02 23:39:07 +000080 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000081#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
82#define ABSTRACT_DECL(DECL)
83#include "clang/AST/DeclNodes.inc"
Douglas Gregor64650af2009-02-02 23:39:07 +000084 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Douglas Gregor64650af2009-02-02 23:39:07 +000086 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000087#define DECL(DERIVED, BASE) \
88 if (n##DERIVED##s > 0) { \
89 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
90 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
91 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
92 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor64650af2009-02-02 23:39:07 +000093 }
Sean Hunt9a555912010-05-30 07:21:58 +000094#define ABSTRACT_DECL(DECL)
95#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +000096
Douglas Gregor64650af2009-02-02 23:39:07 +000097 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000098}
99
Sean Hunt9a555912010-05-30 07:21:58 +0000100void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000101 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000102 default: assert(0 && "Declaration not in DeclNodes.inc!");
103#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
104#define ABSTRACT_DECL(DECL)
105#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000106 }
107}
108
Anders Carlsson67e33202009-06-13 00:08:58 +0000109bool Decl::isTemplateParameterPack() const {
110 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
111 return TTP->isParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Anders Carlsson67e33202009-06-13 00:08:58 +0000113 return false;
114}
115
Douglas Gregore53060f2009-06-25 22:08:12 +0000116bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000117 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000118 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Douglas Gregore53060f2009-06-25 22:08:12 +0000120 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
121}
122
Douglas Gregor79c22782010-01-16 20:21:20 +0000123bool Decl::isDefinedOutsideFunctionOrMethod() const {
124 for (const DeclContext *DC = getDeclContext();
125 DC && !DC->isTranslationUnit();
126 DC = DC->getParent())
127 if (DC->isFunctionOrMethod())
128 return false;
129
130 return true;
131}
132
133
Eli Friedman56d29372008-06-07 16:52:53 +0000134//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000135// PrettyStackTraceDecl Implementation
136//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Chris Lattner49f28ca2009-03-05 08:00:35 +0000138void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
139 SourceLocation TheLoc = Loc;
140 if (TheLoc.isInvalid() && TheDecl)
141 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattner49f28ca2009-03-05 08:00:35 +0000143 if (TheLoc.isValid()) {
144 TheLoc.print(OS, SM);
145 OS << ": ";
146 }
147
148 OS << Message;
149
Daniel Dunbarc5236562009-11-21 09:05:59 +0000150 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000151 OS << " '" << DN->getQualifiedNameAsString() << '\'';
152 OS << '\n';
153}
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Chris Lattner49f28ca2009-03-05 08:00:35 +0000155//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000156// Decl Implementation
157//===----------------------------------------------------------------------===//
158
Chris Lattner769dbdf2009-03-27 20:18:19 +0000159// Out-of-line virtual method providing a home for Decl.
160Decl::~Decl() {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000161 assert(!HasAttrs && "attributes should have been freed by Destroy");
162}
163
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000164void Decl::setDeclContext(DeclContext *DC) {
165 if (isOutOfSemaDC())
166 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattneree219fd2009-03-29 06:06:59 +0000168 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000169}
170
171void Decl::setLexicalDeclContext(DeclContext *DC) {
172 if (DC == getLexicalDeclContext())
173 return;
174
175 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000176 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000177 MDC->SemanticDC = getDeclContext();
178 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000179 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000180 } else {
181 getMultipleDC()->LexicalDC = DC;
182 }
183}
184
John McCall9aeed322009-10-01 00:25:31 +0000185bool Decl::isInAnonymousNamespace() const {
186 const DeclContext *DC = getDeclContext();
187 do {
188 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
189 if (ND->isAnonymousNamespace())
190 return true;
191 } while ((DC = DC->getParent()));
192
193 return false;
194}
195
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000196TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000197 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
198 return TUD;
199
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000200 DeclContext *DC = getDeclContext();
201 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000203 while (!DC->isTranslationUnit()) {
204 DC = DC->getParent();
205 assert(DC && "This decl is not contained in a translation unit!");
206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000208 return cast<TranslationUnitDecl>(DC);
209}
210
211ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000212 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000213}
214
Tanya Lattner12ead492010-02-17 02:17:21 +0000215bool Decl::isUsed() const {
216 if (Used)
217 return true;
218
219 // Check for used attribute.
220 if (hasAttr<UsedAttr>())
221 return true;
222
223 // Check redeclarations for used attribute.
224 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
225 if (I->hasAttr<UsedAttr>() || I->Used)
226 return true;
227 }
228
229 return false;
230}
231
232
Chris Lattner769dbdf2009-03-27 20:18:19 +0000233unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
234 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000235 case Function:
236 case CXXMethod:
237 case CXXConstructor:
238 case CXXDestructor:
239 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000240 case EnumConstant:
241 case Var:
242 case ImplicitParam:
243 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000244 case NonTypeTemplateParm:
245 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000246 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000247 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000248
John McCall0d6b1642010-04-23 18:46:30 +0000249 case ObjCCompatibleAlias:
250 case ObjCInterface:
251 return IDNS_Ordinary | IDNS_Type;
252
253 case Typedef:
254 case UnresolvedUsingTypename:
255 case TemplateTypeParm:
256 return IDNS_Ordinary | IDNS_Type;
257
John McCall9488ea12009-11-17 05:59:44 +0000258 case UsingShadow:
259 return 0; // we'll actually overwrite this later
260
John McCall7ba107a2009-11-18 02:36:19 +0000261 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000262 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000263
264 case Using:
265 return IDNS_Using;
266
Chris Lattner769dbdf2009-03-27 20:18:19 +0000267 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000268 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattner769dbdf2009-03-27 20:18:19 +0000270 case Field:
271 case ObjCAtDefsField:
272 case ObjCIvar:
273 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattner769dbdf2009-03-27 20:18:19 +0000275 case Record:
276 case CXXRecord:
277 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000278 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner769dbdf2009-03-27 20:18:19 +0000280 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000281 case NamespaceAlias:
282 return IDNS_Namespace;
283
Chris Lattner769dbdf2009-03-27 20:18:19 +0000284 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000285 return IDNS_Ordinary;
286
Chris Lattner769dbdf2009-03-27 20:18:19 +0000287 case ClassTemplate:
288 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000289 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner769dbdf2009-03-27 20:18:19 +0000291 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000292 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000293 case FriendTemplate:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000294 case LinkageSpec:
295 case FileScopeAsm:
296 case StaticAssert:
297 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000298 case ObjCPropertyImpl:
299 case ObjCForwardProtocol:
300 case Block:
301 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000302
Chris Lattner769dbdf2009-03-27 20:18:19 +0000303 case UsingDirective:
304 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000305 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000306 case ObjCImplementation:
307 case ObjCCategory:
308 case ObjCCategoryImpl:
309 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000310 return 0;
311 }
John McCall9488ea12009-11-17 05:59:44 +0000312
313 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000314}
315
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000316void Decl::addAttr(Attr *NewAttr) {
317 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000318
319 NewAttr->setNext(ExistingAttr);
320 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Eli Friedman56d29372008-06-07 16:52:53 +0000322 HasAttrs = true;
323}
324
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000325void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000326 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Eli Friedman56d29372008-06-07 16:52:53 +0000328 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000329 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000330}
331
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000332const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000333 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000334 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000335}
336
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000337void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000338 bool HasLHSAttr = this->HasAttrs;
339 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Eli Friedman56d29372008-06-07 16:52:53 +0000341 // Usually, neither decl has attrs, nothing to do.
342 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Eli Friedman56d29372008-06-07 16:52:53 +0000344 // If 'this' has no attrs, swap the other way.
345 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000346 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000348 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Eli Friedman56d29372008-06-07 16:52:53 +0000350 // Handle the case when both decls have attrs.
351 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000352 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000353 return;
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Eli Friedman56d29372008-06-07 16:52:53 +0000356 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000357 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
358 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000359 this->HasAttrs = false;
360 RHS->HasAttrs = true;
361}
362
363
Chris Lattnercc581472009-03-04 06:05:19 +0000364void Decl::Destroy(ASTContext &C) {
365 // Free attributes for this decl.
366 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000367 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000368 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000369 HasAttrs = false;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000372#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000373 // FIXME: Once ownership is fully understood, we can enable this code
374 if (DeclContext *DC = dyn_cast<DeclContext>(this))
375 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000376
Chris Lattner244a67d2009-03-28 06:04:26 +0000377 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000378 // within the loop, only the Destroy method for the first Decl
379 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Chris Lattner244a67d2009-03-28 06:04:26 +0000381 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000383 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000384 Decl* Tmp = N->getNextDeclInContext();
385 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000386 N->Destroy(C);
387 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000388 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000389
Ted Kremenek94a39002009-12-01 00:07:10 +0000390 if (isOutOfSemaDC())
391 delete (C) getMultipleDC();
392
Eli Friedman56d29372008-06-07 16:52:53 +0000393 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000394 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000395#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000396}
397
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000398Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000399 Decl::Kind DK = D->getDeclKind();
400 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000401#define DECL(NAME, BASE)
402#define DECL_CONTEXT(NAME) \
403 case Decl::NAME: \
404 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
405#define DECL_CONTEXT_BASE(NAME)
406#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000407 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000408#define DECL(NAME, BASE)
409#define DECL_CONTEXT_BASE(NAME) \
410 if (DK >= first##NAME && DK <= last##NAME) \
411 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
412#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000413 assert(false && "a decl that inherits DeclContext isn't handled");
414 return 0;
415 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000416}
417
418DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000419 Decl::Kind DK = D->getKind();
420 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000421#define DECL(NAME, BASE)
422#define DECL_CONTEXT(NAME) \
423 case Decl::NAME: \
424 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
425#define DECL_CONTEXT_BASE(NAME)
426#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000427 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000428#define DECL(NAME, BASE)
429#define DECL_CONTEXT_BASE(NAME) \
430 if (DK >= first##NAME && DK <= last##NAME) \
431 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
432#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000433 assert(false && "a decl that inherits DeclContext isn't handled");
434 return 0;
435 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000436}
437
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000438CompoundStmt* Decl::getCompoundBody() const {
439 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000440}
441
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000442SourceLocation Decl::getBodyRBrace() const {
443 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000444 if (!Body)
445 return SourceLocation();
446 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
447 return CS->getRBracLoc();
448 assert(isa<CXXTryStmt>(Body) &&
449 "Body can only be CompoundStmt or CXXTryStmt");
450 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
451}
452
Anders Carlsson1329c272009-03-25 23:38:06 +0000453#ifndef NDEBUG
454void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000455 // Suppress this check if any of the following hold:
456 // 1. this is the translation unit (and thus has no parent)
457 // 2. this is a template parameter (and thus doesn't belong to its context)
458 // 3. this is a ParmVarDecl (which can be in a record context during
459 // the brief period between its creation and the creation of the
460 // FunctionDecl)
461 // 4. the context is not a record
Anders Carlsson35eda442009-08-29 20:47:47 +0000462 if (isa<TranslationUnitDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000463 !isa<CXXRecordDecl>(getDeclContext()) ||
464 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000465 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
467 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000468 "Access specifier is AS_none inside a record decl");
469}
470
471#endif
472
Eli Friedman56d29372008-06-07 16:52:53 +0000473//===----------------------------------------------------------------------===//
474// DeclContext Implementation
475//===----------------------------------------------------------------------===//
476
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000477bool DeclContext::classof(const Decl *D) {
478 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000479#define DECL(NAME, BASE)
480#define DECL_CONTEXT(NAME) case Decl::NAME:
481#define DECL_CONTEXT_BASE(NAME)
482#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000483 return true;
484 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000485#define DECL(NAME, BASE)
486#define DECL_CONTEXT_BASE(NAME) \
487 if (D->getKind() >= Decl::first##NAME && \
488 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000489 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000490#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000491 return false;
492 }
493}
494
Douglas Gregor44b43212008-12-11 16:49:14 +0000495DeclContext::~DeclContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000496 // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
497 // ~DeclContext() is not guaranteed to be called when ASTContext uses
498 // a BumpPtrAllocator.
John McCall0c01d182010-03-24 05:22:00 +0000499 // delete LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000500}
501
502void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000503 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000504 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000505}
506
Douglas Gregore942bbe2009-09-10 16:57:35 +0000507/// \brief Find the parent context of this context that will be
508/// used for unqualified name lookup.
509///
510/// Generally, the parent lookup context is the semantic context. However, for
511/// a friend function the parent lookup context is the lexical context, which
512/// is the class in which the friend is declared.
513DeclContext *DeclContext::getLookupParent() {
514 // FIXME: Find a better way to identify friends
515 if (isa<FunctionDecl>(this))
516 if (getParent()->getLookupContext()->isFileContext() &&
517 getLexicalParent()->getLookupContext()->isRecord())
518 return getLexicalParent();
519
520 return getParent();
521}
522
Douglas Gregorbc221632009-05-28 16:34:51 +0000523bool DeclContext::isDependentContext() const {
524 if (isFileContext())
525 return false;
526
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000527 if (isa<ClassTemplatePartialSpecializationDecl>(this))
528 return true;
529
Douglas Gregorbc221632009-05-28 16:34:51 +0000530 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
531 if (Record->getDescribedClassTemplate())
532 return true;
533
John McCall0c01d182010-03-24 05:22:00 +0000534 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000535 if (Function->getDescribedFunctionTemplate())
536 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000537
John McCall0c01d182010-03-24 05:22:00 +0000538 // Friend function declarations are dependent if their *lexical*
539 // context is dependent.
540 if (cast<Decl>(this)->getFriendObjectKind())
541 return getLexicalParent()->isDependentContext();
542 }
543
Douglas Gregorbc221632009-05-28 16:34:51 +0000544 return getParent() && getParent()->isDependentContext();
545}
546
Douglas Gregor074149e2009-01-05 19:45:36 +0000547bool DeclContext::isTransparentContext() const {
548 if (DeclKind == Decl::Enum)
549 return true; // FIXME: Check for C++0x scoped enums
550 else if (DeclKind == Decl::LinkageSpec)
551 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000552 else if (DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000553 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000554 else if (DeclKind == Decl::Namespace)
555 return false; // FIXME: Check for C++0x inline namespaces
556
557 return false;
558}
559
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000560bool DeclContext::Encloses(DeclContext *DC) {
561 if (getPrimaryContext() != this)
562 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000564 for (; DC; DC = DC->getParent())
565 if (DC->getPrimaryContext() == this)
566 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000567 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000568}
569
Steve Naroff0701bbb2009-01-08 17:28:14 +0000570DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000571 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000572 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000573 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000574 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000575 // There is only one DeclContext for these entities.
576 return this;
577
578 case Decl::Namespace:
579 // The original namespace is our primary context.
580 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
581
Douglas Gregor44b43212008-12-11 16:49:14 +0000582 case Decl::ObjCMethod:
583 return this;
584
585 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000586 case Decl::ObjCProtocol:
587 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000588 // FIXME: Can Objective-C interfaces be forward-declared?
589 return this;
590
Steve Naroff0701bbb2009-01-08 17:28:14 +0000591 case Decl::ObjCImplementation:
592 case Decl::ObjCCategoryImpl:
593 return this;
594
Douglas Gregor44b43212008-12-11 16:49:14 +0000595 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000596 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000597 // If this is a tag type that has a definition or is currently
598 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000599 TagDecl *Tag = cast<TagDecl>(this);
600 assert(isa<TagType>(Tag->TypeForDecl) ||
601 isa<InjectedClassNameType>(Tag->TypeForDecl));
602
603 if (TagDecl *Def = Tag->getDefinition())
604 return Def;
605
606 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
607 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
608 if (TagTy->isBeingDefined())
609 // FIXME: is it necessarily being defined in the decl
610 // that owns the type?
611 return TagTy->getDecl();
612 }
613
614 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000615 }
616
Sean Hunt9a555912010-05-30 07:21:58 +0000617 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000618 "Unknown DeclContext kind");
619 return this;
620 }
621}
622
623DeclContext *DeclContext::getNextContext() {
624 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000625 case Decl::Namespace:
626 // Return the next namespace
627 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
628
629 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000630 return 0;
631 }
632}
633
Douglas Gregor2cf26342009-04-09 22:27:44 +0000634/// \brief Load the declarations within this lexical storage from an
635/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000636void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000637DeclContext::LoadLexicalDeclsFromExternalStorage() const {
638 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000639 assert(hasExternalLexicalStorage() && Source && "No external storage?");
640
John McCall76bd1f32010-06-01 09:23:16 +0000641 llvm::SmallVector<Decl*, 64> Decls;
642 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000643 return;
644
645 // There is no longer any lexical storage in this context
646 ExternalLexicalStorage = false;
647
648 if (Decls.empty())
649 return;
650
651 // Resolve all of the declaration IDs into declarations, building up
652 // a chain of declarations via the Decl::NextDeclInContext field.
653 Decl *FirstNewDecl = 0;
654 Decl *PrevDecl = 0;
655 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000656 Decl *D = Decls[I];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000657 if (PrevDecl)
658 PrevDecl->NextDeclInContext = D;
659 else
660 FirstNewDecl = D;
661
662 PrevDecl = D;
663 }
664
665 // Splice the newly-read declarations into the beginning of the list
666 // of declarations.
667 PrevDecl->NextDeclInContext = FirstDecl;
668 FirstDecl = FirstNewDecl;
669 if (!LastDecl)
670 LastDecl = PrevDecl;
671}
672
John McCall76bd1f32010-06-01 09:23:16 +0000673DeclContext::lookup_result
674ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
675 DeclarationName Name) {
676 ASTContext &Context = DC->getParentASTContext();
677 StoredDeclsMap *Map;
678 if (!(Map = DC->LookupPtr))
679 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000680
John McCall76bd1f32010-06-01 09:23:16 +0000681 StoredDeclsList &List = (*Map)[Name];
682 assert(List.isNull());
683 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000684
John McCall76bd1f32010-06-01 09:23:16 +0000685 return DeclContext::lookup_result();
686}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000687
John McCall76bd1f32010-06-01 09:23:16 +0000688DeclContext::lookup_result
689ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
690 const VisibleDeclaration &VD) {
691 ASTContext &Context = DC->getParentASTContext();
692 StoredDeclsMap *Map;
693 if (!(Map = DC->LookupPtr))
694 Map = DC->CreateStoredDeclsMap(Context);
695
696 StoredDeclsList &List = (*Map)[VD.Name];
697 List.setFromDeclIDs(VD.Declarations);
698 return List.getLookupResult(Context);
699}
700
701DeclContext::lookup_result
702ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
703 DeclarationName Name,
704 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
705 ASTContext &Context = DC->getParentASTContext();;
706
707 StoredDeclsMap *Map;
708 if (!(Map = DC->LookupPtr))
709 Map = DC->CreateStoredDeclsMap(Context);
710
711 StoredDeclsList &List = (*Map)[Name];
712 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
713 if (List.isNull())
714 List.setOnlyValue(Decls[I]);
715 else
716 List.AddSubsequentDecl(Decls[I]);
717 }
718
719 return List.getLookupResult(Context);
720}
721
722void ExternalASTSource::SetExternalVisibleDecls(const DeclContext *DC,
723 const llvm::SmallVectorImpl<VisibleDeclaration> &Decls) {
724 // There is no longer any visible storage in this context.
725 DC->ExternalVisibleStorage = false;
726
727 assert(!DC->LookupPtr && "Have a lookup map before de-serialization?");
728 StoredDeclsMap *Map = DC->CreateStoredDeclsMap(DC->getParentASTContext());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000729 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
730 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
731 }
732}
733
John McCall76bd1f32010-06-01 09:23:16 +0000734void ExternalASTSource::SetExternalVisibleDecls(const DeclContext *DC,
735 const llvm::SmallVectorImpl<NamedDecl*> &Decls) {
736 // There is no longer any visible storage in this context.
737 DC->ExternalVisibleStorage = false;
738
739 assert(!DC->LookupPtr && "Have a lookup map before de-serialization?");
740 StoredDeclsMap &Map = *DC->CreateStoredDeclsMap(DC->getParentASTContext());
741 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
742 StoredDeclsList &List = Map[Decls[I]->getDeclName()];
743 if (List.isNull())
744 List.setOnlyValue(Decls[I]);
745 else
746 List.AddSubsequentDecl(Decls[I]);
747 }
748}
749
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000750DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000751 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000752 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000753
754 // FIXME: Check whether we need to load some declarations from
755 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000756 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000757}
758
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000759DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000760 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000761 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000762
Mike Stump1eb44332009-09-09 15:08:12 +0000763 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000764}
765
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000766bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000767 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000768 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000769
770 return !FirstDecl;
771}
772
John McCall9f54ad42009-12-10 09:41:52 +0000773void DeclContext::removeDecl(Decl *D) {
774 assert(D->getLexicalDeclContext() == this &&
775 "decl being removed from non-lexical context");
776 assert((D->NextDeclInContext || D == LastDecl) &&
777 "decl is not in decls list");
778
779 // Remove D from the decl chain. This is O(n) but hopefully rare.
780 if (D == FirstDecl) {
781 if (D == LastDecl)
782 FirstDecl = LastDecl = 0;
783 else
784 FirstDecl = D->NextDeclInContext;
785 } else {
786 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
787 assert(I && "decl not found in linked list");
788 if (I->NextDeclInContext == D) {
789 I->NextDeclInContext = D->NextDeclInContext;
790 if (D == LastDecl) LastDecl = I;
791 break;
792 }
793 }
794 }
795
796 // Mark that D is no longer in the decl chain.
797 D->NextDeclInContext = 0;
798
799 // Remove D from the lookup table if necessary.
800 if (isa<NamedDecl>(D)) {
801 NamedDecl *ND = cast<NamedDecl>(D);
802
John McCall0c01d182010-03-24 05:22:00 +0000803 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
804 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000805
John McCall9f54ad42009-12-10 09:41:52 +0000806 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
807 assert(Pos != Map->end() && "no lookup entry for decl");
808 Pos->second.remove(ND);
809 }
810}
811
John McCall3f9a8a62009-08-11 06:59:38 +0000812void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000813 assert(D->getLexicalDeclContext() == this &&
814 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000815 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000816 "Decl already inserted into a DeclContext");
817
818 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000819 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000820 LastDecl = D;
821 } else {
822 FirstDecl = LastDecl = D;
823 }
John McCall3f9a8a62009-08-11 06:59:38 +0000824}
825
826void DeclContext::addDecl(Decl *D) {
827 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000828
829 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000830 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000831}
832
Douglas Gregor074149e2009-01-05 19:45:36 +0000833/// buildLookup - Build the lookup data structure with all of the
834/// declarations in DCtx (and any other contexts linked to it or
835/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000836void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000837 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000838 for (decl_iterator D = DCtx->decls_begin(),
839 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000840 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000841 // Insert this declaration into the lookup structure, but only
842 // if it's semantically in its decl context. During non-lazy
843 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000844 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000845 if (D->getDeclContext() == DCtx)
846 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000847
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000848 // Insert any forward-declared Objective-C interfaces into the lookup
849 // data structure.
850 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
851 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
852 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000853 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000854
Douglas Gregor074149e2009-01-05 19:45:36 +0000855 // If this declaration is itself a transparent declaration context,
856 // add its members (recursively).
857 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
858 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000859 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000860 }
861 }
862}
863
Mike Stump1eb44332009-09-09 15:08:12 +0000864DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000865DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000866 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000867 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000868 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000869
John McCall76bd1f32010-06-01 09:23:16 +0000870 if (hasExternalVisibleStorage()) {
871 // Check to see if we've already cached the lookup results.
872 if (LookupPtr) {
873 StoredDeclsMap::iterator I = LookupPtr->find(Name);
874 if (I != LookupPtr->end())
875 return I->second.getLookupResult(getParentASTContext());
876 }
877
878 ExternalASTSource *Source = getParentASTContext().getExternalSource();
879 return Source->FindExternalVisibleDeclsByName(this, Name);
880 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000881
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000882 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000883 /// all of the linked DeclContexts (in declaration order!) and
884 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000885 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000886 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000887
Douglas Gregorc36c5402009-04-09 17:29:08 +0000888 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000889 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000890 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000891
John McCall0c01d182010-03-24 05:22:00 +0000892 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
893 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000894 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000895 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000896}
897
Mike Stump1eb44332009-09-09 15:08:12 +0000898DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000899DeclContext::lookup(DeclarationName Name) const {
900 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000901}
902
Chris Lattner0cf2b192009-03-27 19:19:59 +0000903DeclContext *DeclContext::getLookupContext() {
904 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000905 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000906 while (Ctx->isTransparentContext())
907 Ctx = Ctx->getParent();
908 return Ctx;
909}
910
Douglas Gregor88b70942009-02-25 22:02:03 +0000911DeclContext *DeclContext::getEnclosingNamespaceContext() {
912 DeclContext *Ctx = this;
913 // Skip through non-namespace, non-translation-unit contexts.
914 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
915 Ctx = Ctx->getParent();
916 return Ctx->getPrimaryContext();
917}
918
John McCallab88d972009-08-31 22:39:49 +0000919void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000920 // FIXME: This feels like a hack. Should DeclarationName support
921 // template-ids, or is there a better way to keep specializations
922 // from being visible?
923 if (isa<ClassTemplateSpecializationDecl>(D))
924 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000925 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
926 if (FD->isFunctionTemplateSpecialization())
927 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000928
Steve Naroff0701bbb2009-01-08 17:28:14 +0000929 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000930 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000931 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000932 return;
933 }
934
935 // If we already have a lookup data structure, perform the insertion
936 // into it. Otherwise, be lazy and don't build that structure until
937 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000938 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000939 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000940
Douglas Gregor074149e2009-01-05 19:45:36 +0000941 // If we are a transparent context, insert into our parent context,
942 // too. This operation is recursive.
943 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000944 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000945}
946
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000947void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000948 // Skip unnamed declarations.
949 if (!D->getDeclName())
950 return;
951
Douglas Gregorcc636682009-02-17 23:15:12 +0000952 // FIXME: This feels like a hack. Should DeclarationName support
953 // template-ids, or is there a better way to keep specializations
954 // from being visible?
955 if (isa<ClassTemplateSpecializationDecl>(D))
956 return;
957
Ted Kremenek3478eb62010-02-11 07:12:28 +0000958 ASTContext *C = 0;
959 if (!LookupPtr) {
960 C = &getParentASTContext();
John McCall0c01d182010-03-24 05:22:00 +0000961 CreateStoredDeclsMap(*C);
Ted Kremenek3478eb62010-02-11 07:12:28 +0000962 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000963
964 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000965 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000966 if (DeclNameEntries.isNull()) {
967 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000968 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000969 }
Chris Lattner91942502009-02-20 00:55:03 +0000970
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000971 // If it is possible that this is a redeclaration, check to see if there is
972 // already a decl for which declarationReplaces returns true. If there is
973 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +0000974 if (!C)
975 C = &getParentASTContext();
976
977 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000978 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000980 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000981 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000982}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000983
984/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
985/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000986DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000987DeclContext::getUsingDirectives() const {
988 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000989 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
990 reinterpret_cast<udir_iterator>(Result.second));
991}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000992
993void StoredDeclsList::materializeDecls(ASTContext &Context) {
994 if (isNull())
995 return;
996
997 switch ((DataKind)(Data & 0x03)) {
998 case DK_Decl:
999 case DK_Decl_Vector:
1000 break;
1001
1002 case DK_DeclID: {
1003 // Resolve this declaration ID to an actual declaration by
1004 // querying the external AST source.
1005 unsigned DeclID = Data >> 2;
1006
1007 ExternalASTSource *Source = Context.getExternalSource();
1008 assert(Source && "No external AST source available!");
1009
John McCall76bd1f32010-06-01 09:23:16 +00001010 Data = reinterpret_cast<uintptr_t>(Source->GetExternalDecl(DeclID));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001011 break;
1012 }
1013
1014 case DK_ID_Vector: {
1015 // We have a vector of declaration IDs. Resolve all of them to
1016 // actual declarations.
1017 VectorTy &Vector = *getAsVector();
1018 ExternalASTSource *Source = Context.getExternalSource();
1019 assert(Source && "No external AST source available!");
1020
1021 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
John McCall76bd1f32010-06-01 09:23:16 +00001022 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetExternalDecl(Vector[I]));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001023
1024 Data = (Data & ~0x03) | DK_Decl_Vector;
1025 break;
1026 }
1027 }
1028}
Ted Kremenek3478eb62010-02-11 07:12:28 +00001029
1030//===----------------------------------------------------------------------===//
1031// Creation and Destruction of StoredDeclsMaps. //
1032//===----------------------------------------------------------------------===//
1033
John McCall0c01d182010-03-24 05:22:00 +00001034StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1035 assert(!LookupPtr && "context already has a decls map");
1036 assert(getPrimaryContext() == this &&
1037 "creating decls map on non-primary context");
1038
1039 StoredDeclsMap *M;
1040 bool Dependent = isDependentContext();
1041 if (Dependent)
1042 M = new DependentStoredDeclsMap();
1043 else
1044 M = new StoredDeclsMap();
1045 M->Previous = C.LastSDM;
1046 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1047 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001048 return M;
1049}
1050
1051void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001052 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1053 // pointer because the subclass doesn't add anything that needs to
1054 // be deleted.
1055
1056 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1057}
1058
1059void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1060 while (Map) {
1061 // Advance the iteration before we invalidate memory.
1062 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1063
1064 if (Dependent)
1065 delete static_cast<DependentStoredDeclsMap*>(Map);
1066 else
1067 delete Map;
1068
1069 Map = Next.getPointer();
1070 Dependent = Next.getInt();
1071 }
1072}
1073
John McCall0c01d182010-03-24 05:22:00 +00001074DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1075 DeclContext *Parent,
1076 const PartialDiagnostic &PDiag) {
1077 assert(Parent->isDependentContext()
1078 && "cannot iterate dependent diagnostics of non-dependent context");
1079 Parent = Parent->getPrimaryContext();
1080 if (!Parent->LookupPtr)
1081 Parent->CreateStoredDeclsMap(C);
1082
1083 DependentStoredDeclsMap *Map
1084 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1085
Douglas Gregorb8365182010-03-29 23:56:53 +00001086 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001087 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001088 PartialDiagnostic::Storage *DiagStorage = 0;
1089 if (PDiag.hasStorage())
1090 DiagStorage = new (C) PartialDiagnostic::Storage;
1091
1092 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001093
1094 // TODO: Maybe we shouldn't reverse the order during insertion.
1095 DD->NextDiagnostic = Map->FirstDiagnostic;
1096 Map->FirstDiagnostic = DD;
1097
1098 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001099}