blob: 8296f2931b8a766b16c3cbd848bf8af24c758286 [file] [log] [blame]
Eli Friedmana8a09ac2008-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 Gregor469fc9a2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregord2b6edc2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor8acb7272008-12-11 16:49:14 +000022#include "clang/AST/Type.h"
Sebastian Redlbc9ef252009-04-26 20:35:05 +000023#include "clang/AST/Stmt.h"
24#include "clang/AST/StmtCXX.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattnerc309ade2009-03-05 08:00:35 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor6e71edc2008-12-23 21:05:05 +000027#include <algorithm>
Chris Lattner7d6220c2009-03-02 22:20:04 +000028#include <cstdio>
Douglas Gregorddfd9d52008-12-23 00:26:44 +000029#include <vector>
Eli Friedmana8a09ac2008-06-07 16:52:53 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Statistics
34//===----------------------------------------------------------------------===//
35
Douglas Gregor469fc9a2009-02-02 23:39:07 +000036#define DECL(Derived, Base) static int n##Derived##s = 0;
37#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000038
39static bool StatSwitch = false;
40
Eli Friedmana8a09ac2008-06-07 16:52:53 +000041const char *Decl::getDeclKindName() const {
42 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000043 default: assert(0 && "Declaration not in DeclNodes.def!");
44#define DECL(Derived, Base) case Derived: return #Derived;
45#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000046 }
47}
48
Steve Naroffe5f128a2009-01-20 19:53:53 +000049const char *DeclContext::getDeclKindName() const {
50 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000051 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argiris Kirtzidis2d9b7612009-02-16 14:28:33 +000052#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor469fc9a2009-02-02 23:39:07 +000053#include "clang/AST/DeclNodes.def"
Steve Naroffe5f128a2009-01-20 19:53:53 +000054 }
55}
56
Eli Friedmana8a09ac2008-06-07 16:52:53 +000057bool Decl::CollectingStats(bool Enable) {
58 if (Enable)
59 StatSwitch = true;
60 return StatSwitch;
61}
62
63void Decl::PrintStats() {
64 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedmana8a09ac2008-06-07 16:52:53 +000065
Douglas Gregor469fc9a2009-02-02 23:39:07 +000066 int totalDecls = 0;
67#define DECL(Derived, Base) totalDecls += n##Derived##s;
68#include "clang/AST/DeclNodes.def"
69 fprintf(stderr, " %d decls total.\n", totalDecls);
70
71 int totalBytes = 0;
72#define DECL(Derived, Base) \
73 if (n##Derived##s > 0) { \
74 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
75 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
76 n##Derived##s, (int)sizeof(Derived##Decl), \
77 (int)(n##Derived##s * sizeof(Derived##Decl))); \
78 }
79#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000080
Douglas Gregor469fc9a2009-02-02 23:39:07 +000081 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedmana8a09ac2008-06-07 16:52:53 +000082}
83
84void Decl::addDeclKind(Kind k) {
85 switch (k) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000086 default: assert(0 && "Declaration not in DeclNodes.def!");
87#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
88#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000089 }
90}
91
Anders Carlssonc2018a72009-06-13 00:08:58 +000092bool Decl::isTemplateParameterPack() const {
93 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
94 return TTP->isParameterPack();
95
96 return false;
97}
98
Douglas Gregorb60eb752009-06-25 22:08:12 +000099bool Decl::isFunctionOrFunctionTemplate() const {
Anders Carlsson5847da52009-06-26 05:26:50 +0000100 if (const UsingDecl *UD = dyn_cast<UsingDecl>(this))
101 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
102
Douglas Gregorb60eb752009-06-25 22:08:12 +0000103 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
104}
105
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000106//===----------------------------------------------------------------------===//
Chris Lattnerc309ade2009-03-05 08:00:35 +0000107// PrettyStackTraceDecl Implementation
108//===----------------------------------------------------------------------===//
109
110void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
111 SourceLocation TheLoc = Loc;
112 if (TheLoc.isInvalid() && TheDecl)
113 TheLoc = TheDecl->getLocation();
114
115 if (TheLoc.isValid()) {
116 TheLoc.print(OS, SM);
117 OS << ": ";
118 }
119
120 OS << Message;
121
122 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
123 OS << " '" << DN->getQualifiedNameAsString() << '\'';
124 OS << '\n';
125}
126
127//===----------------------------------------------------------------------===//
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000128// Decl Implementation
129//===----------------------------------------------------------------------===//
130
Chris Lattner8752fe02009-03-27 20:18:19 +0000131// Out-of-line virtual method providing a home for Decl.
132Decl::~Decl() {
133 if (isOutOfSemaDC())
134 delete getMultipleDC();
135
136 assert(!HasAttrs && "attributes should have been freed by Destroy");
137}
138
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000139void Decl::setDeclContext(DeclContext *DC) {
140 if (isOutOfSemaDC())
141 delete getMultipleDC();
142
Chris Lattner5ce4f6d2009-03-29 06:06:59 +0000143 DeclCtx = DC;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000144}
145
146void Decl::setLexicalDeclContext(DeclContext *DC) {
147 if (DC == getLexicalDeclContext())
148 return;
149
150 if (isInSemaDC()) {
151 MultipleDC *MDC = new MultipleDC();
152 MDC->SemanticDC = getDeclContext();
153 MDC->LexicalDC = DC;
Chris Lattner5ce4f6d2009-03-29 06:06:59 +0000154 DeclCtx = MDC;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000155 } else {
156 getMultipleDC()->LexicalDC = DC;
157 }
158}
159
Argiris Kirtzidis2a6dca12009-06-29 17:38:40 +0000160TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argiris Kirtzidisf7993cb2009-06-30 02:34:53 +0000161 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
162 return TUD;
163
Argiris Kirtzidis2a6dca12009-06-29 17:38:40 +0000164 DeclContext *DC = getDeclContext();
165 assert(DC && "This decl is not contained in a translation unit!");
166
167 while (!DC->isTranslationUnit()) {
168 DC = DC->getParent();
169 assert(DC && "This decl is not contained in a translation unit!");
170 }
171
172 return cast<TranslationUnitDecl>(DC);
173}
174
175ASTContext &Decl::getASTContext() const {
176 return getTranslationUnitDecl()->getASTContext();
177}
178
Chris Lattner8752fe02009-03-27 20:18:19 +0000179unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
180 switch (DeclKind) {
181 default:
182 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
183 return IDNS_Ordinary;
184 assert(0 && "Unknown decl kind!");
185 case OverloadedFunction:
186 case Typedef:
187 case EnumConstant:
188 case Var:
189 case ImplicitParam:
190 case ParmVar:
191 case OriginalParmVar:
192 case NonTypeTemplateParm:
Douglas Gregor683a1142009-06-20 00:51:54 +0000193 case Using:
Chris Lattner8752fe02009-03-27 20:18:19 +0000194 case ObjCMethod:
195 case ObjCContainer:
196 case ObjCCategory:
197 case ObjCInterface:
Chris Lattner8752fe02009-03-27 20:18:19 +0000198 case ObjCProperty:
199 case ObjCCompatibleAlias:
200 return IDNS_Ordinary;
John McCall36493082009-08-11 06:59:38 +0000201
John McCall7be34f42009-08-11 21:13:21 +0000202 case FriendClass:
John McCall36493082009-08-11 06:59:38 +0000203 case FriendFunction:
204 return IDNS_Friend;
Chris Lattner8752fe02009-03-27 20:18:19 +0000205
206 case ObjCProtocol:
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000207 return IDNS_ObjCProtocol;
Chris Lattner8752fe02009-03-27 20:18:19 +0000208
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000209 case ObjCImplementation:
210 return IDNS_ObjCImplementation;
211
212 case ObjCCategoryImpl:
213 return IDNS_ObjCCategoryImpl;
214
Chris Lattner8752fe02009-03-27 20:18:19 +0000215 case Field:
216 case ObjCAtDefsField:
217 case ObjCIvar:
218 return IDNS_Member;
219
220 case Record:
221 case CXXRecord:
222 case Enum:
223 case TemplateTypeParm:
224 return IDNS_Tag;
225
226 case Namespace:
227 case Template:
228 case FunctionTemplate:
229 case ClassTemplate:
230 case TemplateTemplateParm:
Anders Carlsson1bf91fb2009-03-28 23:02:53 +0000231 case NamespaceAlias:
Chris Lattner8752fe02009-03-27 20:18:19 +0000232 return IDNS_Tag | IDNS_Ordinary;
233
234 // Never have names.
235 case LinkageSpec:
236 case FileScopeAsm:
237 case StaticAssert:
238 case ObjCClass:
Chris Lattner8752fe02009-03-27 20:18:19 +0000239 case ObjCPropertyImpl:
240 case ObjCForwardProtocol:
241 case Block:
242 case TranslationUnit:
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000243
Chris Lattner8752fe02009-03-27 20:18:19 +0000244 // Aren't looked up?
245 case UsingDirective:
246 case ClassTemplateSpecialization:
Douglas Gregor58944ac2009-05-31 09:31:02 +0000247 case ClassTemplatePartialSpecialization:
Chris Lattner8752fe02009-03-27 20:18:19 +0000248 return 0;
249 }
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000250}
251
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000252void Decl::addAttr(Attr *NewAttr) {
253 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000254
255 NewAttr->setNext(ExistingAttr);
256 ExistingAttr = NewAttr;
257
258 HasAttrs = true;
259}
260
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000261void Decl::invalidateAttrs() {
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000262 if (!HasAttrs) return;
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000263
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000264 HasAttrs = false;
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000265 getASTContext().eraseDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000266}
267
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000268const Attr *Decl::getAttrsImpl() const {
Chris Lattner004a9bb2009-03-21 06:27:31 +0000269 assert(HasAttrs && "getAttrs() should verify this!");
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000270 return getASTContext().getDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000271}
272
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000273void Decl::swapAttrs(Decl *RHS) {
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000274 bool HasLHSAttr = this->HasAttrs;
275 bool HasRHSAttr = RHS->HasAttrs;
276
277 // Usually, neither decl has attrs, nothing to do.
278 if (!HasLHSAttr && !HasRHSAttr) return;
279
280 // If 'this' has no attrs, swap the other way.
281 if (!HasLHSAttr)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000282 return RHS->swapAttrs(this);
283
284 ASTContext &Context = getASTContext();
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000285
286 // Handle the case when both decls have attrs.
287 if (HasRHSAttr) {
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000288 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000289 return;
290 }
291
292 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000293 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
294 Context.eraseDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000295 this->HasAttrs = false;
296 RHS->HasAttrs = true;
297}
298
299
Chris Lattner0fc6d642009-03-04 06:05:19 +0000300void Decl::Destroy(ASTContext &C) {
301 // Free attributes for this decl.
302 if (HasAttrs) {
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000303 C.getDeclAttrs(this)->Destroy(C);
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000304 invalidateAttrs();
Chris Lattner0fc6d642009-03-04 06:05:19 +0000305 HasAttrs = false;
306 }
307
Douglas Gregor8314d742009-01-13 19:47:12 +0000308#if 0
Douglas Gregorf4006be2009-01-20 04:25:11 +0000309 // FIXME: Once ownership is fully understood, we can enable this code
310 if (DeclContext *DC = dyn_cast<DeclContext>(this))
311 DC->decls_begin()->Destroy(C);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000312
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000313 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000314 // within the loop, only the Destroy method for the first Decl
315 // will deallocate all of the Decls in a chain.
316
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000317 Decl* N = getNextDeclInContext();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000318
319 while (N) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000320 Decl* Tmp = N->getNextDeclInContext();
321 N->NextDeclInContext = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000322 N->Destroy(C);
323 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000324 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000325
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000326 this->~Decl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000327 C.Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000328#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000329}
330
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000331Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000332 Decl::Kind DK = D->getDeclKind();
333 switch(DK) {
334#define DECL_CONTEXT(Name) \
335 case Decl::Name: \
336 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
337#define DECL_CONTEXT_BASE(Name)
338#include "clang/AST/DeclNodes.def"
339 default:
340#define DECL_CONTEXT_BASE(Name) \
341 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
342 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
343#include "clang/AST/DeclNodes.def"
344 assert(false && "a decl that inherits DeclContext isn't handled");
345 return 0;
346 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000347}
348
349DeclContext *Decl::castToDeclContext(const Decl *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000350 Decl::Kind DK = D->getKind();
351 switch(DK) {
352#define DECL_CONTEXT(Name) \
353 case Decl::Name: \
354 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
355#define DECL_CONTEXT_BASE(Name)
356#include "clang/AST/DeclNodes.def"
357 default:
358#define DECL_CONTEXT_BASE(Name) \
359 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
360 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
361#include "clang/AST/DeclNodes.def"
362 assert(false && "a decl that inherits DeclContext isn't handled");
363 return 0;
364 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000365}
366
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000367CompoundStmt* Decl::getCompoundBody() const {
368 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000369}
370
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000371SourceLocation Decl::getBodyRBrace() const {
372 Stmt *Body = getBody();
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000373 if (!Body)
374 return SourceLocation();
375 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
376 return CS->getRBracLoc();
377 assert(isa<CXXTryStmt>(Body) &&
378 "Body can only be CompoundStmt or CXXTryStmt");
379 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
380}
381
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000382#ifndef NDEBUG
383void Decl::CheckAccessDeclContext() const {
Douglas Gregora470cea2009-04-07 20:58:25 +0000384 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
385 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000386 "Access specifier is AS_none inside a record decl");
387}
388
389#endif
390
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000391//===----------------------------------------------------------------------===//
392// DeclContext Implementation
393//===----------------------------------------------------------------------===//
394
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000395bool DeclContext::classof(const Decl *D) {
396 switch (D->getKind()) {
397#define DECL_CONTEXT(Name) case Decl::Name:
398#define DECL_CONTEXT_BASE(Name)
399#include "clang/AST/DeclNodes.def"
400 return true;
401 default:
402#define DECL_CONTEXT_BASE(Name) \
403 if (D->getKind() >= Decl::Name##First && \
404 D->getKind() <= Decl::Name##Last) \
405 return true;
406#include "clang/AST/DeclNodes.def"
407 return false;
408 }
409}
410
Douglas Gregor8acb7272008-12-11 16:49:14 +0000411DeclContext::~DeclContext() {
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000412 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000413}
414
415void DeclContext::DestroyDecls(ASTContext &C) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000416 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregorf4006be2009-01-20 04:25:11 +0000417 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000418}
419
Douglas Gregor1a6b88e2009-05-28 16:34:51 +0000420bool DeclContext::isDependentContext() const {
421 if (isFileContext())
422 return false;
423
Douglas Gregor58944ac2009-05-31 09:31:02 +0000424 if (isa<ClassTemplatePartialSpecializationDecl>(this))
425 return true;
426
Douglas Gregor1a6b88e2009-05-28 16:34:51 +0000427 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
428 if (Record->getDescribedClassTemplate())
429 return true;
430
431 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
432 if (Function->getDescribedFunctionTemplate())
433 return true;
434
435 return getParent() && getParent()->isDependentContext();
436}
437
Douglas Gregord8028382009-01-05 19:45:36 +0000438bool DeclContext::isTransparentContext() const {
439 if (DeclKind == Decl::Enum)
440 return true; // FIXME: Check for C++0x scoped enums
441 else if (DeclKind == Decl::LinkageSpec)
442 return true;
Douglas Gregor1db12932009-02-26 00:02:51 +0000443 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor723d3332009-01-07 00:43:41 +0000444 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000445 else if (DeclKind == Decl::Namespace)
446 return false; // FIXME: Check for C++0x inline namespaces
447
448 return false;
449}
450
Steve Naroffab63fd62009-01-08 17:28:14 +0000451DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000452 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000453 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000454 case Decl::LinkageSpec:
455 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000456 // There is only one DeclContext for these entities.
457 return this;
458
459 case Decl::Namespace:
460 // The original namespace is our primary context.
461 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
462
Douglas Gregor8acb7272008-12-11 16:49:14 +0000463 case Decl::ObjCMethod:
464 return this;
465
466 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000467 case Decl::ObjCProtocol:
468 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000469 // FIXME: Can Objective-C interfaces be forward-declared?
470 return this;
471
Steve Naroffab63fd62009-01-08 17:28:14 +0000472 case Decl::ObjCImplementation:
473 case Decl::ObjCCategoryImpl:
474 return this;
475
Douglas Gregor8acb7272008-12-11 16:49:14 +0000476 default:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000477 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
478 // If this is a tag type that has a definition or is currently
479 // being defined, that definition is our primary context.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000480 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Douglas Gregora08b6c72009-02-17 23:15:12 +0000481 if (TagT->isBeingDefined() ||
482 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
483 return TagT->getDecl();
484 return this;
485 }
486
Douglas Gregor8acb7272008-12-11 16:49:14 +0000487 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
488 "Unknown DeclContext kind");
489 return this;
490 }
491}
492
493DeclContext *DeclContext::getNextContext() {
494 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000495 case Decl::Namespace:
496 // Return the next namespace
497 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
498
499 default:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000500 return 0;
501 }
502}
503
Douglas Gregorc34897d2009-04-09 22:27:44 +0000504/// \brief Load the declarations within this lexical storage from an
505/// external source.
506void
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000507DeclContext::LoadLexicalDeclsFromExternalStorage() const {
508 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000509 assert(hasExternalLexicalStorage() && Source && "No external storage?");
510
Eli Friedmancdf59742009-04-27 23:43:36 +0000511 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000512 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
513 Decls))
514 return;
515
516 // There is no longer any lexical storage in this context
517 ExternalLexicalStorage = false;
518
519 if (Decls.empty())
520 return;
521
522 // Resolve all of the declaration IDs into declarations, building up
523 // a chain of declarations via the Decl::NextDeclInContext field.
524 Decl *FirstNewDecl = 0;
525 Decl *PrevDecl = 0;
526 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
527 Decl *D = Source->GetDecl(Decls[I]);
528 if (PrevDecl)
529 PrevDecl->NextDeclInContext = D;
530 else
531 FirstNewDecl = D;
532
533 PrevDecl = D;
534 }
535
536 // Splice the newly-read declarations into the beginning of the list
537 // of declarations.
538 PrevDecl->NextDeclInContext = FirstDecl;
539 FirstDecl = FirstNewDecl;
540 if (!LastDecl)
541 LastDecl = PrevDecl;
542}
543
544void
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000545DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000546 DeclContext *This = const_cast<DeclContext *>(this);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000547 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000548 assert(hasExternalVisibleStorage() && Source && "No external storage?");
549
550 llvm::SmallVector<VisibleDeclaration, 64> Decls;
551 if (Source->ReadDeclsVisibleInContext(This, Decls))
552 return;
553
554 // There is no longer any visible storage in this context
555 ExternalVisibleStorage = false;
556
557 // Load the declaration IDs for all of the names visible in this
558 // context.
559 assert(!LookupPtr && "Have a lookup map before de-serialization?");
560 StoredDeclsMap *Map = new StoredDeclsMap;
561 LookupPtr = Map;
562 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
563 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
564 }
565}
566
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000567DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000568 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000569 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000570
571 // FIXME: Check whether we need to load some declarations from
572 // external storage.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000573 return decl_iterator(FirstDecl);
574}
575
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000576DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000577 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000578 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000579
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000580 return decl_iterator();
581}
582
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000583bool DeclContext::decls_empty() const {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000584 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000585 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorac8f2802009-04-10 17:25:41 +0000586
587 return !FirstDecl;
588}
589
John McCall36493082009-08-11 06:59:38 +0000590void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner53602072009-02-20 00:56:18 +0000591 assert(D->getLexicalDeclContext() == this &&
592 "Decl inserted into wrong lexical context");
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000593 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregord1675382009-01-09 19:42:16 +0000594 "Decl already inserted into a DeclContext");
595
596 if (FirstDecl) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000597 LastDecl->NextDeclInContext = D;
Douglas Gregord1675382009-01-09 19:42:16 +0000598 LastDecl = D;
599 } else {
600 FirstDecl = LastDecl = D;
601 }
John McCall36493082009-08-11 06:59:38 +0000602}
603
604void DeclContext::addDecl(Decl *D) {
605 addHiddenDecl(D);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000606
607 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000608 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000609}
610
Douglas Gregord8028382009-01-05 19:45:36 +0000611/// buildLookup - Build the lookup data structure with all of the
612/// declarations in DCtx (and any other contexts linked to it or
613/// transparent contexts nested within it).
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000614void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000615 for (; DCtx; DCtx = DCtx->getNextContext()) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000616 for (decl_iterator D = DCtx->decls_begin(),
617 DEnd = DCtx->decls_end();
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000618 D != DEnd; ++D) {
John McCall36493082009-08-11 06:59:38 +0000619 // Insert this declaration into the lookup structure, but only
620 // if it's semantically in its decl context. During non-lazy
621 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000622 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall36493082009-08-11 06:59:38 +0000623 if (D->getDeclContext() == DCtx)
624 makeDeclVisibleInContextImpl(ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000625
626 // If this declaration is itself a transparent declaration context,
627 // add its members (recursively).
628 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
629 if (InnerCtx->isTransparentContext())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000630 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000631 }
632 }
633}
634
Douglas Gregor8acb7272008-12-11 16:49:14 +0000635DeclContext::lookup_result
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000636DeclContext::lookup(DeclarationName Name) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000637 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000638 if (PrimaryContext != this)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000639 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000640
Douglas Gregorc34897d2009-04-09 22:27:44 +0000641 if (hasExternalVisibleStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000642 LoadVisibleDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000643
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000644 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000645 /// all of the linked DeclContexts (in declaration order!) and
646 /// inserting their values.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000647 if (!LookupPtr) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000648 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000649
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000650 if (!LookupPtr)
Chris Lattner265f01d2009-02-20 00:55:03 +0000651 return lookup_result(0, 0);
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000652 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000653
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000654 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
655 StoredDeclsMap::iterator Pos = Map->find(Name);
656 if (Pos == Map->end())
657 return lookup_result(0, 0);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000658 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000659}
660
661DeclContext::lookup_const_result
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000662DeclContext::lookup(DeclarationName Name) const {
663 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000664}
665
Chris Lattner9a502bd2009-03-27 19:19:59 +0000666DeclContext *DeclContext::getLookupContext() {
667 DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000668 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000669 while (Ctx->isTransparentContext())
670 Ctx = Ctx->getParent();
671 return Ctx;
672}
673
Douglas Gregor0d93f692009-02-25 22:02:03 +0000674DeclContext *DeclContext::getEnclosingNamespaceContext() {
675 DeclContext *Ctx = this;
676 // Skip through non-namespace, non-translation-unit contexts.
677 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
678 Ctx = Ctx->getParent();
679 return Ctx->getPrimaryContext();
680}
681
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000682void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000683 // FIXME: This feels like a hack. Should DeclarationName support
684 // template-ids, or is there a better way to keep specializations
685 // from being visible?
686 if (isa<ClassTemplateSpecializationDecl>(D))
687 return;
688
Steve Naroffab63fd62009-01-08 17:28:14 +0000689 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000690 if (PrimaryContext != this) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000691 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000692 return;
693 }
694
695 // If we already have a lookup data structure, perform the insertion
696 // into it. Otherwise, be lazy and don't build that structure until
697 // someone asks for it.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000698 if (LookupPtr)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000699 makeDeclVisibleInContextImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000700
Douglas Gregord8028382009-01-05 19:45:36 +0000701 // If we are a transparent context, insert into our parent context,
702 // too. This operation is recursive.
703 if (isTransparentContext())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000704 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000705}
706
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000707void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000708 // Skip unnamed declarations.
709 if (!D->getDeclName())
710 return;
711
Douglas Gregora08b6c72009-02-17 23:15:12 +0000712 // FIXME: This feels like a hack. Should DeclarationName support
713 // template-ids, or is there a better way to keep specializations
714 // from being visible?
715 if (isa<ClassTemplateSpecializationDecl>(D))
716 return;
717
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000718 if (!LookupPtr)
719 LookupPtr = new StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000720
721 // Insert this declaration into the map.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000722 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner77820d52009-02-20 01:44:05 +0000723 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
724 if (DeclNameEntries.isNull()) {
725 DeclNameEntries.setOnlyValue(D);
Chris Lattner6719b1f2009-02-19 07:00:44 +0000726 return;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000727 }
Chris Lattner265f01d2009-02-20 00:55:03 +0000728
Chris Lattnerba589d42009-02-20 01:10:07 +0000729 // If it is possible that this is a redeclaration, check to see if there is
730 // already a decl for which declarationReplaces returns true. If there is
731 // one, just replace it and return.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000732 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D))
Chris Lattner77820d52009-02-20 01:44:05 +0000733 return;
Chris Lattner265f01d2009-02-20 00:55:03 +0000734
Chris Lattner6719b1f2009-02-19 07:00:44 +0000735 // Put this declaration into the appropriate slot.
Chris Lattner77820d52009-02-20 01:44:05 +0000736 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000737}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000738
739/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
740/// this context.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000741DeclContext::udir_iterator_range
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000742DeclContext::getUsingDirectives() const {
743 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor7a7be652009-02-03 19:21:40 +0000744 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
745 reinterpret_cast<udir_iterator>(Result.second));
746}
Douglas Gregorc34897d2009-04-09 22:27:44 +0000747
748void StoredDeclsList::materializeDecls(ASTContext &Context) {
749 if (isNull())
750 return;
751
752 switch ((DataKind)(Data & 0x03)) {
753 case DK_Decl:
754 case DK_Decl_Vector:
755 break;
756
757 case DK_DeclID: {
758 // Resolve this declaration ID to an actual declaration by
759 // querying the external AST source.
760 unsigned DeclID = Data >> 2;
761
762 ExternalASTSource *Source = Context.getExternalSource();
763 assert(Source && "No external AST source available!");
764
765 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
766 break;
767 }
768
769 case DK_ID_Vector: {
770 // We have a vector of declaration IDs. Resolve all of them to
771 // actual declarations.
772 VectorTy &Vector = *getAsVector();
773 ExternalASTSource *Source = Context.getExternalSource();
774 assert(Source && "No external AST source available!");
775
776 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
777 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
778
779 Data = (Data & ~0x03) | DK_Decl_Vector;
780 break;
781 }
782 }
783}