blob: e4ff679c0419c08cbfc497b1aeeff4e70290ce25 [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.
Douglas Gregorff331c12010-07-25 18:17:45 +0000160Decl::~Decl() { }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000161
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000162void Decl::setDeclContext(DeclContext *DC) {
163 if (isOutOfSemaDC())
164 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattneree219fd2009-03-29 06:06:59 +0000166 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000167}
168
169void Decl::setLexicalDeclContext(DeclContext *DC) {
170 if (DC == getLexicalDeclContext())
171 return;
172
173 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000174 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000175 MDC->SemanticDC = getDeclContext();
176 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000177 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000178 } else {
179 getMultipleDC()->LexicalDC = DC;
180 }
181}
182
John McCall9aeed322009-10-01 00:25:31 +0000183bool Decl::isInAnonymousNamespace() const {
184 const DeclContext *DC = getDeclContext();
185 do {
186 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
187 if (ND->isAnonymousNamespace())
188 return true;
189 } while ((DC = DC->getParent()));
190
191 return false;
192}
193
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000194TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000195 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
196 return TUD;
197
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000198 DeclContext *DC = getDeclContext();
199 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000201 while (!DC->isTranslationUnit()) {
202 DC = DC->getParent();
203 assert(DC && "This decl is not contained in a translation unit!");
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000206 return cast<TranslationUnitDecl>(DC);
207}
208
209ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000210 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000211}
212
Douglas Gregorc070cc62010-06-17 23:14:26 +0000213bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000214 if (Used)
215 return true;
216
217 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000218 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000219 return true;
220
221 // Check redeclarations for used attribute.
222 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000223 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000224 return true;
225 }
226
227 return false;
228}
229
230
Chris Lattner769dbdf2009-03-27 20:18:19 +0000231unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
232 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000233 case Function:
234 case CXXMethod:
235 case CXXConstructor:
236 case CXXDestructor:
237 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000238 case EnumConstant:
239 case Var:
240 case ImplicitParam:
241 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000242 case NonTypeTemplateParm:
243 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000244 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000245 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000246
John McCall0d6b1642010-04-23 18:46:30 +0000247 case ObjCCompatibleAlias:
248 case ObjCInterface:
249 return IDNS_Ordinary | IDNS_Type;
250
251 case Typedef:
252 case UnresolvedUsingTypename:
253 case TemplateTypeParm:
254 return IDNS_Ordinary | IDNS_Type;
255
John McCall9488ea12009-11-17 05:59:44 +0000256 case UsingShadow:
257 return 0; // we'll actually overwrite this later
258
John McCall7ba107a2009-11-18 02:36:19 +0000259 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000260 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000261
262 case Using:
263 return IDNS_Using;
264
Chris Lattner769dbdf2009-03-27 20:18:19 +0000265 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000266 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattner769dbdf2009-03-27 20:18:19 +0000268 case Field:
269 case ObjCAtDefsField:
270 case ObjCIvar:
271 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner769dbdf2009-03-27 20:18:19 +0000273 case Record:
274 case CXXRecord:
275 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000276 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner769dbdf2009-03-27 20:18:19 +0000278 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000279 case NamespaceAlias:
280 return IDNS_Namespace;
281
Chris Lattner769dbdf2009-03-27 20:18:19 +0000282 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000283 return IDNS_Ordinary;
284
Chris Lattner769dbdf2009-03-27 20:18:19 +0000285 case ClassTemplate:
286 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000287 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattner769dbdf2009-03-27 20:18:19 +0000289 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000290 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000291 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000292 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000293 case LinkageSpec:
294 case FileScopeAsm:
295 case StaticAssert:
296 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000297 case ObjCPropertyImpl:
298 case ObjCForwardProtocol:
299 case Block:
300 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000301
Chris Lattner769dbdf2009-03-27 20:18:19 +0000302 case UsingDirective:
303 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000304 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000305 case ObjCImplementation:
306 case ObjCCategory:
307 case ObjCCategoryImpl:
308 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000309 return 0;
310 }
John McCall9488ea12009-11-17 05:59:44 +0000311
312 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000313}
314
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000315void Decl::initAttrs(Attr *attrs) {
316 assert(!HasAttrs && "Decl already contains attrs.");
317
318 Attr *&AttrBlank = getASTContext().getDeclAttrs(this);
319 assert(AttrBlank == 0 && "HasAttrs was wrong?");
320
321 AttrBlank = attrs;
322 HasAttrs = true;
323}
324
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000325void Decl::addAttr(Attr *NewAttr) {
326 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000327
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000328 assert(NewAttr->getNext() == 0 && "Chain of attributes will be truncated!");
Eli Friedman56d29372008-06-07 16:52:53 +0000329 NewAttr->setNext(ExistingAttr);
330 ExistingAttr = NewAttr;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Eli Friedman56d29372008-06-07 16:52:53 +0000332 HasAttrs = true;
333}
334
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000335void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000336 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Eli Friedman56d29372008-06-07 16:52:53 +0000338 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000339 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000340}
341
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000342const Attr *Decl::getAttrsImpl() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000343 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000344 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000345}
346
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000347void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000348 bool HasLHSAttr = this->HasAttrs;
349 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Eli Friedman56d29372008-06-07 16:52:53 +0000351 // Usually, neither decl has attrs, nothing to do.
352 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Eli Friedman56d29372008-06-07 16:52:53 +0000354 // If 'this' has no attrs, swap the other way.
355 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000356 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000358 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Eli Friedman56d29372008-06-07 16:52:53 +0000360 // Handle the case when both decls have attrs.
361 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000362 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000363 return;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Eli Friedman56d29372008-06-07 16:52:53 +0000366 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000367 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
368 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000369 this->HasAttrs = false;
370 RHS->HasAttrs = true;
371}
372
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000373Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000374 Decl::Kind DK = D->getDeclKind();
375 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000376#define DECL(NAME, BASE)
377#define DECL_CONTEXT(NAME) \
378 case Decl::NAME: \
379 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
380#define DECL_CONTEXT_BASE(NAME)
381#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000382 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000383#define DECL(NAME, BASE)
384#define DECL_CONTEXT_BASE(NAME) \
385 if (DK >= first##NAME && DK <= last##NAME) \
386 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
387#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000388 assert(false && "a decl that inherits DeclContext isn't handled");
389 return 0;
390 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000391}
392
393DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000394 Decl::Kind DK = D->getKind();
395 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000396#define DECL(NAME, BASE)
397#define DECL_CONTEXT(NAME) \
398 case Decl::NAME: \
399 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
400#define DECL_CONTEXT_BASE(NAME)
401#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000402 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000403#define DECL(NAME, BASE)
404#define DECL_CONTEXT_BASE(NAME) \
405 if (DK >= first##NAME && DK <= last##NAME) \
406 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
407#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000408 assert(false && "a decl that inherits DeclContext isn't handled");
409 return 0;
410 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000411}
412
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000413SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000414 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
415 // FunctionDecl stores EndRangeLoc for this purpose.
416 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
417 const FunctionDecl *Definition;
418 if (FD->hasBody(Definition))
419 return Definition->getSourceRange().getEnd();
420 return SourceLocation();
421 }
422
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000423 if (Stmt *Body = getBody())
424 return Body->getSourceRange().getEnd();
425
426 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000427}
428
Anders Carlsson1329c272009-03-25 23:38:06 +0000429#ifndef NDEBUG
430void Decl::CheckAccessDeclContext() const {
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000431 // FIXME: Disable this until rdar://8146294 "access specifier for inner class
432 // templates is not set or checked" is fixed.
433 return;
John McCall46460a62010-01-20 21:53:11 +0000434 // Suppress this check if any of the following hold:
435 // 1. this is the translation unit (and thus has no parent)
436 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000437 // 3. the context is not a record
438 // 4. it's invalid
Anders Carlsson35eda442009-08-29 20:47:47 +0000439 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000440 isa<TemplateTypeParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000441 !isa<CXXRecordDecl>(getDeclContext()) ||
442 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000443 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
445 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000446 "Access specifier is AS_none inside a record decl");
447}
448
449#endif
450
Eli Friedman56d29372008-06-07 16:52:53 +0000451//===----------------------------------------------------------------------===//
452// DeclContext Implementation
453//===----------------------------------------------------------------------===//
454
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000455bool DeclContext::classof(const Decl *D) {
456 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000457#define DECL(NAME, BASE)
458#define DECL_CONTEXT(NAME) case Decl::NAME:
459#define DECL_CONTEXT_BASE(NAME)
460#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000461 return true;
462 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000463#define DECL(NAME, BASE)
464#define DECL_CONTEXT_BASE(NAME) \
465 if (D->getKind() >= Decl::first##NAME && \
466 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000467 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000468#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000469 return false;
470 }
471}
472
Douglas Gregora2da7802010-07-25 18:38:02 +0000473DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000474
Douglas Gregore942bbe2009-09-10 16:57:35 +0000475/// \brief Find the parent context of this context that will be
476/// used for unqualified name lookup.
477///
478/// Generally, the parent lookup context is the semantic context. However, for
479/// a friend function the parent lookup context is the lexical context, which
480/// is the class in which the friend is declared.
481DeclContext *DeclContext::getLookupParent() {
482 // FIXME: Find a better way to identify friends
483 if (isa<FunctionDecl>(this))
484 if (getParent()->getLookupContext()->isFileContext() &&
485 getLexicalParent()->getLookupContext()->isRecord())
486 return getLexicalParent();
487
488 return getParent();
489}
490
Douglas Gregorbc221632009-05-28 16:34:51 +0000491bool DeclContext::isDependentContext() const {
492 if (isFileContext())
493 return false;
494
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000495 if (isa<ClassTemplatePartialSpecializationDecl>(this))
496 return true;
497
Douglas Gregorbc221632009-05-28 16:34:51 +0000498 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
499 if (Record->getDescribedClassTemplate())
500 return true;
501
John McCall0c01d182010-03-24 05:22:00 +0000502 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000503 if (Function->getDescribedFunctionTemplate())
504 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
John McCall0c01d182010-03-24 05:22:00 +0000506 // Friend function declarations are dependent if their *lexical*
507 // context is dependent.
508 if (cast<Decl>(this)->getFriendObjectKind())
509 return getLexicalParent()->isDependentContext();
510 }
511
Douglas Gregorbc221632009-05-28 16:34:51 +0000512 return getParent() && getParent()->isDependentContext();
513}
514
Douglas Gregor074149e2009-01-05 19:45:36 +0000515bool DeclContext::isTransparentContext() const {
516 if (DeclKind == Decl::Enum)
517 return true; // FIXME: Check for C++0x scoped enums
518 else if (DeclKind == Decl::LinkageSpec)
519 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000520 else if (DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000521 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000522 else if (DeclKind == Decl::Namespace)
523 return false; // FIXME: Check for C++0x inline namespaces
524
525 return false;
526}
527
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000528bool DeclContext::Encloses(DeclContext *DC) {
529 if (getPrimaryContext() != this)
530 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000532 for (; DC; DC = DC->getParent())
533 if (DC->getPrimaryContext() == this)
534 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000535 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000536}
537
Steve Naroff0701bbb2009-01-08 17:28:14 +0000538DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000539 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000540 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000541 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000542 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000543 // There is only one DeclContext for these entities.
544 return this;
545
546 case Decl::Namespace:
547 // The original namespace is our primary context.
548 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
549
Douglas Gregor44b43212008-12-11 16:49:14 +0000550 case Decl::ObjCMethod:
551 return this;
552
553 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000554 case Decl::ObjCProtocol:
555 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000556 // FIXME: Can Objective-C interfaces be forward-declared?
557 return this;
558
Steve Naroff0701bbb2009-01-08 17:28:14 +0000559 case Decl::ObjCImplementation:
560 case Decl::ObjCCategoryImpl:
561 return this;
562
Douglas Gregor44b43212008-12-11 16:49:14 +0000563 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000564 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000565 // If this is a tag type that has a definition or is currently
566 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000567 TagDecl *Tag = cast<TagDecl>(this);
568 assert(isa<TagType>(Tag->TypeForDecl) ||
569 isa<InjectedClassNameType>(Tag->TypeForDecl));
570
571 if (TagDecl *Def = Tag->getDefinition())
572 return Def;
573
574 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
575 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
576 if (TagTy->isBeingDefined())
577 // FIXME: is it necessarily being defined in the decl
578 // that owns the type?
579 return TagTy->getDecl();
580 }
581
582 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000583 }
584
Sean Hunt9a555912010-05-30 07:21:58 +0000585 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000586 "Unknown DeclContext kind");
587 return this;
588 }
589}
590
591DeclContext *DeclContext::getNextContext() {
592 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000593 case Decl::Namespace:
594 // Return the next namespace
595 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
596
597 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000598 return 0;
599 }
600}
601
Douglas Gregor2cf26342009-04-09 22:27:44 +0000602/// \brief Load the declarations within this lexical storage from an
603/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000604void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000605DeclContext::LoadLexicalDeclsFromExternalStorage() const {
606 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000607 assert(hasExternalLexicalStorage() && Source && "No external storage?");
608
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000609 // Notify that we have a DeclContext that is initializing.
610 ExternalASTSource::Deserializing ADeclContext(Source);
611
John McCall76bd1f32010-06-01 09:23:16 +0000612 llvm::SmallVector<Decl*, 64> Decls;
613 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000614 return;
615
616 // There is no longer any lexical storage in this context
617 ExternalLexicalStorage = false;
618
619 if (Decls.empty())
620 return;
621
622 // Resolve all of the declaration IDs into declarations, building up
623 // a chain of declarations via the Decl::NextDeclInContext field.
624 Decl *FirstNewDecl = 0;
625 Decl *PrevDecl = 0;
626 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000627 Decl *D = Decls[I];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000628 if (PrevDecl)
629 PrevDecl->NextDeclInContext = D;
630 else
631 FirstNewDecl = D;
632
633 PrevDecl = D;
634 }
635
636 // Splice the newly-read declarations into the beginning of the list
637 // of declarations.
638 PrevDecl->NextDeclInContext = FirstDecl;
639 FirstDecl = FirstNewDecl;
640 if (!LastDecl)
641 LastDecl = PrevDecl;
642}
643
John McCall76bd1f32010-06-01 09:23:16 +0000644DeclContext::lookup_result
645ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
646 DeclarationName Name) {
647 ASTContext &Context = DC->getParentASTContext();
648 StoredDeclsMap *Map;
649 if (!(Map = DC->LookupPtr))
650 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000651
John McCall76bd1f32010-06-01 09:23:16 +0000652 StoredDeclsList &List = (*Map)[Name];
653 assert(List.isNull());
654 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000655
John McCall76bd1f32010-06-01 09:23:16 +0000656 return DeclContext::lookup_result();
657}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000658
John McCall76bd1f32010-06-01 09:23:16 +0000659DeclContext::lookup_result
660ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
661 const VisibleDeclaration &VD) {
662 ASTContext &Context = DC->getParentASTContext();
663 StoredDeclsMap *Map;
664 if (!(Map = DC->LookupPtr))
665 Map = DC->CreateStoredDeclsMap(Context);
666
667 StoredDeclsList &List = (*Map)[VD.Name];
668 List.setFromDeclIDs(VD.Declarations);
669 return List.getLookupResult(Context);
670}
671
672DeclContext::lookup_result
673ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
674 DeclarationName Name,
675 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
676 ASTContext &Context = DC->getParentASTContext();;
677
678 StoredDeclsMap *Map;
679 if (!(Map = DC->LookupPtr))
680 Map = DC->CreateStoredDeclsMap(Context);
681
682 StoredDeclsList &List = (*Map)[Name];
683 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
684 if (List.isNull())
685 List.setOnlyValue(Decls[I]);
686 else
687 List.AddSubsequentDecl(Decls[I]);
688 }
689
690 return List.getLookupResult(Context);
691}
692
693void ExternalASTSource::SetExternalVisibleDecls(const DeclContext *DC,
694 const llvm::SmallVectorImpl<VisibleDeclaration> &Decls) {
695 // There is no longer any visible storage in this context.
696 DC->ExternalVisibleStorage = false;
697
698 assert(!DC->LookupPtr && "Have a lookup map before de-serialization?");
699 StoredDeclsMap *Map = DC->CreateStoredDeclsMap(DC->getParentASTContext());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000700 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
701 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
702 }
703}
704
John McCall76bd1f32010-06-01 09:23:16 +0000705void ExternalASTSource::SetExternalVisibleDecls(const DeclContext *DC,
706 const llvm::SmallVectorImpl<NamedDecl*> &Decls) {
707 // There is no longer any visible storage in this context.
708 DC->ExternalVisibleStorage = false;
709
710 assert(!DC->LookupPtr && "Have a lookup map before de-serialization?");
711 StoredDeclsMap &Map = *DC->CreateStoredDeclsMap(DC->getParentASTContext());
712 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
713 StoredDeclsList &List = Map[Decls[I]->getDeclName()];
714 if (List.isNull())
715 List.setOnlyValue(Decls[I]);
716 else
717 List.AddSubsequentDecl(Decls[I]);
718 }
719}
720
Sebastian Redl681d7232010-07-27 00:17:23 +0000721DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
722 return decl_iterator(FirstDecl);
723}
724
725DeclContext::decl_iterator DeclContext::noload_decls_end() const {
726 return decl_iterator();
727}
728
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000729DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000730 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000731 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000732
733 // FIXME: Check whether we need to load some declarations from
734 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000735 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000736}
737
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000738DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000739 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000740 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000741
Mike Stump1eb44332009-09-09 15:08:12 +0000742 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000743}
744
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000745bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000746 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000747 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000748
749 return !FirstDecl;
750}
751
John McCall9f54ad42009-12-10 09:41:52 +0000752void DeclContext::removeDecl(Decl *D) {
753 assert(D->getLexicalDeclContext() == this &&
754 "decl being removed from non-lexical context");
755 assert((D->NextDeclInContext || D == LastDecl) &&
756 "decl is not in decls list");
757
758 // Remove D from the decl chain. This is O(n) but hopefully rare.
759 if (D == FirstDecl) {
760 if (D == LastDecl)
761 FirstDecl = LastDecl = 0;
762 else
763 FirstDecl = D->NextDeclInContext;
764 } else {
765 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
766 assert(I && "decl not found in linked list");
767 if (I->NextDeclInContext == D) {
768 I->NextDeclInContext = D->NextDeclInContext;
769 if (D == LastDecl) LastDecl = I;
770 break;
771 }
772 }
773 }
774
775 // Mark that D is no longer in the decl chain.
776 D->NextDeclInContext = 0;
777
778 // Remove D from the lookup table if necessary.
779 if (isa<NamedDecl>(D)) {
780 NamedDecl *ND = cast<NamedDecl>(D);
781
John McCall0c01d182010-03-24 05:22:00 +0000782 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
783 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000784
John McCall9f54ad42009-12-10 09:41:52 +0000785 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
786 assert(Pos != Map->end() && "no lookup entry for decl");
787 Pos->second.remove(ND);
788 }
789}
790
John McCall3f9a8a62009-08-11 06:59:38 +0000791void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000792 assert(D->getLexicalDeclContext() == this &&
793 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000794 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000795 "Decl already inserted into a DeclContext");
796
797 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000798 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000799 LastDecl = D;
800 } else {
801 FirstDecl = LastDecl = D;
802 }
John McCall3f9a8a62009-08-11 06:59:38 +0000803}
804
805void DeclContext::addDecl(Decl *D) {
806 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000807
808 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000809 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000810}
811
Douglas Gregor074149e2009-01-05 19:45:36 +0000812/// buildLookup - Build the lookup data structure with all of the
813/// declarations in DCtx (and any other contexts linked to it or
814/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000815void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000816 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000817 for (decl_iterator D = DCtx->decls_begin(),
818 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000819 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000820 // Insert this declaration into the lookup structure, but only
821 // if it's semantically in its decl context. During non-lazy
822 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000823 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000824 if (D->getDeclContext() == DCtx)
825 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000826
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000827 // Insert any forward-declared Objective-C interfaces into the lookup
828 // data structure.
829 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
830 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
831 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000832 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000833
Douglas Gregor074149e2009-01-05 19:45:36 +0000834 // If this declaration is itself a transparent declaration context,
835 // add its members (recursively).
836 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
837 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000838 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000839 }
840 }
841}
842
Mike Stump1eb44332009-09-09 15:08:12 +0000843DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000844DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000845 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000846 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000847 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000848
John McCall76bd1f32010-06-01 09:23:16 +0000849 if (hasExternalVisibleStorage()) {
850 // Check to see if we've already cached the lookup results.
851 if (LookupPtr) {
852 StoredDeclsMap::iterator I = LookupPtr->find(Name);
853 if (I != LookupPtr->end())
854 return I->second.getLookupResult(getParentASTContext());
855 }
856
857 ExternalASTSource *Source = getParentASTContext().getExternalSource();
858 return Source->FindExternalVisibleDeclsByName(this, Name);
859 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000860
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000861 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000862 /// all of the linked DeclContexts (in declaration order!) and
863 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000864 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000865 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000866
Douglas Gregorc36c5402009-04-09 17:29:08 +0000867 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000868 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000869 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000870
John McCall0c01d182010-03-24 05:22:00 +0000871 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
872 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000873 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000874 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000875}
876
Mike Stump1eb44332009-09-09 15:08:12 +0000877DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000878DeclContext::lookup(DeclarationName Name) const {
879 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000880}
881
Chris Lattner0cf2b192009-03-27 19:19:59 +0000882DeclContext *DeclContext::getLookupContext() {
883 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000884 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000885 while (Ctx->isTransparentContext())
886 Ctx = Ctx->getParent();
887 return Ctx;
888}
889
Douglas Gregor88b70942009-02-25 22:02:03 +0000890DeclContext *DeclContext::getEnclosingNamespaceContext() {
891 DeclContext *Ctx = this;
892 // Skip through non-namespace, non-translation-unit contexts.
893 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
894 Ctx = Ctx->getParent();
895 return Ctx->getPrimaryContext();
896}
897
John McCallab88d972009-08-31 22:39:49 +0000898void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000899 // FIXME: This feels like a hack. Should DeclarationName support
900 // template-ids, or is there a better way to keep specializations
901 // from being visible?
902 if (isa<ClassTemplateSpecializationDecl>(D))
903 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000904 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
905 if (FD->isFunctionTemplateSpecialization())
906 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000907
Steve Naroff0701bbb2009-01-08 17:28:14 +0000908 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000909 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000910 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000911 return;
912 }
913
914 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000915 // into it. If we haven't deserialized externally stored decls, deserialize
916 // them so we can add the decl. Otherwise, be lazy and don't build that
917 // structure until someone asks for it.
918 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000919 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000920
Douglas Gregor074149e2009-01-05 19:45:36 +0000921 // If we are a transparent context, insert into our parent context,
922 // too. This operation is recursive.
923 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000924 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000925}
926
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000927void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000928 // Skip unnamed declarations.
929 if (!D->getDeclName())
930 return;
931
Douglas Gregorcc636682009-02-17 23:15:12 +0000932 // FIXME: This feels like a hack. Should DeclarationName support
933 // template-ids, or is there a better way to keep specializations
934 // from being visible?
935 if (isa<ClassTemplateSpecializationDecl>(D))
936 return;
937
Douglas Gregor18274032010-07-03 00:47:00 +0000938 // If there is an external AST source, load any declarations it knows about
939 // with this declaration's name.
940 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
941 if (hasExternalVisibleStorage())
942 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
943
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000944 ASTContext *C = 0;
945 if (!LookupPtr) {
946 C = &getParentASTContext();
947 CreateStoredDeclsMap(*C);
948 }
949
Douglas Gregor44b43212008-12-11 16:49:14 +0000950 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000951 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000952 if (DeclNameEntries.isNull()) {
953 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000954 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000955 }
Chris Lattner91942502009-02-20 00:55:03 +0000956
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000957 // If it is possible that this is a redeclaration, check to see if there is
958 // already a decl for which declarationReplaces returns true. If there is
959 // one, just replace it and return.
Ted Kremenek3478eb62010-02-11 07:12:28 +0000960 if (!C)
961 C = &getParentASTContext();
962
963 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000964 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000966 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000967 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000968}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000969
970/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
971/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000972DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000973DeclContext::getUsingDirectives() const {
974 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000975 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
976 reinterpret_cast<udir_iterator>(Result.second));
977}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000978
979void StoredDeclsList::materializeDecls(ASTContext &Context) {
980 if (isNull())
981 return;
982
983 switch ((DataKind)(Data & 0x03)) {
984 case DK_Decl:
985 case DK_Decl_Vector:
986 break;
987
988 case DK_DeclID: {
989 // Resolve this declaration ID to an actual declaration by
990 // querying the external AST source.
991 unsigned DeclID = Data >> 2;
992
993 ExternalASTSource *Source = Context.getExternalSource();
994 assert(Source && "No external AST source available!");
995
John McCall76bd1f32010-06-01 09:23:16 +0000996 Data = reinterpret_cast<uintptr_t>(Source->GetExternalDecl(DeclID));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000997 break;
998 }
999
1000 case DK_ID_Vector: {
1001 // We have a vector of declaration IDs. Resolve all of them to
1002 // actual declarations.
1003 VectorTy &Vector = *getAsVector();
1004 ExternalASTSource *Source = Context.getExternalSource();
1005 assert(Source && "No external AST source available!");
1006
1007 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
John McCall76bd1f32010-06-01 09:23:16 +00001008 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetExternalDecl(Vector[I]));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001009
1010 Data = (Data & ~0x03) | DK_Decl_Vector;
1011 break;
1012 }
1013 }
1014}
Ted Kremenek3478eb62010-02-11 07:12:28 +00001015
1016//===----------------------------------------------------------------------===//
1017// Creation and Destruction of StoredDeclsMaps. //
1018//===----------------------------------------------------------------------===//
1019
John McCall0c01d182010-03-24 05:22:00 +00001020StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1021 assert(!LookupPtr && "context already has a decls map");
1022 assert(getPrimaryContext() == this &&
1023 "creating decls map on non-primary context");
1024
1025 StoredDeclsMap *M;
1026 bool Dependent = isDependentContext();
1027 if (Dependent)
1028 M = new DependentStoredDeclsMap();
1029 else
1030 M = new StoredDeclsMap();
1031 M->Previous = C.LastSDM;
1032 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1033 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001034 return M;
1035}
1036
1037void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001038 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1039 // pointer because the subclass doesn't add anything that needs to
1040 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001041 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1042}
1043
1044void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1045 while (Map) {
1046 // Advance the iteration before we invalidate memory.
1047 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1048
1049 if (Dependent)
1050 delete static_cast<DependentStoredDeclsMap*>(Map);
1051 else
1052 delete Map;
1053
1054 Map = Next.getPointer();
1055 Dependent = Next.getInt();
1056 }
1057}
1058
John McCall0c01d182010-03-24 05:22:00 +00001059DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1060 DeclContext *Parent,
1061 const PartialDiagnostic &PDiag) {
1062 assert(Parent->isDependentContext()
1063 && "cannot iterate dependent diagnostics of non-dependent context");
1064 Parent = Parent->getPrimaryContext();
1065 if (!Parent->LookupPtr)
1066 Parent->CreateStoredDeclsMap(C);
1067
1068 DependentStoredDeclsMap *Map
1069 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1070
Douglas Gregorb8365182010-03-29 23:56:53 +00001071 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001072 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001073 PartialDiagnostic::Storage *DiagStorage = 0;
1074 if (PDiag.hasStorage())
1075 DiagStorage = new (C) PartialDiagnostic::Storage;
1076
1077 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001078
1079 // TODO: Maybe we shouldn't reverse the order during insertion.
1080 DD->NextDiagnostic = Map->FirstDiagnostic;
1081 Map->FirstDiagnostic = DD;
1082
1083 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001084}