DeclBase.cpp revision f5c9f9fd6f5e2850b9b0f19283430245b696c6e5
156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//                     The LLVM Compiler Infrastructure
456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// This file is distributed under the University of Illinois Open Source
656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// License. See LICENSE.TXT for details.
756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
1056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// This file implements the Decl and DeclContext classes.
1156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
1256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
1356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
1456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "clang/AST/DeclBase.h"
1564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/Decl.h"
16c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor#include "clang/AST/DeclContextInternals.h"
17d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
1892b7f70c924cbf4514e9e434cea7def51ab49860John McCall#include "clang/AST/DeclFriend.h"
19aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclObjC.h"
20aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
210c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "clang/AST/ASTContext.h"
2444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor#include "clang/AST/Type.h"
25d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl#include "clang/AST/Stmt.h"
26d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl#include "clang/AST/StmtCXX.h"
27100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h"
280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor#include "clang/Basic/TargetInfo.h"
2956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "llvm/ADT/DenseMap.h"
3049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner#include "llvm/Support/raw_ostream.h"
316ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor#include <algorithm>
3256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanusing namespace clang;
3356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//  Statistics
3656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
389a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
399a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
409a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
4156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
4256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanstatic bool StatSwitch = false;
4356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
441e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregorvoid *Decl::AllocateDeserializedDecl(const ASTContext &Context,
451e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                     unsigned ID,
461e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                     unsigned Size) {
475d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  // Allocate an extra 8 bytes worth of storage, which ensures that the
485d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  // resulting pointer will still be 8-byte aligned. At present, we're only
495d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  // using the latter 4 bytes of this storage.
505d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  void *Start = Context.Allocate(Size + 8);
515d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  void *Result = (char*)Start + 8;
52b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor
53b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  // Store the global declaration ID
54b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  unsigned *IDPtr = (unsigned*)Result - 1;
55b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  *IDPtr = ID;
56b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor
57b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  return Result;
581e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
591e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
6056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanconst char *Decl::getDeclKindName() const {
6156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (DeclKind) {
62b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration not in DeclNodes.inc!");
639a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
649a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
659a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
6756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
6856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
6942738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregorvoid Decl::setInvalidDecl(bool Invalid) {
7042738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  InvalidDecl = Invalid;
7142738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  if (Invalid) {
7242738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // Defensive maneuver for ill-formed code: we're likely not to make it to
7342738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // a point where we set the access specifier, so default it to "public"
7442738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // to avoid triggering asserts elsewhere in the front end.
7542738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    setAccess(AS_public);
7642738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  }
7742738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor}
7842738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor
790a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroffconst char *DeclContext::getDeclKindName() const {
800a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  switch (DeclKind) {
81b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
829a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
839a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
849a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
850a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  }
860a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff}
870a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff
8856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanbool Decl::CollectingStats(bool Enable) {
892024f4d4b0d57616f79ea742fa782d633d414462Kovarththanan Rajaratnam  if (Enable) StatSwitch = true;
9056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  return StatSwitch;
9156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
9256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
9356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::PrintStats() {
94b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "\n*** Decl Stats:\n";
951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalDecls = 0;
979a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
989a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
999a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
100b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "  " << totalDecls << " decls total.\n";
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalBytes = 0;
1039a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE)                                             \
1049a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt  if (n##DERIVED##s > 0) {                                              \
1059a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl));         \
106b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth    llvm::errs() << "    " << n##DERIVED##s << " " #DERIVED " decls, "  \
107b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << sizeof(DERIVED##Decl) << " each ("                  \
108b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << n##DERIVED##s * sizeof(DERIVED##Decl)               \
109b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << " bytes)\n";                                        \
11064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  }
1119a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1129a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "Total bytes = " << totalBytes << "\n";
11556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
11656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
1179a55591af3e5506b95a9718e15380129fbfc5ebcSean Huntvoid Decl::add(Kind k) {
11856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (k) {
119b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration not in DeclNodes.inc!");
1209a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
1219a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1229a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
12356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
12456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
12556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
12667e332009c6e349dc34700f539747afcff990336Anders Carlssonbool Decl::isTemplateParameterPack() const {
12767e332009c6e349dc34700f539747afcff990336Anders Carlsson  if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
12867e332009c6e349dc34700f539747afcff990336Anders Carlsson    return TTP->isParameterPack();
12910738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  if (const NonTypeTemplateParmDecl *NTTP
13061c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                = dyn_cast<NonTypeTemplateParmDecl>(this))
13110738d36b150aa65206890c1c845cdba076e4200Douglas Gregor    return NTTP->isParameterPack();
13261c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  if (const TemplateTemplateParmDecl *TTP
13361c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(this))
13461c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor    return TTP->isParameterPack();
13567e332009c6e349dc34700f539747afcff990336Anders Carlsson  return false;
13667e332009c6e349dc34700f539747afcff990336Anders Carlsson}
13767e332009c6e349dc34700f539747afcff990336Anders Carlsson
1381fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool Decl::isParameterPack() const {
1391fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
1401fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor    return Parm->isParameterPack();
1411fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
1421fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isTemplateParameterPack();
1431fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
1441fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
145e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool Decl::isFunctionOrFunctionTemplate() const {
1469488ea120e093068021f944176c3d610dd540914John McCall  if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
14758badb7a655d021fc67bb7ed0af95d6ea0c63eb1Anders Carlsson    return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
150e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
151e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
152ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowskibool Decl::isTemplateDecl() const {
153ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski  return isa<TemplateDecl>(this);
154ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski}
155ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski
156c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidisconst DeclContext *Decl::getParentFunctionOrMethod() const {
157c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  for (const DeclContext *DC = getDeclContext();
158c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis       DC && !DC->isTranslationUnit() && !DC->isNamespace();
15979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor       DC = DC->getParent())
16079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    if (DC->isFunctionOrMethod())
161c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis      return DC;
16279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
163c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  return 0;
16479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor}
16579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
1664c3e0ee8e6208eb42c4adb78a7d35b641fd85ae9Douglas Gregor
16756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
16849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner// PrettyStackTraceDecl Implementation
16949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyStackTraceDecl::print(raw_ostream &OS) const {
17249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation TheLoc = Loc;
17349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isInvalid() && TheDecl)
17449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc = TheDecl->getLocation();
1751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isValid()) {
17749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc.print(OS, SM);
17849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << ": ";
17949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  }
18049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
18149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << Message;
18249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
183c52365674b5b157a0486f75c12dd9f4cc41d8089Daniel Dunbar  if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
18449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << " '" << DN->getQualifiedNameAsString() << '\'';
18549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << '\n';
18649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner}
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
18956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// Decl Implementation
19056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
19156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
192da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor// Out-of-line virtual method providing a home for Decl.
193da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas GregorDecl::~Decl() { }
194f4a03cc2b022fab0ffac6c65449555c52036deceDouglas Gregor
1954afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setDeclContext(DeclContext *DC) {
196ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  DeclCtx = DC;
1974afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
1984afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1994afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setLexicalDeclContext(DeclContext *DC) {
2004afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (DC == getLexicalDeclContext())
2014afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return;
2024afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2034afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isInSemaDC()) {
20494a39005e3733f2e498f2642be95507dda184ca1Ted Kremenek    MultipleDC *MDC = new (getASTContext()) MultipleDC();
2054afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MDC->SemanticDC = getDeclContext();
2064afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MDC->LexicalDC = DC;
207ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    DeclCtx = MDC;
2084afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  } else {
2094afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    getMultipleDC()->LexicalDC = DC;
2104afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
2114afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
2124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2139aeed32282fe8a775c24c01c923717ca86695685John McCallbool Decl::isInAnonymousNamespace() const {
2149aeed32282fe8a775c24c01c923717ca86695685John McCall  const DeclContext *DC = getDeclContext();
2159aeed32282fe8a775c24c01c923717ca86695685John McCall  do {
2169aeed32282fe8a775c24c01c923717ca86695685John McCall    if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2179aeed32282fe8a775c24c01c923717ca86695685John McCall      if (ND->isAnonymousNamespace())
2189aeed32282fe8a775c24c01c923717ca86695685John McCall        return true;
2199aeed32282fe8a775c24c01c923717ca86695685John McCall  } while ((DC = DC->getParent()));
2209aeed32282fe8a775c24c01c923717ca86695685John McCall
2219aeed32282fe8a775c24c01c923717ca86695685John McCall  return false;
2229aeed32282fe8a775c24c01c923717ca86695685John McCall}
2239aeed32282fe8a775c24c01c923717ca86695685John McCall
2243708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisTranslationUnitDecl *Decl::getTranslationUnitDecl() {
2259b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis  if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
2269b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis    return TUD;
2279b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis
2283708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  DeclContext *DC = getDeclContext();
2293708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  assert(DC && "This decl is not contained in a translation unit!");
2301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2313708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  while (!DC->isTranslationUnit()) {
2323708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    DC = DC->getParent();
2333708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    assert(DC && "This decl is not contained in a translation unit!");
2343708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  }
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2363708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  return cast<TranslationUnitDecl>(DC);
2373708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2383708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2393708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisASTContext &Decl::getASTContext() const {
2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return getTranslationUnitDecl()->getASTContext();
2413708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2423708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2437b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener *Decl::getASTMutationListener() const {
2447b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  return getASTContext().getASTMutationListener();
2457b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis}
2467b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
247c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregorbool Decl::isUsed(bool CheckUsedAttr) const {
24812ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  if (Used)
24912ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
25012ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
25112ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  // Check for used attribute.
252c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (CheckUsedAttr && hasAttr<UsedAttr>())
25312ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
25412ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
25512ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  // Check redeclarations for used attribute.
25612ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
257c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
25812ead498c4bc279472b21d446bfccec0f654779eTanya Lattner      return true;
25912ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  }
26012ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
26112ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  return false;
26212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner}
26312ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
2646b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidisbool Decl::isReferenced() const {
2656b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (Referenced)
2666b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    return true;
2676b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
2686b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  // Check redeclarations.
2696b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2706b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    if (I->Referenced)
2716b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis      return true;
2726b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
2736b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  return false;
2746b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis}
2756b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
2760a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// \brief Determine the availability of the given declaration based on
2770a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// the target platform.
2780a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor///
2790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// When it returns an availability result other than \c AR_Available,
2800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// if the \p Message parameter is non-NULL, it will be set to a
2810a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// string describing why the entity is unavailable.
2820a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor///
2830a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// FIXME: Make these strings localizable, since they end up in
2840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// diagnostics.
2850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorstatic AvailabilityResult CheckAvailability(ASTContext &Context,
2860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                            const AvailabilityAttr *A,
2870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                            std::string *Message) {
288bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
2895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef PrettyPlatformName
2900a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
2910a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (PrettyPlatformName.empty())
2920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    PrettyPlatformName = TargetPlatform;
2930a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
294bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
2950a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (TargetMinVersion.empty())
2960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Available;
2970a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
2980a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Match the platform name.
2990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (A->getPlatform()->getName() != TargetPlatform)
3000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Available;
301006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian
302006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  std::string HintMessage;
303006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  if (!A->getMessage().empty()) {
304006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian    HintMessage = " - ";
305006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian    HintMessage += A->getMessage();
306006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  }
307006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian
308b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  // Make sure that this declaration has not been marked 'unavailable'.
309b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  if (A->getUnavailable()) {
310b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    if (Message) {
311b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor      Message->clear();
312b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor      llvm::raw_string_ostream Out(*Message);
313006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian      Out << "not available on " << PrettyPlatformName
314006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << HintMessage;
315b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    }
316b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor
317b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    return AR_Unavailable;
318b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  }
319b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor
3200a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration has already been introduced.
3210a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getIntroduced().empty() &&
3220a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      TargetMinVersion < A->getIntroduced()) {
3230a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3240a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3250a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3260a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "introduced in " << PrettyPlatformName << ' '
327006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getIntroduced() << HintMessage;
3280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3290a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_NotYetIntroduced;
3310a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3320a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3330a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration hasn't been obsoleted.
3340a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
3350a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3360a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3380a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "obsoleted in " << PrettyPlatformName << ' '
339006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getObsoleted() << HintMessage;
3400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3410a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3420a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Unavailable;
3430a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3440a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3450a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration hasn't been deprecated.
3460a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
3470a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3480a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3490a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3500a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "first deprecated in " << PrettyPlatformName << ' '
351006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getDeprecated() << HintMessage;
3520a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3530a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3540a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Deprecated;
3550a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3560a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3570a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return AR_Available;
3580a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
3590a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3600a0d2b179085a52c10402feebeb6db8b4d96a140Douglas GregorAvailabilityResult Decl::getAvailability(std::string *Message) const {
3610a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  AvailabilityResult Result = AR_Available;
3620a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  std::string ResultMessage;
3630a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3640a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
3650a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
3660a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Result >= AR_Deprecated)
3670a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        continue;
3680a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3690a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Message)
3700a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        ResultMessage = Deprecated->getMessage();
3710a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3720a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Result = AR_Deprecated;
3730a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      continue;
3740a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3750a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3760a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
3770a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Message)
3780a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        *Message = Unavailable->getMessage();
3790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return AR_Unavailable;
3800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3810a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3820a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
3830a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
3840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                Message);
3850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (AR == AR_Unavailable)
3870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        return AR_Unavailable;
3880a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3890a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (AR > Result) {
3900a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        Result = AR;
3910a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        if (Message)
3920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor          ResultMessage.swap(*Message);
3930a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      }
3940a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      continue;
3950a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3970a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3980a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (Message)
3990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    Message->swap(ResultMessage);
4000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return Result;
4010a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4020a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4030a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorbool Decl::canBeWeakImported(bool &IsDefinition) const {
4040a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  IsDefinition = false;
4050a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
4060a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (!Var->hasExternalStorage() || Var->getInit()) {
4070a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      IsDefinition = true;
4080a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return false;
4090a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4100a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
4110a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (FD->hasBody()) {
4120a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      IsDefinition = true;
4130a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return false;
4140a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4150a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
4160a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return false;
4170a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
4180a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor             isa<ObjCInterfaceDecl>(this)))
4190a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return false;
4200a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4210a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return true;
4220a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4230a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4240a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorbool Decl::isWeakImported() const {
4250a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  bool IsDefinition;
4260a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!canBeWeakImported(IsDefinition))
4270a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return false;
4280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4290a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
4300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (isa<WeakImportAttr>(*A))
4310a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return true;
4320a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4330a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
4340a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (CheckAvailability(getASTContext(), Availability, 0)
4350a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                         == AR_NotYetIntroduced)
4360a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        return true;
4370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4380a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
4390a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return false;
4410a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
44212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
443769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattnerunsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
444769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  switch (DeclKind) {
4459488ea120e093068021f944176c3d610dd540914John McCall    case Function:
4469488ea120e093068021f944176c3d610dd540914John McCall    case CXXMethod:
4479488ea120e093068021f944176c3d610dd540914John McCall    case CXXConstructor:
4489488ea120e093068021f944176c3d610dd540914John McCall    case CXXDestructor:
4499488ea120e093068021f944176c3d610dd540914John McCall    case CXXConversion:
450769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case EnumConstant:
451769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Var:
452769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ImplicitParam:
453769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ParmVar:
454769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case NonTypeTemplateParm:
455769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCMethod:
456769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProperty:
45700b40d3f2fb8b2f9043daf3dd4558bff98346b3cDaniel Dunbar      return IDNS_Ordinary;
458ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    case Label:
459ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      return IDNS_Label;
46087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    case IndirectField:
46187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return IDNS_Ordinary | IDNS_Member;
46287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
4630d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case ObjCCompatibleAlias:
4640d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case ObjCInterface:
4650d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Type;
4660d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
4670d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case Typedef:
468162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case TypeAlias:
4693e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case TypeAliasTemplate:
4700d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case UnresolvedUsingTypename:
4710d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case TemplateTypeParm:
4720d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Type;
4730d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
4749488ea120e093068021f944176c3d610dd540914John McCall    case UsingShadow:
4759488ea120e093068021f944176c3d610dd540914John McCall      return 0; // we'll actually overwrite this later
4769488ea120e093068021f944176c3d610dd540914John McCall
4777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    case UnresolvedUsingValue:
4787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      return IDNS_Ordinary | IDNS_Using;
4799488ea120e093068021f944176c3d610dd540914John McCall
4809488ea120e093068021f944176c3d610dd540914John McCall    case Using:
4819488ea120e093068021f944176c3d610dd540914John McCall      return IDNS_Using;
4829488ea120e093068021f944176c3d610dd540914John McCall
483769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProtocol:
4848fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCProtocol;
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
486769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Field:
487769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCAtDefsField:
488769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCIvar:
489769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Member;
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
491769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Record:
492769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case CXXRecord:
493769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Enum:
4940d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Tag | IDNS_Type;
4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
496769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Namespace:
4970d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case NamespaceAlias:
4980d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Namespace;
4990d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
500769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FunctionTemplate:
5010d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary;
5020d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
503769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplate:
504769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTemplateParm:
5050d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
507769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Never have names.
50802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    case Friend:
509dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    case FriendTemplate:
5106206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    case AccessSpec:
511769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case LinkageSpec:
512769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FileScopeAsm:
513769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case StaticAssert:
514769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCPropertyImpl:
515769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Block:
516769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TranslationUnit:
517769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
518769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case UsingDirective:
519769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplateSpecialization:
520c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    case ClassTemplatePartialSpecialization:
521af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    case ClassScopeFunctionSpecialization:
522bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCImplementation:
523bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCCategory:
524bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCCategoryImpl:
52515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    case Import:
526bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor      // Never looked up by name.
527769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return 0;
528769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  }
5299488ea120e093068021f944176c3d610dd540914John McCall
5309488ea120e093068021f944176c3d610dd540914John McCall  return 0;
53156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
53256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
533cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntvoid Decl::setAttrs(const AttrVec &attrs) {
5341715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  assert(!HasAttrs && "Decl already contains attrs.");
5351715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
536cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
537cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  assert(AttrBlank.empty() && "HasAttrs was wrong?");
5381715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
5391715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  AttrBlank = attrs;
5401715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  HasAttrs = true;
5411715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis}
5421715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
543cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntvoid Decl::dropAttrs() {
54456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasAttrs) return;
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = false;
54740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  getASTContext().eraseDeclAttrs(this);
54856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
54956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
550cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntconst AttrVec &Decl::getAttrs() const {
551cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  assert(HasAttrs && "No attrs to get!");
55240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  return getASTContext().getDeclAttrs(this);
55356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
55456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
55540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisvoid Decl::swapAttrs(Decl *RHS) {
55656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasLHSAttr = this->HasAttrs;
55756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasRHSAttr = RHS->HasAttrs;
5581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Usually, neither decl has attrs, nothing to do.
56056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr && !HasRHSAttr) return;
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // If 'this' has no attrs, swap the other way.
56356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr)
56440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    return RHS->swapAttrs(this);
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  ASTContext &Context = getASTContext();
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Handle the case when both decls have attrs.
56956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (HasRHSAttr) {
57068584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
57156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    return;
57256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Otherwise, LHS has an attr and RHS doesn't.
57568584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
57668584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  Context.eraseDeclAttrs(this);
57756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  this->HasAttrs = false;
57856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  RHS->HasAttrs = true;
57956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
58056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
58142220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDecl *Decl::castFromDeclContext (const DeclContext *D) {
5823d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getDeclKind();
5833d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
5849a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
5859a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) \
5869a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    case Decl::NAME:       \
5879a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
5889a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
5899a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
5903d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
5919a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
5929a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                  \
5939a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (DK >= first##NAME && DK <= last##NAME) \
5949a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
5959a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
596b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("a decl that inherits DeclContext isn't handled");
5973d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
59842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
59942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
60042220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDeclContext *Decl::castToDeclContext(const Decl *D) {
6013d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getKind();
6023d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
6039a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6049a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) \
6059a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    case Decl::NAME:       \
6069a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
6079a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
6089a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6093d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
6109a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6119a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                                   \
6129a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (DK >= first##NAME && DK <= last##NAME)                  \
6139a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
6149a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
615b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("a decl that inherits DeclContext isn't handled");
6163d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
61742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
61842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
6196fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisSourceLocation Decl::getBodyRBrace() const {
62006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
62106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // FunctionDecl stores EndRangeLoc for this purpose.
62206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
62306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    const FunctionDecl *Definition;
62406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
62506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return Definition->getSourceRange().getEnd();
62606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    return SourceLocation();
62706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
62806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
6296717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis  if (Stmt *Body = getBody())
6306717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis    return Body->getSourceRange().getEnd();
6316717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis
6326717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis  return SourceLocation();
633d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl}
634d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
6351329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlssonvoid Decl::CheckAccessDeclContext() const {
6363a1c36c099df4dcc94d1d24516a8a2c809c764d4Douglas Gregor#ifndef NDEBUG
63746460a68f6508775e98c19b4bb8454bb471aac24John McCall  // Suppress this check if any of the following hold:
63846460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 1. this is the translation unit (and thus has no parent)
63946460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 2. this is a template parameter (and thus doesn't belong to its context)
640d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 3. this is a non-type template parameter
641d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 4. the context is not a record
642d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 5. it's invalid
643d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 6. it's a C++0x static_assert.
64435eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson  if (isa<TranslationUnitDecl>(this) ||
64504aed0edee1fe0bad807afb69f484c5e807800afArgyrios Kyrtzidis      isa<TemplateTypeParmDecl>(this) ||
646d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      isa<NonTypeTemplateParmDecl>(this) ||
647fdd8ab11bc40c7f206c8f1e892faa002cc6536b1Douglas Gregor      !isa<CXXRecordDecl>(getDeclContext()) ||
64865b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      isInvalidDecl() ||
64965b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      isa<StaticAssertDecl>(this) ||
65065b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
65165b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      // as DeclContext (?).
652d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      isa<ParmVarDecl>(this) ||
653d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
654d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      // AS_none as access specifier.
655bc84532e762a41141bd94037cd5d1133f234088eFrancois Pichet      isa<CXXRecordDecl>(this) ||
656bc84532e762a41141bd94037cd5d1133f234088eFrancois Pichet      isa<ClassScopeFunctionSpecializationDecl>(this))
65735eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson    return;
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(Access != AS_none &&
6601329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson         "Access specifier is AS_none inside a record decl");
6613a1c36c099df4dcc94d1d24516a8a2c809c764d4Douglas Gregor#endif
6621329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson}
6631329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
664aab9e315184d344bbd733f13b68915d02db7b32bJohn McCallDeclContext *Decl::getNonClosureContext() {
6654b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return getDeclContext()->getNonClosureAncestor();
6664b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
6674b9c2d235fb9449e249d74f48ecfec601650de93John McCall
6684b9c2d235fb9449e249d74f48ecfec601650de93John McCallDeclContext *DeclContext::getNonClosureAncestor() {
6694b9c2d235fb9449e249d74f48ecfec601650de93John McCall  DeclContext *DC = this;
670aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall
671aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  // This is basically "while (DC->isClosure()) DC = DC->getParent();"
672aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  // except that it's significantly more efficient to cast to a known
673aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  // decl type and call getDeclContext() than to call getParent().
6747b3f853c6f684c1df423e994482681844c396342John McCall  while (isa<BlockDecl>(DC))
6757b3f853c6f684c1df423e994482681844c396342John McCall    DC = cast<BlockDecl>(DC)->getDeclContext();
676aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall
677aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  assert(!DC->isClosure());
678aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  return DC;
679aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall}
6801329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
68156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
68256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// DeclContext Implementation
68356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
68456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
6853d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidisbool DeclContext::classof(const Decl *D) {
6863d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch (D->getKind()) {
6879a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6889a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) case Decl::NAME:
6899a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
6909a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6913d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return true;
6923d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
6939a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6949a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                 \
6959a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (D->getKind() >= Decl::first##NAME &&  \
6969a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt          D->getKind() <= Decl::last##NAME)     \
6973d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return true;
6989a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6993d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return false;
7003d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
7013d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis}
7023d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis
703a2da780b325e78c6c6bbbb766459a73243c3cf9eDouglas GregorDeclContext::~DeclContext() { }
70444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
705e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// \brief Find the parent context of this context that will be
706e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// used for unqualified name lookup.
707e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor///
708e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// Generally, the parent lookup context is the semantic context. However, for
709e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// a friend function the parent lookup context is the lexical context, which
710e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// is the class in which the friend is declared.
711e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas GregorDeclContext *DeclContext::getLookupParent() {
712e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  // FIXME: Find a better way to identify friends
713e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  if (isa<FunctionDecl>(this))
7147a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (getParent()->getRedeclContext()->isFileContext() &&
7157a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        getLexicalParent()->getRedeclContext()->isRecord())
716e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor      return getLexicalParent();
717e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
718e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  return getParent();
719e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor}
720e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
721410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redlbool DeclContext::isInlineNamespace() const {
722410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  return isNamespace() &&
723410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl         cast<NamespaceDecl>(this)->isInline();
724410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl}
725410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl
726bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregorbool DeclContext::isDependentContext() const {
727bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (isFileContext())
728bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    return false;
729c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
730c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  if (isa<ClassTemplatePartialSpecializationDecl>(this))
731c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return true;
732bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
733bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
734bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Record->getDescribedClassTemplate())
735bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
736bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
7370c01d18094100db92d38daa923c95661512db203John McCall  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
738bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Function->getDescribedFunctionTemplate())
739bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
7401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7410c01d18094100db92d38daa923c95661512db203John McCall    // Friend function declarations are dependent if their *lexical*
7420c01d18094100db92d38daa923c95661512db203John McCall    // context is dependent.
7430c01d18094100db92d38daa923c95661512db203John McCall    if (cast<Decl>(this)->getFriendObjectKind())
7440c01d18094100db92d38daa923c95661512db203John McCall      return getLexicalParent()->isDependentContext();
7450c01d18094100db92d38daa923c95661512db203John McCall  }
7460c01d18094100db92d38daa923c95661512db203John McCall
747bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  return getParent() && getParent()->isDependentContext();
748bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor}
749bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
750074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregorbool DeclContext::isTransparentContext() const {
751074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (DeclKind == Decl::Enum)
7521274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return !cast<EnumDecl>(this)->isScoped();
753074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::LinkageSpec)
754074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true;
755074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
756074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  return false;
757074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
758074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
759ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCallbool DeclContext::isExternCContext() const {
760ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall  const DeclContext *DC = this;
761ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall  while (DC->DeclKind != Decl::TranslationUnit) {
762ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    if (DC->DeclKind == Decl::LinkageSpec)
763ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall      return cast<LinkageSpecDecl>(DC)->getLanguage()
764ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        == LinkageSpecDecl::lang_c;
765ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    DC = DC->getParent();
766ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall  }
767ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall  return false;
768ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall}
769ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall
7707a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redlbool DeclContext::Encloses(const DeclContext *DC) const {
7716dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  if (getPrimaryContext() != this)
7726dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    return getPrimaryContext()->Encloses(DC);
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7746dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  for (; DC; DC = DC->getParent())
7756dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    if (DC->getPrimaryContext() == this)
7766dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor      return true;
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
7786dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor}
7796dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor
7800701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve NaroffDeclContext *DeclContext::getPrimaryContext() {
78144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
78244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::TranslationUnit:
783074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::LinkageSpec:
7841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Decl::Block:
78544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // There is only one DeclContext for these entities.
78644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
78744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
78844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
78944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // The original namespace is our primary context.
79044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
79144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
79244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCMethod:
79344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
79444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
79544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCInterface:
79653df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor    if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
79753df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor      return Def;
79853df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
79953df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor    return this;
80053df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
8010701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCProtocol:
8021d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
8031d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor      return Def;
8041d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor
8051d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    return this;
80653df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
8070701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategory:
80844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
80944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
8100701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCImplementation:
8110701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategoryImpl:
8120701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff    return this;
8130701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff
81444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
8159a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
816cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // If this is a tag type that has a definition or is currently
817cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // being defined, that definition is our primary context.
8183cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      TagDecl *Tag = cast<TagDecl>(this);
8193cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TagType>(Tag->TypeForDecl) ||
8203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             isa<InjectedClassNameType>(Tag->TypeForDecl));
8213cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (TagDecl *Def = Tag->getDefinition())
8233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        return Def;
8243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
8263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
8273cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (TagTy->isBeingDefined())
8283cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          // FIXME: is it necessarily being defined in the decl
8293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          // that owns the type?
8303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return TagTy->getDecl();
8313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      }
8323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return Tag;
834cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    }
835cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
8369a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
83744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          "Unknown DeclContext kind");
83844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
83944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
84044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
84144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
842f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregorvoid
843f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas GregorDeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
844f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  Contexts.clear();
845f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
846f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  if (DeclKind != Decl::Namespace) {
847f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    Contexts.push_back(this);
848f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    return;
84944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
850f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
851f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
852f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  for (NamespaceDecl *N = Self->getMostRecentDeclaration(); N;
853f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor       N = N->getPreviousDeclaration())
854f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    Contexts.push_back(N);
855f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
856f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  std::reverse(Contexts.begin(), Contexts.end());
85744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
85844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
859eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisstd::pair<Decl *, Decl *>
860ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios KyrtzidisDeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
861ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                            bool FieldsAlreadyLoaded) {
86246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  // Build up a chain of declarations via the Decl::NextInContextAndBits field.
863eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *FirstNewDecl = 0;
864eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *PrevDecl = 0;
865eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
866ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis    if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
867ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis      continue;
868ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis
869eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    Decl *D = Decls[I];
870eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    if (PrevDecl)
87146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      PrevDecl->NextInContextAndBits.setPointer(D);
872eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    else
873eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis      FirstNewDecl = D;
874eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
875eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    PrevDecl = D;
876eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  }
877eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
878eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return std::make_pair(FirstNewDecl, PrevDecl);
879eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
880eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
8812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Load the declarations within this lexical storage from an
8822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// external source.
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
88417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::LoadLexicalDeclsFromExternalStorage() const {
88517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  ExternalASTSource *Source = getParentASTContext().getExternalSource();
8862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalLexicalStorage() && Source && "No external storage?");
8872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8880dbbc042518e6ba903dd6f815cbb8704595cc937Argyrios Kyrtzidis  // Notify that we have a DeclContext that is initializing.
8890dbbc042518e6ba903dd6f815cbb8704595cc937Argyrios Kyrtzidis  ExternalASTSource::Deserializing ADeclContext(Source);
8909fc18c97991b1267221ee71d13d8fb2f036b387bDouglas Gregor
891ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  // Load the external declarations, if any.
8925f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 64> Decls;
8932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalLexicalStorage = false;
894ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  switch (Source->FindExternalLexicalDecls(this, Decls)) {
895ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Success:
896ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    break;
897ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor
898ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Failure:
899ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_AlreadyLoaded:
900ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    return;
901ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  }
9022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Decls.empty())
9042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
9052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
906ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  // We may have already loaded just the fields of this record, in which case
907ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  // we need to ignore them.
908ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  bool FieldsAlreadyLoaded = false;
909ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
910ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis    FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
911ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis
9122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Splice the newly-read declarations into the beginning of the list
9132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // of declarations.
914eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *ExternalFirst, *ExternalLast;
915ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
916ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                                                          FieldsAlreadyLoaded);
91746cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
918eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  FirstDecl = ExternalFirst;
9192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!LastDecl)
920eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LastDecl = ExternalLast;
9212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
9222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
92376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallDeclContext::lookup_result
92476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
92576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall                                                    DeclarationName Name) {
92676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  ASTContext &Context = DC->getParentASTContext();
92776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsMap *Map;
92876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  if (!(Map = DC->LookupPtr))
92976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    Map = DC->CreateStoredDeclsMap(Context);
93076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
93176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsList &List = (*Map)[Name];
93276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  assert(List.isNull());
93376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  (void) List;
93476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
93576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  return DeclContext::lookup_result();
93676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall}
9372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
93876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallDeclContext::lookup_result
93976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
94076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall                                                  DeclarationName Name,
94145df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis                                                  ArrayRef<NamedDecl*> Decls) {
94276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  ASTContext &Context = DC->getParentASTContext();;
94376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
94476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsMap *Map;
94576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  if (!(Map = DC->LookupPtr))
94676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    Map = DC->CreateStoredDeclsMap(Context);
9472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
94876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsList &List = (*Map)[Name];
94945df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis  for (ArrayRef<NamedDecl*>::iterator
95045df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis         I = Decls.begin(), E = Decls.end(); I != E; ++I) {
95176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    if (List.isNull())
95245df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis      List.setOnlyValue(*I);
95376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    else
95445df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis      List.AddSubsequentDecl(*I);
95576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  }
95676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
957074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  return List.getLookupResult();
95876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall}
95976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
960681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian RedlDeclContext::decl_iterator DeclContext::noload_decls_begin() const {
961681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  return decl_iterator(FirstDecl);
962681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl}
963681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl
964681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian RedlDeclContext::decl_iterator DeclContext::noload_decls_end() const {
965681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  return decl_iterator();
966681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl}
967681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl
96817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::decl_iterator DeclContext::decls_begin() const {
9692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
97017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
9712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return decl_iterator(FirstDecl);
9736ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
9746ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
97517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::decl_iterator DeclContext::decls_end() const {
9762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
97717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
9782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return decl_iterator();
9806ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
9816ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
98217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidisbool DeclContext::decls_empty() const {
9838038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  if (hasExternalLexicalStorage())
98417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
9858038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
9868038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  return !FirstDecl;
9878038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor}
9888038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
9899f54ad4381370c6b771424b53d219e661d6d6706John McCallvoid DeclContext::removeDecl(Decl *D) {
9909f54ad4381370c6b771424b53d219e661d6d6706John McCall  assert(D->getLexicalDeclContext() == this &&
9919f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl being removed from non-lexical context");
99246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
9939f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl is not in decls list");
9949f54ad4381370c6b771424b53d219e661d6d6706John McCall
9959f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the decl chain.  This is O(n) but hopefully rare.
9969f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (D == FirstDecl) {
9979f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (D == LastDecl)
9989f54ad4381370c6b771424b53d219e661d6d6706John McCall      FirstDecl = LastDecl = 0;
9999f54ad4381370c6b771424b53d219e661d6d6706John McCall    else
100046cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      FirstDecl = D->NextInContextAndBits.getPointer();
10019f54ad4381370c6b771424b53d219e661d6d6706John McCall  } else {
100246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
10039f54ad4381370c6b771424b53d219e661d6d6706John McCall      assert(I && "decl not found in linked list");
100446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      if (I->NextInContextAndBits.getPointer() == D) {
100546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor        I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
10069f54ad4381370c6b771424b53d219e661d6d6706John McCall        if (D == LastDecl) LastDecl = I;
10079f54ad4381370c6b771424b53d219e661d6d6706John McCall        break;
10089f54ad4381370c6b771424b53d219e661d6d6706John McCall      }
10099f54ad4381370c6b771424b53d219e661d6d6706John McCall    }
10109f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
10119f54ad4381370c6b771424b53d219e661d6d6706John McCall
10129f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Mark that D is no longer in the decl chain.
101346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  D->NextInContextAndBits.setPointer(0);
10149f54ad4381370c6b771424b53d219e661d6d6706John McCall
10159f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the lookup table if necessary.
10169f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (isa<NamedDecl>(D)) {
10179f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *ND = cast<NamedDecl>(D);
10189f54ad4381370c6b771424b53d219e661d6d6706John McCall
101902368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann    // Remove only decls that have a name
102002368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann    if (!ND->getDeclName()) return;
102102368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann
10220c01d18094100db92d38daa923c95661512db203John McCall    StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
10230c01d18094100db92d38daa923c95661512db203John McCall    if (!Map) return;
10249f54ad4381370c6b771424b53d219e661d6d6706John McCall
10259f54ad4381370c6b771424b53d219e661d6d6706John McCall    StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
10269f54ad4381370c6b771424b53d219e661d6d6706John McCall    assert(Pos != Map->end() && "no lookup entry for decl");
1027d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann    if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1028d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann      Pos->second.remove(ND);
10299f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
10309f54ad4381370c6b771424b53d219e661d6d6706John McCall}
10319f54ad4381370c6b771424b53d219e661d6d6706John McCall
10323f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addHiddenDecl(Decl *D) {
10337f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner  assert(D->getLexicalDeclContext() == this &&
10347f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner         "Decl inserted into wrong lexical context");
10351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!D->getNextDeclInContext() && D != LastDecl &&
10366037fcba3431b47de1a994c9b286feac17894effDouglas Gregor         "Decl already inserted into a DeclContext");
10376037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
10386037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  if (FirstDecl) {
103946cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    LastDecl->NextInContextAndBits.setPointer(D);
10406037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    LastDecl = D;
10416037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  } else {
10426037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    FirstDecl = LastDecl = D;
10436037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  }
104427c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor
104527c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  // Notify a C++ record declaration that we've added a member, so it can
104627c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  // update it's class-specific state.
104727c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
104827c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor    Record->addedMember(D);
1049e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
1050e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  // If this is a newly-created (not de-serialized) import declaration, wire
1051e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  // it in to the list of local import declarations.
1052e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!D->isFromASTFile()) {
1053e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1054e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor      D->getASTContext().addedLocalImportDecl(Import);
1055e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
10563f9a8a60614b763785d54ad08821745d03a4af70John McCall}
10573f9a8a60614b763785d54ad08821745d03a4af70John McCall
10583f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addDecl(Decl *D) {
10593f9a8a60614b763785d54ad08821745d03a4af70John McCall  addHiddenDecl(D);
10604afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
10614afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
106217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    ND->getDeclContext()->makeDeclVisibleInContext(ND);
106344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
106444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10659faf810f08132aabb34a478297dfeea89c3bbe17Sean Callananvoid DeclContext::addDeclInternal(Decl *D) {
10669faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  addHiddenDecl(D);
10679faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
10689faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
10699faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
10709faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
10719faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
1072074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// buildLookup - Build the lookup data structure with all of the
1073074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// declarations in DCtx (and any other contexts linked to it or
1074074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// transparent contexts nested within it).
107517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidisvoid DeclContext::buildLookup(DeclContext *DCtx) {
1076f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  llvm::SmallVector<DeclContext *, 2> Contexts;
1077f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  DCtx->collectAllContexts(Contexts);
1078f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  for (unsigned I = 0, N = Contexts.size(); I != N; ++I) {
1079f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    for (decl_iterator D = Contexts[I]->decls_begin(),
1080f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor                    DEnd = Contexts[I]->decls_end();
10814f3b8f8ac2f8c89028a2f8793df0a7887df809d4Douglas Gregor         D != DEnd; ++D) {
10823f9a8a60614b763785d54ad08821745d03a4af70John McCall      // Insert this declaration into the lookup structure, but only
10833f9a8a60614b763785d54ad08821745d03a4af70John McCall      // if it's semantically in its decl context.  During non-lazy
10843f9a8a60614b763785d54ad08821745d03a4af70John McCall      // lookup building, this is implicitly enforced by addDecl.
10854afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
1086f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor        if (D->getDeclContext() == Contexts[I])
10879faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan          makeDeclVisibleInContextImpl(ND, false);
1088074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
1089410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl      // If this declaration is itself a transparent declaration context or
1090410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl      // inline namespace, add its members (recursively).
1091074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
1092410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl        if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
109317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis          buildLookup(InnerCtx->getPrimaryContext());
1094074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    }
1095074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  }
1096074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
1097074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::lookup_result
109917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::lookup(DeclarationName Name) {
11000701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
110144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this)
110217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    return PrimaryContext->lookup(Name);
110344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
110476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  if (hasExternalVisibleStorage()) {
110576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    // Check to see if we've already cached the lookup results.
110676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    if (LookupPtr) {
110776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall      StoredDeclsMap::iterator I = LookupPtr->find(Name);
110876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall      if (I != LookupPtr->end())
1109074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis        return I->second.getLookupResult();
111076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    }
111176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
111276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    ExternalASTSource *Source = getParentASTContext().getExternalSource();
111376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    return Source->FindExternalVisibleDeclsByName(this, Name);
111476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  }
11152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
11163fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor  /// If there is no lookup data structure, build one now by walking
111744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// all of the linked DeclContexts (in declaration order!) and
111844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// inserting their values.
1119c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (!LookupPtr) {
112017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    buildLookup(this);
112144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1122c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    if (!LookupPtr)
1123a5fdd9ce694b1c2dbfd225cb6f55ef743d1ab562Douglas Gregor      return lookup_result(lookup_iterator(0), lookup_iterator(0));
1124c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  }
112544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
11260c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
11270c01d18094100db92d38daa923c95661512db203John McCall  if (Pos == LookupPtr->end())
1128a5fdd9ce694b1c2dbfd225cb6f55ef743d1ab562Douglas Gregor    return lookup_result(lookup_iterator(0), lookup_iterator(0));
1129074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  return Pos->second.getLookupResult();
113044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
113144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
11321eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::lookup_const_result
113317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::lookup(DeclarationName Name) const {
113417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  return const_cast<DeclContext*>(this)->lookup(Name);
113544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
113644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1137b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregorvoid DeclContext::localUncachedLookup(DeclarationName Name,
1138b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor                                  llvm::SmallVectorImpl<NamedDecl *> &Results) {
1139b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  Results.clear();
1140b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1141b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // If there's no external storage, just perform a normal lookup and copy
1142b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // the results.
1143b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1144b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    lookup_result LookupResults = lookup(Name);
1145b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1146b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    return;
1147b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1148b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1149b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // If we have a lookup table, check there first. Maybe we'll get lucky.
1150b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  if (LookupPtr) {
1151b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1152b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    if (Pos != LookupPtr->end()) {
1153b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor      Results.insert(Results.end(),
1154b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor                     Pos->second.getLookupResult().first,
1155b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor                     Pos->second.getLookupResult().second);
1156b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor      return;
1157b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    }
1158b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1159b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1160b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // Slow case: grovel through the declarations in our chain looking for
1161b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // matches.
1162b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1163b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1164b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor      if (ND->getDeclName() == Name)
1165b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor        Results.push_back(ND);
1166b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1167b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor}
1168b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
11697a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian RedlDeclContext *DeclContext::getRedeclContext() {
11700cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *Ctx = this;
1171410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  // Skip through transparent contexts.
1172410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  while (Ctx->isTransparentContext())
1173ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor    Ctx = Ctx->getParent();
1174ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  return Ctx;
1175ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor}
1176ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor
117788b7094185b9d4fe9820c731b6936d8d37f6143eDouglas GregorDeclContext *DeclContext::getEnclosingNamespaceContext() {
117888b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *Ctx = this;
117988b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  // Skip through non-namespace, non-translation-unit contexts.
118051a8a378012b5d6a1306fdd75bd135fea3e23b7bSebastian Redl  while (!Ctx->isFileContext())
118188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    Ctx = Ctx->getParent();
118288b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  return Ctx->getPrimaryContext();
118388b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor}
118488b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
11857a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redlbool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
11867a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  // For non-file contexts, this is equivalent to Equals.
11877a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  if (!isFileContext())
11887a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    return O->Equals(this);
11897a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
11907a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  do {
11917a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (O->Equals(this))
11927a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl      return true;
11937a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
11947a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
11957a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (!NS || !NS->isInline())
11967a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl      break;
11977a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    O = NS->getParent();
11987a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  } while (O);
11997a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
12007a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  return false;
12017a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl}
12027a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
12039faf810f08132aabb34a478297dfeea89c3bbe17Sean Callananvoid DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
12049faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan{
12059faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    makeDeclVisibleInContextWithFlags(D, false, Recoverable);
12069faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
12079faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
12089faf810f08132aabb34a478297dfeea89c3bbe17Sean Callananvoid DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
12099faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan{
12109faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    makeDeclVisibleInContextWithFlags(D, true, Recoverable);
12119faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
12129faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
12139faf810f08132aabb34a478297dfeea89c3bbe17Sean Callananvoid DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
1214cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // FIXME: This feels like a hack. Should DeclarationName support
1215cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // template-ids, or is there a better way to keep specializations
1216cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // from being visible?
12179a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
1218cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
12196bc20135a2c46f97da15994095616a305be35c6aEli Friedman  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
12206bc20135a2c46f97da15994095616a305be35c6aEli Friedman    if (FD->isFunctionTemplateSpecialization())
12216bc20135a2c46f97da15994095616a305be35c6aEli Friedman      return;
1222cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
12230701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
122444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this) {
12259faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
122644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return;
122744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
122844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
122944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // If we already have a lookup data structure, perform the insertion
12305586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  // into it. If we haven't deserialized externally stored decls, deserialize
12315586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  // them so we can add the decl. Otherwise, be lazy and don't build that
12325586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  // structure until someone asks for it.
12335586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
12349faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    makeDeclVisibleInContextImpl(D, Internal);
1235074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
1236410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  // If we are a transparent context or inline namespace, insert into our
1237410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  // parent context, too. This operation is recursive.
1238410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  if (isTransparentContext() || isInlineNamespace())
12399faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1240100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis
1241100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  Decl *DCAsDecl = cast<Decl>(this);
1242100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  // Notify that a decl was made visible unless it's a Tag being defined.
1243100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1244100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis    if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1245100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis      L->AddedVisibleDecl(this, D);
124644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
124744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
12489faf810f08132aabb34a478297dfeea89c3bbe17Sean Callananvoid DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1249074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // Skip unnamed declarations.
1250074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (!D->getDeclName())
1251074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return;
1252074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
12535cb0ef4aed9a4a1741260e16a99d18682335ab9bDouglas Gregor  // Skip entities that can't be found by name lookup into a particular
12545cb0ef4aed9a4a1741260e16a99d18682335ab9bDouglas Gregor  // context.
12555cb0ef4aed9a4a1741260e16a99d18682335ab9bDouglas Gregor  if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
12565cb0ef4aed9a4a1741260e16a99d18682335ab9bDouglas Gregor      D->isTemplateParameter())
1257cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
1258cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
12595586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  ASTContext *C = 0;
12605586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  if (!LookupPtr) {
12615586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis    C = &getParentASTContext();
12625586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis    CreateStoredDeclsMap(*C);
12635586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  }
12645586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis
1265074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If there is an external AST source, load any declarations it knows about
1266074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // with this declaration's name.
1267074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If the lookup table contains an entry about this name it means that we
1268074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // have already checked the external source.
12699faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  if (!Internal)
12709faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
12719faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan      if (hasExternalVisibleStorage() &&
12729faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan          LookupPtr->find(D->getDeclName()) == LookupPtr->end())
12739faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan        Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1274074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
127544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // Insert this declaration into the map.
12760c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
127767762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  if (DeclNameEntries.isNull()) {
127867762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    DeclNameEntries.setOnlyValue(D);
1279bd6c80037626a37ce3936a36d9ae287f475845b7Chris Lattner    return;
128044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
128191942501b6f71a41d3a09bedec19be479832c718Chris Lattner
1282bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // If it is possible that this is a redeclaration, check to see if there is
1283bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // already a decl for which declarationReplaces returns true.  If there is
1284bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // one, just replace it and return.
1285074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (DeclNameEntries.HandleRedeclaration(D))
128667762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    return;
12871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1288bd6c80037626a37ce3936a36d9ae287f475845b7Chris Lattner  // Put this declaration into the appropriate slot.
128967762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  DeclNameEntries.AddSubsequentDecl(D);
129044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
12912a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
12922a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
12932a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// this context.
12941eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::udir_iterator_range
129517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::getUsingDirectives() const {
129617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
12972a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
12982a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor                             reinterpret_cast<udir_iterator>(Result.second));
12992a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor}
13002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
13013478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
13023478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek// Creation and Destruction of StoredDeclsMaps.                               //
13033478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
13043478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
13050c01d18094100db92d38daa923c95661512db203John McCallStoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
13060c01d18094100db92d38daa923c95661512db203John McCall  assert(!LookupPtr && "context already has a decls map");
13070c01d18094100db92d38daa923c95661512db203John McCall  assert(getPrimaryContext() == this &&
13080c01d18094100db92d38daa923c95661512db203John McCall         "creating decls map on non-primary context");
13090c01d18094100db92d38daa923c95661512db203John McCall
13100c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap *M;
13110c01d18094100db92d38daa923c95661512db203John McCall  bool Dependent = isDependentContext();
13120c01d18094100db92d38daa923c95661512db203John McCall  if (Dependent)
13130c01d18094100db92d38daa923c95661512db203John McCall    M = new DependentStoredDeclsMap();
13140c01d18094100db92d38daa923c95661512db203John McCall  else
13150c01d18094100db92d38daa923c95661512db203John McCall    M = new StoredDeclsMap();
13160c01d18094100db92d38daa923c95661512db203John McCall  M->Previous = C.LastSDM;
13170c01d18094100db92d38daa923c95661512db203John McCall  C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
13180c01d18094100db92d38daa923c95661512db203John McCall  LookupPtr = M;
13193478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  return M;
13203478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
13213478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
13223478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenekvoid ASTContext::ReleaseDeclContextMaps() {
13230c01d18094100db92d38daa923c95661512db203John McCall  // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
13240c01d18094100db92d38daa923c95661512db203John McCall  // pointer because the subclass doesn't add anything that needs to
13250c01d18094100db92d38daa923c95661512db203John McCall  // be deleted.
13260c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
13270c01d18094100db92d38daa923c95661512db203John McCall}
13280c01d18094100db92d38daa923c95661512db203John McCall
13290c01d18094100db92d38daa923c95661512db203John McCallvoid StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
13300c01d18094100db92d38daa923c95661512db203John McCall  while (Map) {
13310c01d18094100db92d38daa923c95661512db203John McCall    // Advance the iteration before we invalidate memory.
13320c01d18094100db92d38daa923c95661512db203John McCall    llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
13330c01d18094100db92d38daa923c95661512db203John McCall
13340c01d18094100db92d38daa923c95661512db203John McCall    if (Dependent)
13350c01d18094100db92d38daa923c95661512db203John McCall      delete static_cast<DependentStoredDeclsMap*>(Map);
13360c01d18094100db92d38daa923c95661512db203John McCall    else
13370c01d18094100db92d38daa923c95661512db203John McCall      delete Map;
13380c01d18094100db92d38daa923c95661512db203John McCall
13390c01d18094100db92d38daa923c95661512db203John McCall    Map = Next.getPointer();
13400c01d18094100db92d38daa923c95661512db203John McCall    Dependent = Next.getInt();
13410c01d18094100db92d38daa923c95661512db203John McCall  }
13420c01d18094100db92d38daa923c95661512db203John McCall}
13430c01d18094100db92d38daa923c95661512db203John McCall
13440c01d18094100db92d38daa923c95661512db203John McCallDependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
13450c01d18094100db92d38daa923c95661512db203John McCall                                                 DeclContext *Parent,
13460c01d18094100db92d38daa923c95661512db203John McCall                                           const PartialDiagnostic &PDiag) {
13470c01d18094100db92d38daa923c95661512db203John McCall  assert(Parent->isDependentContext()
13480c01d18094100db92d38daa923c95661512db203John McCall         && "cannot iterate dependent diagnostics of non-dependent context");
13490c01d18094100db92d38daa923c95661512db203John McCall  Parent = Parent->getPrimaryContext();
13500c01d18094100db92d38daa923c95661512db203John McCall  if (!Parent->LookupPtr)
13510c01d18094100db92d38daa923c95661512db203John McCall    Parent->CreateStoredDeclsMap(C);
13520c01d18094100db92d38daa923c95661512db203John McCall
13530c01d18094100db92d38daa923c95661512db203John McCall  DependentStoredDeclsMap *Map
13540c01d18094100db92d38daa923c95661512db203John McCall    = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
13550c01d18094100db92d38daa923c95661512db203John McCall
1356b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  // Allocate the copy of the PartialDiagnostic via the ASTContext's
1357fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  // BumpPtrAllocator, rather than the ASTContext itself.
1358b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  PartialDiagnostic::Storage *DiagStorage = 0;
1359b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  if (PDiag.hasStorage())
1360b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor    DiagStorage = new (C) PartialDiagnostic::Storage;
1361b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor
1362b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
13630c01d18094100db92d38daa923c95661512db203John McCall
13640c01d18094100db92d38daa923c95661512db203John McCall  // TODO: Maybe we shouldn't reverse the order during insertion.
13650c01d18094100db92d38daa923c95661512db203John McCall  DD->NextDiagnostic = Map->FirstDiagnostic;
13660c01d18094100db92d38daa923c95661512db203John McCall  Map->FirstDiagnostic = DD;
13670c01d18094100db92d38daa923c95661512db203John McCall
13680c01d18094100db92d38daa923c95661512db203John McCall  return DD;
13693478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
1370