blob: 0821da3b87d16fb076e76182afd8b83a504866cb [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
Douglas Gregorc070cc62010-06-17 23:14:26 +0000215bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000216 if (Used)
217 return true;
218
219 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000220 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000221 return true;
222
223 // Check redeclarations for used attribute.
224 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000225 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000226 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:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000294 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000295 case LinkageSpec:
296 case FileScopeAsm:
297 case StaticAssert:
298 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000299 case ObjCPropertyImpl:
300 case ObjCForwardProtocol:
301 case Block:
302 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000303
Chris Lattner769dbdf2009-03-27 20:18:19 +0000304 case UsingDirective:
305 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000306 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000307 case ObjCImplementation:
308 case ObjCCategory:
309 case ObjCCategoryImpl:
310 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000311 return 0;
312 }
John McCall9488ea12009-11-17 05:59:44 +0000313
314 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000315}
316
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000317void Decl::initAttrs(Attr *attrs) {
318 assert(!HasAttrs && "Decl already contains attrs.");
319
320 Attr *&AttrBlank = getASTContext().getDeclAttrs(this);
321 assert(AttrBlank == 0 && "HasAttrs was wrong?");
322
323 AttrBlank = attrs;
324 HasAttrs = true;
325}
326
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000327void Decl::addAttr(Attr *NewAttr) {
328 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000329
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000330 assert(NewAttr->getNext() == 0 && "Chain of attributes will be truncated!");
Eli Friedman56d29372008-06-07 16:52:53 +0000331 NewAttr->setNext(ExistingAttr);
332 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Eli Friedman56d29372008-06-07 16:52:53 +0000334 HasAttrs = true;
335}
336
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000337void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000338 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Eli Friedman56d29372008-06-07 16:52:53 +0000340 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000341 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000342}
343
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000344const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000345 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000346 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000347}
348
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000349void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000350 bool HasLHSAttr = this->HasAttrs;
351 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Eli Friedman56d29372008-06-07 16:52:53 +0000353 // Usually, neither decl has attrs, nothing to do.
354 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Eli Friedman56d29372008-06-07 16:52:53 +0000356 // If 'this' has no attrs, swap the other way.
357 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000358 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000360 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Eli Friedman56d29372008-06-07 16:52:53 +0000362 // Handle the case when both decls have attrs.
363 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000364 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000365 return;
366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Eli Friedman56d29372008-06-07 16:52:53 +0000368 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000369 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
370 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000371 this->HasAttrs = false;
372 RHS->HasAttrs = true;
373}
374
375
Chris Lattnercc581472009-03-04 06:05:19 +0000376void Decl::Destroy(ASTContext &C) {
377 // Free attributes for this decl.
378 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000379 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000380 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000381 HasAttrs = false;
382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000384#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000385 // FIXME: Once ownership is fully understood, we can enable this code
386 if (DeclContext *DC = dyn_cast<DeclContext>(this))
387 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000388
Chris Lattner244a67d2009-03-28 06:04:26 +0000389 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000390 // within the loop, only the Destroy method for the first Decl
391 // will deallocate all of the Decls in a chain.
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Chris Lattner244a67d2009-03-28 06:04:26 +0000393 Decl* N = getNextDeclInContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000395 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000396 Decl* Tmp = N->getNextDeclInContext();
397 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000398 N->Destroy(C);
399 N = Tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000400 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000401
Ted Kremenek94a39002009-12-01 00:07:10 +0000402 if (isOutOfSemaDC())
403 delete (C) getMultipleDC();
404
Eli Friedman56d29372008-06-07 16:52:53 +0000405 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000406 C.Deallocate((void *)this);
Ted Kremenek94a39002009-12-01 00:07:10 +0000407#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000408}
409
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000410Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000411 Decl::Kind DK = D->getDeclKind();
412 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000413#define DECL(NAME, BASE)
414#define DECL_CONTEXT(NAME) \
415 case Decl::NAME: \
416 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
417#define DECL_CONTEXT_BASE(NAME)
418#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000419 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000420#define DECL(NAME, BASE)
421#define DECL_CONTEXT_BASE(NAME) \
422 if (DK >= first##NAME && DK <= last##NAME) \
423 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
424#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000425 assert(false && "a decl that inherits DeclContext isn't handled");
426 return 0;
427 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000428}
429
430DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000431 Decl::Kind DK = D->getKind();
432 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000433#define DECL(NAME, BASE)
434#define DECL_CONTEXT(NAME) \
435 case Decl::NAME: \
436 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
437#define DECL_CONTEXT_BASE(NAME)
438#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000439 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000440#define DECL(NAME, BASE)
441#define DECL_CONTEXT_BASE(NAME) \
442 if (DK >= first##NAME && DK <= last##NAME) \
443 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
444#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000445 assert(false && "a decl that inherits DeclContext isn't handled");
446 return 0;
447 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000448}
449
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000450CompoundStmt* Decl::getCompoundBody() const {
451 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000452}
453
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000454SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000455 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
456 // FunctionDecl stores EndRangeLoc for this purpose.
457 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
458 const FunctionDecl *Definition;
459 if (FD->hasBody(Definition))
460 return Definition->getSourceRange().getEnd();
461 return SourceLocation();
462 }
463
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000464 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000465 if (!Body)
466 return SourceLocation();
467 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
468 return CS->getRBracLoc();
469 assert(isa<CXXTryStmt>(Body) &&
470 "Body can only be CompoundStmt or CXXTryStmt");
471 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
472}
473
Anders Carlsson1329c272009-03-25 23:38:06 +0000474#ifndef NDEBUG
475void Decl::CheckAccessDeclContext() const {
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000476 // FIXME: Disable this until rdar://8146294 "access specifier for inner class
477 // templates is not set or checked" is fixed.
478 return;
John McCall46460a62010-01-20 21:53:11 +0000479 // Suppress this check if any of the following hold:
480 // 1. this is the translation unit (and thus has no parent)
481 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000482 // 3. the context is not a record
483 // 4. it's invalid
Anders Carlsson35eda442009-08-29 20:47:47 +0000484 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000485 isa<TemplateTypeParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000486 !isa<CXXRecordDecl>(getDeclContext()) ||
487 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000488 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
490 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000491 "Access specifier is AS_none inside a record decl");
492}
493
494#endif
495
Eli Friedman56d29372008-06-07 16:52:53 +0000496//===----------------------------------------------------------------------===//
497// DeclContext Implementation
498//===----------------------------------------------------------------------===//
499
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000500bool DeclContext::classof(const Decl *D) {
501 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000502#define DECL(NAME, BASE)
503#define DECL_CONTEXT(NAME) case Decl::NAME:
504#define DECL_CONTEXT_BASE(NAME)
505#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000506 return true;
507 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000508#define DECL(NAME, BASE)
509#define DECL_CONTEXT_BASE(NAME) \
510 if (D->getKind() >= Decl::first##NAME && \
511 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000512 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000513#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000514 return false;
515 }
516}
517
Douglas Gregor44b43212008-12-11 16:49:14 +0000518DeclContext::~DeclContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000519 // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
520 // ~DeclContext() is not guaranteed to be called when ASTContext uses
521 // a BumpPtrAllocator.
John McCall0c01d182010-03-24 05:22:00 +0000522 // delete LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000523}
524
525void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000526 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000527 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000528}
529
Douglas Gregore942bbe2009-09-10 16:57:35 +0000530/// \brief Find the parent context of this context that will be
531/// used for unqualified name lookup.
532///
533/// Generally, the parent lookup context is the semantic context. However, for
534/// a friend function the parent lookup context is the lexical context, which
535/// is the class in which the friend is declared.
536DeclContext *DeclContext::getLookupParent() {
537 // FIXME: Find a better way to identify friends
538 if (isa<FunctionDecl>(this))
539 if (getParent()->getLookupContext()->isFileContext() &&
540 getLexicalParent()->getLookupContext()->isRecord())
541 return getLexicalParent();
542
543 return getParent();
544}
545
Douglas Gregorbc221632009-05-28 16:34:51 +0000546bool DeclContext::isDependentContext() const {
547 if (isFileContext())
548 return false;
549
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000550 if (isa<ClassTemplatePartialSpecializationDecl>(this))
551 return true;
552
Douglas Gregorbc221632009-05-28 16:34:51 +0000553 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
554 if (Record->getDescribedClassTemplate())
555 return true;
556
John McCall0c01d182010-03-24 05:22:00 +0000557 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000558 if (Function->getDescribedFunctionTemplate())
559 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
John McCall0c01d182010-03-24 05:22:00 +0000561 // Friend function declarations are dependent if their *lexical*
562 // context is dependent.
563 if (cast<Decl>(this)->getFriendObjectKind())
564 return getLexicalParent()->isDependentContext();
565 }
566
Douglas Gregorbc221632009-05-28 16:34:51 +0000567 return getParent() && getParent()->isDependentContext();
568}
569
Douglas Gregor074149e2009-01-05 19:45:36 +0000570bool DeclContext::isTransparentContext() const {
571 if (DeclKind == Decl::Enum)
572 return true; // FIXME: Check for C++0x scoped enums
573 else if (DeclKind == Decl::LinkageSpec)
574 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000575 else if (DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000576 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000577 else if (DeclKind == Decl::Namespace)
578 return false; // FIXME: Check for C++0x inline namespaces
579
580 return false;
581}
582
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000583bool DeclContext::Encloses(DeclContext *DC) {
584 if (getPrimaryContext() != this)
585 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000587 for (; DC; DC = DC->getParent())
588 if (DC->getPrimaryContext() == this)
589 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000590 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000591}
592
Steve Naroff0701bbb2009-01-08 17:28:14 +0000593DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000594 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000595 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000596 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000597 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000598 // There is only one DeclContext for these entities.
599 return this;
600
601 case Decl::Namespace:
602 // The original namespace is our primary context.
603 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
604
Douglas Gregor44b43212008-12-11 16:49:14 +0000605 case Decl::ObjCMethod:
606 return this;
607
608 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000609 case Decl::ObjCProtocol:
610 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000611 // FIXME: Can Objective-C interfaces be forward-declared?
612 return this;
613
Steve Naroff0701bbb2009-01-08 17:28:14 +0000614 case Decl::ObjCImplementation:
615 case Decl::ObjCCategoryImpl:
616 return this;
617
Douglas Gregor44b43212008-12-11 16:49:14 +0000618 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000619 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000620 // If this is a tag type that has a definition or is currently
621 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000622 TagDecl *Tag = cast<TagDecl>(this);
623 assert(isa<TagType>(Tag->TypeForDecl) ||
624 isa<InjectedClassNameType>(Tag->TypeForDecl));
625
626 if (TagDecl *Def = Tag->getDefinition())
627 return Def;
628
629 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
630 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
631 if (TagTy->isBeingDefined())
632 // FIXME: is it necessarily being defined in the decl
633 // that owns the type?
634 return TagTy->getDecl();
635 }
636
637 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000638 }
639
Sean Hunt9a555912010-05-30 07:21:58 +0000640 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000641 "Unknown DeclContext kind");
642 return this;
643 }
644}
645
646DeclContext *DeclContext::getNextContext() {
647 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000648 case Decl::Namespace:
649 // Return the next namespace
650 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
651
652 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000653 return 0;
654 }
655}
656
Douglas Gregor2cf26342009-04-09 22:27:44 +0000657/// \brief Load the declarations within this lexical storage from an
658/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000659void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000660DeclContext::LoadLexicalDeclsFromExternalStorage() const {
661 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000662 assert(hasExternalLexicalStorage() && Source && "No external storage?");
663
John McCall76bd1f32010-06-01 09:23:16 +0000664 llvm::SmallVector<Decl*, 64> Decls;
665 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000666 return;
667
668 // There is no longer any lexical storage in this context
669 ExternalLexicalStorage = false;
670
671 if (Decls.empty())
672 return;
673
674 // Resolve all of the declaration IDs into declarations, building up
675 // a chain of declarations via the Decl::NextDeclInContext field.
676 Decl *FirstNewDecl = 0;
677 Decl *PrevDecl = 0;
678 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000679 Decl *D = Decls[I];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000680 if (PrevDecl)
681 PrevDecl->NextDeclInContext = D;
682 else
683 FirstNewDecl = D;
684
685 PrevDecl = D;
686 }
687
688 // Splice the newly-read declarations into the beginning of the list
689 // of declarations.
690 PrevDecl->NextDeclInContext = FirstDecl;
691 FirstDecl = FirstNewDecl;
692 if (!LastDecl)
693 LastDecl = PrevDecl;
694}
695
John McCall76bd1f32010-06-01 09:23:16 +0000696DeclContext::lookup_result
697ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
698 DeclarationName Name) {
699 ASTContext &Context = DC->getParentASTContext();
700 StoredDeclsMap *Map;
701 if (!(Map = DC->LookupPtr))
702 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000703
John McCall76bd1f32010-06-01 09:23:16 +0000704 StoredDeclsList &List = (*Map)[Name];
705 assert(List.isNull());
706 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000707
John McCall76bd1f32010-06-01 09:23:16 +0000708 return DeclContext::lookup_result();
709}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000710
John McCall76bd1f32010-06-01 09:23:16 +0000711DeclContext::lookup_result
712ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
713 const VisibleDeclaration &VD) {
714 ASTContext &Context = DC->getParentASTContext();
715 StoredDeclsMap *Map;
716 if (!(Map = DC->LookupPtr))
717 Map = DC->CreateStoredDeclsMap(Context);
718
719 StoredDeclsList &List = (*Map)[VD.Name];
720 List.setFromDeclIDs(VD.Declarations);
721 return List.getLookupResult(Context);
722}
723
724DeclContext::lookup_result
725ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
726 DeclarationName Name,
727 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
728 ASTContext &Context = DC->getParentASTContext();;
729
730 StoredDeclsMap *Map;
731 if (!(Map = DC->LookupPtr))
732 Map = DC->CreateStoredDeclsMap(Context);
733
734 StoredDeclsList &List = (*Map)[Name];
735 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
736 if (List.isNull())
737 List.setOnlyValue(Decls[I]);
738 else
739 List.AddSubsequentDecl(Decls[I]);
740 }
741
742 return List.getLookupResult(Context);
743}
744
745void ExternalASTSource::SetExternalVisibleDecls(const DeclContext *DC,
746 const llvm::SmallVectorImpl<VisibleDeclaration> &Decls) {
747 // There is no longer any visible storage in this context.
748 DC->ExternalVisibleStorage = false;
749
750 assert(!DC->LookupPtr && "Have a lookup map before de-serialization?");
751 StoredDeclsMap *Map = DC->CreateStoredDeclsMap(DC->getParentASTContext());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000752 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
753 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
754 }
755}
756
John McCall76bd1f32010-06-01 09:23:16 +0000757void ExternalASTSource::SetExternalVisibleDecls(const DeclContext *DC,
758 const llvm::SmallVectorImpl<NamedDecl*> &Decls) {
759 // There is no longer any visible storage in this context.
760 DC->ExternalVisibleStorage = false;
761
762 assert(!DC->LookupPtr && "Have a lookup map before de-serialization?");
763 StoredDeclsMap &Map = *DC->CreateStoredDeclsMap(DC->getParentASTContext());
764 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
765 StoredDeclsList &List = Map[Decls[I]->getDeclName()];
766 if (List.isNull())
767 List.setOnlyValue(Decls[I]);
768 else
769 List.AddSubsequentDecl(Decls[I]);
770 }
771}
772
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000773DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000774 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000775 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000776
777 // FIXME: Check whether we need to load some declarations from
778 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000779 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000780}
781
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000782DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000783 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000784 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000785
Mike Stump1eb44332009-09-09 15:08:12 +0000786 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000787}
788
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000789bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000790 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000791 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000792
793 return !FirstDecl;
794}
795
John McCall9f54ad42009-12-10 09:41:52 +0000796void DeclContext::removeDecl(Decl *D) {
797 assert(D->getLexicalDeclContext() == this &&
798 "decl being removed from non-lexical context");
799 assert((D->NextDeclInContext || D == LastDecl) &&
800 "decl is not in decls list");
801
802 // Remove D from the decl chain. This is O(n) but hopefully rare.
803 if (D == FirstDecl) {
804 if (D == LastDecl)
805 FirstDecl = LastDecl = 0;
806 else
807 FirstDecl = D->NextDeclInContext;
808 } else {
809 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
810 assert(I && "decl not found in linked list");
811 if (I->NextDeclInContext == D) {
812 I->NextDeclInContext = D->NextDeclInContext;
813 if (D == LastDecl) LastDecl = I;
814 break;
815 }
816 }
817 }
818
819 // Mark that D is no longer in the decl chain.
820 D->NextDeclInContext = 0;
821
822 // Remove D from the lookup table if necessary.
823 if (isa<NamedDecl>(D)) {
824 NamedDecl *ND = cast<NamedDecl>(D);
825
John McCall0c01d182010-03-24 05:22:00 +0000826 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
827 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000828
John McCall9f54ad42009-12-10 09:41:52 +0000829 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
830 assert(Pos != Map->end() && "no lookup entry for decl");
831 Pos->second.remove(ND);
832 }
833}
834
John McCall3f9a8a62009-08-11 06:59:38 +0000835void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000836 assert(D->getLexicalDeclContext() == this &&
837 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000838 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000839 "Decl already inserted into a DeclContext");
840
841 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000842 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000843 LastDecl = D;
844 } else {
845 FirstDecl = LastDecl = D;
846 }
John McCall3f9a8a62009-08-11 06:59:38 +0000847}
848
849void DeclContext::addDecl(Decl *D) {
850 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000851
852 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000853 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000854}
855
Douglas Gregor074149e2009-01-05 19:45:36 +0000856/// buildLookup - Build the lookup data structure with all of the
857/// declarations in DCtx (and any other contexts linked to it or
858/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000859void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000860 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000861 for (decl_iterator D = DCtx->decls_begin(),
862 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000863 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000864 // Insert this declaration into the lookup structure, but only
865 // if it's semantically in its decl context. During non-lazy
866 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000867 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000868 if (D->getDeclContext() == DCtx)
869 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000870
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000871 // Insert any forward-declared Objective-C interfaces into the lookup
872 // data structure.
873 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
874 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
875 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000876 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000877
Douglas Gregor074149e2009-01-05 19:45:36 +0000878 // If this declaration is itself a transparent declaration context,
879 // add its members (recursively).
880 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
881 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000882 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000883 }
884 }
885}
886
Mike Stump1eb44332009-09-09 15:08:12 +0000887DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000888DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000889 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000890 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000891 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000892
John McCall76bd1f32010-06-01 09:23:16 +0000893 if (hasExternalVisibleStorage()) {
894 // Check to see if we've already cached the lookup results.
895 if (LookupPtr) {
896 StoredDeclsMap::iterator I = LookupPtr->find(Name);
897 if (I != LookupPtr->end())
898 return I->second.getLookupResult(getParentASTContext());
899 }
900
901 ExternalASTSource *Source = getParentASTContext().getExternalSource();
902 return Source->FindExternalVisibleDeclsByName(this, Name);
903 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000904
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000905 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000906 /// all of the linked DeclContexts (in declaration order!) and
907 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000908 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000909 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000910
Douglas Gregorc36c5402009-04-09 17:29:08 +0000911 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000912 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000913 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000914
John McCall0c01d182010-03-24 05:22:00 +0000915 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
916 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000917 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000918 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000919}
920
Mike Stump1eb44332009-09-09 15:08:12 +0000921DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000922DeclContext::lookup(DeclarationName Name) const {
923 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000924}
925
Chris Lattner0cf2b192009-03-27 19:19:59 +0000926DeclContext *DeclContext::getLookupContext() {
927 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000928 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000929 while (Ctx->isTransparentContext())
930 Ctx = Ctx->getParent();
931 return Ctx;
932}
933
Douglas Gregor88b70942009-02-25 22:02:03 +0000934DeclContext *DeclContext::getEnclosingNamespaceContext() {
935 DeclContext *Ctx = this;
936 // Skip through non-namespace, non-translation-unit contexts.
937 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
938 Ctx = Ctx->getParent();
939 return Ctx->getPrimaryContext();
940}
941
John McCallab88d972009-08-31 22:39:49 +0000942void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000943 // FIXME: This feels like a hack. Should DeclarationName support
944 // template-ids, or is there a better way to keep specializations
945 // from being visible?
946 if (isa<ClassTemplateSpecializationDecl>(D))
947 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000948 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
949 if (FD->isFunctionTemplateSpecialization())
950 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000951
Steve Naroff0701bbb2009-01-08 17:28:14 +0000952 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000953 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000954 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000955 return;
956 }
957
958 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000959 // into it. If we haven't deserialized externally stored decls, deserialize
960 // them so we can add the decl. Otherwise, be lazy and don't build that
961 // structure until someone asks for it.
962 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000963 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000964
Douglas Gregor074149e2009-01-05 19:45:36 +0000965 // If we are a transparent context, insert into our parent context,
966 // too. This operation is recursive.
967 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000968 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000969}
970
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000971void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000972 // Skip unnamed declarations.
973 if (!D->getDeclName())
974 return;
975
Douglas Gregorcc636682009-02-17 23:15:12 +0000976 // FIXME: This feels like a hack. Should DeclarationName support
977 // template-ids, or is there a better way to keep specializations
978 // from being visible?
979 if (isa<ClassTemplateSpecializationDecl>(D))
980 return;
981
Douglas Gregor18274032010-07-03 00:47:00 +0000982 // If there is an external AST source, load any declarations it knows about
983 // with this declaration's name.
984 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
985 if (hasExternalVisibleStorage())
986 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
987
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000988 ASTContext *C = 0;
989 if (!LookupPtr) {
990 C = &getParentASTContext();
991 CreateStoredDeclsMap(*C);
992 }
993
Douglas Gregor44b43212008-12-11 16:49:14 +0000994 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000995 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000996 if (DeclNameEntries.isNull()) {
997 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000998 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000999 }
Chris Lattner91942502009-02-20 00:55:03 +00001000
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001001 // If it is possible that this is a redeclaration, check to see if there is
1002 // already a decl for which declarationReplaces returns true. If there is
1003 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +00001004 if (!C)
1005 C = &getParentASTContext();
1006
1007 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +00001008 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001010 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001011 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001012}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001013
1014/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1015/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001016DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001017DeclContext::getUsingDirectives() const {
1018 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001019 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1020 reinterpret_cast<udir_iterator>(Result.second));
1021}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001022
1023void StoredDeclsList::materializeDecls(ASTContext &Context) {
1024 if (isNull())
1025 return;
1026
1027 switch ((DataKind)(Data & 0x03)) {
1028 case DK_Decl:
1029 case DK_Decl_Vector:
1030 break;
1031
1032 case DK_DeclID: {
1033 // Resolve this declaration ID to an actual declaration by
1034 // querying the external AST source.
1035 unsigned DeclID = Data >> 2;
1036
1037 ExternalASTSource *Source = Context.getExternalSource();
1038 assert(Source && "No external AST source available!");
1039
John McCall76bd1f32010-06-01 09:23:16 +00001040 Data = reinterpret_cast<uintptr_t>(Source->GetExternalDecl(DeclID));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001041 break;
1042 }
1043
1044 case DK_ID_Vector: {
1045 // We have a vector of declaration IDs. Resolve all of them to
1046 // actual declarations.
1047 VectorTy &Vector = *getAsVector();
1048 ExternalASTSource *Source = Context.getExternalSource();
1049 assert(Source && "No external AST source available!");
1050
1051 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
John McCall76bd1f32010-06-01 09:23:16 +00001052 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetExternalDecl(Vector[I]));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001053
1054 Data = (Data & ~0x03) | DK_Decl_Vector;
1055 break;
1056 }
1057 }
1058}
Ted Kremenek3478eb62010-02-11 07:12:28 +00001059
1060//===----------------------------------------------------------------------===//
1061// Creation and Destruction of StoredDeclsMaps. //
1062//===----------------------------------------------------------------------===//
1063
John McCall0c01d182010-03-24 05:22:00 +00001064StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1065 assert(!LookupPtr && "context already has a decls map");
1066 assert(getPrimaryContext() == this &&
1067 "creating decls map on non-primary context");
1068
1069 StoredDeclsMap *M;
1070 bool Dependent = isDependentContext();
1071 if (Dependent)
1072 M = new DependentStoredDeclsMap();
1073 else
1074 M = new StoredDeclsMap();
1075 M->Previous = C.LastSDM;
1076 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1077 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001078 return M;
1079}
1080
1081void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001082 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1083 // pointer because the subclass doesn't add anything that needs to
1084 // be deleted.
1085
1086 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1087}
1088
1089void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1090 while (Map) {
1091 // Advance the iteration before we invalidate memory.
1092 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1093
1094 if (Dependent)
1095 delete static_cast<DependentStoredDeclsMap*>(Map);
1096 else
1097 delete Map;
1098
1099 Map = Next.getPointer();
1100 Dependent = Next.getInt();
1101 }
1102}
1103
John McCall0c01d182010-03-24 05:22:00 +00001104DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1105 DeclContext *Parent,
1106 const PartialDiagnostic &PDiag) {
1107 assert(Parent->isDependentContext()
1108 && "cannot iterate dependent diagnostics of non-dependent context");
1109 Parent = Parent->getPrimaryContext();
1110 if (!Parent->LookupPtr)
1111 Parent->CreateStoredDeclsMap(C);
1112
1113 DependentStoredDeclsMap *Map
1114 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1115
Douglas Gregorb8365182010-03-29 23:56:53 +00001116 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001117 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001118 PartialDiagnostic::Storage *DiagStorage = 0;
1119 if (PDiag.hasStorage())
1120 DiagStorage = new (C) PartialDiagnostic::Storage;
1121
1122 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001123
1124 // TODO: Maybe we shouldn't reverse the order during insertion.
1125 DD->NextDiagnostic = Map->FirstDiagnostic;
1126 Map->FirstDiagnostic = DD;
1127
1128 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001129}