blob: b38d0c23b7fc00ae207575b4d1c06cc087772d40 [file] [log] [blame]
Eli Friedman7dbab8a2008-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 Gregor8bd3c2e2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorb1fe2c92009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCallbbbbe4e2010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCallc62bb642010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor91f84212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redla7b98a72009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000027#include "llvm/ADT/DenseMap.h"
Chris Lattnereae6cb62009-03-05 08:00:35 +000028#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000029#include <algorithm>
Chris Lattnerc25d8a72009-03-02 22:20:04 +000030#include <cstdio>
Douglas Gregor55297ac2008-12-23 00:26:44 +000031#include <vector>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Eli Friedman7dbab8a2008-06-07 16:52:53 +000044const char *Decl::getDeclKindName() const {
45 switch (DeclKind) {
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +000050 }
51}
52
Douglas Gregor90d47172010-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 Naroff5faaef72009-01-20 19:53:53 +000063const char *DeclContext::getDeclKindName() const {
64 switch (DeclKind) {
Alexis Hunted053252010-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 Naroff5faaef72009-01-20 19:53:53 +000069 }
70}
71
Eli Friedman7dbab8a2008-06-07 16:52:53 +000072bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000073 if (Enable) StatSwitch = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +000074 return StatSwitch;
75}
76
77void Decl::PrintStats() {
78 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump11289f42009-09-09 15:08:12 +000079
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000080 int totalDecls = 0;
Alexis Hunted053252010-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 Gregor8bd3c2e2009-02-02 23:39:07 +000084 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump11289f42009-09-09 15:08:12 +000085
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000086 int totalBytes = 0;
Alexis Hunted053252010-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 Gregor8bd3c2e2009-02-02 23:39:07 +000093 }
Alexis Hunted053252010-05-30 07:21:58 +000094#define ABSTRACT_DECL(DECL)
95#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +000096
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000097 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman7dbab8a2008-06-07 16:52:53 +000098}
99
Alexis Hunted053252010-05-30 07:21:58 +0000100void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000101 switch (k) {
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +0000106 }
107}
108
Anders Carlssonaa73b912009-06-13 00:08:58 +0000109bool Decl::isTemplateParameterPack() const {
110 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
111 return TTP->isParameterPack();
Mike Stump11289f42009-09-09 15:08:12 +0000112
Anders Carlssonaa73b912009-06-13 00:08:58 +0000113 return false;
114}
115
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000116bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000117 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000118 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000119
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000120 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
121}
122
Douglas Gregorf5974fa2010-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 Friedman7dbab8a2008-06-07 16:52:53 +0000134//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000135// PrettyStackTraceDecl Implementation
136//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000137
Chris Lattnereae6cb62009-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 Stump11289f42009-09-09 15:08:12 +0000142
Chris Lattnereae6cb62009-03-05 08:00:35 +0000143 if (TheLoc.isValid()) {
144 TheLoc.print(OS, SM);
145 OS << ": ";
146 }
147
148 OS << Message;
149
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000150 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000151 OS << " '" << DN->getQualifiedNameAsString() << '\'';
152 OS << '\n';
153}
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattnereae6cb62009-03-05 08:00:35 +0000155//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000156// Decl Implementation
157//===----------------------------------------------------------------------===//
158
Chris Lattner8e097192009-03-27 20:18:19 +0000159// Out-of-line virtual method providing a home for Decl.
Douglas Gregorb412e172010-07-25 18:17:45 +0000160Decl::~Decl() { }
Chris Lattner8e097192009-03-27 20:18:19 +0000161
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000162void Decl::setDeclContext(DeclContext *DC) {
163 if (isOutOfSemaDC())
164 delete getMultipleDC();
Mike Stump11289f42009-09-09 15:08:12 +0000165
Chris Lattnerb81eb052009-03-29 06:06:59 +0000166 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000167}
168
169void Decl::setLexicalDeclContext(DeclContext *DC) {
170 if (DC == getLexicalDeclContext())
171 return;
172
173 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000174 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000175 MDC->SemanticDC = getDeclContext();
176 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000177 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000178 } else {
179 getMultipleDC()->LexicalDC = DC;
180 }
181}
182
John McCall4fa53422009-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 Kyrtzidis743e7db2009-06-29 17:38:40 +0000194TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000195 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
196 return TUD;
197
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000198 DeclContext *DC = getDeclContext();
199 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000200
Argyrios Kyrtzidis743e7db2009-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 Stump11289f42009-09-09 15:08:12 +0000205
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000206 return cast<TranslationUnitDecl>(DC);
207}
208
209ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000210 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000211}
212
Douglas Gregorebada0772010-06-17 23:14:26 +0000213bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000214 if (Used)
215 return true;
216
217 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000218 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-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 Gregorebada0772010-06-17 23:14:26 +0000223 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000224 return true;
225 }
226
227 return false;
228}
229
230
Chris Lattner8e097192009-03-27 20:18:19 +0000231unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
232 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000233 case Function:
234 case CXXMethod:
235 case CXXConstructor:
236 case CXXDestructor:
237 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000238 case EnumConstant:
239 case Var:
240 case ImplicitParam:
241 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000242 case NonTypeTemplateParm:
243 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000244 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000245 return IDNS_Ordinary;
John McCalla2456712010-04-23 02:41:41 +0000246
John McCalle87beb22010-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 McCall3f746822009-11-17 05:59:44 +0000256 case UsingShadow:
257 return 0; // we'll actually overwrite this later
258
John McCalle61f2ba2009-11-18 02:36:19 +0000259 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000260 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000261
262 case Using:
263 return IDNS_Using;
264
Chris Lattner8e097192009-03-27 20:18:19 +0000265 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000266 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattner8e097192009-03-27 20:18:19 +0000268 case Field:
269 case ObjCAtDefsField:
270 case ObjCIvar:
271 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Chris Lattner8e097192009-03-27 20:18:19 +0000273 case Record:
274 case CXXRecord:
275 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000276 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattner8e097192009-03-27 20:18:19 +0000278 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000279 case NamespaceAlias:
280 return IDNS_Namespace;
281
Chris Lattner8e097192009-03-27 20:18:19 +0000282 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000283 return IDNS_Ordinary;
284
Chris Lattner8e097192009-03-27 20:18:19 +0000285 case ClassTemplate:
286 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000287 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner8e097192009-03-27 20:18:19 +0000289 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000290 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000291 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000292 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000293 case LinkageSpec:
294 case FileScopeAsm:
295 case StaticAssert:
296 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000297 case ObjCPropertyImpl:
298 case ObjCForwardProtocol:
299 case Block:
300 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000301
Chris Lattner8e097192009-03-27 20:18:19 +0000302 case UsingDirective:
303 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000304 case ClassTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000305 case ObjCImplementation:
306 case ObjCCategory:
307 case ObjCCategoryImpl:
308 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000309 return 0;
310 }
John McCall3f746822009-11-17 05:59:44 +0000311
312 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000313}
314
Argyrios Kyrtzidis91167172010-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 Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000325void Decl::addAttr(Attr *NewAttr) {
326 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000327
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000328 assert(NewAttr->getNext() == 0 && "Chain of attributes will be truncated!");
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000329 NewAttr->setNext(ExistingAttr);
330 ExistingAttr = NewAttr;
Mike Stump11289f42009-09-09 15:08:12 +0000331
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000332 HasAttrs = true;
333}
334
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000335void Decl::invalidateAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000336 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000337
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000338 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000339 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000340}
341
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000342const Attr *Decl::getAttrsImpl() const {
Mike Stump11289f42009-09-09 15:08:12 +0000343 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000344 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000345}
346
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000347void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000348 bool HasLHSAttr = this->HasAttrs;
349 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000350
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000351 // Usually, neither decl has attrs, nothing to do.
352 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000354 // If 'this' has no attrs, swap the other way.
355 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000356 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000357
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000358 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000359
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000360 // Handle the case when both decls have attrs.
361 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000362 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000363 return;
364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000366 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000367 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
368 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000369 this->HasAttrs = false;
370 RHS->HasAttrs = true;
371}
372
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000373Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000374 Decl::Kind DK = D->getDeclKind();
375 switch(DK) {
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000382 default:
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000388 assert(false && "a decl that inherits DeclContext isn't handled");
389 return 0;
390 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000391}
392
393DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000394 Decl::Kind DK = D->getKind();
395 switch(DK) {
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000402 default:
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000408 assert(false && "a decl that inherits DeclContext isn't handled");
409 return 0;
410 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000411}
412
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000413SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-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 Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000423 if (Stmt *Body = getBody())
424 return Body->getSourceRange().getEnd();
425
426 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000427}
428
Anders Carlssona28908d2009-03-25 23:38:06 +0000429#ifndef NDEBUG
430void Decl::CheckAccessDeclContext() const {
Argyrios Kyrtzidisa45855f2010-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 McCall401982f2010-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 Kyrtzidisa45855f2010-07-02 11:55:44 +0000437 // 3. the context is not a record
438 // 4. it's invalid
Anders Carlssonadf36b22009-08-29 20:47:47 +0000439 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000440 isa<TemplateTypeParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000441 !isa<CXXRecordDecl>(getDeclContext()) ||
442 isInvalidDecl())
Anders Carlssonadf36b22009-08-29 20:47:47 +0000443 return;
Mike Stump11289f42009-09-09 15:08:12 +0000444
445 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000446 "Access specifier is AS_none inside a record decl");
447}
448
449#endif
450
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000451//===----------------------------------------------------------------------===//
452// DeclContext Implementation
453//===----------------------------------------------------------------------===//
454
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000455bool DeclContext::classof(const Decl *D) {
456 switch (D->getKind()) {
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000461 return true;
462 default:
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000467 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000468#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000469 return false;
470 }
471}
472
Douglas Gregor91f84212008-12-11 16:49:14 +0000473DeclContext::~DeclContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000474 // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
475 // ~DeclContext() is not guaranteed to be called when ASTContext uses
476 // a BumpPtrAllocator.
John McCallc62bb642010-03-24 05:22:00 +0000477 // delete LookupPtr;
Douglas Gregor91f84212008-12-11 16:49:14 +0000478}
479
Douglas Gregor7f737c02009-09-10 16:57:35 +0000480/// \brief Find the parent context of this context that will be
481/// used for unqualified name lookup.
482///
483/// Generally, the parent lookup context is the semantic context. However, for
484/// a friend function the parent lookup context is the lexical context, which
485/// is the class in which the friend is declared.
486DeclContext *DeclContext::getLookupParent() {
487 // FIXME: Find a better way to identify friends
488 if (isa<FunctionDecl>(this))
489 if (getParent()->getLookupContext()->isFileContext() &&
490 getLexicalParent()->getLookupContext()->isRecord())
491 return getLexicalParent();
492
493 return getParent();
494}
495
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000496bool DeclContext::isDependentContext() const {
497 if (isFileContext())
498 return false;
499
Douglas Gregor2373c592009-05-31 09:31:02 +0000500 if (isa<ClassTemplatePartialSpecializationDecl>(this))
501 return true;
502
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000503 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
504 if (Record->getDescribedClassTemplate())
505 return true;
506
John McCallc62bb642010-03-24 05:22:00 +0000507 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000508 if (Function->getDescribedFunctionTemplate())
509 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000510
John McCallc62bb642010-03-24 05:22:00 +0000511 // Friend function declarations are dependent if their *lexical*
512 // context is dependent.
513 if (cast<Decl>(this)->getFriendObjectKind())
514 return getLexicalParent()->isDependentContext();
515 }
516
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000517 return getParent() && getParent()->isDependentContext();
518}
519
Douglas Gregor07665a62009-01-05 19:45:36 +0000520bool DeclContext::isTransparentContext() const {
521 if (DeclKind == Decl::Enum)
522 return true; // FIXME: Check for C++0x scoped enums
523 else if (DeclKind == Decl::LinkageSpec)
524 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000525 else if (DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord)
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000526 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor07665a62009-01-05 19:45:36 +0000527 else if (DeclKind == Decl::Namespace)
528 return false; // FIXME: Check for C++0x inline namespaces
529
530 return false;
531}
532
Douglas Gregore985a3b2009-08-27 06:03:53 +0000533bool DeclContext::Encloses(DeclContext *DC) {
534 if (getPrimaryContext() != this)
535 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000536
Douglas Gregore985a3b2009-08-27 06:03:53 +0000537 for (; DC; DC = DC->getParent())
538 if (DC->getPrimaryContext() == this)
539 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000540 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000541}
542
Steve Naroff35c62ae2009-01-08 17:28:14 +0000543DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000544 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000545 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000546 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000547 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000548 // There is only one DeclContext for these entities.
549 return this;
550
551 case Decl::Namespace:
552 // The original namespace is our primary context.
553 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
554
Douglas Gregor91f84212008-12-11 16:49:14 +0000555 case Decl::ObjCMethod:
556 return this;
557
558 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000559 case Decl::ObjCProtocol:
560 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000561 // FIXME: Can Objective-C interfaces be forward-declared?
562 return this;
563
Steve Naroff35c62ae2009-01-08 17:28:14 +0000564 case Decl::ObjCImplementation:
565 case Decl::ObjCCategoryImpl:
566 return this;
567
Douglas Gregor91f84212008-12-11 16:49:14 +0000568 default:
Alexis Hunted053252010-05-30 07:21:58 +0000569 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000570 // If this is a tag type that has a definition or is currently
571 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000572 TagDecl *Tag = cast<TagDecl>(this);
573 assert(isa<TagType>(Tag->TypeForDecl) ||
574 isa<InjectedClassNameType>(Tag->TypeForDecl));
575
576 if (TagDecl *Def = Tag->getDefinition())
577 return Def;
578
579 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
580 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
581 if (TagTy->isBeingDefined())
582 // FIXME: is it necessarily being defined in the decl
583 // that owns the type?
584 return TagTy->getDecl();
585 }
586
587 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000588 }
589
Alexis Hunted053252010-05-30 07:21:58 +0000590 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000591 "Unknown DeclContext kind");
592 return this;
593 }
594}
595
596DeclContext *DeclContext::getNextContext() {
597 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000598 case Decl::Namespace:
599 // Return the next namespace
600 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
601
602 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000603 return 0;
604 }
605}
606
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000607/// \brief Load the declarations within this lexical storage from an
608/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000609void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000610DeclContext::LoadLexicalDeclsFromExternalStorage() const {
611 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000612 assert(hasExternalLexicalStorage() && Source && "No external storage?");
613
John McCall75b960e2010-06-01 09:23:16 +0000614 llvm::SmallVector<Decl*, 64> Decls;
615 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000616 return;
617
618 // There is no longer any lexical storage in this context
619 ExternalLexicalStorage = false;
620
621 if (Decls.empty())
622 return;
623
624 // Resolve all of the declaration IDs into declarations, building up
625 // a chain of declarations via the Decl::NextDeclInContext field.
626 Decl *FirstNewDecl = 0;
627 Decl *PrevDecl = 0;
628 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
John McCall75b960e2010-06-01 09:23:16 +0000629 Decl *D = Decls[I];
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000630 if (PrevDecl)
631 PrevDecl->NextDeclInContext = D;
632 else
633 FirstNewDecl = D;
634
635 PrevDecl = D;
636 }
637
638 // Splice the newly-read declarations into the beginning of the list
639 // of declarations.
640 PrevDecl->NextDeclInContext = FirstDecl;
641 FirstDecl = FirstNewDecl;
642 if (!LastDecl)
643 LastDecl = PrevDecl;
644}
645
John McCall75b960e2010-06-01 09:23:16 +0000646DeclContext::lookup_result
647ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
648 DeclarationName Name) {
649 ASTContext &Context = DC->getParentASTContext();
650 StoredDeclsMap *Map;
651 if (!(Map = DC->LookupPtr))
652 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000653
John McCall75b960e2010-06-01 09:23:16 +0000654 StoredDeclsList &List = (*Map)[Name];
655 assert(List.isNull());
656 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000657
John McCall75b960e2010-06-01 09:23:16 +0000658 return DeclContext::lookup_result();
659}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000660
John McCall75b960e2010-06-01 09:23:16 +0000661DeclContext::lookup_result
662ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
663 const VisibleDeclaration &VD) {
664 ASTContext &Context = DC->getParentASTContext();
665 StoredDeclsMap *Map;
666 if (!(Map = DC->LookupPtr))
667 Map = DC->CreateStoredDeclsMap(Context);
668
669 StoredDeclsList &List = (*Map)[VD.Name];
670 List.setFromDeclIDs(VD.Declarations);
671 return List.getLookupResult(Context);
672}
673
674DeclContext::lookup_result
675ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
676 DeclarationName Name,
677 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
678 ASTContext &Context = DC->getParentASTContext();;
679
680 StoredDeclsMap *Map;
681 if (!(Map = DC->LookupPtr))
682 Map = DC->CreateStoredDeclsMap(Context);
683
684 StoredDeclsList &List = (*Map)[Name];
685 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
686 if (List.isNull())
687 List.setOnlyValue(Decls[I]);
688 else
689 List.AddSubsequentDecl(Decls[I]);
690 }
691
692 return List.getLookupResult(Context);
693}
694
695void ExternalASTSource::SetExternalVisibleDecls(const DeclContext *DC,
696 const llvm::SmallVectorImpl<VisibleDeclaration> &Decls) {
697 // There is no longer any visible storage in this context.
698 DC->ExternalVisibleStorage = false;
699
700 assert(!DC->LookupPtr && "Have a lookup map before de-serialization?");
701 StoredDeclsMap *Map = DC->CreateStoredDeclsMap(DC->getParentASTContext());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000702 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
703 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
704 }
705}
706
John McCall75b960e2010-06-01 09:23:16 +0000707void ExternalASTSource::SetExternalVisibleDecls(const DeclContext *DC,
708 const llvm::SmallVectorImpl<NamedDecl*> &Decls) {
709 // There is no longer any visible storage in this context.
710 DC->ExternalVisibleStorage = false;
711
712 assert(!DC->LookupPtr && "Have a lookup map before de-serialization?");
713 StoredDeclsMap &Map = *DC->CreateStoredDeclsMap(DC->getParentASTContext());
714 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
715 StoredDeclsList &List = Map[Decls[I]->getDeclName()];
716 if (List.isNull())
717 List.setOnlyValue(Decls[I]);
718 else
719 List.AddSubsequentDecl(Decls[I]);
720 }
721}
722
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000723DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000724 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000725 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000726
727 // FIXME: Check whether we need to load some declarations from
728 // external storage.
Mike Stump11289f42009-09-09 15:08:12 +0000729 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000730}
731
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000732DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000733 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000734 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000735
Mike Stump11289f42009-09-09 15:08:12 +0000736 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000737}
738
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000739bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000740 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000741 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000742
743 return !FirstDecl;
744}
745
John McCall84d87672009-12-10 09:41:52 +0000746void DeclContext::removeDecl(Decl *D) {
747 assert(D->getLexicalDeclContext() == this &&
748 "decl being removed from non-lexical context");
749 assert((D->NextDeclInContext || D == LastDecl) &&
750 "decl is not in decls list");
751
752 // Remove D from the decl chain. This is O(n) but hopefully rare.
753 if (D == FirstDecl) {
754 if (D == LastDecl)
755 FirstDecl = LastDecl = 0;
756 else
757 FirstDecl = D->NextDeclInContext;
758 } else {
759 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
760 assert(I && "decl not found in linked list");
761 if (I->NextDeclInContext == D) {
762 I->NextDeclInContext = D->NextDeclInContext;
763 if (D == LastDecl) LastDecl = I;
764 break;
765 }
766 }
767 }
768
769 // Mark that D is no longer in the decl chain.
770 D->NextDeclInContext = 0;
771
772 // Remove D from the lookup table if necessary.
773 if (isa<NamedDecl>(D)) {
774 NamedDecl *ND = cast<NamedDecl>(D);
775
John McCallc62bb642010-03-24 05:22:00 +0000776 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
777 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +0000778
John McCall84d87672009-12-10 09:41:52 +0000779 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
780 assert(Pos != Map->end() && "no lookup entry for decl");
781 Pos->second.remove(ND);
782 }
783}
784
John McCalld1e9d832009-08-11 06:59:38 +0000785void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +0000786 assert(D->getLexicalDeclContext() == this &&
787 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +0000788 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +0000789 "Decl already inserted into a DeclContext");
790
791 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +0000792 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +0000793 LastDecl = D;
794 } else {
795 FirstDecl = LastDecl = D;
796 }
John McCalld1e9d832009-08-11 06:59:38 +0000797}
798
799void DeclContext::addDecl(Decl *D) {
800 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000801
802 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000803 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +0000804}
805
Douglas Gregor07665a62009-01-05 19:45:36 +0000806/// buildLookup - Build the lookup data structure with all of the
807/// declarations in DCtx (and any other contexts linked to it or
808/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000809void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000810 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +0000811 for (decl_iterator D = DCtx->decls_begin(),
812 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +0000813 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +0000814 // Insert this declaration into the lookup structure, but only
815 // if it's semantically in its decl context. During non-lazy
816 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000817 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +0000818 if (D->getDeclContext() == DCtx)
819 makeDeclVisibleInContextImpl(ND);
Douglas Gregor07665a62009-01-05 19:45:36 +0000820
Ted Kremenek707ece62009-11-17 22:58:30 +0000821 // Insert any forward-declared Objective-C interfaces into the lookup
822 // data structure.
823 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
824 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
825 I != IEnd; ++I)
Ted Kremenek9b124e12009-11-18 00:28:11 +0000826 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenek707ece62009-11-17 22:58:30 +0000827
Douglas Gregor07665a62009-01-05 19:45:36 +0000828 // If this declaration is itself a transparent declaration context,
829 // add its members (recursively).
830 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
831 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000832 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +0000833 }
834 }
835}
836
Mike Stump11289f42009-09-09 15:08:12 +0000837DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000838DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +0000839 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000840 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000841 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000842
John McCall75b960e2010-06-01 09:23:16 +0000843 if (hasExternalVisibleStorage()) {
844 // Check to see if we've already cached the lookup results.
845 if (LookupPtr) {
846 StoredDeclsMap::iterator I = LookupPtr->find(Name);
847 if (I != LookupPtr->end())
848 return I->second.getLookupResult(getParentASTContext());
849 }
850
851 ExternalASTSource *Source = getParentASTContext().getExternalSource();
852 return Source->FindExternalVisibleDeclsByName(this, Name);
853 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000854
Douglas Gregor55297ac2008-12-23 00:26:44 +0000855 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +0000856 /// all of the linked DeclContexts (in declaration order!) and
857 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +0000858 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000859 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +0000860
Douglas Gregor9615ec22009-04-09 17:29:08 +0000861 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +0000862 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +0000863 }
Douglas Gregor91f84212008-12-11 16:49:14 +0000864
John McCallc62bb642010-03-24 05:22:00 +0000865 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
866 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +0000867 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000868 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor91f84212008-12-11 16:49:14 +0000869}
870
Mike Stump11289f42009-09-09 15:08:12 +0000871DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000872DeclContext::lookup(DeclarationName Name) const {
873 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000874}
875
Chris Lattner17a1bfa2009-03-27 19:19:59 +0000876DeclContext *DeclContext::getLookupContext() {
877 DeclContext *Ctx = this;
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000878 // Skip through transparent contexts.
Douglas Gregor6ad0ef52009-01-06 23:51:29 +0000879 while (Ctx->isTransparentContext())
880 Ctx = Ctx->getParent();
881 return Ctx;
882}
883
Douglas Gregorf47b9112009-02-25 22:02:03 +0000884DeclContext *DeclContext::getEnclosingNamespaceContext() {
885 DeclContext *Ctx = this;
886 // Skip through non-namespace, non-translation-unit contexts.
887 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
888 Ctx = Ctx->getParent();
889 return Ctx->getPrimaryContext();
890}
891
John McCall759e32b2009-08-31 22:39:49 +0000892void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000893 // FIXME: This feels like a hack. Should DeclarationName support
894 // template-ids, or is there a better way to keep specializations
895 // from being visible?
896 if (isa<ClassTemplateSpecializationDecl>(D))
897 return;
Eli Friedman73168192009-12-08 05:40:03 +0000898 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
899 if (FD->isFunctionTemplateSpecialization())
900 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000901
Steve Naroff35c62ae2009-01-08 17:28:14 +0000902 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000903 if (PrimaryContext != this) {
John McCall759e32b2009-08-31 22:39:49 +0000904 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +0000905 return;
906 }
907
908 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +0000909 // into it. If we haven't deserialized externally stored decls, deserialize
910 // them so we can add the decl. Otherwise, be lazy and don't build that
911 // structure until someone asks for it.
912 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000913 makeDeclVisibleInContextImpl(D);
Douglas Gregor07665a62009-01-05 19:45:36 +0000914
Douglas Gregor07665a62009-01-05 19:45:36 +0000915 // If we are a transparent context, insert into our parent context,
916 // too. This operation is recursive.
917 if (isTransparentContext())
John McCall759e32b2009-08-31 22:39:49 +0000918 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +0000919}
920
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000921void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000922 // Skip unnamed declarations.
923 if (!D->getDeclName())
924 return;
925
Douglas Gregor67a65642009-02-17 23:15:12 +0000926 // FIXME: This feels like a hack. Should DeclarationName support
927 // template-ids, or is there a better way to keep specializations
928 // from being visible?
929 if (isa<ClassTemplateSpecializationDecl>(D))
930 return;
931
Douglas Gregor9672f922010-07-03 00:47:00 +0000932 // If there is an external AST source, load any declarations it knows about
933 // with this declaration's name.
934 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
935 if (hasExternalVisibleStorage())
936 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
937
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +0000938 ASTContext *C = 0;
939 if (!LookupPtr) {
940 C = &getParentASTContext();
941 CreateStoredDeclsMap(*C);
942 }
943
Douglas Gregor91f84212008-12-11 16:49:14 +0000944 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +0000945 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +0000946 if (DeclNameEntries.isNull()) {
947 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +0000948 return;
Douglas Gregor91f84212008-12-11 16:49:14 +0000949 }
Chris Lattner24e24d52009-02-20 00:55:03 +0000950
Chris Lattner29578f32009-02-20 01:10:07 +0000951 // If it is possible that this is a redeclaration, check to see if there is
952 // already a decl for which declarationReplaces returns true. If there is
953 // one, just replace it and return.
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000954 if (!C)
955 C = &getParentASTContext();
956
957 if (DeclNameEntries.HandleRedeclaration(*C, D))
Chris Lattnercaae7162009-02-20 01:44:05 +0000958 return;
Mike Stump11289f42009-09-09 15:08:12 +0000959
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +0000960 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +0000961 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +0000962}
Douglas Gregor889ceb72009-02-03 19:21:40 +0000963
964/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
965/// this context.
Mike Stump11289f42009-09-09 15:08:12 +0000966DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000967DeclContext::getUsingDirectives() const {
968 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +0000969 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
970 reinterpret_cast<udir_iterator>(Result.second));
971}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000972
973void StoredDeclsList::materializeDecls(ASTContext &Context) {
974 if (isNull())
975 return;
976
977 switch ((DataKind)(Data & 0x03)) {
978 case DK_Decl:
979 case DK_Decl_Vector:
980 break;
981
982 case DK_DeclID: {
983 // Resolve this declaration ID to an actual declaration by
984 // querying the external AST source.
985 unsigned DeclID = Data >> 2;
986
987 ExternalASTSource *Source = Context.getExternalSource();
988 assert(Source && "No external AST source available!");
989
John McCall75b960e2010-06-01 09:23:16 +0000990 Data = reinterpret_cast<uintptr_t>(Source->GetExternalDecl(DeclID));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000991 break;
992 }
993
994 case DK_ID_Vector: {
995 // We have a vector of declaration IDs. Resolve all of them to
996 // actual declarations.
997 VectorTy &Vector = *getAsVector();
998 ExternalASTSource *Source = Context.getExternalSource();
999 assert(Source && "No external AST source available!");
1000
1001 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
John McCall75b960e2010-06-01 09:23:16 +00001002 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetExternalDecl(Vector[I]));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001003
1004 Data = (Data & ~0x03) | DK_Decl_Vector;
1005 break;
1006 }
1007 }
1008}
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001009
1010//===----------------------------------------------------------------------===//
1011// Creation and Destruction of StoredDeclsMaps. //
1012//===----------------------------------------------------------------------===//
1013
John McCallc62bb642010-03-24 05:22:00 +00001014StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1015 assert(!LookupPtr && "context already has a decls map");
1016 assert(getPrimaryContext() == this &&
1017 "creating decls map on non-primary context");
1018
1019 StoredDeclsMap *M;
1020 bool Dependent = isDependentContext();
1021 if (Dependent)
1022 M = new DependentStoredDeclsMap();
1023 else
1024 M = new StoredDeclsMap();
1025 M->Previous = C.LastSDM;
1026 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1027 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001028 return M;
1029}
1030
1031void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001032 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1033 // pointer because the subclass doesn't add anything that needs to
1034 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001035 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1036}
1037
1038void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1039 while (Map) {
1040 // Advance the iteration before we invalidate memory.
1041 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1042
1043 if (Dependent)
1044 delete static_cast<DependentStoredDeclsMap*>(Map);
1045 else
1046 delete Map;
1047
1048 Map = Next.getPointer();
1049 Dependent = Next.getInt();
1050 }
1051}
1052
John McCallc62bb642010-03-24 05:22:00 +00001053DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1054 DeclContext *Parent,
1055 const PartialDiagnostic &PDiag) {
1056 assert(Parent->isDependentContext()
1057 && "cannot iterate dependent diagnostics of non-dependent context");
1058 Parent = Parent->getPrimaryContext();
1059 if (!Parent->LookupPtr)
1060 Parent->CreateStoredDeclsMap(C);
1061
1062 DependentStoredDeclsMap *Map
1063 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1064
Douglas Gregora55530e2010-03-29 23:56:53 +00001065 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001066 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001067 PartialDiagnostic::Storage *DiagStorage = 0;
1068 if (PDiag.hasStorage())
1069 DiagStorage = new (C) PartialDiagnostic::Storage;
1070
1071 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001072
1073 // TODO: Maybe we shouldn't reverse the order during insertion.
1074 DD->NextDiagnostic = Map->FirstDiagnostic;
1075 Map->FirstDiagnostic = DD;
1076
1077 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001078}