blob: 11d28df4be7e27bf66c203bb19c0ed9890c4fe00 [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"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman56d29372008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000031#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000032#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000033#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Statistics
38//===----------------------------------------------------------------------===//
39
Sean Hunt9a555912010-05-30 07:21:58 +000040#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
41#define ABSTRACT_DECL(DECL)
42#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000043
44static bool StatSwitch = false;
45
Eli Friedman56d29372008-06-07 16:52:53 +000046const char *Decl::getDeclKindName() const {
47 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000048 default: assert(0 && "Declaration not in DeclNodes.inc!");
49#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
50#define ABSTRACT_DECL(DECL)
51#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000052 }
53}
54
Douglas Gregor42738572010-03-05 00:26:45 +000055void Decl::setInvalidDecl(bool Invalid) {
56 InvalidDecl = Invalid;
57 if (Invalid) {
58 // Defensive maneuver for ill-formed code: we're likely not to make it to
59 // a point where we set the access specifier, so default it to "public"
60 // to avoid triggering asserts elsewhere in the front end.
61 setAccess(AS_public);
62 }
63}
64
Steve Naroff0a473932009-01-20 19:53:53 +000065const char *DeclContext::getDeclKindName() const {
66 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000067 default: assert(0 && "Declaration context not in DeclNodes.inc!");
68#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
69#define ABSTRACT_DECL(DECL)
70#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000071 }
72}
73
Eli Friedman56d29372008-06-07 16:52:53 +000074bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000075 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000076 return StatSwitch;
77}
78
79void Decl::PrintStats() {
80 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000081
Douglas Gregor64650af2009-02-02 23:39:07 +000082 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000083#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
84#define ABSTRACT_DECL(DECL)
85#include "clang/AST/DeclNodes.inc"
Douglas Gregor64650af2009-02-02 23:39:07 +000086 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000087
Douglas Gregor64650af2009-02-02 23:39:07 +000088 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000089#define DECL(DERIVED, BASE) \
90 if (n##DERIVED##s > 0) { \
91 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
92 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
93 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
94 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor64650af2009-02-02 23:39:07 +000095 }
Sean Hunt9a555912010-05-30 07:21:58 +000096#define ABSTRACT_DECL(DECL)
97#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +000098
Douglas Gregor64650af2009-02-02 23:39:07 +000099 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +0000100}
101
Sean Hunt9a555912010-05-30 07:21:58 +0000102void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000103 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000104 default: assert(0 && "Declaration not in DeclNodes.inc!");
105#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
106#define ABSTRACT_DECL(DECL)
107#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000108 }
109}
110
Anders Carlsson67e33202009-06-13 00:08:58 +0000111bool Decl::isTemplateParameterPack() const {
112 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
113 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000114 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000115 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000116 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000117 if (const TemplateTemplateParmDecl *TTP
118 = dyn_cast<TemplateTemplateParmDecl>(this))
119 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000120 return false;
121}
122
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000123bool Decl::isParameterPack() const {
124 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
125 return Parm->isParameterPack();
126
127 return isTemplateParameterPack();
128}
129
Douglas Gregore53060f2009-06-25 22:08:12 +0000130bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000131 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000132 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Douglas Gregore53060f2009-06-25 22:08:12 +0000134 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
135}
136
Douglas Gregor79c22782010-01-16 20:21:20 +0000137bool Decl::isDefinedOutsideFunctionOrMethod() const {
138 for (const DeclContext *DC = getDeclContext();
139 DC && !DC->isTranslationUnit();
140 DC = DC->getParent())
141 if (DC->isFunctionOrMethod())
142 return false;
143
144 return true;
145}
146
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000147
Eli Friedman56d29372008-06-07 16:52:53 +0000148//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000149// PrettyStackTraceDecl Implementation
150//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattner49f28ca2009-03-05 08:00:35 +0000152void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
153 SourceLocation TheLoc = Loc;
154 if (TheLoc.isInvalid() && TheDecl)
155 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattner49f28ca2009-03-05 08:00:35 +0000157 if (TheLoc.isValid()) {
158 TheLoc.print(OS, SM);
159 OS << ": ";
160 }
161
162 OS << Message;
163
Daniel Dunbarc5236562009-11-21 09:05:59 +0000164 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000165 OS << " '" << DN->getQualifiedNameAsString() << '\'';
166 OS << '\n';
167}
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattner49f28ca2009-03-05 08:00:35 +0000169//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000170// Decl Implementation
171//===----------------------------------------------------------------------===//
172
Douglas Gregorda2142f2011-02-19 18:51:44 +0000173// Out-of-line virtual method providing a home for Decl.
174Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000175
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000176void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000177 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000178}
179
180void Decl::setLexicalDeclContext(DeclContext *DC) {
181 if (DC == getLexicalDeclContext())
182 return;
183
184 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000185 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000186 MDC->SemanticDC = getDeclContext();
187 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000188 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000189 } else {
190 getMultipleDC()->LexicalDC = DC;
191 }
192}
193
John McCall9aeed322009-10-01 00:25:31 +0000194bool Decl::isInAnonymousNamespace() const {
195 const DeclContext *DC = getDeclContext();
196 do {
197 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
198 if (ND->isAnonymousNamespace())
199 return true;
200 } while ((DC = DC->getParent()));
201
202 return false;
203}
204
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000205TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000206 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
207 return TUD;
208
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000209 DeclContext *DC = getDeclContext();
210 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000212 while (!DC->isTranslationUnit()) {
213 DC = DC->getParent();
214 assert(DC && "This decl is not contained in a translation unit!");
215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000217 return cast<TranslationUnitDecl>(DC);
218}
219
220ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000221 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000222}
223
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000224ASTMutationListener *Decl::getASTMutationListener() const {
225 return getASTContext().getASTMutationListener();
226}
227
Douglas Gregorc070cc62010-06-17 23:14:26 +0000228bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000229 if (Used)
230 return true;
231
232 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000233 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000234 return true;
235
236 // Check redeclarations for used attribute.
237 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000238 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000239 return true;
240 }
241
242 return false;
243}
244
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000245/// \brief Determine the availability of the given declaration based on
246/// the target platform.
247///
248/// When it returns an availability result other than \c AR_Available,
249/// if the \p Message parameter is non-NULL, it will be set to a
250/// string describing why the entity is unavailable.
251///
252/// FIXME: Make these strings localizable, since they end up in
253/// diagnostics.
254static AvailabilityResult CheckAvailability(ASTContext &Context,
255 const AvailabilityAttr *A,
256 std::string *Message) {
257 llvm::StringRef TargetPlatform = Context.Target.getPlatformName();
258 llvm::StringRef PrettyPlatformName
259 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
260 if (PrettyPlatformName.empty())
261 PrettyPlatformName = TargetPlatform;
262
263 VersionTuple TargetMinVersion = Context.Target.getPlatformMinVersion();
264 if (TargetMinVersion.empty())
265 return AR_Available;
266
267 // Match the platform name.
268 if (A->getPlatform()->getName() != TargetPlatform)
269 return AR_Available;
270
271 // Make sure that this declaration has already been introduced.
272 if (!A->getIntroduced().empty() &&
273 TargetMinVersion < A->getIntroduced()) {
274 if (Message) {
275 Message->clear();
276 llvm::raw_string_ostream Out(*Message);
277 Out << "introduced in " << PrettyPlatformName << ' '
278 << A->getIntroduced();
279 }
280
281 return AR_NotYetIntroduced;
282 }
283
284 // Make sure that this declaration hasn't been obsoleted.
285 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
286 if (Message) {
287 Message->clear();
288 llvm::raw_string_ostream Out(*Message);
289 Out << "obsoleted in " << PrettyPlatformName << ' '
290 << A->getObsoleted();
291 }
292
293 return AR_Unavailable;
294 }
295
296 // Make sure that this declaration hasn't been deprecated.
297 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
298 if (Message) {
299 Message->clear();
300 llvm::raw_string_ostream Out(*Message);
301 Out << "first deprecated in " << PrettyPlatformName << ' '
302 << A->getDeprecated();
303 }
304
305 return AR_Deprecated;
306 }
307
308 return AR_Available;
309}
310
311AvailabilityResult Decl::getAvailability(std::string *Message) const {
312 AvailabilityResult Result = AR_Available;
313 std::string ResultMessage;
314
315 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
316 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
317 if (Result >= AR_Deprecated)
318 continue;
319
320 if (Message)
321 ResultMessage = Deprecated->getMessage();
322
323 Result = AR_Deprecated;
324 continue;
325 }
326
327 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
328 if (Message)
329 *Message = Unavailable->getMessage();
330 return AR_Unavailable;
331 }
332
333 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
334 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
335 Message);
336
337 if (AR == AR_Unavailable)
338 return AR_Unavailable;
339
340 if (AR > Result) {
341 Result = AR;
342 if (Message)
343 ResultMessage.swap(*Message);
344 }
345 continue;
346 }
347 }
348
349 if (Message)
350 Message->swap(ResultMessage);
351 return Result;
352}
353
354bool Decl::canBeWeakImported(bool &IsDefinition) const {
355 IsDefinition = false;
356 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
357 if (!Var->hasExternalStorage() || Var->getInit()) {
358 IsDefinition = true;
359 return false;
360 }
361 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
362 if (FD->hasBody()) {
363 IsDefinition = true;
364 return false;
365 }
366 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
367 return false;
368 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
369 isa<ObjCInterfaceDecl>(this)))
370 return false;
371
372 return true;
373}
374
375bool Decl::isWeakImported() const {
376 bool IsDefinition;
377 if (!canBeWeakImported(IsDefinition))
378 return false;
379
380 ASTContext &Context = getASTContext();
381 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
382 if (isa<WeakImportAttr>(*A))
383 return true;
384
385 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
386 if (CheckAvailability(getASTContext(), Availability, 0)
387 == AR_NotYetIntroduced)
388 return true;
389 }
390 }
391
392 return false;
393}
Tanya Lattner12ead492010-02-17 02:17:21 +0000394
Chris Lattner769dbdf2009-03-27 20:18:19 +0000395unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
396 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000397 case Function:
398 case CXXMethod:
399 case CXXConstructor:
400 case CXXDestructor:
401 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000402 case EnumConstant:
403 case Var:
404 case ImplicitParam:
405 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000406 case NonTypeTemplateParm:
407 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000408 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000409 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000410 case Label:
411 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000412 case IndirectField:
413 return IDNS_Ordinary | IDNS_Member;
414
John McCall0d6b1642010-04-23 18:46:30 +0000415 case ObjCCompatibleAlias:
416 case ObjCInterface:
417 return IDNS_Ordinary | IDNS_Type;
418
419 case Typedef:
420 case UnresolvedUsingTypename:
421 case TemplateTypeParm:
422 return IDNS_Ordinary | IDNS_Type;
423
John McCall9488ea12009-11-17 05:59:44 +0000424 case UsingShadow:
425 return 0; // we'll actually overwrite this later
426
John McCall7ba107a2009-11-18 02:36:19 +0000427 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000428 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000429
430 case Using:
431 return IDNS_Using;
432
Chris Lattner769dbdf2009-03-27 20:18:19 +0000433 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000434 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattner769dbdf2009-03-27 20:18:19 +0000436 case Field:
437 case ObjCAtDefsField:
438 case ObjCIvar:
439 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chris Lattner769dbdf2009-03-27 20:18:19 +0000441 case Record:
442 case CXXRecord:
443 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000444 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattner769dbdf2009-03-27 20:18:19 +0000446 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000447 case NamespaceAlias:
448 return IDNS_Namespace;
449
Chris Lattner769dbdf2009-03-27 20:18:19 +0000450 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000451 return IDNS_Ordinary;
452
Chris Lattner769dbdf2009-03-27 20:18:19 +0000453 case ClassTemplate:
454 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000455 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner769dbdf2009-03-27 20:18:19 +0000457 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000458 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000459 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000460 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000461 case LinkageSpec:
462 case FileScopeAsm:
463 case StaticAssert:
464 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000465 case ObjCPropertyImpl:
466 case ObjCForwardProtocol:
467 case Block:
468 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000469
Chris Lattner769dbdf2009-03-27 20:18:19 +0000470 case UsingDirective:
471 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000472 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000473 case ObjCImplementation:
474 case ObjCCategory:
475 case ObjCCategoryImpl:
476 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000477 return 0;
478 }
John McCall9488ea12009-11-17 05:59:44 +0000479
480 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000481}
482
Sean Huntcf807c42010-08-18 23:23:40 +0000483void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000484 assert(!HasAttrs && "Decl already contains attrs.");
485
Sean Huntcf807c42010-08-18 23:23:40 +0000486 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
487 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000488
489 AttrBlank = attrs;
490 HasAttrs = true;
491}
492
Sean Huntcf807c42010-08-18 23:23:40 +0000493void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000494 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Eli Friedman56d29372008-06-07 16:52:53 +0000496 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000497 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000498}
499
Sean Huntcf807c42010-08-18 23:23:40 +0000500const AttrVec &Decl::getAttrs() const {
501 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000502 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000503}
504
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000505void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000506 bool HasLHSAttr = this->HasAttrs;
507 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Eli Friedman56d29372008-06-07 16:52:53 +0000509 // Usually, neither decl has attrs, nothing to do.
510 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Eli Friedman56d29372008-06-07 16:52:53 +0000512 // If 'this' has no attrs, swap the other way.
513 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000514 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000516 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Eli Friedman56d29372008-06-07 16:52:53 +0000518 // Handle the case when both decls have attrs.
519 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000520 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000521 return;
522 }
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Eli Friedman56d29372008-06-07 16:52:53 +0000524 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000525 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
526 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000527 this->HasAttrs = false;
528 RHS->HasAttrs = true;
529}
530
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000531Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000532 Decl::Kind DK = D->getDeclKind();
533 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000534#define DECL(NAME, BASE)
535#define DECL_CONTEXT(NAME) \
536 case Decl::NAME: \
537 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
538#define DECL_CONTEXT_BASE(NAME)
539#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000540 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000541#define DECL(NAME, BASE)
542#define DECL_CONTEXT_BASE(NAME) \
543 if (DK >= first##NAME && DK <= last##NAME) \
544 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
545#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000546 assert(false && "a decl that inherits DeclContext isn't handled");
547 return 0;
548 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000549}
550
551DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000552 Decl::Kind DK = D->getKind();
553 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000554#define DECL(NAME, BASE)
555#define DECL_CONTEXT(NAME) \
556 case Decl::NAME: \
557 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
558#define DECL_CONTEXT_BASE(NAME)
559#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000560 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000561#define DECL(NAME, BASE)
562#define DECL_CONTEXT_BASE(NAME) \
563 if (DK >= first##NAME && DK <= last##NAME) \
564 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
565#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000566 assert(false && "a decl that inherits DeclContext isn't handled");
567 return 0;
568 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000569}
570
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000571SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000572 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
573 // FunctionDecl stores EndRangeLoc for this purpose.
574 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
575 const FunctionDecl *Definition;
576 if (FD->hasBody(Definition))
577 return Definition->getSourceRange().getEnd();
578 return SourceLocation();
579 }
580
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000581 if (Stmt *Body = getBody())
582 return Body->getSourceRange().getEnd();
583
584 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000585}
586
Anders Carlsson1329c272009-03-25 23:38:06 +0000587void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000588#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000589 // Suppress this check if any of the following hold:
590 // 1. this is the translation unit (and thus has no parent)
591 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000592 // 3. this is a non-type template parameter
593 // 4. the context is not a record
594 // 5. it's invalid
595 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000596 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000597 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000598 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000599 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000600 isInvalidDecl() ||
601 isa<StaticAssertDecl>(this) ||
602 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
603 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000604 isa<ParmVarDecl>(this) ||
605 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
606 // AS_none as access specifier.
607 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000608 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
610 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000611 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000612#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000613}
614
John McCallaab9e312011-02-22 22:25:23 +0000615DeclContext *Decl::getNonClosureContext() {
616 DeclContext *DC = getDeclContext();
617
618 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
619 // except that it's significantly more efficient to cast to a known
620 // decl type and call getDeclContext() than to call getParent().
621 do {
622 if (isa<BlockDecl>(DC)) {
623 DC = cast<BlockDecl>(DC)->getDeclContext();
624 continue;
625 }
626 } while (false);
627
628 assert(!DC->isClosure());
629 return DC;
630}
Anders Carlsson1329c272009-03-25 23:38:06 +0000631
Eli Friedman56d29372008-06-07 16:52:53 +0000632//===----------------------------------------------------------------------===//
633// DeclContext Implementation
634//===----------------------------------------------------------------------===//
635
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000636bool DeclContext::classof(const Decl *D) {
637 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000638#define DECL(NAME, BASE)
639#define DECL_CONTEXT(NAME) case Decl::NAME:
640#define DECL_CONTEXT_BASE(NAME)
641#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000642 return true;
643 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000644#define DECL(NAME, BASE)
645#define DECL_CONTEXT_BASE(NAME) \
646 if (D->getKind() >= Decl::first##NAME && \
647 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000648 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000649#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000650 return false;
651 }
652}
653
Douglas Gregora2da7802010-07-25 18:38:02 +0000654DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000655
Douglas Gregore942bbe2009-09-10 16:57:35 +0000656/// \brief Find the parent context of this context that will be
657/// used for unqualified name lookup.
658///
659/// Generally, the parent lookup context is the semantic context. However, for
660/// a friend function the parent lookup context is the lexical context, which
661/// is the class in which the friend is declared.
662DeclContext *DeclContext::getLookupParent() {
663 // FIXME: Find a better way to identify friends
664 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000665 if (getParent()->getRedeclContext()->isFileContext() &&
666 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000667 return getLexicalParent();
668
669 return getParent();
670}
671
Sebastian Redl410c4f22010-08-31 20:53:31 +0000672bool DeclContext::isInlineNamespace() const {
673 return isNamespace() &&
674 cast<NamespaceDecl>(this)->isInline();
675}
676
Douglas Gregorbc221632009-05-28 16:34:51 +0000677bool DeclContext::isDependentContext() const {
678 if (isFileContext())
679 return false;
680
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000681 if (isa<ClassTemplatePartialSpecializationDecl>(this))
682 return true;
683
Douglas Gregorbc221632009-05-28 16:34:51 +0000684 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
685 if (Record->getDescribedClassTemplate())
686 return true;
687
John McCall0c01d182010-03-24 05:22:00 +0000688 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000689 if (Function->getDescribedFunctionTemplate())
690 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
John McCall0c01d182010-03-24 05:22:00 +0000692 // Friend function declarations are dependent if their *lexical*
693 // context is dependent.
694 if (cast<Decl>(this)->getFriendObjectKind())
695 return getLexicalParent()->isDependentContext();
696 }
697
Douglas Gregorbc221632009-05-28 16:34:51 +0000698 return getParent() && getParent()->isDependentContext();
699}
700
Douglas Gregor074149e2009-01-05 19:45:36 +0000701bool DeclContext::isTransparentContext() const {
702 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000703 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000704 else if (DeclKind == Decl::LinkageSpec)
705 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000706
707 return false;
708}
709
John McCallac65c622010-10-26 04:59:26 +0000710bool DeclContext::isExternCContext() const {
711 const DeclContext *DC = this;
712 while (DC->DeclKind != Decl::TranslationUnit) {
713 if (DC->DeclKind == Decl::LinkageSpec)
714 return cast<LinkageSpecDecl>(DC)->getLanguage()
715 == LinkageSpecDecl::lang_c;
716 DC = DC->getParent();
717 }
718 return false;
719}
720
Sebastian Redl7a126a42010-08-31 00:36:30 +0000721bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000722 if (getPrimaryContext() != this)
723 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000725 for (; DC; DC = DC->getParent())
726 if (DC->getPrimaryContext() == this)
727 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000728 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000729}
730
Steve Naroff0701bbb2009-01-08 17:28:14 +0000731DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000732 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000733 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000734 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000735 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000736 // There is only one DeclContext for these entities.
737 return this;
738
739 case Decl::Namespace:
740 // The original namespace is our primary context.
741 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
742
Douglas Gregor44b43212008-12-11 16:49:14 +0000743 case Decl::ObjCMethod:
744 return this;
745
746 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000747 case Decl::ObjCProtocol:
748 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000749 // FIXME: Can Objective-C interfaces be forward-declared?
750 return this;
751
Steve Naroff0701bbb2009-01-08 17:28:14 +0000752 case Decl::ObjCImplementation:
753 case Decl::ObjCCategoryImpl:
754 return this;
755
Douglas Gregor44b43212008-12-11 16:49:14 +0000756 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000757 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000758 // If this is a tag type that has a definition or is currently
759 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000760 TagDecl *Tag = cast<TagDecl>(this);
761 assert(isa<TagType>(Tag->TypeForDecl) ||
762 isa<InjectedClassNameType>(Tag->TypeForDecl));
763
764 if (TagDecl *Def = Tag->getDefinition())
765 return Def;
766
767 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
768 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
769 if (TagTy->isBeingDefined())
770 // FIXME: is it necessarily being defined in the decl
771 // that owns the type?
772 return TagTy->getDecl();
773 }
774
775 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000776 }
777
Sean Hunt9a555912010-05-30 07:21:58 +0000778 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000779 "Unknown DeclContext kind");
780 return this;
781 }
782}
783
784DeclContext *DeclContext::getNextContext() {
785 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000786 case Decl::Namespace:
787 // Return the next namespace
788 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
789
790 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000791 return 0;
792 }
793}
794
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000795std::pair<Decl *, Decl *>
796DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
797 // Build up a chain of declarations via the Decl::NextDeclInContext field.
798 Decl *FirstNewDecl = 0;
799 Decl *PrevDecl = 0;
800 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
801 Decl *D = Decls[I];
802 if (PrevDecl)
803 PrevDecl->NextDeclInContext = D;
804 else
805 FirstNewDecl = D;
806
807 PrevDecl = D;
808 }
809
810 return std::make_pair(FirstNewDecl, PrevDecl);
811}
812
Douglas Gregor2cf26342009-04-09 22:27:44 +0000813/// \brief Load the declarations within this lexical storage from an
814/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000815void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816DeclContext::LoadLexicalDeclsFromExternalStorage() const {
817 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000818 assert(hasExternalLexicalStorage() && Source && "No external storage?");
819
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000820 // Notify that we have a DeclContext that is initializing.
821 ExternalASTSource::Deserializing ADeclContext(Source);
822
John McCall76bd1f32010-06-01 09:23:16 +0000823 llvm::SmallVector<Decl*, 64> Decls;
824 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000825 return;
826
827 // There is no longer any lexical storage in this context
828 ExternalLexicalStorage = false;
829
830 if (Decls.empty())
831 return;
832
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000833 // We may have already loaded just the fields of this record, in which case
834 // don't add the decls, just replace the FirstDecl/LastDecl chain.
835 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
836 if (RD->LoadedFieldsFromExternalStorage) {
837 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
838 return;
839 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000840
841 // Splice the newly-read declarations into the beginning of the list
842 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000843 Decl *ExternalFirst, *ExternalLast;
844 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
845 ExternalLast->NextDeclInContext = FirstDecl;
846 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000847 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000848 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000849}
850
John McCall76bd1f32010-06-01 09:23:16 +0000851DeclContext::lookup_result
852ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
853 DeclarationName Name) {
854 ASTContext &Context = DC->getParentASTContext();
855 StoredDeclsMap *Map;
856 if (!(Map = DC->LookupPtr))
857 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000858
John McCall76bd1f32010-06-01 09:23:16 +0000859 StoredDeclsList &List = (*Map)[Name];
860 assert(List.isNull());
861 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000862
John McCall76bd1f32010-06-01 09:23:16 +0000863 return DeclContext::lookup_result();
864}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000865
John McCall76bd1f32010-06-01 09:23:16 +0000866DeclContext::lookup_result
867ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000868 DeclarationName Name,
869 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
870 ASTContext &Context = DC->getParentASTContext();;
871
872 StoredDeclsMap *Map;
873 if (!(Map = DC->LookupPtr))
874 Map = DC->CreateStoredDeclsMap(Context);
875
876 StoredDeclsList &List = (*Map)[Name];
877 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
878 if (List.isNull())
879 List.setOnlyValue(Decls[I]);
880 else
881 List.AddSubsequentDecl(Decls[I]);
882 }
883
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000884 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000885}
886
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000887void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
888 DeclarationName Name,
889 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
890 assert(DC->LookupPtr);
891 StoredDeclsMap &Map = *DC->LookupPtr;
892
893 // If there's an entry in the table the visible decls for this name have
894 // already been deserialized.
895 if (Map.find(Name) == Map.end()) {
896 StoredDeclsList &List = Map[Name];
897 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
898 if (List.isNull())
899 List.setOnlyValue(Decls[I]);
900 else
901 List.AddSubsequentDecl(Decls[I]);
902 }
903 }
904}
905
Sebastian Redl681d7232010-07-27 00:17:23 +0000906DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
907 return decl_iterator(FirstDecl);
908}
909
910DeclContext::decl_iterator DeclContext::noload_decls_end() const {
911 return decl_iterator();
912}
913
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000914DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000915 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000916 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000917
918 // FIXME: Check whether we need to load some declarations from
919 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000920 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000921}
922
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000923DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000924 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000925 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000926
Mike Stump1eb44332009-09-09 15:08:12 +0000927 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000928}
929
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000930bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000931 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000932 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000933
934 return !FirstDecl;
935}
936
John McCall9f54ad42009-12-10 09:41:52 +0000937void DeclContext::removeDecl(Decl *D) {
938 assert(D->getLexicalDeclContext() == this &&
939 "decl being removed from non-lexical context");
940 assert((D->NextDeclInContext || D == LastDecl) &&
941 "decl is not in decls list");
942
943 // Remove D from the decl chain. This is O(n) but hopefully rare.
944 if (D == FirstDecl) {
945 if (D == LastDecl)
946 FirstDecl = LastDecl = 0;
947 else
948 FirstDecl = D->NextDeclInContext;
949 } else {
950 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
951 assert(I && "decl not found in linked list");
952 if (I->NextDeclInContext == D) {
953 I->NextDeclInContext = D->NextDeclInContext;
954 if (D == LastDecl) LastDecl = I;
955 break;
956 }
957 }
958 }
959
960 // Mark that D is no longer in the decl chain.
961 D->NextDeclInContext = 0;
962
963 // Remove D from the lookup table if necessary.
964 if (isa<NamedDecl>(D)) {
965 NamedDecl *ND = cast<NamedDecl>(D);
966
John McCall0c01d182010-03-24 05:22:00 +0000967 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
968 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000969
John McCall9f54ad42009-12-10 09:41:52 +0000970 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
971 assert(Pos != Map->end() && "no lookup entry for decl");
972 Pos->second.remove(ND);
973 }
974}
975
John McCall3f9a8a62009-08-11 06:59:38 +0000976void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000977 assert(D->getLexicalDeclContext() == this &&
978 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000979 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000980 "Decl already inserted into a DeclContext");
981
982 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000983 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000984 LastDecl = D;
985 } else {
986 FirstDecl = LastDecl = D;
987 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000988
989 // Notify a C++ record declaration that we've added a member, so it can
990 // update it's class-specific state.
991 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
992 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +0000993}
994
995void DeclContext::addDecl(Decl *D) {
996 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000997
998 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000999 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001000}
1001
Douglas Gregor074149e2009-01-05 19:45:36 +00001002/// buildLookup - Build the lookup data structure with all of the
1003/// declarations in DCtx (and any other contexts linked to it or
1004/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001005void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001006 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001007 for (decl_iterator D = DCtx->decls_begin(),
1008 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001009 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001010 // Insert this declaration into the lookup structure, but only
1011 // if it's semantically in its decl context. During non-lazy
1012 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001013 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +00001014 if (D->getDeclContext() == DCtx)
1015 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +00001016
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001017 // Insert any forward-declared Objective-C interfaces into the lookup
1018 // data structure.
1019 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
1020 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
1021 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +00001022 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001023
Sebastian Redl410c4f22010-08-31 20:53:31 +00001024 // If this declaration is itself a transparent declaration context or
1025 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001026 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001027 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001028 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001029 }
1030 }
1031}
1032
Mike Stump1eb44332009-09-09 15:08:12 +00001033DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001035 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001036 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001037 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001038
John McCall76bd1f32010-06-01 09:23:16 +00001039 if (hasExternalVisibleStorage()) {
1040 // Check to see if we've already cached the lookup results.
1041 if (LookupPtr) {
1042 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1043 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001044 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001045 }
1046
1047 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1048 return Source->FindExternalVisibleDeclsByName(this, Name);
1049 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001050
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001051 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001052 /// all of the linked DeclContexts (in declaration order!) and
1053 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001054 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001055 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001056
Douglas Gregorc36c5402009-04-09 17:29:08 +00001057 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001058 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001059 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001060
John McCall0c01d182010-03-24 05:22:00 +00001061 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1062 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001063 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001064 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001065}
1066
Mike Stump1eb44332009-09-09 15:08:12 +00001067DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001068DeclContext::lookup(DeclarationName Name) const {
1069 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001070}
1071
Sebastian Redl7a126a42010-08-31 00:36:30 +00001072DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001073 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001074 // Skip through transparent contexts.
1075 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001076 Ctx = Ctx->getParent();
1077 return Ctx;
1078}
1079
Douglas Gregor88b70942009-02-25 22:02:03 +00001080DeclContext *DeclContext::getEnclosingNamespaceContext() {
1081 DeclContext *Ctx = this;
1082 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001083 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001084 Ctx = Ctx->getParent();
1085 return Ctx->getPrimaryContext();
1086}
1087
Sebastian Redl7a126a42010-08-31 00:36:30 +00001088bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1089 // For non-file contexts, this is equivalent to Equals.
1090 if (!isFileContext())
1091 return O->Equals(this);
1092
1093 do {
1094 if (O->Equals(this))
1095 return true;
1096
1097 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1098 if (!NS || !NS->isInline())
1099 break;
1100 O = NS->getParent();
1101 } while (O);
1102
1103 return false;
1104}
1105
John McCallab88d972009-08-31 22:39:49 +00001106void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001107 // FIXME: This feels like a hack. Should DeclarationName support
1108 // template-ids, or is there a better way to keep specializations
1109 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001110 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001111 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001112 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1113 if (FD->isFunctionTemplateSpecialization())
1114 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001115
Steve Naroff0701bbb2009-01-08 17:28:14 +00001116 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001117 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +00001118 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001119 return;
1120 }
1121
1122 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001123 // into it. If we haven't deserialized externally stored decls, deserialize
1124 // them so we can add the decl. Otherwise, be lazy and don't build that
1125 // structure until someone asks for it.
1126 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001127 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +00001128
Sebastian Redl410c4f22010-08-31 20:53:31 +00001129 // If we are a transparent context or inline namespace, insert into our
1130 // parent context, too. This operation is recursive.
1131 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +00001132 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001133
1134 Decl *DCAsDecl = cast<Decl>(this);
1135 // Notify that a decl was made visible unless it's a Tag being defined.
1136 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1137 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1138 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001139}
1140
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001141void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001142 // Skip unnamed declarations.
1143 if (!D->getDeclName())
1144 return;
1145
Douglas Gregorcc636682009-02-17 23:15:12 +00001146 // FIXME: This feels like a hack. Should DeclarationName support
1147 // template-ids, or is there a better way to keep specializations
1148 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001149 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001150 return;
1151
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001152 ASTContext *C = 0;
1153 if (!LookupPtr) {
1154 C = &getParentASTContext();
1155 CreateStoredDeclsMap(*C);
1156 }
1157
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001158 // If there is an external AST source, load any declarations it knows about
1159 // with this declaration's name.
1160 // If the lookup table contains an entry about this name it means that we
1161 // have already checked the external source.
1162 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1163 if (hasExternalVisibleStorage() &&
1164 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1165 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1166
Douglas Gregor44b43212008-12-11 16:49:14 +00001167 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001168 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001169 if (DeclNameEntries.isNull()) {
1170 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001171 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001172 }
Chris Lattner91942502009-02-20 00:55:03 +00001173
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001174 // If it is possible that this is a redeclaration, check to see if there is
1175 // already a decl for which declarationReplaces returns true. If there is
1176 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001177 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001178 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001180 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001181 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001182}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001183
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001184void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1185 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1186 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1187
1188 if (!LookupPtr)
1189 CreateStoredDeclsMap(getParentASTContext());
1190 Source->MaterializeVisibleDecls(this);
1191}
1192
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001193/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1194/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001195DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001196DeclContext::getUsingDirectives() const {
1197 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001198 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1199 reinterpret_cast<udir_iterator>(Result.second));
1200}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001201
Ted Kremenek3478eb62010-02-11 07:12:28 +00001202//===----------------------------------------------------------------------===//
1203// Creation and Destruction of StoredDeclsMaps. //
1204//===----------------------------------------------------------------------===//
1205
John McCall0c01d182010-03-24 05:22:00 +00001206StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1207 assert(!LookupPtr && "context already has a decls map");
1208 assert(getPrimaryContext() == this &&
1209 "creating decls map on non-primary context");
1210
1211 StoredDeclsMap *M;
1212 bool Dependent = isDependentContext();
1213 if (Dependent)
1214 M = new DependentStoredDeclsMap();
1215 else
1216 M = new StoredDeclsMap();
1217 M->Previous = C.LastSDM;
1218 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1219 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001220 return M;
1221}
1222
1223void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001224 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1225 // pointer because the subclass doesn't add anything that needs to
1226 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001227 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1228}
1229
1230void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1231 while (Map) {
1232 // Advance the iteration before we invalidate memory.
1233 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1234
1235 if (Dependent)
1236 delete static_cast<DependentStoredDeclsMap*>(Map);
1237 else
1238 delete Map;
1239
1240 Map = Next.getPointer();
1241 Dependent = Next.getInt();
1242 }
1243}
1244
John McCall0c01d182010-03-24 05:22:00 +00001245DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1246 DeclContext *Parent,
1247 const PartialDiagnostic &PDiag) {
1248 assert(Parent->isDependentContext()
1249 && "cannot iterate dependent diagnostics of non-dependent context");
1250 Parent = Parent->getPrimaryContext();
1251 if (!Parent->LookupPtr)
1252 Parent->CreateStoredDeclsMap(C);
1253
1254 DependentStoredDeclsMap *Map
1255 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1256
Douglas Gregorb8365182010-03-29 23:56:53 +00001257 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001258 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001259 PartialDiagnostic::Storage *DiagStorage = 0;
1260 if (PDiag.hasStorage())
1261 DiagStorage = new (C) PartialDiagnostic::Storage;
1262
1263 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001264
1265 // TODO: Maybe we shouldn't reverse the order during insertion.
1266 DD->NextDiagnostic = Map->FirstDiagnostic;
1267 Map->FirstDiagnostic = DD;
1268
1269 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001270}