blob: 55484188c869f2a49bcbdeaafc8aec419898a8c4 [file] [log] [blame]
Eli Friedman7dbab8a2008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorb1fe2c92009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCallbbbbe4e2010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCallc62bb642010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor91f84212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redla7b98a72009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattnereae6cb62009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000031#include <algorithm>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Alexis Hunted053252010-05-30 07:21:58 +000038#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
39#define ABSTRACT_DECL(DECL)
40#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Douglas Gregor72172e92012-01-05 21:55:30 +000044void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
45 unsigned ID,
46 unsigned Size) {
47 return Context.Allocate(Size);
48}
49
Eli Friedman7dbab8a2008-06-07 16:52:53 +000050const char *Decl::getDeclKindName() const {
51 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000052 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000053#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
54#define ABSTRACT_DECL(DECL)
55#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000056 }
57}
58
Douglas Gregor90d47172010-03-05 00:26:45 +000059void Decl::setInvalidDecl(bool Invalid) {
60 InvalidDecl = Invalid;
61 if (Invalid) {
62 // Defensive maneuver for ill-formed code: we're likely not to make it to
63 // a point where we set the access specifier, so default it to "public"
64 // to avoid triggering asserts elsewhere in the front end.
65 setAccess(AS_public);
66 }
67}
68
Steve Naroff5faaef72009-01-20 19:53:53 +000069const char *DeclContext::getDeclKindName() const {
70 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000071 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000072#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
73#define ABSTRACT_DECL(DECL)
74#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +000075 }
76}
77
Eli Friedman7dbab8a2008-06-07 16:52:53 +000078bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000079 if (Enable) StatSwitch = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +000080 return StatSwitch;
81}
82
83void Decl::PrintStats() {
Chandler Carruthbfb154a2011-07-04 06:13:27 +000084 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump11289f42009-09-09 15:08:12 +000085
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000086 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +000087#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
88#define ABSTRACT_DECL(DECL)
89#include "clang/AST/DeclNodes.inc"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000090 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump11289f42009-09-09 15:08:12 +000091
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000092 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +000093#define DECL(DERIVED, BASE) \
94 if (n##DERIVED##s > 0) { \
95 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthbfb154a2011-07-04 06:13:27 +000096 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
97 << sizeof(DERIVED##Decl) << " each (" \
98 << n##DERIVED##s * sizeof(DERIVED##Decl) \
99 << " bytes)\n"; \
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000100 }
Alexis Hunted053252010-05-30 07:21:58 +0000101#define ABSTRACT_DECL(DECL)
102#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000104 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000105}
106
Alexis Hunted053252010-05-30 07:21:58 +0000107void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000108 switch (k) {
David Blaikie83d382b2011-09-23 05:06:16 +0000109 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +0000110#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
111#define ABSTRACT_DECL(DECL)
112#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000113 }
114}
115
Anders Carlssonaa73b912009-06-13 00:08:58 +0000116bool Decl::isTemplateParameterPack() const {
117 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
118 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000119 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000120 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000121 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000122 if (const TemplateTemplateParmDecl *TTP
123 = dyn_cast<TemplateTemplateParmDecl>(this))
124 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000125 return false;
126}
127
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000128bool Decl::isParameterPack() const {
129 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
130 return Parm->isParameterPack();
131
132 return isTemplateParameterPack();
133}
134
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000135bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000136 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000137 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000138
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000139 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
140}
141
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000142bool Decl::isTemplateDecl() const {
143 return isa<TemplateDecl>(this);
144}
145
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000146const DeclContext *Decl::getParentFunctionOrMethod() const {
147 for (const DeclContext *DC = getDeclContext();
148 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000149 DC = DC->getParent())
150 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000151 return DC;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000152
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000153 return 0;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000154}
155
Douglas Gregor133eddd2011-02-17 08:47:29 +0000156
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000157//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000158// PrettyStackTraceDecl Implementation
159//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000161void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000162 SourceLocation TheLoc = Loc;
163 if (TheLoc.isInvalid() && TheDecl)
164 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000165
Chris Lattnereae6cb62009-03-05 08:00:35 +0000166 if (TheLoc.isValid()) {
167 TheLoc.print(OS, SM);
168 OS << ": ";
169 }
170
171 OS << Message;
172
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000173 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000174 OS << " '" << DN->getQualifiedNameAsString() << '\'';
175 OS << '\n';
176}
Mike Stump11289f42009-09-09 15:08:12 +0000177
Chris Lattnereae6cb62009-03-05 08:00:35 +0000178//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000179// Decl Implementation
180//===----------------------------------------------------------------------===//
181
Douglas Gregorb11aad82011-02-19 18:51:44 +0000182// Out-of-line virtual method providing a home for Decl.
183Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000184
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000185void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000186 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000187}
188
189void Decl::setLexicalDeclContext(DeclContext *DC) {
190 if (DC == getLexicalDeclContext())
191 return;
192
193 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000194 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000195 MDC->SemanticDC = getDeclContext();
196 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000197 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000198 } else {
199 getMultipleDC()->LexicalDC = DC;
200 }
201}
202
John McCall4fa53422009-10-01 00:25:31 +0000203bool Decl::isInAnonymousNamespace() const {
204 const DeclContext *DC = getDeclContext();
205 do {
206 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
207 if (ND->isAnonymousNamespace())
208 return true;
209 } while ((DC = DC->getParent()));
210
211 return false;
212}
213
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000214TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000215 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
216 return TUD;
217
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000218 DeclContext *DC = getDeclContext();
219 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000220
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000221 while (!DC->isTranslationUnit()) {
222 DC = DC->getParent();
223 assert(DC && "This decl is not contained in a translation unit!");
224 }
Mike Stump11289f42009-09-09 15:08:12 +0000225
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000226 return cast<TranslationUnitDecl>(DC);
227}
228
229ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000230 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000231}
232
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000233ASTMutationListener *Decl::getASTMutationListener() const {
234 return getASTContext().getASTMutationListener();
235}
236
Douglas Gregorebada0772010-06-17 23:14:26 +0000237bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000238 if (Used)
239 return true;
240
241 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000242 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000243 return true;
244
245 // Check redeclarations for used attribute.
246 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorebada0772010-06-17 23:14:26 +0000247 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000248 return true;
249 }
250
251 return false;
252}
253
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000254bool Decl::isReferenced() const {
255 if (Referenced)
256 return true;
257
258 // Check redeclarations.
259 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
260 if (I->Referenced)
261 return true;
262
263 return false;
264}
265
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000266/// \brief Determine the availability of the given declaration based on
267/// the target platform.
268///
269/// When it returns an availability result other than \c AR_Available,
270/// if the \p Message parameter is non-NULL, it will be set to a
271/// string describing why the entity is unavailable.
272///
273/// FIXME: Make these strings localizable, since they end up in
274/// diagnostics.
275static AvailabilityResult CheckAvailability(ASTContext &Context,
276 const AvailabilityAttr *A,
277 std::string *Message) {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000278 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000279 StringRef PrettyPlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000280 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
281 if (PrettyPlatformName.empty())
282 PrettyPlatformName = TargetPlatform;
283
Douglas Gregore8bbc122011-09-02 00:18:52 +0000284 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000285 if (TargetMinVersion.empty())
286 return AR_Available;
287
288 // Match the platform name.
289 if (A->getPlatform()->getName() != TargetPlatform)
290 return AR_Available;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000291
292 std::string HintMessage;
293 if (!A->getMessage().empty()) {
294 HintMessage = " - ";
295 HintMessage += A->getMessage();
296 }
297
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000298 // Make sure that this declaration has not been marked 'unavailable'.
299 if (A->getUnavailable()) {
300 if (Message) {
301 Message->clear();
302 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000303 Out << "not available on " << PrettyPlatformName
304 << HintMessage;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000305 }
306
307 return AR_Unavailable;
308 }
309
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000310 // Make sure that this declaration has already been introduced.
311 if (!A->getIntroduced().empty() &&
312 TargetMinVersion < A->getIntroduced()) {
313 if (Message) {
314 Message->clear();
315 llvm::raw_string_ostream Out(*Message);
316 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000317 << A->getIntroduced() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000318 }
319
320 return AR_NotYetIntroduced;
321 }
322
323 // Make sure that this declaration hasn't been obsoleted.
324 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
325 if (Message) {
326 Message->clear();
327 llvm::raw_string_ostream Out(*Message);
328 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000329 << A->getObsoleted() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000330 }
331
332 return AR_Unavailable;
333 }
334
335 // Make sure that this declaration hasn't been deprecated.
336 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
337 if (Message) {
338 Message->clear();
339 llvm::raw_string_ostream Out(*Message);
340 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000341 << A->getDeprecated() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000342 }
343
344 return AR_Deprecated;
345 }
346
347 return AR_Available;
348}
349
350AvailabilityResult Decl::getAvailability(std::string *Message) const {
351 AvailabilityResult Result = AR_Available;
352 std::string ResultMessage;
353
354 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
355 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
356 if (Result >= AR_Deprecated)
357 continue;
358
359 if (Message)
360 ResultMessage = Deprecated->getMessage();
361
362 Result = AR_Deprecated;
363 continue;
364 }
365
366 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
367 if (Message)
368 *Message = Unavailable->getMessage();
369 return AR_Unavailable;
370 }
371
372 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
373 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
374 Message);
375
376 if (AR == AR_Unavailable)
377 return AR_Unavailable;
378
379 if (AR > Result) {
380 Result = AR;
381 if (Message)
382 ResultMessage.swap(*Message);
383 }
384 continue;
385 }
386 }
387
388 if (Message)
389 Message->swap(ResultMessage);
390 return Result;
391}
392
393bool Decl::canBeWeakImported(bool &IsDefinition) const {
394 IsDefinition = false;
395 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
396 if (!Var->hasExternalStorage() || Var->getInit()) {
397 IsDefinition = true;
398 return false;
399 }
400 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
401 if (FD->hasBody()) {
402 IsDefinition = true;
403 return false;
404 }
405 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
406 return false;
407 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
408 isa<ObjCInterfaceDecl>(this)))
409 return false;
410
411 return true;
412}
413
414bool Decl::isWeakImported() const {
415 bool IsDefinition;
416 if (!canBeWeakImported(IsDefinition))
417 return false;
418
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000419 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
420 if (isa<WeakImportAttr>(*A))
421 return true;
422
423 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
424 if (CheckAvailability(getASTContext(), Availability, 0)
425 == AR_NotYetIntroduced)
426 return true;
427 }
428 }
429
430 return false;
431}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000432
Chris Lattner8e097192009-03-27 20:18:19 +0000433unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
434 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000435 case Function:
436 case CXXMethod:
437 case CXXConstructor:
438 case CXXDestructor:
439 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000440 case EnumConstant:
441 case Var:
442 case ImplicitParam:
443 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000444 case NonTypeTemplateParm:
445 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000446 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000447 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000448 case Label:
449 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000450 case IndirectField:
451 return IDNS_Ordinary | IDNS_Member;
452
John McCalle87beb22010-04-23 18:46:30 +0000453 case ObjCCompatibleAlias:
454 case ObjCInterface:
455 return IDNS_Ordinary | IDNS_Type;
456
457 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000458 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000459 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000460 case UnresolvedUsingTypename:
461 case TemplateTypeParm:
462 return IDNS_Ordinary | IDNS_Type;
463
John McCall3f746822009-11-17 05:59:44 +0000464 case UsingShadow:
465 return 0; // we'll actually overwrite this later
466
John McCalle61f2ba2009-11-18 02:36:19 +0000467 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000468 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000469
470 case Using:
471 return IDNS_Using;
472
Chris Lattner8e097192009-03-27 20:18:19 +0000473 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000474 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000475
Chris Lattner8e097192009-03-27 20:18:19 +0000476 case Field:
477 case ObjCAtDefsField:
478 case ObjCIvar:
479 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Chris Lattner8e097192009-03-27 20:18:19 +0000481 case Record:
482 case CXXRecord:
483 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000484 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000485
Chris Lattner8e097192009-03-27 20:18:19 +0000486 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000487 case NamespaceAlias:
488 return IDNS_Namespace;
489
Chris Lattner8e097192009-03-27 20:18:19 +0000490 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000491 return IDNS_Ordinary;
492
Chris Lattner8e097192009-03-27 20:18:19 +0000493 case ClassTemplate:
494 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000495 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chris Lattner8e097192009-03-27 20:18:19 +0000497 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000498 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000499 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000500 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000501 case LinkageSpec:
502 case FileScopeAsm:
503 case StaticAssert:
Chris Lattner8e097192009-03-27 20:18:19 +0000504 case ObjCPropertyImpl:
Chris Lattner8e097192009-03-27 20:18:19 +0000505 case Block:
506 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000507
Chris Lattner8e097192009-03-27 20:18:19 +0000508 case UsingDirective:
509 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000510 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000511 case ClassScopeFunctionSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000512 case ObjCImplementation:
513 case ObjCCategory:
514 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000515 case Import:
Douglas Gregore93525e2010-04-22 23:19:50 +0000516 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000517 return 0;
518 }
John McCall3f746822009-11-17 05:59:44 +0000519
520 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000521}
522
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000523void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000524 assert(!HasAttrs && "Decl already contains attrs.");
525
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000526 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
527 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000528
529 AttrBlank = attrs;
530 HasAttrs = true;
531}
532
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000533void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000534 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000536 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000537 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000538}
539
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000540const AttrVec &Decl::getAttrs() const {
541 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000542 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000543}
544
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000545void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000546 bool HasLHSAttr = this->HasAttrs;
547 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000549 // Usually, neither decl has attrs, nothing to do.
550 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000551
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000552 // If 'this' has no attrs, swap the other way.
553 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000554 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000555
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000556 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000557
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000558 // Handle the case when both decls have attrs.
559 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000560 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000561 return;
562 }
Mike Stump11289f42009-09-09 15:08:12 +0000563
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000564 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000565 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
566 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000567 this->HasAttrs = false;
568 RHS->HasAttrs = true;
569}
570
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000571Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000572 Decl::Kind DK = D->getDeclKind();
573 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000574#define DECL(NAME, BASE)
575#define DECL_CONTEXT(NAME) \
576 case Decl::NAME: \
577 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
578#define DECL_CONTEXT_BASE(NAME)
579#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000580 default:
Alexis Hunted053252010-05-30 07:21:58 +0000581#define DECL(NAME, BASE)
582#define DECL_CONTEXT_BASE(NAME) \
583 if (DK >= first##NAME && DK <= last##NAME) \
584 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
585#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000586 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000587 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000588}
589
590DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000591 Decl::Kind DK = D->getKind();
592 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000593#define DECL(NAME, BASE)
594#define DECL_CONTEXT(NAME) \
595 case Decl::NAME: \
596 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
597#define DECL_CONTEXT_BASE(NAME)
598#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000599 default:
Alexis Hunted053252010-05-30 07:21:58 +0000600#define DECL(NAME, BASE)
601#define DECL_CONTEXT_BASE(NAME) \
602 if (DK >= first##NAME && DK <= last##NAME) \
603 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
604#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000605 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000606 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000607}
608
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000609SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000610 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
611 // FunctionDecl stores EndRangeLoc for this purpose.
612 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
613 const FunctionDecl *Definition;
614 if (FD->hasBody(Definition))
615 return Definition->getSourceRange().getEnd();
616 return SourceLocation();
617 }
618
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000619 if (Stmt *Body = getBody())
620 return Body->getSourceRange().getEnd();
621
622 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000623}
624
Anders Carlssona28908d2009-03-25 23:38:06 +0000625void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000626#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000627 // Suppress this check if any of the following hold:
628 // 1. this is the translation unit (and thus has no parent)
629 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000630 // 3. this is a non-type template parameter
631 // 4. the context is not a record
632 // 5. it's invalid
633 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000634 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000635 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000636 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000637 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000638 isInvalidDecl() ||
639 isa<StaticAssertDecl>(this) ||
640 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
641 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000642 isa<ParmVarDecl>(this) ||
643 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
644 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000645 isa<CXXRecordDecl>(this) ||
646 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000647 return;
Mike Stump11289f42009-09-09 15:08:12 +0000648
649 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000650 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000651#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000652}
653
John McCallb67608f2011-02-22 22:25:23 +0000654DeclContext *Decl::getNonClosureContext() {
John McCallfe96e0b2011-11-06 09:01:30 +0000655 return getDeclContext()->getNonClosureAncestor();
656}
657
658DeclContext *DeclContext::getNonClosureAncestor() {
659 DeclContext *DC = this;
John McCallb67608f2011-02-22 22:25:23 +0000660
661 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
662 // except that it's significantly more efficient to cast to a known
663 // decl type and call getDeclContext() than to call getParent().
John McCall63b45fe2011-06-23 21:18:31 +0000664 while (isa<BlockDecl>(DC))
665 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallb67608f2011-02-22 22:25:23 +0000666
667 assert(!DC->isClosure());
668 return DC;
669}
Anders Carlssona28908d2009-03-25 23:38:06 +0000670
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000671//===----------------------------------------------------------------------===//
672// DeclContext Implementation
673//===----------------------------------------------------------------------===//
674
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000675bool DeclContext::classof(const Decl *D) {
676 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000677#define DECL(NAME, BASE)
678#define DECL_CONTEXT(NAME) case Decl::NAME:
679#define DECL_CONTEXT_BASE(NAME)
680#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000681 return true;
682 default:
Alexis Hunted053252010-05-30 07:21:58 +0000683#define DECL(NAME, BASE)
684#define DECL_CONTEXT_BASE(NAME) \
685 if (D->getKind() >= Decl::first##NAME && \
686 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000687 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000688#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000689 return false;
690 }
691}
692
Douglas Gregor9c832f72010-07-25 18:38:02 +0000693DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000694
Douglas Gregor7f737c02009-09-10 16:57:35 +0000695/// \brief Find the parent context of this context that will be
696/// used for unqualified name lookup.
697///
698/// Generally, the parent lookup context is the semantic context. However, for
699/// a friend function the parent lookup context is the lexical context, which
700/// is the class in which the friend is declared.
701DeclContext *DeclContext::getLookupParent() {
702 // FIXME: Find a better way to identify friends
703 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000704 if (getParent()->getRedeclContext()->isFileContext() &&
705 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000706 return getLexicalParent();
707
708 return getParent();
709}
710
Sebastian Redlbd595762010-08-31 20:53:31 +0000711bool DeclContext::isInlineNamespace() const {
712 return isNamespace() &&
713 cast<NamespaceDecl>(this)->isInline();
714}
715
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000716bool DeclContext::isDependentContext() const {
717 if (isFileContext())
718 return false;
719
Douglas Gregor2373c592009-05-31 09:31:02 +0000720 if (isa<ClassTemplatePartialSpecializationDecl>(this))
721 return true;
722
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000723 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
724 if (Record->getDescribedClassTemplate())
725 return true;
726
John McCallc62bb642010-03-24 05:22:00 +0000727 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000728 if (Function->getDescribedFunctionTemplate())
729 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000730
John McCallc62bb642010-03-24 05:22:00 +0000731 // Friend function declarations are dependent if their *lexical*
732 // context is dependent.
733 if (cast<Decl>(this)->getFriendObjectKind())
734 return getLexicalParent()->isDependentContext();
735 }
736
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000737 return getParent() && getParent()->isDependentContext();
738}
739
Douglas Gregor07665a62009-01-05 19:45:36 +0000740bool DeclContext::isTransparentContext() const {
741 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000742 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000743 else if (DeclKind == Decl::LinkageSpec)
744 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000745
746 return false;
747}
748
John McCall5fe84122010-10-26 04:59:26 +0000749bool DeclContext::isExternCContext() const {
750 const DeclContext *DC = this;
751 while (DC->DeclKind != Decl::TranslationUnit) {
752 if (DC->DeclKind == Decl::LinkageSpec)
753 return cast<LinkageSpecDecl>(DC)->getLanguage()
754 == LinkageSpecDecl::lang_c;
755 DC = DC->getParent();
756 }
757 return false;
758}
759
Sebastian Redl50c68252010-08-31 00:36:30 +0000760bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000761 if (getPrimaryContext() != this)
762 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Douglas Gregore985a3b2009-08-27 06:03:53 +0000764 for (; DC; DC = DC->getParent())
765 if (DC->getPrimaryContext() == this)
766 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000767 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000768}
769
Steve Naroff35c62ae2009-01-08 17:28:14 +0000770DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000771 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000772 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000773 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000774 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000775 // There is only one DeclContext for these entities.
776 return this;
777
778 case Decl::Namespace:
779 // The original namespace is our primary context.
780 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
781
Douglas Gregor91f84212008-12-11 16:49:14 +0000782 case Decl::ObjCMethod:
783 return this;
784
785 case Decl::ObjCInterface:
Douglas Gregor66b310c2011-12-15 18:03:09 +0000786 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
787 return Def;
788
789 return this;
790
Steve Naroff35c62ae2009-01-08 17:28:14 +0000791 case Decl::ObjCProtocol:
Douglas Gregora715bff2012-01-01 19:51:50 +0000792 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
793 return Def;
794
795 return this;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000796
Steve Naroff35c62ae2009-01-08 17:28:14 +0000797 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000798 return this;
799
Steve Naroff35c62ae2009-01-08 17:28:14 +0000800 case Decl::ObjCImplementation:
801 case Decl::ObjCCategoryImpl:
802 return this;
803
Douglas Gregor91f84212008-12-11 16:49:14 +0000804 default:
Alexis Hunted053252010-05-30 07:21:58 +0000805 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000806 // If this is a tag type that has a definition or is currently
807 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000808 TagDecl *Tag = cast<TagDecl>(this);
809 assert(isa<TagType>(Tag->TypeForDecl) ||
810 isa<InjectedClassNameType>(Tag->TypeForDecl));
811
812 if (TagDecl *Def = Tag->getDefinition())
813 return Def;
814
815 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
816 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
817 if (TagTy->isBeingDefined())
818 // FIXME: is it necessarily being defined in the decl
819 // that owns the type?
820 return TagTy->getDecl();
821 }
822
823 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000824 }
825
Alexis Hunted053252010-05-30 07:21:58 +0000826 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000827 "Unknown DeclContext kind");
828 return this;
829 }
830}
831
832DeclContext *DeclContext::getNextContext() {
833 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000834 case Decl::Namespace:
835 // Return the next namespace
836 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
837
838 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000839 return 0;
840 }
841}
842
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000843std::pair<Decl *, Decl *>
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000844DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
845 bool FieldsAlreadyLoaded) {
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000846 // Build up a chain of declarations via the Decl::NextDeclInContext field.
847 Decl *FirstNewDecl = 0;
848 Decl *PrevDecl = 0;
849 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000850 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
851 continue;
852
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000853 Decl *D = Decls[I];
854 if (PrevDecl)
855 PrevDecl->NextDeclInContext = D;
856 else
857 FirstNewDecl = D;
858
859 PrevDecl = D;
860 }
861
862 return std::make_pair(FirstNewDecl, PrevDecl);
863}
864
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000865/// \brief Load the declarations within this lexical storage from an
866/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000867void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000868DeclContext::LoadLexicalDeclsFromExternalStorage() const {
869 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000870 assert(hasExternalLexicalStorage() && Source && "No external storage?");
871
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000872 // Notify that we have a DeclContext that is initializing.
873 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregorf337ae92011-08-26 21:23:06 +0000874
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000875 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000876 SmallVector<Decl*, 64> Decls;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000877 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000878 switch (Source->FindExternalLexicalDecls(this, Decls)) {
879 case ELR_Success:
880 break;
881
882 case ELR_Failure:
883 case ELR_AlreadyLoaded:
884 return;
885 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000886
887 if (Decls.empty())
888 return;
889
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000890 // We may have already loaded just the fields of this record, in which case
891 // we need to ignore them.
892 bool FieldsAlreadyLoaded = false;
893 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
894 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
895
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000896 // Splice the newly-read declarations into the beginning of the list
897 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000898 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000899 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
900 FieldsAlreadyLoaded);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000901 ExternalLast->NextDeclInContext = FirstDecl;
902 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000903 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000904 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000905}
906
John McCall75b960e2010-06-01 09:23:16 +0000907DeclContext::lookup_result
908ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
909 DeclarationName Name) {
910 ASTContext &Context = DC->getParentASTContext();
911 StoredDeclsMap *Map;
912 if (!(Map = DC->LookupPtr))
913 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000914
John McCall75b960e2010-06-01 09:23:16 +0000915 StoredDeclsList &List = (*Map)[Name];
916 assert(List.isNull());
917 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000918
John McCall75b960e2010-06-01 09:23:16 +0000919 return DeclContext::lookup_result();
920}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000921
John McCall75b960e2010-06-01 09:23:16 +0000922DeclContext::lookup_result
923ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000924 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000925 ArrayRef<NamedDecl*> Decls) {
John McCall75b960e2010-06-01 09:23:16 +0000926 ASTContext &Context = DC->getParentASTContext();;
927
928 StoredDeclsMap *Map;
929 if (!(Map = DC->LookupPtr))
930 Map = DC->CreateStoredDeclsMap(Context);
931
932 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000933 for (ArrayRef<NamedDecl*>::iterator
934 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall75b960e2010-06-01 09:23:16 +0000935 if (List.isNull())
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000936 List.setOnlyValue(*I);
John McCall75b960e2010-06-01 09:23:16 +0000937 else
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000938 List.AddSubsequentDecl(*I);
John McCall75b960e2010-06-01 09:23:16 +0000939 }
940
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000941 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000942}
943
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000944DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
945 return decl_iterator(FirstDecl);
946}
947
948DeclContext::decl_iterator DeclContext::noload_decls_end() const {
949 return decl_iterator();
950}
951
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000952DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000953 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000954 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000955
Mike Stump11289f42009-09-09 15:08:12 +0000956 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000957}
958
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000959DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000960 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000961 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000962
Mike Stump11289f42009-09-09 15:08:12 +0000963 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000964}
965
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000966bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000967 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000968 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000969
970 return !FirstDecl;
971}
972
John McCall84d87672009-12-10 09:41:52 +0000973void DeclContext::removeDecl(Decl *D) {
974 assert(D->getLexicalDeclContext() == this &&
975 "decl being removed from non-lexical context");
976 assert((D->NextDeclInContext || D == LastDecl) &&
977 "decl is not in decls list");
978
979 // Remove D from the decl chain. This is O(n) but hopefully rare.
980 if (D == FirstDecl) {
981 if (D == LastDecl)
982 FirstDecl = LastDecl = 0;
983 else
984 FirstDecl = D->NextDeclInContext;
985 } else {
986 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
987 assert(I && "decl not found in linked list");
988 if (I->NextDeclInContext == D) {
989 I->NextDeclInContext = D->NextDeclInContext;
990 if (D == LastDecl) LastDecl = I;
991 break;
992 }
993 }
994 }
995
996 // Mark that D is no longer in the decl chain.
997 D->NextDeclInContext = 0;
998
999 // Remove D from the lookup table if necessary.
1000 if (isa<NamedDecl>(D)) {
1001 NamedDecl *ND = cast<NamedDecl>(D);
1002
Axel Naumanncb2c52f2011-08-26 14:06:12 +00001003 // Remove only decls that have a name
1004 if (!ND->getDeclName()) return;
1005
John McCallc62bb642010-03-24 05:22:00 +00001006 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
1007 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +00001008
John McCall84d87672009-12-10 09:41:52 +00001009 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1010 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannfbc7b982011-11-08 18:21:06 +00001011 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1012 Pos->second.remove(ND);
John McCall84d87672009-12-10 09:41:52 +00001013 }
1014}
1015
John McCalld1e9d832009-08-11 06:59:38 +00001016void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001017 assert(D->getLexicalDeclContext() == this &&
1018 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001019 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001020 "Decl already inserted into a DeclContext");
1021
1022 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +00001023 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +00001024 LastDecl = D;
1025 } else {
1026 FirstDecl = LastDecl = D;
1027 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001028
1029 // Notify a C++ record declaration that we've added a member, so it can
1030 // update it's class-specific state.
1031 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1032 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001033
1034 // If this is a newly-created (not de-serialized) import declaration, wire
1035 // it in to the list of local import declarations.
1036 if (!D->isFromASTFile()) {
1037 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1038 D->getASTContext().addedLocalImportDecl(Import);
1039 }
John McCalld1e9d832009-08-11 06:59:38 +00001040}
1041
1042void DeclContext::addDecl(Decl *D) {
1043 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001044
1045 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001046 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +00001047}
1048
Sean Callanan95e74be2011-10-21 02:57:43 +00001049void DeclContext::addDeclInternal(Decl *D) {
1050 addHiddenDecl(D);
1051
1052 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1053 ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
1054}
1055
Douglas Gregor07665a62009-01-05 19:45:36 +00001056/// buildLookup - Build the lookup data structure with all of the
1057/// declarations in DCtx (and any other contexts linked to it or
1058/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001059void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001060 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +00001061 for (decl_iterator D = DCtx->decls_begin(),
1062 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +00001063 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +00001064 // Insert this declaration into the lookup structure, but only
1065 // if it's semantically in its decl context. During non-lazy
1066 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001067 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +00001068 if (D->getDeclContext() == DCtx)
Sean Callanan95e74be2011-10-21 02:57:43 +00001069 makeDeclVisibleInContextImpl(ND, false);
Douglas Gregor07665a62009-01-05 19:45:36 +00001070
Sebastian Redlbd595762010-08-31 20:53:31 +00001071 // If this declaration is itself a transparent declaration context or
1072 // inline namespace, add its members (recursively).
Douglas Gregor07665a62009-01-05 19:45:36 +00001073 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redlbd595762010-08-31 20:53:31 +00001074 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001075 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +00001076 }
1077 }
1078}
1079
Mike Stump11289f42009-09-09 15:08:12 +00001080DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001081DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +00001082 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001083 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001084 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001085
John McCall75b960e2010-06-01 09:23:16 +00001086 if (hasExternalVisibleStorage()) {
1087 // Check to see if we've already cached the lookup results.
1088 if (LookupPtr) {
1089 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1090 if (I != LookupPtr->end())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001091 return I->second.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001092 }
1093
1094 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1095 return Source->FindExternalVisibleDeclsByName(this, Name);
1096 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001097
Douglas Gregor55297ac2008-12-23 00:26:44 +00001098 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +00001099 /// all of the linked DeclContexts (in declaration order!) and
1100 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +00001101 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001102 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +00001103
Douglas Gregor9615ec22009-04-09 17:29:08 +00001104 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001105 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +00001106 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001107
John McCallc62bb642010-03-24 05:22:00 +00001108 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1109 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001110 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001111 return Pos->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001112}
1113
Mike Stump11289f42009-09-09 15:08:12 +00001114DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001115DeclContext::lookup(DeclarationName Name) const {
1116 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001117}
1118
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001119void DeclContext::localUncachedLookup(DeclarationName Name,
1120 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1121 Results.clear();
1122
1123 // If there's no external storage, just perform a normal lookup and copy
1124 // the results.
1125 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1126 lookup_result LookupResults = lookup(Name);
1127 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1128 return;
1129 }
1130
1131 // If we have a lookup table, check there first. Maybe we'll get lucky.
1132 if (LookupPtr) {
1133 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1134 if (Pos != LookupPtr->end()) {
1135 Results.insert(Results.end(),
1136 Pos->second.getLookupResult().first,
1137 Pos->second.getLookupResult().second);
1138 return;
1139 }
1140 }
1141
1142 // Slow case: grovel through the declarations in our chain looking for
1143 // matches.
1144 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1145 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1146 if (ND->getDeclName() == Name)
1147 Results.push_back(ND);
1148 }
1149}
1150
Sebastian Redl50c68252010-08-31 00:36:30 +00001151DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001152 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001153 // Skip through transparent contexts.
1154 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001155 Ctx = Ctx->getParent();
1156 return Ctx;
1157}
1158
Douglas Gregorf47b9112009-02-25 22:02:03 +00001159DeclContext *DeclContext::getEnclosingNamespaceContext() {
1160 DeclContext *Ctx = this;
1161 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001162 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001163 Ctx = Ctx->getParent();
1164 return Ctx->getPrimaryContext();
1165}
1166
Sebastian Redl50c68252010-08-31 00:36:30 +00001167bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1168 // For non-file contexts, this is equivalent to Equals.
1169 if (!isFileContext())
1170 return O->Equals(this);
1171
1172 do {
1173 if (O->Equals(this))
1174 return true;
1175
1176 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1177 if (!NS || !NS->isInline())
1178 break;
1179 O = NS->getParent();
1180 } while (O);
1181
1182 return false;
1183}
1184
Sean Callanan95e74be2011-10-21 02:57:43 +00001185void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
1186{
1187 makeDeclVisibleInContextWithFlags(D, false, Recoverable);
1188}
1189
1190void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
1191{
1192 makeDeclVisibleInContextWithFlags(D, true, Recoverable);
1193}
1194
1195void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +00001196 // FIXME: This feels like a hack. Should DeclarationName support
1197 // template-ids, or is there a better way to keep specializations
1198 // from being visible?
Douglas Gregorfd7c2252011-03-04 17:52:15 +00001199 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001200 return;
Eli Friedman73168192009-12-08 05:40:03 +00001201 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1202 if (FD->isFunctionTemplateSpecialization())
1203 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001204
Steve Naroff35c62ae2009-01-08 17:28:14 +00001205 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001206 if (PrimaryContext != this) {
Sean Callanan95e74be2011-10-21 02:57:43 +00001207 PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +00001208 return;
1209 }
1210
1211 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001212 // into it. If we haven't deserialized externally stored decls, deserialize
1213 // them so we can add the decl. Otherwise, be lazy and don't build that
1214 // structure until someone asks for it.
1215 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Sean Callanan95e74be2011-10-21 02:57:43 +00001216 makeDeclVisibleInContextImpl(D, Internal);
Douglas Gregor07665a62009-01-05 19:45:36 +00001217
Sebastian Redlbd595762010-08-31 20:53:31 +00001218 // If we are a transparent context or inline namespace, insert into our
1219 // parent context, too. This operation is recursive.
1220 if (isTransparentContext() || isInlineNamespace())
Sean Callanan95e74be2011-10-21 02:57:43 +00001221 getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00001222
1223 Decl *DCAsDecl = cast<Decl>(this);
1224 // Notify that a decl was made visible unless it's a Tag being defined.
1225 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1226 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1227 L->AddedVisibleDecl(this, D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001228}
1229
Sean Callanan95e74be2011-10-21 02:57:43 +00001230void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001231 // Skip unnamed declarations.
1232 if (!D->getDeclName())
1233 return;
1234
Douglas Gregor0de016d2011-05-06 23:32:38 +00001235 // Skip entities that can't be found by name lookup into a particular
1236 // context.
1237 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1238 D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001239 return;
1240
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001241 ASTContext *C = 0;
1242 if (!LookupPtr) {
1243 C = &getParentASTContext();
1244 CreateStoredDeclsMap(*C);
1245 }
1246
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001247 // If there is an external AST source, load any declarations it knows about
1248 // with this declaration's name.
1249 // If the lookup table contains an entry about this name it means that we
1250 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001251 if (!Internal)
1252 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1253 if (hasExternalVisibleStorage() &&
1254 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1255 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001256
Douglas Gregor91f84212008-12-11 16:49:14 +00001257 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +00001258 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001259 if (DeclNameEntries.isNull()) {
1260 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001261 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001262 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001263
Chris Lattner29578f32009-02-20 01:10:07 +00001264 // If it is possible that this is a redeclaration, check to see if there is
1265 // already a decl for which declarationReplaces returns true. If there is
1266 // one, just replace it and return.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001267 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattnercaae7162009-02-20 01:44:05 +00001268 return;
Mike Stump11289f42009-09-09 15:08:12 +00001269
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001270 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +00001271 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001272}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001273
1274/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1275/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001276DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001277DeclContext::getUsingDirectives() const {
1278 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001279 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1280 reinterpret_cast<udir_iterator>(Result.second));
1281}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001282
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001283//===----------------------------------------------------------------------===//
1284// Creation and Destruction of StoredDeclsMaps. //
1285//===----------------------------------------------------------------------===//
1286
John McCallc62bb642010-03-24 05:22:00 +00001287StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1288 assert(!LookupPtr && "context already has a decls map");
1289 assert(getPrimaryContext() == this &&
1290 "creating decls map on non-primary context");
1291
1292 StoredDeclsMap *M;
1293 bool Dependent = isDependentContext();
1294 if (Dependent)
1295 M = new DependentStoredDeclsMap();
1296 else
1297 M = new StoredDeclsMap();
1298 M->Previous = C.LastSDM;
1299 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1300 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001301 return M;
1302}
1303
1304void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001305 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1306 // pointer because the subclass doesn't add anything that needs to
1307 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001308 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1309}
1310
1311void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1312 while (Map) {
1313 // Advance the iteration before we invalidate memory.
1314 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1315
1316 if (Dependent)
1317 delete static_cast<DependentStoredDeclsMap*>(Map);
1318 else
1319 delete Map;
1320
1321 Map = Next.getPointer();
1322 Dependent = Next.getInt();
1323 }
1324}
1325
John McCallc62bb642010-03-24 05:22:00 +00001326DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1327 DeclContext *Parent,
1328 const PartialDiagnostic &PDiag) {
1329 assert(Parent->isDependentContext()
1330 && "cannot iterate dependent diagnostics of non-dependent context");
1331 Parent = Parent->getPrimaryContext();
1332 if (!Parent->LookupPtr)
1333 Parent->CreateStoredDeclsMap(C);
1334
1335 DependentStoredDeclsMap *Map
1336 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1337
Douglas Gregora55530e2010-03-29 23:56:53 +00001338 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001339 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001340 PartialDiagnostic::Storage *DiagStorage = 0;
1341 if (PDiag.hasStorage())
1342 DiagStorage = new (C) PartialDiagnostic::Storage;
1343
1344 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001345
1346 // TODO: Maybe we shouldn't reverse the order during insertion.
1347 DD->NextDiagnostic = Map->FirstDiagnostic;
1348 Map->FirstDiagnostic = DD;
1349
1350 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001351}