blob: 437089a415f26af02a7e51bc3079c949fd9394d1 [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"
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Eli Friedman56d29372008-06-07 16:52:53 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000030#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000031#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000032#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000033using namespace clang;
34
35//===----------------------------------------------------------------------===//
36// Statistics
37//===----------------------------------------------------------------------===//
38
Sean Hunt9a555912010-05-30 07:21:58 +000039#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
40#define ABSTRACT_DECL(DECL)
41#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000042
43static bool StatSwitch = false;
44
Douglas Gregor770877f2011-02-17 08:12:32 +000045namespace {
46 template<typename Class>
47 inline SourceRange getSourceRangeImpl(const Decl *D,
48 SourceRange (Class::*)() const) {
49 return static_cast<const Class *>(D)->getSourceRange();
50 }
51
52 inline SourceRange getSourceRangeImpl(const Decl *D,
53 SourceRange (Decl::*)() const) {
54 return D->getLocation();
55 }
56}
57
58SourceRange Decl::getSourceRange() const {
59 switch (getKind()) {
60#define ABSTRACT_DECL(Type)
61#define DECL(Type, Base) \
62 case Type: return getSourceRangeImpl(this, &Type##Decl::getSourceRange);
63#include "clang/AST/DeclNodes.inc"
64 }
65
66 return getLocation();
67}
68
Eli Friedman56d29372008-06-07 16:52:53 +000069const char *Decl::getDeclKindName() const {
70 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000071 default: assert(0 && "Declaration not in DeclNodes.inc!");
72#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
73#define ABSTRACT_DECL(DECL)
74#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000075 }
76}
77
Douglas Gregor42738572010-03-05 00:26:45 +000078void Decl::setInvalidDecl(bool Invalid) {
79 InvalidDecl = Invalid;
80 if (Invalid) {
81 // Defensive maneuver for ill-formed code: we're likely not to make it to
82 // a point where we set the access specifier, so default it to "public"
83 // to avoid triggering asserts elsewhere in the front end.
84 setAccess(AS_public);
85 }
86}
87
Steve Naroff0a473932009-01-20 19:53:53 +000088const char *DeclContext::getDeclKindName() const {
89 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000090 default: assert(0 && "Declaration context not in DeclNodes.inc!");
91#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
92#define ABSTRACT_DECL(DECL)
93#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000094 }
95}
96
Eli Friedman56d29372008-06-07 16:52:53 +000097bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000098 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000099 return StatSwitch;
100}
101
102void Decl::PrintStats() {
103 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregor64650af2009-02-02 23:39:07 +0000105 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000106#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
107#define ABSTRACT_DECL(DECL)
108#include "clang/AST/DeclNodes.inc"
Douglas Gregor64650af2009-02-02 23:39:07 +0000109 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Douglas Gregor64650af2009-02-02 23:39:07 +0000111 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000112#define DECL(DERIVED, BASE) \
113 if (n##DERIVED##s > 0) { \
114 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
115 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
116 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
117 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor64650af2009-02-02 23:39:07 +0000118 }
Sean Hunt9a555912010-05-30 07:21:58 +0000119#define ABSTRACT_DECL(DECL)
120#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Douglas Gregor64650af2009-02-02 23:39:07 +0000122 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +0000123}
124
Sean Hunt9a555912010-05-30 07:21:58 +0000125void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000126 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000127 default: assert(0 && "Declaration not in DeclNodes.inc!");
128#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
129#define ABSTRACT_DECL(DECL)
130#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000131 }
132}
133
Anders Carlsson67e33202009-06-13 00:08:58 +0000134bool Decl::isTemplateParameterPack() const {
135 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
136 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000137 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000138 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000139 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000140 if (const TemplateTemplateParmDecl *TTP
141 = dyn_cast<TemplateTemplateParmDecl>(this))
142 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000143 return false;
144}
145
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000146bool Decl::isParameterPack() const {
147 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
148 return Parm->isParameterPack();
149
150 return isTemplateParameterPack();
151}
152
Douglas Gregore53060f2009-06-25 22:08:12 +0000153bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000154 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000155 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregore53060f2009-06-25 22:08:12 +0000157 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
158}
159
Douglas Gregor79c22782010-01-16 20:21:20 +0000160bool Decl::isDefinedOutsideFunctionOrMethod() const {
161 for (const DeclContext *DC = getDeclContext();
162 DC && !DC->isTranslationUnit();
163 DC = DC->getParent())
164 if (DC->isFunctionOrMethod())
165 return false;
166
167 return true;
168}
169
Douglas Gregor0eccdca2011-02-17 07:58:36 +0000170namespace {
171 template<typename Class, typename Result>
Douglas Gregor87558362011-02-17 08:14:56 +0000172 inline Result *getCanonicalDeclImpl(Decl *D, Result *(Class::*)()) {
173 return static_cast<Class *>(D)->getCanonicalDecl();
Douglas Gregor0eccdca2011-02-17 07:58:36 +0000174 }
175
Douglas Gregor87558362011-02-17 08:14:56 +0000176 inline Decl *getCanonicalDeclImpl(Decl *D, Decl *(Decl::*)()) {
Douglas Gregor0eccdca2011-02-17 07:58:36 +0000177 // No specific implementation.
178 return D;
179 }
180}
181
182Decl *Decl::getCanonicalDecl() {
183 switch (getKind()) {
184#define ABSTRACT_DECL(Type)
185#define DECL(Type, Base) \
186 case Type: \
Douglas Gregor87558362011-02-17 08:14:56 +0000187 return getCanonicalDeclImpl(this, &Type##Decl::getCanonicalDecl);
Douglas Gregor0eccdca2011-02-17 07:58:36 +0000188#include "clang/AST/DeclNodes.inc"
189 }
Douglas Gregor0eccdca2011-02-17 07:58:36 +0000190
Douglas Gregor770877f2011-02-17 08:12:32 +0000191 return this;
Douglas Gregor0eccdca2011-02-17 07:58:36 +0000192}
Douglas Gregor79c22782010-01-16 20:21:20 +0000193
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000194Decl *Decl::getNextRedeclaration() {
195 switch (getKind()) {
196 case Var:
197 return static_cast<VarDecl *>(this)->getNextRedeclaration();
198
199 case Function:
200 case CXXMethod:
201 case CXXConstructor:
202 case CXXDestructor:
203 case CXXConversion:
204 return static_cast<FunctionDecl *>(this)->getNextRedeclaration();
205
206 case Typedef:
207 return static_cast<TypedefDecl *>(this)->getNextRedeclaration();
208
209 case Enum:
210 case Record:
211 case CXXRecord:
212 case ClassTemplateSpecialization:
213 case ClassTemplatePartialSpecialization:
214 return static_cast<TagDecl *>(this)->getNextRedeclaration();
215
216 case ObjCMethod:
217 return static_cast<ObjCMethodDecl *>(this)->getNextRedeclaration();
218
219 case FunctionTemplate:
220 case ClassTemplate:
221 return static_cast<RedeclarableTemplateDecl *>(this)
222 ->getNextRedeclaration();
223
224 case Namespace:
225 case UsingDirective:
226 case NamespaceAlias:
227 case Label:
228 case UnresolvedUsingTypename:
229 case TemplateTypeParm:
230 case EnumConstant:
231 case UnresolvedUsingValue:
232 case IndirectField:
233 case Field:
234 case ObjCIvar:
235 case ObjCAtDefsField:
236 case ImplicitParam:
237 case ParmVar:
238 case NonTypeTemplateParm:
239 case TemplateTemplateParm:
240 case Using:
241 case UsingShadow:
242 case ObjCCategory:
243 case ObjCProtocol:
244 case ObjCInterface:
245 case ObjCCategoryImpl:
246 case ObjCImplementation:
247 case ObjCProperty:
248 case ObjCCompatibleAlias:
249 case LinkageSpec:
250 case ObjCPropertyImpl:
251 case ObjCForwardProtocol:
252 case ObjCClass:
253 case FileScopeAsm:
254 case AccessSpec:
255 case Friend:
256 case FriendTemplate:
257 case StaticAssert:
258 case Block:
259 case TranslationUnit:
260 return this;
261 }
262
263 return this;
264}
265
Eli Friedman56d29372008-06-07 16:52:53 +0000266//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000267// PrettyStackTraceDecl Implementation
268//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattner49f28ca2009-03-05 08:00:35 +0000270void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
271 SourceLocation TheLoc = Loc;
272 if (TheLoc.isInvalid() && TheDecl)
273 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattner49f28ca2009-03-05 08:00:35 +0000275 if (TheLoc.isValid()) {
276 TheLoc.print(OS, SM);
277 OS << ": ";
278 }
279
280 OS << Message;
281
Daniel Dunbarc5236562009-11-21 09:05:59 +0000282 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000283 OS << " '" << DN->getQualifiedNameAsString() << '\'';
284 OS << '\n';
285}
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattner49f28ca2009-03-05 08:00:35 +0000287//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000288// Decl Implementation
289//===----------------------------------------------------------------------===//
290
Chris Lattner769dbdf2009-03-27 20:18:19 +0000291// Out-of-line virtual method providing a home for Decl.
Douglas Gregorff331c12010-07-25 18:17:45 +0000292Decl::~Decl() { }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000293
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000294bool Decl::isOutOfLine() const {
295 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
296 return VD->isOutOfLine();
297 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
298 return FD->isOutOfLine();
299
300 return getLexicalDeclContext() != getDeclContext();
301}
302
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000303void Decl::setDeclContext(DeclContext *DC) {
304 if (isOutOfSemaDC())
305 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattneree219fd2009-03-29 06:06:59 +0000307 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000308}
309
310void Decl::setLexicalDeclContext(DeclContext *DC) {
311 if (DC == getLexicalDeclContext())
312 return;
313
314 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000315 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000316 MDC->SemanticDC = getDeclContext();
317 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000318 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000319 } else {
320 getMultipleDC()->LexicalDC = DC;
321 }
322}
323
John McCall9aeed322009-10-01 00:25:31 +0000324bool Decl::isInAnonymousNamespace() const {
325 const DeclContext *DC = getDeclContext();
326 do {
327 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
328 if (ND->isAnonymousNamespace())
329 return true;
330 } while ((DC = DC->getParent()));
331
332 return false;
333}
334
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000335TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000336 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
337 return TUD;
338
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000339 DeclContext *DC = getDeclContext();
340 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000342 while (!DC->isTranslationUnit()) {
343 DC = DC->getParent();
344 assert(DC && "This decl is not contained in a translation unit!");
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000347 return cast<TranslationUnitDecl>(DC);
348}
349
350ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000351 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000352}
353
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000354ASTMutationListener *Decl::getASTMutationListener() const {
355 return getASTContext().getASTMutationListener();
356}
357
Douglas Gregorc070cc62010-06-17 23:14:26 +0000358bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000359 if (Used)
360 return true;
361
362 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000363 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000364 return true;
365
366 // Check redeclarations for used attribute.
367 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000368 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000369 return true;
370 }
371
372 return false;
373}
374
375
Chris Lattner769dbdf2009-03-27 20:18:19 +0000376unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
377 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000378 case Function:
379 case CXXMethod:
380 case CXXConstructor:
381 case CXXDestructor:
382 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000383 case EnumConstant:
384 case Var:
385 case ImplicitParam:
386 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000387 case NonTypeTemplateParm:
388 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000389 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000390 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000391 case Label:
392 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000393 case IndirectField:
394 return IDNS_Ordinary | IDNS_Member;
395
John McCall0d6b1642010-04-23 18:46:30 +0000396 case ObjCCompatibleAlias:
397 case ObjCInterface:
398 return IDNS_Ordinary | IDNS_Type;
399
400 case Typedef:
401 case UnresolvedUsingTypename:
402 case TemplateTypeParm:
403 return IDNS_Ordinary | IDNS_Type;
404
John McCall9488ea12009-11-17 05:59:44 +0000405 case UsingShadow:
406 return 0; // we'll actually overwrite this later
407
John McCall7ba107a2009-11-18 02:36:19 +0000408 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000409 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000410
411 case Using:
412 return IDNS_Using;
413
Chris Lattner769dbdf2009-03-27 20:18:19 +0000414 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000415 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner769dbdf2009-03-27 20:18:19 +0000417 case Field:
418 case ObjCAtDefsField:
419 case ObjCIvar:
420 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner769dbdf2009-03-27 20:18:19 +0000422 case Record:
423 case CXXRecord:
424 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000425 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattner769dbdf2009-03-27 20:18:19 +0000427 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000428 case NamespaceAlias:
429 return IDNS_Namespace;
430
Chris Lattner769dbdf2009-03-27 20:18:19 +0000431 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000432 return IDNS_Ordinary;
433
Chris Lattner769dbdf2009-03-27 20:18:19 +0000434 case ClassTemplate:
435 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000436 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner769dbdf2009-03-27 20:18:19 +0000438 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000439 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000440 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000441 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000442 case LinkageSpec:
443 case FileScopeAsm:
444 case StaticAssert:
445 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000446 case ObjCPropertyImpl:
447 case ObjCForwardProtocol:
448 case Block:
449 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000450
Chris Lattner769dbdf2009-03-27 20:18:19 +0000451 case UsingDirective:
452 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000453 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000454 case ObjCImplementation:
455 case ObjCCategory:
456 case ObjCCategoryImpl:
457 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000458 return 0;
459 }
John McCall9488ea12009-11-17 05:59:44 +0000460
461 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000462}
463
Sean Huntcf807c42010-08-18 23:23:40 +0000464void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000465 assert(!HasAttrs && "Decl already contains attrs.");
466
Sean Huntcf807c42010-08-18 23:23:40 +0000467 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
468 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000469
470 AttrBlank = attrs;
471 HasAttrs = true;
472}
473
Sean Huntcf807c42010-08-18 23:23:40 +0000474void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000475 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Eli Friedman56d29372008-06-07 16:52:53 +0000477 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000478 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000479}
480
Sean Huntcf807c42010-08-18 23:23:40 +0000481const AttrVec &Decl::getAttrs() const {
482 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000483 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000484}
485
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000486void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000487 bool HasLHSAttr = this->HasAttrs;
488 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Eli Friedman56d29372008-06-07 16:52:53 +0000490 // Usually, neither decl has attrs, nothing to do.
491 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Eli Friedman56d29372008-06-07 16:52:53 +0000493 // If 'this' has no attrs, swap the other way.
494 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000495 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000497 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Eli Friedman56d29372008-06-07 16:52:53 +0000499 // Handle the case when both decls have attrs.
500 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000501 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000502 return;
503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Eli Friedman56d29372008-06-07 16:52:53 +0000505 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000506 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
507 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000508 this->HasAttrs = false;
509 RHS->HasAttrs = true;
510}
511
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000512Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000513 Decl::Kind DK = D->getDeclKind();
514 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000515#define DECL(NAME, BASE)
516#define DECL_CONTEXT(NAME) \
517 case Decl::NAME: \
518 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
519#define DECL_CONTEXT_BASE(NAME)
520#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000521 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000522#define DECL(NAME, BASE)
523#define DECL_CONTEXT_BASE(NAME) \
524 if (DK >= first##NAME && DK <= last##NAME) \
525 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
526#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000527 assert(false && "a decl that inherits DeclContext isn't handled");
528 return 0;
529 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000530}
531
532DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000533 Decl::Kind DK = D->getKind();
534 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000535#define DECL(NAME, BASE)
536#define DECL_CONTEXT(NAME) \
537 case Decl::NAME: \
538 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
539#define DECL_CONTEXT_BASE(NAME)
540#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000541 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000542#define DECL(NAME, BASE)
543#define DECL_CONTEXT_BASE(NAME) \
544 if (DK >= first##NAME && DK <= last##NAME) \
545 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
546#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000547 assert(false && "a decl that inherits DeclContext isn't handled");
548 return 0;
549 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000550}
551
Douglas Gregor1aa3d812011-02-17 07:13:24 +0000552Stmt *Decl::getBody() const {
553 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
554 return FD->getBody();
555 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(this))
556 return MD->getBody();
557 if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
558 return BD->getBody();
559
560 return 0;
561}
562
563bool Decl::hasBody() const {
564 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
565 return FD->hasBody();
566
567 return getBody() != 0;
568}
569
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000570SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000571 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
572 // FunctionDecl stores EndRangeLoc for this purpose.
573 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
574 const FunctionDecl *Definition;
575 if (FD->hasBody(Definition))
576 return Definition->getSourceRange().getEnd();
577 return SourceLocation();
578 }
579
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000580 if (Stmt *Body = getBody())
581 return Body->getSourceRange().getEnd();
582
583 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000584}
585
Anders Carlsson1329c272009-03-25 23:38:06 +0000586void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000587#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000588 // Suppress this check if any of the following hold:
589 // 1. this is the translation unit (and thus has no parent)
590 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000591 // 3. this is a non-type template parameter
592 // 4. the context is not a record
593 // 5. it's invalid
594 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000595 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000596 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000597 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000598 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000599 isInvalidDecl() ||
600 isa<StaticAssertDecl>(this) ||
601 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
602 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000603 isa<ParmVarDecl>(this) ||
604 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
605 // AS_none as access specifier.
606 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000607 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000608
609 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000610 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000611#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000612}
613
Anders Carlsson1329c272009-03-25 23:38:06 +0000614
Eli Friedman56d29372008-06-07 16:52:53 +0000615//===----------------------------------------------------------------------===//
616// DeclContext Implementation
617//===----------------------------------------------------------------------===//
618
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000619bool DeclContext::classof(const Decl *D) {
620 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000621#define DECL(NAME, BASE)
622#define DECL_CONTEXT(NAME) case Decl::NAME:
623#define DECL_CONTEXT_BASE(NAME)
624#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000625 return true;
626 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000627#define DECL(NAME, BASE)
628#define DECL_CONTEXT_BASE(NAME) \
629 if (D->getKind() >= Decl::first##NAME && \
630 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000631 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000632#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000633 return false;
634 }
635}
636
Douglas Gregora2da7802010-07-25 18:38:02 +0000637DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000638
Douglas Gregore942bbe2009-09-10 16:57:35 +0000639/// \brief Find the parent context of this context that will be
640/// used for unqualified name lookup.
641///
642/// Generally, the parent lookup context is the semantic context. However, for
643/// a friend function the parent lookup context is the lexical context, which
644/// is the class in which the friend is declared.
645DeclContext *DeclContext::getLookupParent() {
646 // FIXME: Find a better way to identify friends
647 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000648 if (getParent()->getRedeclContext()->isFileContext() &&
649 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000650 return getLexicalParent();
651
652 return getParent();
653}
654
Sebastian Redl410c4f22010-08-31 20:53:31 +0000655bool DeclContext::isInlineNamespace() const {
656 return isNamespace() &&
657 cast<NamespaceDecl>(this)->isInline();
658}
659
Douglas Gregorbc221632009-05-28 16:34:51 +0000660bool DeclContext::isDependentContext() const {
661 if (isFileContext())
662 return false;
663
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000664 if (isa<ClassTemplatePartialSpecializationDecl>(this))
665 return true;
666
Douglas Gregorbc221632009-05-28 16:34:51 +0000667 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
668 if (Record->getDescribedClassTemplate())
669 return true;
670
John McCall0c01d182010-03-24 05:22:00 +0000671 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000672 if (Function->getDescribedFunctionTemplate())
673 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000674
John McCall0c01d182010-03-24 05:22:00 +0000675 // Friend function declarations are dependent if their *lexical*
676 // context is dependent.
677 if (cast<Decl>(this)->getFriendObjectKind())
678 return getLexicalParent()->isDependentContext();
679 }
680
Douglas Gregorbc221632009-05-28 16:34:51 +0000681 return getParent() && getParent()->isDependentContext();
682}
683
Douglas Gregor074149e2009-01-05 19:45:36 +0000684bool DeclContext::isTransparentContext() const {
685 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000686 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000687 else if (DeclKind == Decl::LinkageSpec)
688 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000689
690 return false;
691}
692
John McCallac65c622010-10-26 04:59:26 +0000693bool DeclContext::isExternCContext() const {
694 const DeclContext *DC = this;
695 while (DC->DeclKind != Decl::TranslationUnit) {
696 if (DC->DeclKind == Decl::LinkageSpec)
697 return cast<LinkageSpecDecl>(DC)->getLanguage()
698 == LinkageSpecDecl::lang_c;
699 DC = DC->getParent();
700 }
701 return false;
702}
703
Sebastian Redl7a126a42010-08-31 00:36:30 +0000704bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000705 if (getPrimaryContext() != this)
706 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000708 for (; DC; DC = DC->getParent())
709 if (DC->getPrimaryContext() == this)
710 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000711 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000712}
713
Steve Naroff0701bbb2009-01-08 17:28:14 +0000714DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000715 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000716 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000717 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000718 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000719 // There is only one DeclContext for these entities.
720 return this;
721
722 case Decl::Namespace:
723 // The original namespace is our primary context.
724 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
725
Douglas Gregor44b43212008-12-11 16:49:14 +0000726 case Decl::ObjCMethod:
727 return this;
728
729 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000730 case Decl::ObjCProtocol:
731 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000732 // FIXME: Can Objective-C interfaces be forward-declared?
733 return this;
734
Steve Naroff0701bbb2009-01-08 17:28:14 +0000735 case Decl::ObjCImplementation:
736 case Decl::ObjCCategoryImpl:
737 return this;
738
Douglas Gregor44b43212008-12-11 16:49:14 +0000739 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000740 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000741 // If this is a tag type that has a definition or is currently
742 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000743 TagDecl *Tag = cast<TagDecl>(this);
744 assert(isa<TagType>(Tag->TypeForDecl) ||
745 isa<InjectedClassNameType>(Tag->TypeForDecl));
746
747 if (TagDecl *Def = Tag->getDefinition())
748 return Def;
749
750 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
751 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
752 if (TagTy->isBeingDefined())
753 // FIXME: is it necessarily being defined in the decl
754 // that owns the type?
755 return TagTy->getDecl();
756 }
757
758 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000759 }
760
Sean Hunt9a555912010-05-30 07:21:58 +0000761 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000762 "Unknown DeclContext kind");
763 return this;
764 }
765}
766
767DeclContext *DeclContext::getNextContext() {
768 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000769 case Decl::Namespace:
770 // Return the next namespace
771 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
772
773 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000774 return 0;
775 }
776}
777
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000778std::pair<Decl *, Decl *>
779DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
780 // Build up a chain of declarations via the Decl::NextDeclInContext field.
781 Decl *FirstNewDecl = 0;
782 Decl *PrevDecl = 0;
783 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
784 Decl *D = Decls[I];
785 if (PrevDecl)
786 PrevDecl->NextDeclInContext = D;
787 else
788 FirstNewDecl = D;
789
790 PrevDecl = D;
791 }
792
793 return std::make_pair(FirstNewDecl, PrevDecl);
794}
795
Douglas Gregor2cf26342009-04-09 22:27:44 +0000796/// \brief Load the declarations within this lexical storage from an
797/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000798void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000799DeclContext::LoadLexicalDeclsFromExternalStorage() const {
800 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000801 assert(hasExternalLexicalStorage() && Source && "No external storage?");
802
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000803 // Notify that we have a DeclContext that is initializing.
804 ExternalASTSource::Deserializing ADeclContext(Source);
805
John McCall76bd1f32010-06-01 09:23:16 +0000806 llvm::SmallVector<Decl*, 64> Decls;
807 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000808 return;
809
810 // There is no longer any lexical storage in this context
811 ExternalLexicalStorage = false;
812
813 if (Decls.empty())
814 return;
815
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000816 // We may have already loaded just the fields of this record, in which case
817 // don't add the decls, just replace the FirstDecl/LastDecl chain.
818 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
819 if (RD->LoadedFieldsFromExternalStorage) {
820 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
821 return;
822 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000823
824 // Splice the newly-read declarations into the beginning of the list
825 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000826 Decl *ExternalFirst, *ExternalLast;
827 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
828 ExternalLast->NextDeclInContext = FirstDecl;
829 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000830 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000831 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000832}
833
John McCall76bd1f32010-06-01 09:23:16 +0000834DeclContext::lookup_result
835ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
836 DeclarationName Name) {
837 ASTContext &Context = DC->getParentASTContext();
838 StoredDeclsMap *Map;
839 if (!(Map = DC->LookupPtr))
840 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000841
John McCall76bd1f32010-06-01 09:23:16 +0000842 StoredDeclsList &List = (*Map)[Name];
843 assert(List.isNull());
844 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000845
John McCall76bd1f32010-06-01 09:23:16 +0000846 return DeclContext::lookup_result();
847}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000848
John McCall76bd1f32010-06-01 09:23:16 +0000849DeclContext::lookup_result
850ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000851 DeclarationName Name,
852 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
853 ASTContext &Context = DC->getParentASTContext();;
854
855 StoredDeclsMap *Map;
856 if (!(Map = DC->LookupPtr))
857 Map = DC->CreateStoredDeclsMap(Context);
858
859 StoredDeclsList &List = (*Map)[Name];
860 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
861 if (List.isNull())
862 List.setOnlyValue(Decls[I]);
863 else
864 List.AddSubsequentDecl(Decls[I]);
865 }
866
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000867 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000868}
869
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000870void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
871 DeclarationName Name,
872 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
873 assert(DC->LookupPtr);
874 StoredDeclsMap &Map = *DC->LookupPtr;
875
876 // If there's an entry in the table the visible decls for this name have
877 // already been deserialized.
878 if (Map.find(Name) == Map.end()) {
879 StoredDeclsList &List = Map[Name];
880 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
881 if (List.isNull())
882 List.setOnlyValue(Decls[I]);
883 else
884 List.AddSubsequentDecl(Decls[I]);
885 }
886 }
887}
888
Sebastian Redl681d7232010-07-27 00:17:23 +0000889DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
890 return decl_iterator(FirstDecl);
891}
892
893DeclContext::decl_iterator DeclContext::noload_decls_end() const {
894 return decl_iterator();
895}
896
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000897DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000898 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000899 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000900
901 // FIXME: Check whether we need to load some declarations from
902 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000903 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000904}
905
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000906DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000907 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000908 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000909
Mike Stump1eb44332009-09-09 15:08:12 +0000910 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000911}
912
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000913bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000914 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000915 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000916
917 return !FirstDecl;
918}
919
John McCall9f54ad42009-12-10 09:41:52 +0000920void DeclContext::removeDecl(Decl *D) {
921 assert(D->getLexicalDeclContext() == this &&
922 "decl being removed from non-lexical context");
923 assert((D->NextDeclInContext || D == LastDecl) &&
924 "decl is not in decls list");
925
926 // Remove D from the decl chain. This is O(n) but hopefully rare.
927 if (D == FirstDecl) {
928 if (D == LastDecl)
929 FirstDecl = LastDecl = 0;
930 else
931 FirstDecl = D->NextDeclInContext;
932 } else {
933 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
934 assert(I && "decl not found in linked list");
935 if (I->NextDeclInContext == D) {
936 I->NextDeclInContext = D->NextDeclInContext;
937 if (D == LastDecl) LastDecl = I;
938 break;
939 }
940 }
941 }
942
943 // Mark that D is no longer in the decl chain.
944 D->NextDeclInContext = 0;
945
946 // Remove D from the lookup table if necessary.
947 if (isa<NamedDecl>(D)) {
948 NamedDecl *ND = cast<NamedDecl>(D);
949
John McCall0c01d182010-03-24 05:22:00 +0000950 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
951 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000952
John McCall9f54ad42009-12-10 09:41:52 +0000953 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
954 assert(Pos != Map->end() && "no lookup entry for decl");
955 Pos->second.remove(ND);
956 }
957}
958
John McCall3f9a8a62009-08-11 06:59:38 +0000959void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000960 assert(D->getLexicalDeclContext() == this &&
961 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000962 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000963 "Decl already inserted into a DeclContext");
964
965 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000966 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000967 LastDecl = D;
968 } else {
969 FirstDecl = LastDecl = D;
970 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000971
972 // Notify a C++ record declaration that we've added a member, so it can
973 // update it's class-specific state.
974 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
975 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +0000976}
977
978void DeclContext::addDecl(Decl *D) {
979 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000980
981 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000982 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000983}
984
Douglas Gregor074149e2009-01-05 19:45:36 +0000985/// buildLookup - Build the lookup data structure with all of the
986/// declarations in DCtx (and any other contexts linked to it or
987/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000988void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000989 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000990 for (decl_iterator D = DCtx->decls_begin(),
991 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000992 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000993 // Insert this declaration into the lookup structure, but only
994 // if it's semantically in its decl context. During non-lazy
995 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000996 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000997 if (D->getDeclContext() == DCtx)
998 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000999
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001000 // Insert any forward-declared Objective-C interfaces into the lookup
1001 // data structure.
1002 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
1003 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
1004 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +00001005 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001006
Sebastian Redl410c4f22010-08-31 20:53:31 +00001007 // If this declaration is itself a transparent declaration context or
1008 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001009 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001010 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001011 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001012 }
1013 }
1014}
1015
Mike Stump1eb44332009-09-09 15:08:12 +00001016DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001017DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001018 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001019 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001020 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001021
John McCall76bd1f32010-06-01 09:23:16 +00001022 if (hasExternalVisibleStorage()) {
1023 // Check to see if we've already cached the lookup results.
1024 if (LookupPtr) {
1025 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1026 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001027 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001028 }
1029
1030 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1031 return Source->FindExternalVisibleDeclsByName(this, Name);
1032 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001033
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001034 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001035 /// all of the linked DeclContexts (in declaration order!) and
1036 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001037 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001038 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001039
Douglas Gregorc36c5402009-04-09 17:29:08 +00001040 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001041 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001042 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001043
John McCall0c01d182010-03-24 05:22:00 +00001044 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1045 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001046 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001047 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001048}
1049
Mike Stump1eb44332009-09-09 15:08:12 +00001050DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001051DeclContext::lookup(DeclarationName Name) const {
1052 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001053}
1054
Sebastian Redl7a126a42010-08-31 00:36:30 +00001055DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001056 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001057 // Skip through transparent contexts.
1058 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001059 Ctx = Ctx->getParent();
1060 return Ctx;
1061}
1062
Douglas Gregor88b70942009-02-25 22:02:03 +00001063DeclContext *DeclContext::getEnclosingNamespaceContext() {
1064 DeclContext *Ctx = this;
1065 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001066 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001067 Ctx = Ctx->getParent();
1068 return Ctx->getPrimaryContext();
1069}
1070
Sebastian Redl7a126a42010-08-31 00:36:30 +00001071bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1072 // For non-file contexts, this is equivalent to Equals.
1073 if (!isFileContext())
1074 return O->Equals(this);
1075
1076 do {
1077 if (O->Equals(this))
1078 return true;
1079
1080 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1081 if (!NS || !NS->isInline())
1082 break;
1083 O = NS->getParent();
1084 } while (O);
1085
1086 return false;
1087}
1088
John McCallab88d972009-08-31 22:39:49 +00001089void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001090 // FIXME: This feels like a hack. Should DeclarationName support
1091 // template-ids, or is there a better way to keep specializations
1092 // from being visible?
1093 if (isa<ClassTemplateSpecializationDecl>(D))
1094 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001095 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1096 if (FD->isFunctionTemplateSpecialization())
1097 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001098
Steve Naroff0701bbb2009-01-08 17:28:14 +00001099 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001100 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +00001101 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001102 return;
1103 }
1104
1105 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001106 // into it. If we haven't deserialized externally stored decls, deserialize
1107 // them so we can add the decl. Otherwise, be lazy and don't build that
1108 // structure until someone asks for it.
1109 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001110 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +00001111
Sebastian Redl410c4f22010-08-31 20:53:31 +00001112 // If we are a transparent context or inline namespace, insert into our
1113 // parent context, too. This operation is recursive.
1114 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +00001115 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001116
1117 Decl *DCAsDecl = cast<Decl>(this);
1118 // Notify that a decl was made visible unless it's a Tag being defined.
1119 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1120 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1121 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001122}
1123
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001124void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001125 // Skip unnamed declarations.
1126 if (!D->getDeclName())
1127 return;
1128
Douglas Gregorcc636682009-02-17 23:15:12 +00001129 // FIXME: This feels like a hack. Should DeclarationName support
1130 // template-ids, or is there a better way to keep specializations
1131 // from being visible?
1132 if (isa<ClassTemplateSpecializationDecl>(D))
1133 return;
1134
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001135 ASTContext *C = 0;
1136 if (!LookupPtr) {
1137 C = &getParentASTContext();
1138 CreateStoredDeclsMap(*C);
1139 }
1140
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001141 // If there is an external AST source, load any declarations it knows about
1142 // with this declaration's name.
1143 // If the lookup table contains an entry about this name it means that we
1144 // have already checked the external source.
1145 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1146 if (hasExternalVisibleStorage() &&
1147 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1148 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1149
Douglas Gregor44b43212008-12-11 16:49:14 +00001150 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001151 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001152 if (DeclNameEntries.isNull()) {
1153 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001154 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001155 }
Chris Lattner91942502009-02-20 00:55:03 +00001156
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001157 // If it is possible that this is a redeclaration, check to see if there is
1158 // already a decl for which declarationReplaces returns true. If there is
1159 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001160 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001161 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001163 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001164 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001165}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001166
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001167void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1168 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1169 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1170
1171 if (!LookupPtr)
1172 CreateStoredDeclsMap(getParentASTContext());
1173 Source->MaterializeVisibleDecls(this);
1174}
1175
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001176/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1177/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001178DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001179DeclContext::getUsingDirectives() const {
1180 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001181 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1182 reinterpret_cast<udir_iterator>(Result.second));
1183}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001184
Ted Kremenek3478eb62010-02-11 07:12:28 +00001185//===----------------------------------------------------------------------===//
1186// Creation and Destruction of StoredDeclsMaps. //
1187//===----------------------------------------------------------------------===//
1188
John McCall0c01d182010-03-24 05:22:00 +00001189StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1190 assert(!LookupPtr && "context already has a decls map");
1191 assert(getPrimaryContext() == this &&
1192 "creating decls map on non-primary context");
1193
1194 StoredDeclsMap *M;
1195 bool Dependent = isDependentContext();
1196 if (Dependent)
1197 M = new DependentStoredDeclsMap();
1198 else
1199 M = new StoredDeclsMap();
1200 M->Previous = C.LastSDM;
1201 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1202 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001203 return M;
1204}
1205
1206void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001207 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1208 // pointer because the subclass doesn't add anything that needs to
1209 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001210 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1211}
1212
1213void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1214 while (Map) {
1215 // Advance the iteration before we invalidate memory.
1216 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1217
1218 if (Dependent)
1219 delete static_cast<DependentStoredDeclsMap*>(Map);
1220 else
1221 delete Map;
1222
1223 Map = Next.getPointer();
1224 Dependent = Next.getInt();
1225 }
1226}
1227
John McCall0c01d182010-03-24 05:22:00 +00001228DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1229 DeclContext *Parent,
1230 const PartialDiagnostic &PDiag) {
1231 assert(Parent->isDependentContext()
1232 && "cannot iterate dependent diagnostics of non-dependent context");
1233 Parent = Parent->getPrimaryContext();
1234 if (!Parent->LookupPtr)
1235 Parent->CreateStoredDeclsMap(C);
1236
1237 DependentStoredDeclsMap *Map
1238 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1239
Douglas Gregorb8365182010-03-29 23:56:53 +00001240 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001241 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001242 PartialDiagnostic::Storage *DiagStorage = 0;
1243 if (PDiag.hasStorage())
1244 DiagStorage = new (C) PartialDiagnostic::Storage;
1245
1246 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001247
1248 // TODO: Maybe we shouldn't reverse the order during insertion.
1249 DD->NextDiagnostic = Map->FirstDiagnostic;
1250 Map->FirstDiagnostic = DD;
1251
1252 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001253}