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"
1555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/ASTContext.h"
1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/ASTMutationListener.h"
1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/Attr.h"
1864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/Decl.h"
19d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/DeclContextInternals.h"
2192b7f70c924cbf4514e9e434cea7def51ab49860John McCall#include "clang/AST/DeclFriend.h"
22aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclObjC.h"
23c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev#include "clang/AST/DeclOpenMP.h"
24aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
250c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
262fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/ExternalASTSource.h"
27d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl#include "clang/AST/Stmt.h"
28d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl#include "clang/AST/StmtCXX.h"
292fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/Type.h"
300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor#include "clang/Basic/TargetInfo.h"
3156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "llvm/ADT/DenseMap.h"
3249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner#include "llvm/Support/raw_ostream.h"
336ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor#include <algorithm>
3456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanusing namespace clang;
3556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//  Statistics
3856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
409a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
419a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
429a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
4356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
446bd992946bda92193fadce7e4890d4465d2702f4Douglas Gregorvoid Decl::updateOutOfDate(IdentifierInfo &II) const {
456bd992946bda92193fadce7e4890d4465d2702f4Douglas Gregor  getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
466bd992946bda92193fadce7e4890d4465d2702f4Douglas Gregor}
476bd992946bda92193fadce7e4890d4465d2702f4Douglas Gregor
48651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid *Decl::operator new(std::size_t Size, const ASTContext &Context,
49651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         unsigned ID, std::size_t Extra) {
505d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  // Allocate an extra 8 bytes worth of storage, which ensures that the
51c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // resulting pointer will still be 8-byte aligned.
52651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void *Start = Context.Allocate(Size + Extra + 8);
535d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  void *Result = (char*)Start + 8;
54651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
55c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  unsigned *PrefixPtr = (unsigned *)Result - 2;
56651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
57c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // Zero out the first 4 bytes; this is used to store the owning module ID.
58c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  PrefixPtr[0] = 0;
59651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
60c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // Store the global declaration ID in the second 4 bytes.
61c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  PrefixPtr[1] = ID;
62651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
63b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  return Result;
641e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
651e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
66651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
67651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         DeclContext *Parent, std::size_t Extra) {
68651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  assert(!Parent || &Parent->getParentASTContext() == &Ctx);
69651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return ::operator new(Size + Extra, Ctx);
70651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
71651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
72ca2ab45341c448284cf93770018c717810575f86Douglas GregorModule *Decl::getOwningModuleSlow() const {
73ca2ab45341c448284cf93770018c717810575f86Douglas Gregor  assert(isFromASTFile() && "Not from AST file?");
74ca2ab45341c448284cf93770018c717810575f86Douglas Gregor  return getASTContext().getExternalSource()->getModule(getOwningModuleID());
75ca2ab45341c448284cf93770018c717810575f86Douglas Gregor}
76ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
7756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanconst char *Decl::getDeclKindName() const {
7856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (DeclKind) {
79b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration not in DeclNodes.inc!");
809a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
819a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
829a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
8356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
8456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
8556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
8642738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregorvoid Decl::setInvalidDecl(bool Invalid) {
8742738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  InvalidDecl = Invalid;
88651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
89ba50b3e8bc68bace2f6715111bbbb8510965be01Argyrios Kyrtzidis  if (Invalid && !isa<ParmVarDecl>(this)) {
9042738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // Defensive maneuver for ill-formed code: we're likely not to make it to
9142738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // a point where we set the access specifier, so default it to "public"
9242738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // to avoid triggering asserts elsewhere in the front end.
9342738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    setAccess(AS_public);
9442738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  }
9542738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor}
9642738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor
970a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroffconst char *DeclContext::getDeclKindName() const {
980a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  switch (DeclKind) {
99b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
1009a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
1019a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1029a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
1030a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  }
1040a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff}
1050a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff
10602892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbarbool Decl::StatisticsEnabled = false;
10702892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbarvoid Decl::EnableStatistics() {
10802892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  StatisticsEnabled = true;
10956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
11056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
11156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::PrintStats() {
112b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "\n*** Decl Stats:\n";
1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalDecls = 0;
1159a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
1169a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1179a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
118b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "  " << totalDecls << " decls total.\n";
1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalBytes = 0;
1219a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE)                                             \
1229a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt  if (n##DERIVED##s > 0) {                                              \
1239a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl));         \
124b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth    llvm::errs() << "    " << n##DERIVED##s << " " #DERIVED " decls, "  \
125b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << sizeof(DERIVED##Decl) << " each ("                  \
126b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << n##DERIVED##s * sizeof(DERIVED##Decl)               \
127b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << " bytes)\n";                                        \
12864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  }
1299a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1309a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
1311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
132b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "Total bytes = " << totalBytes << "\n";
13356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
13456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
1359a55591af3e5506b95a9718e15380129fbfc5ebcSean Huntvoid Decl::add(Kind k) {
13656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (k) {
1379a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
1389a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1399a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
14056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
14156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
14256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
14367e332009c6e349dc34700f539747afcff990336Anders Carlssonbool Decl::isTemplateParameterPack() const {
14467e332009c6e349dc34700f539747afcff990336Anders Carlsson  if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
14567e332009c6e349dc34700f539747afcff990336Anders Carlsson    return TTP->isParameterPack();
14610738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  if (const NonTypeTemplateParmDecl *NTTP
14761c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                = dyn_cast<NonTypeTemplateParmDecl>(this))
14810738d36b150aa65206890c1c845cdba076e4200Douglas Gregor    return NTTP->isParameterPack();
14961c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  if (const TemplateTemplateParmDecl *TTP
15061c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(this))
15161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor    return TTP->isParameterPack();
15267e332009c6e349dc34700f539747afcff990336Anders Carlsson  return false;
15367e332009c6e349dc34700f539747afcff990336Anders Carlsson}
15467e332009c6e349dc34700f539747afcff990336Anders Carlsson
1551fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool Decl::isParameterPack() const {
1561fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
1571fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor    return Parm->isParameterPack();
1581fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
1591fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isTemplateParameterPack();
1601fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
1611fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
162651f13cea278ec967336033dd032faef0e9fc2ecStephen HinesFunctionDecl *Decl::getAsFunction() {
163651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
164651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return FD;
165651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(this))
166651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return FTD->getTemplatedDecl();
1676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
168e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
169e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
170ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowskibool Decl::isTemplateDecl() const {
171ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski  return isa<TemplateDecl>(this);
172ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski}
173ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski
174c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidisconst DeclContext *Decl::getParentFunctionOrMethod() const {
175c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  for (const DeclContext *DC = getDeclContext();
176c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis       DC && !DC->isTranslationUnit() && !DC->isNamespace();
17779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor       DC = DC->getParent())
17879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    if (DC->isFunctionOrMethod())
179c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis      return DC;
18079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
1816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
18279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor}
18379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
1844c3e0ee8e6208eb42c4adb78a7d35b641fd85ae9Douglas Gregor
18556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
18649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner// PrettyStackTraceDecl Implementation
18749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
1881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyStackTraceDecl::print(raw_ostream &OS) const {
19049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation TheLoc = Loc;
19149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isInvalid() && TheDecl)
19249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc = TheDecl->getLocation();
1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isValid()) {
19549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc.print(OS, SM);
19649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << ": ";
19749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  }
19849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
19949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << Message;
20049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
201b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer  if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
202b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    OS << " '";
203b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    DN->printQualifiedName(OS);
204b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    OS << '\'';
205b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer  }
20649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << '\n';
20749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner}
2081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
21056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// Decl Implementation
21156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
21256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
213da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor// Out-of-line virtual method providing a home for Decl.
214da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas GregorDecl::~Decl() { }
215f4a03cc2b022fab0ffac6c65449555c52036deceDouglas Gregor
2164afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setDeclContext(DeclContext *DC) {
217ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  DeclCtx = DC;
2184afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
2194afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2204afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setLexicalDeclContext(DeclContext *DC) {
2214afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (DC == getLexicalDeclContext())
2224afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return;
2234afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2244afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isInSemaDC()) {
2254bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    setDeclContextsImpl(getDeclContext(), DC, getASTContext());
2264afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  } else {
2274afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    getMultipleDC()->LexicalDC = DC;
2284afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
2294afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
2304afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2314bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidisvoid Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
2324bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis                               ASTContext &Ctx) {
2334bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  if (SemaDC == LexicalDC) {
2344bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    DeclCtx = SemaDC;
2354bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  } else {
2364bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
2374bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    MDC->SemanticDC = SemaDC;
2384bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    MDC->LexicalDC = LexicalDC;
2394bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    DeclCtx = MDC;
2404bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  }
2414bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis}
2424bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis
2439aeed32282fe8a775c24c01c923717ca86695685John McCallbool Decl::isInAnonymousNamespace() const {
2449aeed32282fe8a775c24c01c923717ca86695685John McCall  const DeclContext *DC = getDeclContext();
2459aeed32282fe8a775c24c01c923717ca86695685John McCall  do {
2469aeed32282fe8a775c24c01c923717ca86695685John McCall    if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2479aeed32282fe8a775c24c01c923717ca86695685John McCall      if (ND->isAnonymousNamespace())
2489aeed32282fe8a775c24c01c923717ca86695685John McCall        return true;
2499aeed32282fe8a775c24c01c923717ca86695685John McCall  } while ((DC = DC->getParent()));
2509aeed32282fe8a775c24c01c923717ca86695685John McCall
2519aeed32282fe8a775c24c01c923717ca86695685John McCall  return false;
2529aeed32282fe8a775c24c01c923717ca86695685John McCall}
2539aeed32282fe8a775c24c01c923717ca86695685John McCall
2546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesbool Decl::isInStdNamespace() const {
2556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return getDeclContext()->isStdNamespace();
2566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
2576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
2583708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisTranslationUnitDecl *Decl::getTranslationUnitDecl() {
2599b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis  if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
2609b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis    return TUD;
2619b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis
2623708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  DeclContext *DC = getDeclContext();
2633708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  assert(DC && "This decl is not contained in a translation unit!");
2641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2653708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  while (!DC->isTranslationUnit()) {
2663708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    DC = DC->getParent();
2673708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    assert(DC && "This decl is not contained in a translation unit!");
2683708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  }
2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2703708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  return cast<TranslationUnitDecl>(DC);
2713708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2723708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2733708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisASTContext &Decl::getASTContext() const {
2741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return getTranslationUnitDecl()->getASTContext();
2753708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2763708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2777b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener *Decl::getASTMutationListener() const {
2787b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  return getASTContext().getASTMutationListener();
2797b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis}
2807b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
2812fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramerunsigned Decl::getMaxAlignment() const {
2822fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  if (!hasAttrs())
2832fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer    return 0;
2842fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer
2852fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  unsigned Align = 0;
2862fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  const AttrVec &V = getAttrs();
2872fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  ASTContext &Ctx = getASTContext();
2882fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
2892fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  for (; I != E; ++I)
2902fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer    Align = std::max(Align, I->getAlignment(Ctx));
2912fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  return Align;
2922fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer}
2932fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer
294c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregorbool Decl::isUsed(bool CheckUsedAttr) const {
29512ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  if (Used)
29612ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
29712ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
29812ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  // Check for used attribute.
299c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (CheckUsedAttr && hasAttr<UsedAttr>())
30012ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
301919b7e69781daf68766bb9af11ca1b9f246ce935Rafael Espindola
30212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  return false;
30312ead498c4bc279472b21d446bfccec0f654779eTanya Lattner}
30412ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
30586164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedmanvoid Decl::markUsed(ASTContext &C) {
30686164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  if (Used)
30786164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman    return;
30886164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman
30986164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  if (C.getASTMutationListener())
31086164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman    C.getASTMutationListener()->DeclarationMarkedUsed(this);
31186164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman
31286164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  Used = true;
31386164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman}
31486164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman
3156b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidisbool Decl::isReferenced() const {
3166b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (Referenced)
3176b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    return true;
3186b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
3196b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  // Check redeclarations.
320651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto I : redecls())
3216b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    if (I->Referenced)
3226b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis      return true;
3236b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
3246b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  return false;
3256b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis}
3266b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
3270a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// \brief Determine the availability of the given declaration based on
3280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// the target platform.
3290a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor///
3300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// When it returns an availability result other than \c AR_Available,
3310a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// if the \p Message parameter is non-NULL, it will be set to a
3320a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// string describing why the entity is unavailable.
3330a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor///
3340a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// FIXME: Make these strings localizable, since they end up in
3350a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// diagnostics.
3360a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorstatic AvailabilityResult CheckAvailability(ASTContext &Context,
3370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                            const AvailabilityAttr *A,
3380a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                            std::string *Message) {
339bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
3405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef PrettyPlatformName
3410a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
3420a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (PrettyPlatformName.empty())
3430a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    PrettyPlatformName = TargetPlatform;
3440a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
345bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
3460a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (TargetMinVersion.empty())
3470a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Available;
3480a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3490a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Match the platform name.
3500a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (A->getPlatform()->getName() != TargetPlatform)
3510a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Available;
352006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian
353006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  std::string HintMessage;
354006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  if (!A->getMessage().empty()) {
355006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian    HintMessage = " - ";
356006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian    HintMessage += A->getMessage();
357006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  }
358006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian
359b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  // Make sure that this declaration has not been marked 'unavailable'.
360b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  if (A->getUnavailable()) {
361b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    if (Message) {
362b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor      Message->clear();
363b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor      llvm::raw_string_ostream Out(*Message);
364006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian      Out << "not available on " << PrettyPlatformName
365006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << HintMessage;
366b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    }
367b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor
368b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    return AR_Unavailable;
369b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  }
370b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor
3710a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration has already been introduced.
3720a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getIntroduced().empty() &&
3730a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      TargetMinVersion < A->getIntroduced()) {
3740a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3750a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3760a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3770a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "introduced in " << PrettyPlatformName << ' '
378006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getIntroduced() << HintMessage;
3790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3810a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_NotYetIntroduced;
3820a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3830a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration hasn't been obsoleted.
3850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
3860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3880a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3890a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "obsoleted in " << PrettyPlatformName << ' '
390006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getObsoleted() << HintMessage;
3910a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3930a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Unavailable;
3940a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3950a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration hasn't been deprecated.
3970a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
3980a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
4000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
4010a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "first deprecated in " << PrettyPlatformName << ' '
402006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getDeprecated() << HintMessage;
4030a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4040a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4050a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Deprecated;
4060a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
4070a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4080a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return AR_Available;
4090a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4100a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4110a0d2b179085a52c10402feebeb6db8b4d96a140Douglas GregorAvailabilityResult Decl::getAvailability(std::string *Message) const {
4120a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  AvailabilityResult Result = AR_Available;
4130a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  std::string ResultMessage;
4140a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
415651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *A : attrs()) {
416651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
4170a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Result >= AR_Deprecated)
4180a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        continue;
4190a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4200a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Message)
4210a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        ResultMessage = Deprecated->getMessage();
4220a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4230a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Result = AR_Deprecated;
4240a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      continue;
4250a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4260a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
427651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
4280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Message)
4290a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        *Message = Unavailable->getMessage();
4300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return AR_Unavailable;
4310a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4320a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
433651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
4340a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
4350a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                Message);
4360a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (AR == AR_Unavailable)
4380a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        return AR_Unavailable;
4390a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (AR > Result) {
4410a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        Result = AR;
4420a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        if (Message)
4430a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor          ResultMessage.swap(*Message);
4440a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      }
4450a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      continue;
4460a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4470a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
4480a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4490a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (Message)
4500a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    Message->swap(ResultMessage);
4510a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return Result;
4520a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4530a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4540a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorbool Decl::canBeWeakImported(bool &IsDefinition) const {
4550a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  IsDefinition = false;
456260611a32535c851237926bfcf78869b13c07d5bJohn McCall
457260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Variables, if they aren't definitions.
4580a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
459d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola    if (Var->isThisDeclarationADefinition()) {
4600a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      IsDefinition = true;
4610a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return false;
4620a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
463260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
464260611a32535c851237926bfcf78869b13c07d5bJohn McCall
465260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Functions, if they aren't definitions.
4660a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
4670a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (FD->hasBody()) {
4680a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      IsDefinition = true;
4690a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return false;
4700a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
471260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
4720a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
473260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Objective-C classes, if this is the non-fragile runtime.
474260611a32535c851237926bfcf78869b13c07d5bJohn McCall  } else if (isa<ObjCInterfaceDecl>(this) &&
4750b92fcb1353d2d8b31b6c485e6caa14568aca43bJohn McCall             getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
476260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
477260611a32535c851237926bfcf78869b13c07d5bJohn McCall
478260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Nothing else.
479260611a32535c851237926bfcf78869b13c07d5bJohn McCall  } else {
480260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return false;
481260611a32535c851237926bfcf78869b13c07d5bJohn McCall  }
4820a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4830a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorbool Decl::isWeakImported() const {
4850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  bool IsDefinition;
4860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!canBeWeakImported(IsDefinition))
4870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return false;
4880a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *A : attrs()) {
490651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (isa<WeakImportAttr>(A))
4910a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return true;
4920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
493651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
4946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (CheckAvailability(getASTContext(), Availability,
4956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                            nullptr) == AR_NotYetIntroduced)
4960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        return true;
4970a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4980a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
4990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
5000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return false;
5010a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
50212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
503769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattnerunsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
504769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  switch (DeclKind) {
5059488ea120e093068021f944176c3d610dd540914John McCall    case Function:
5069488ea120e093068021f944176c3d610dd540914John McCall    case CXXMethod:
5079488ea120e093068021f944176c3d610dd540914John McCall    case CXXConstructor:
5089488ea120e093068021f944176c3d610dd540914John McCall    case CXXDestructor:
5099488ea120e093068021f944176c3d610dd540914John McCall    case CXXConversion:
510769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case EnumConstant:
511769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Var:
512769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ImplicitParam:
513769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ParmVar:
514769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case NonTypeTemplateParm:
515769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCMethod:
516769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProperty:
51776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    case MSProperty:
51800b40d3f2fb8b2f9043daf3dd4558bff98346b3cDaniel Dunbar      return IDNS_Ordinary;
519ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    case Label:
520ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      return IDNS_Label;
52187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    case IndirectField:
52287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return IDNS_Ordinary | IDNS_Member;
52387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
5240d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case ObjCCompatibleAlias:
5250d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case ObjCInterface:
5260d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Type;
5270d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
5280d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case Typedef:
529162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case TypeAlias:
5303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case TypeAliasTemplate:
5310d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case UnresolvedUsingTypename:
5320d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case TemplateTypeParm:
5330d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Type;
5340d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
5359488ea120e093068021f944176c3d610dd540914John McCall    case UsingShadow:
5369488ea120e093068021f944176c3d610dd540914John McCall      return 0; // we'll actually overwrite this later
5379488ea120e093068021f944176c3d610dd540914John McCall
5387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    case UnresolvedUsingValue:
5397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      return IDNS_Ordinary | IDNS_Using;
5409488ea120e093068021f944176c3d610dd540914John McCall
5419488ea120e093068021f944176c3d610dd540914John McCall    case Using:
5429488ea120e093068021f944176c3d610dd540914John McCall      return IDNS_Using;
5439488ea120e093068021f944176c3d610dd540914John McCall
544769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProtocol:
5458fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCProtocol;
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
547769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Field:
548769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCAtDefsField:
549769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCIvar:
550769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Member;
5511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
552769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Record:
553769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case CXXRecord:
554769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Enum:
5550d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Tag | IDNS_Type;
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
557769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Namespace:
5580d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case NamespaceAlias:
5590d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Namespace;
5600d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
561769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FunctionTemplate:
562ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    case VarTemplate:
5630d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary;
5640d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
565769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplate:
566769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTemplateParm:
5670d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
569769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Never have names.
57002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    case Friend:
571dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    case FriendTemplate:
5726206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    case AccessSpec:
573769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case LinkageSpec:
574769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FileScopeAsm:
575769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case StaticAssert:
576769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCPropertyImpl:
577769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Block:
5786afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    case Captured:
579769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TranslationUnit:
580769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
581769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case UsingDirective:
582769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplateSpecialization:
583c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    case ClassTemplatePartialSpecialization:
584af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    case ClassScopeFunctionSpecialization:
585ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    case VarTemplateSpecialization:
586ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    case VarTemplatePartialSpecialization:
587bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCImplementation:
588bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCCategory:
589bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCCategoryImpl:
59015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    case Import:
591c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    case OMPThreadPrivate:
592684aa73192d92850a926870be62a1787eb5b7ed9Michael Han    case Empty:
593bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor      // Never looked up by name.
594769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return 0;
595769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  }
5969488ea120e093068021f944176c3d610dd540914John McCall
5973026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid DeclKind!");
59856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
59956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
6004bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidisvoid Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
6011715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  assert(!HasAttrs && "Decl already contains attrs.");
6021715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
6034bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
604cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  assert(AttrBlank.empty() && "HasAttrs was wrong?");
6051715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
6061715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  AttrBlank = attrs;
6071715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  HasAttrs = true;
6081715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis}
6091715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
610cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntvoid Decl::dropAttrs() {
61156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasAttrs) return;
6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = false;
61440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  getASTContext().eraseDeclAttrs(this);
61556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
61656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
617cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntconst AttrVec &Decl::getAttrs() const {
618cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  assert(HasAttrs && "No attrs to get!");
61940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  return getASTContext().getDeclAttrs(this);
62056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
62156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
62242220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDecl *Decl::castFromDeclContext (const DeclContext *D) {
6233d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getDeclKind();
6243d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
6259a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6269a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) \
6279a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    case Decl::NAME:       \
6289a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
6299a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
6309a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6313d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
6329a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6339a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                  \
6349a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (DK >= first##NAME && DK <= last##NAME) \
6359a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
6369a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
637b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("a decl that inherits DeclContext isn't handled");
6383d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
63942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
64042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
64142220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDeclContext *Decl::castToDeclContext(const Decl *D) {
6423d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getKind();
6433d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
6449a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6459a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) \
6469a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    case Decl::NAME:       \
6479a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
6489a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
6499a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6503d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
6519a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6529a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                                   \
6539a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (DK >= first##NAME && DK <= last##NAME)                  \
6549a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
6559a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
656b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("a decl that inherits DeclContext isn't handled");
6573d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
65842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
65942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
6606fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisSourceLocation Decl::getBodyRBrace() const {
66106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
66206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // FunctionDecl stores EndRangeLoc for this purpose.
66306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
66406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    const FunctionDecl *Definition;
66506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
66606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return Definition->getSourceRange().getEnd();
66706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    return SourceLocation();
66806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
66906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
6706717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis  if (Stmt *Body = getBody())
6716717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis    return Body->getSourceRange().getEnd();
6726717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis
6736717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis  return SourceLocation();
674d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl}
675d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
676651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesbool Decl::AccessDeclContextSanity() const {
6773a1c36c099df4dcc94d1d24516a8a2c809c764d4Douglas Gregor#ifndef NDEBUG
67846460a68f6508775e98c19b4bb8454bb471aac24John McCall  // Suppress this check if any of the following hold:
67946460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 1. this is the translation unit (and thus has no parent)
68046460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 2. this is a template parameter (and thus doesn't belong to its context)
681d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 3. this is a non-type template parameter
682d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 4. the context is not a record
683d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 5. it's invalid
684d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 6. it's a C++0x static_assert.
68535eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson  if (isa<TranslationUnitDecl>(this) ||
68604aed0edee1fe0bad807afb69f484c5e807800afArgyrios Kyrtzidis      isa<TemplateTypeParmDecl>(this) ||
687d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      isa<NonTypeTemplateParmDecl>(this) ||
688fdd8ab11bc40c7f206c8f1e892faa002cc6536b1Douglas Gregor      !isa<CXXRecordDecl>(getDeclContext()) ||
68965b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      isInvalidDecl() ||
69065b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      isa<StaticAssertDecl>(this) ||
69165b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
69265b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      // as DeclContext (?).
693d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      isa<ParmVarDecl>(this) ||
694d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
695d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      // AS_none as access specifier.
696bc84532e762a41141bd94037cd5d1133f234088eFrancois Pichet      isa<CXXRecordDecl>(this) ||
697bc84532e762a41141bd94037cd5d1133f234088eFrancois Pichet      isa<ClassScopeFunctionSpecializationDecl>(this))
698651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return true;
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(Access != AS_none &&
7011329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson         "Access specifier is AS_none inside a record decl");
7023a1c36c099df4dcc94d1d24516a8a2c809c764d4Douglas Gregor#endif
703651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return true;
7041329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson}
7051329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
706f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallstatic Decl::Kind getKind(const Decl *D) { return D->getKind(); }
707f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallstatic Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
708f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall
709651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesconst FunctionType *Decl::getFunctionType(bool BlocksToo) const {
710651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType Ty;
711651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (const ValueDecl *D = dyn_cast<ValueDecl>(this))
712651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Ty = D->getType();
713651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  else if (const TypedefNameDecl *D = dyn_cast<TypedefNameDecl>(this))
714651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Ty = D->getUnderlyingType();
715651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  else
7166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
717651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
718651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Ty->isFunctionPointerType())
719651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Ty = Ty->getAs<PointerType>()->getPointeeType();
720651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  else if (BlocksToo && Ty->isBlockPointerType())
721651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
722651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
723651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return Ty->getAs<FunctionType>();
724651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
725651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
726651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
727f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall/// Starting at a given context (a Decl or DeclContext), look for a
728f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall/// code context that is not a closure (a lambda, block, etc.).
729f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCalltemplate <class T> static Decl *getNonClosureContext(T *D) {
730f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  if (getKind(D) == Decl::CXXMethod) {
731f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
732000817b3b2ab95c5b9cbb76950cd01b79fe93f55John McCall    if (MD->getOverloadedOperator() == OO_Call &&
733000817b3b2ab95c5b9cbb76950cd01b79fe93f55John McCall        MD->getParent()->isLambda())
734f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall      return getNonClosureContext(MD->getParent()->getParent());
735f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return MD;
736f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
737f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return FD;
738f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
739f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return MD;
740f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
741f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return getNonClosureContext(BD->getParent());
742f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
743f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return getNonClosureContext(CD->getParent());
744f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else {
7456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
746f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  }
7474b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
7484b9c2d235fb9449e249d74f48ecfec601650de93John McCall
749f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallDecl *Decl::getNonClosureContext() {
750f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  return ::getNonClosureContext(this);
751f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall}
752aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall
753f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallDecl *DeclContext::getNonClosureAncestor() {
754f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  return ::getNonClosureContext(this);
755aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall}
7561329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
75756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
75856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// DeclContext Implementation
75956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
76056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
7613d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidisbool DeclContext::classof(const Decl *D) {
7623d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch (D->getKind()) {
7639a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
7649a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) case Decl::NAME:
7659a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
7669a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
7673d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return true;
7683d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
7699a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
7709a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                 \
7719a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (D->getKind() >= Decl::first##NAME &&  \
7729a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt          D->getKind() <= Decl::last##NAME)     \
7733d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return true;
7749a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
7753d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return false;
7763d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
7773d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis}
7783d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis
779a2da780b325e78c6c6bbbb766459a73243c3cf9eDouglas GregorDeclContext::~DeclContext() { }
78044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
781e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// \brief Find the parent context of this context that will be
782e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// used for unqualified name lookup.
783e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor///
784e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// Generally, the parent lookup context is the semantic context. However, for
785e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// a friend function the parent lookup context is the lexical context, which
786e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// is the class in which the friend is declared.
787e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas GregorDeclContext *DeclContext::getLookupParent() {
788e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  // FIXME: Find a better way to identify friends
789e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  if (isa<FunctionDecl>(this))
7907a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (getParent()->getRedeclContext()->isFileContext() &&
7917a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        getLexicalParent()->getRedeclContext()->isRecord())
792e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor      return getLexicalParent();
793e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
794e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  return getParent();
795e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor}
796e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
797410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redlbool DeclContext::isInlineNamespace() const {
798410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  return isNamespace() &&
799410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl         cast<NamespaceDecl>(this)->isInline();
800410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl}
801410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl
8026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesbool DeclContext::isStdNamespace() const {
8036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!isNamespace())
8046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return false;
8056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
8066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const NamespaceDecl *ND = cast<NamespaceDecl>(this);
8076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (ND->isInline()) {
8086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return ND->getParent()->isStdNamespace();
8096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
8106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
8116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!getParent()->getRedeclContext()->isTranslationUnit())
8126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return false;
8136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
8146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const IdentifierInfo *II = ND->getIdentifier();
8156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return II && II->isStr("std");
8166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
8176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
818bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregorbool DeclContext::isDependentContext() const {
819bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (isFileContext())
820bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    return false;
821c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
822c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  if (isa<ClassTemplatePartialSpecializationDecl>(this))
823c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return true;
824bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
825f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor  if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
826bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Record->getDescribedClassTemplate())
827bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
828f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor
829f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor    if (Record->isDependentLambda())
830f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor      return true;
831f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor  }
832f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor
8330c01d18094100db92d38daa923c95661512db203John McCall  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
834bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Function->getDescribedFunctionTemplate())
835bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8370c01d18094100db92d38daa923c95661512db203John McCall    // Friend function declarations are dependent if their *lexical*
8380c01d18094100db92d38daa923c95661512db203John McCall    // context is dependent.
8390c01d18094100db92d38daa923c95661512db203John McCall    if (cast<Decl>(this)->getFriendObjectKind())
8400c01d18094100db92d38daa923c95661512db203John McCall      return getLexicalParent()->isDependentContext();
8410c01d18094100db92d38daa923c95661512db203John McCall  }
8420c01d18094100db92d38daa923c95661512db203John McCall
843bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  return getParent() && getParent()->isDependentContext();
844bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor}
845bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
846074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregorbool DeclContext::isTransparentContext() const {
847074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (DeclKind == Decl::Enum)
8481274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return !cast<EnumDecl>(this)->isScoped();
849074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::LinkageSpec)
850074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true;
851074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
852074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  return false;
853074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
854074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
855142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlovstatic bool isLinkageSpecContext(const DeclContext *DC,
856142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov                                 LinkageSpecDecl::LanguageIDs ID) {
857142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  while (DC->getDeclKind() != Decl::TranslationUnit) {
858142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov    if (DC->getDeclKind() == Decl::LinkageSpec)
859142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov      return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
8606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    DC = DC->getLexicalParent();
861142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  }
862142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  return false;
863142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov}
864142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov
865142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlovbool DeclContext::isExternCContext() const {
866142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_c);
867142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov}
868142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov
869142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlovbool DeclContext::isExternCXXContext() const {
870142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_cxx);
871142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov}
872142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov
8737a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redlbool DeclContext::Encloses(const DeclContext *DC) const {
8746dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  if (getPrimaryContext() != this)
8756dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    return getPrimaryContext()->Encloses(DC);
8761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8776dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  for (; DC; DC = DC->getParent())
8786dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    if (DC->getPrimaryContext() == this)
8796dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor      return true;
8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
8816dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor}
8826dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor
8830701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve NaroffDeclContext *DeclContext::getPrimaryContext() {
88444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
88544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::TranslationUnit:
886074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::LinkageSpec:
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Decl::Block:
8886afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  case Decl::Captured:
88944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // There is only one DeclContext for these entities.
89044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
89144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
89244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
89344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // The original namespace is our primary context.
89444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
89544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
89644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCMethod:
89744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
89844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
89944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCInterface:
90053df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor    if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
90153df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor      return Def;
90253df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
90353df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor    return this;
90453df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
9050701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCProtocol:
9061d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
9071d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor      return Def;
9081d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor
9091d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    return this;
91053df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
9110701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategory:
91244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
91344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
9140701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCImplementation:
9150701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategoryImpl:
9160701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff    return this;
9170701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff
91844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
9199a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
920cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // If this is a tag type that has a definition or is currently
921cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // being defined, that definition is our primary context.
9223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      TagDecl *Tag = cast<TagDecl>(this);
9233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
9243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (TagDecl *Def = Tag->getDefinition())
9253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        return Def;
9263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
9276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (const TagType *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
9286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        // Note, TagType::getDecl returns the (partial) definition one exists.
9296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        TagDecl *PossiblePartialDef = TagTy->getDecl();
9306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (PossiblePartialDef->isBeingDefined())
9316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return PossiblePartialDef;
9326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      } else {
9336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
9343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      }
9353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
9363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return Tag;
937cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    }
938cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
9399a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
94044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          "Unknown DeclContext kind");
94144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
94244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
94344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
94444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
945f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregorvoid
946cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri GribenkoDeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
947f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  Contexts.clear();
948f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
949f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  if (DeclKind != Decl::Namespace) {
950f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    Contexts.push_back(this);
951f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    return;
95244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
953f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
954f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
955ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
956ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor       N = N->getPreviousDecl())
957f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    Contexts.push_back(N);
958f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
959f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  std::reverse(Contexts.begin(), Contexts.end());
96044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
96144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
962eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisstd::pair<Decl *, Decl *>
963341785ec52f87c0803ba52dc88faac4e136f8593Bill WendlingDeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
964ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                            bool FieldsAlreadyLoaded) {
96546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  // Build up a chain of declarations via the Decl::NextInContextAndBits field.
9666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Decl *FirstNewDecl = nullptr;
9676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Decl *PrevDecl = nullptr;
968eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
969ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis    if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
970ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis      continue;
971ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis
972eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    Decl *D = Decls[I];
973eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    if (PrevDecl)
97446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      PrevDecl->NextInContextAndBits.setPointer(D);
975eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    else
976eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis      FirstNewDecl = D;
977eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
978eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    PrevDecl = D;
979eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  }
980eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
981eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return std::make_pair(FirstNewDecl, PrevDecl);
982eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
983eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
984bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith/// \brief We have just acquired external visible storage, and we already have
985bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith/// built a lookup map. For every name in the map, pull in the new names from
986bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith/// the external storage.
987651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid DeclContext::reconcileExternalVisibleStorage() const {
9888896339a3c654dc44877d52a926eb89ab3ef30b5Richard Smith  assert(NeedToReconcileExternalVisibleStorage && LookupPtr.getPointer());
989bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  NeedToReconcileExternalVisibleStorage = false;
990bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith
991651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto &Lookup : *LookupPtr.getPointer())
992651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Lookup.second.setHasExternalDecls();
993bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith}
994bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith
9952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Load the declarations within this lexical storage from an
9962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// external source.
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
99817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::LoadLexicalDeclsFromExternalStorage() const {
99917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  ExternalASTSource *Source = getParentASTContext().getExternalSource();
10002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalLexicalStorage() && Source && "No external storage?");
10012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
10020dbbc042518e6ba903dd6f815cbb8704595cc937Argyrios Kyrtzidis  // Notify that we have a DeclContext that is initializing.
10030dbbc042518e6ba903dd6f815cbb8704595cc937Argyrios Kyrtzidis  ExternalASTSource::Deserializing ADeclContext(Source);
10049fc18c97991b1267221ee71d13d8fb2f036b387bDouglas Gregor
1005ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  // Load the external declarations, if any.
10065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 64> Decls;
10072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalLexicalStorage = false;
1008ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  switch (Source->FindExternalLexicalDecls(this, Decls)) {
1009ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Success:
1010ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    break;
1011ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor
1012ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Failure:
1013ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_AlreadyLoaded:
1014ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    return;
1015ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  }
10162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
10172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Decls.empty())
10182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
10192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1020ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  // We may have already loaded just the fields of this record, in which case
1021ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  // we need to ignore them.
1022ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  bool FieldsAlreadyLoaded = false;
1023ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
1024ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis    FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
1025ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis
10262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Splice the newly-read declarations into the beginning of the list
10272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // of declarations.
1028eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *ExternalFirst, *ExternalLast;
1029651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::tie(ExternalFirst, ExternalLast) =
1030651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      BuildDeclChain(Decls, FieldsAlreadyLoaded);
103146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
1032eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  FirstDecl = ExternalFirst;
10332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!LastDecl)
1034eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LastDecl = ExternalLast;
10352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
10362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
103776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallDeclContext::lookup_result
103876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
103976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall                                                    DeclarationName Name) {
104076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  ASTContext &Context = DC->getParentASTContext();
104176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsMap *Map;
1042c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(Map = DC->LookupPtr.getPointer()))
104376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    Map = DC->CreateStoredDeclsMap(Context);
1044651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (DC->NeedToReconcileExternalVisibleStorage)
1045651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DC->reconcileExternalVisibleStorage();
104676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
1047b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  (*Map)[Name].removeExternalDecls();
104876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
104976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  return DeclContext::lookup_result();
105076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall}
10512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
105276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallDeclContext::lookup_result
105376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
105476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall                                                  DeclarationName Name,
105545df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis                                                  ArrayRef<NamedDecl*> Decls) {
10561ad23d62007162df82b58bca31b4aa277a5f6586Dmitri Gribenko  ASTContext &Context = DC->getParentASTContext();
105776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsMap *Map;
1058c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(Map = DC->LookupPtr.getPointer()))
105976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    Map = DC->CreateStoredDeclsMap(Context);
1060651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (DC->NeedToReconcileExternalVisibleStorage)
1061651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    DC->reconcileExternalVisibleStorage();
10622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
106376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsList &List = (*Map)[Name];
1064ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith
1065ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  // Clear out any old external visible declarations, to avoid quadratic
1066ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  // performance in the redeclaration checks below.
1067ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  List.removeExternalDecls();
1068ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith
1069ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  if (!List.isNull()) {
1070ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // We have both existing declarations and new declarations for this name.
1071ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // Some of the declarations may simply replace existing ones. Handle those
1072ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // first.
1073ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    llvm::SmallVector<unsigned, 8> Skip;
1074ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    for (unsigned I = 0, N = Decls.size(); I != N; ++I)
1075ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      if (List.HandleRedeclaration(Decls[I]))
1076ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        Skip.push_back(I);
1077ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    Skip.push_back(Decls.size());
1078ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith
1079ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // Add in any new declarations.
1080ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    unsigned SkipPos = 0;
1081ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1082ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      if (I == Skip[SkipPos])
1083ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        ++SkipPos;
1084ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      else
1085ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        List.AddSubsequentDecl(Decls[I]);
1086ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    }
1087ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  } else {
1088ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // Convert the array to a StoredDeclsList.
1089ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    for (ArrayRef<NamedDecl*>::iterator
1090ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith           I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1091ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      if (List.isNull())
1092ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        List.setOnlyValue(*I);
1093ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      else
1094ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        List.AddSubsequentDecl(*I);
1095ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    }
109676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  }
109776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
1098074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  return List.getLookupResult();
109976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall}
110076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
110117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::decl_iterator DeclContext::decls_begin() const {
11022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
110317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return decl_iterator(FirstDecl);
11056ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
11066ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
110717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidisbool DeclContext::decls_empty() const {
11088038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  if (hasExternalLexicalStorage())
110917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
11108038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
11118038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  return !FirstDecl;
11128038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor}
11138038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
1114cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callananbool DeclContext::containsDecl(Decl *D) const {
1115cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan  return (D->getLexicalDeclContext() == this &&
1116cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan          (D->NextInContextAndBits.getPointer() || D == LastDecl));
1117cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan}
1118cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan
11199f54ad4381370c6b771424b53d219e661d6d6706John McCallvoid DeclContext::removeDecl(Decl *D) {
11209f54ad4381370c6b771424b53d219e661d6d6706John McCall  assert(D->getLexicalDeclContext() == this &&
11219f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl being removed from non-lexical context");
112246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
11239f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl is not in decls list");
11249f54ad4381370c6b771424b53d219e661d6d6706John McCall
11259f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the decl chain.  This is O(n) but hopefully rare.
11269f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (D == FirstDecl) {
11279f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (D == LastDecl)
11286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      FirstDecl = LastDecl = nullptr;
11299f54ad4381370c6b771424b53d219e661d6d6706John McCall    else
113046cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      FirstDecl = D->NextInContextAndBits.getPointer();
11319f54ad4381370c6b771424b53d219e661d6d6706John McCall  } else {
113246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
11339f54ad4381370c6b771424b53d219e661d6d6706John McCall      assert(I && "decl not found in linked list");
113446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      if (I->NextInContextAndBits.getPointer() == D) {
113546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor        I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
11369f54ad4381370c6b771424b53d219e661d6d6706John McCall        if (D == LastDecl) LastDecl = I;
11379f54ad4381370c6b771424b53d219e661d6d6706John McCall        break;
11389f54ad4381370c6b771424b53d219e661d6d6706John McCall      }
11399f54ad4381370c6b771424b53d219e661d6d6706John McCall    }
11409f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
11419f54ad4381370c6b771424b53d219e661d6d6706John McCall
11429f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Mark that D is no longer in the decl chain.
11436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  D->NextInContextAndBits.setPointer(nullptr);
11449f54ad4381370c6b771424b53d219e661d6d6706John McCall
11459f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the lookup table if necessary.
11469f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (isa<NamedDecl>(D)) {
11479f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *ND = cast<NamedDecl>(D);
11489f54ad4381370c6b771424b53d219e661d6d6706John McCall
114902368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann    // Remove only decls that have a name
115002368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann    if (!ND->getDeclName()) return;
115102368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann
1152c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
11530c01d18094100db92d38daa923c95661512db203John McCall    if (!Map) return;
11549f54ad4381370c6b771424b53d219e661d6d6706John McCall
11559f54ad4381370c6b771424b53d219e661d6d6706John McCall    StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
11569f54ad4381370c6b771424b53d219e661d6d6706John McCall    assert(Pos != Map->end() && "no lookup entry for decl");
1157d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann    if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1158d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann      Pos->second.remove(ND);
11599f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
11609f54ad4381370c6b771424b53d219e661d6d6706John McCall}
11619f54ad4381370c6b771424b53d219e661d6d6706John McCall
11623f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addHiddenDecl(Decl *D) {
11637f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner  assert(D->getLexicalDeclContext() == this &&
11647f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner         "Decl inserted into wrong lexical context");
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!D->getNextDeclInContext() && D != LastDecl &&
11666037fcba3431b47de1a994c9b286feac17894effDouglas Gregor         "Decl already inserted into a DeclContext");
11676037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
11686037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  if (FirstDecl) {
116946cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    LastDecl->NextInContextAndBits.setPointer(D);
11706037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    LastDecl = D;
11716037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  } else {
11726037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    FirstDecl = LastDecl = D;
11736037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  }
117427c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor
117527c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  // Notify a C++ record declaration that we've added a member, so it can
117627c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  // update it's class-specific state.
117727c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
117827c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor    Record->addedMember(D);
1179e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
1180e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  // If this is a newly-created (not de-serialized) import declaration, wire
1181e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  // it in to the list of local import declarations.
1182e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!D->isFromASTFile()) {
1183e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1184e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor      D->getASTContext().addedLocalImportDecl(Import);
1185e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
11863f9a8a60614b763785d54ad08821745d03a4af70John McCall}
11873f9a8a60614b763785d54ad08821745d03a4af70John McCall
11883f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addDecl(Decl *D) {
11893f9a8a60614b763785d54ad08821745d03a4af70John McCall  addHiddenDecl(D);
11904afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
11914afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1192c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ND->getDeclContext()->getPrimaryContext()->
1193c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(ND, false, true);
119444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
119544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
11969faf810f08132aabb34a478297dfeea89c3bbe17Sean Callananvoid DeclContext::addDeclInternal(Decl *D) {
11979faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  addHiddenDecl(D);
11989faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
11999faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1200c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ND->getDeclContext()->getPrimaryContext()->
1201c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(ND, true, true);
1202c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1203c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1204c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// shouldBeHidden - Determine whether a declaration which was declared
1205c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// within its semantic context should be invisible to qualified name lookup.
1206c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithstatic bool shouldBeHidden(NamedDecl *D) {
1207c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip unnamed declarations.
1208c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!D->getDeclName())
1209c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1210c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1211c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip entities that can't be found by name lookup into a particular
1212c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // context.
1213c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1214c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      D->isTemplateParameter())
1215c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1216c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1217c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip template specializations.
1218c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // FIXME: This feels like a hack. Should DeclarationName support
1219c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // template-ids, or is there a better way to keep specializations
1220c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // from being visible?
1221c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (isa<ClassTemplateSpecializationDecl>(D))
1222c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1223c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1224c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (FD->isFunctionTemplateSpecialization())
1225c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      return true;
1226c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1227c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return false;
1228c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1229c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1230c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// buildLookup - Build the lookup data structure with all of the
1231c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// declarations in this DeclContext (and any other contexts linked
1232c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// to it or transparent contexts nested within it) and return it.
1233651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines///
1234651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// Note that the produced map may miss out declarations from an
1235651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// external source. If it does, those entries will be marked with
1236651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// the 'hasExternalDecls' flag.
1237c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard SmithStoredDeclsMap *DeclContext::buildLookup() {
1238c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1239c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
12408896339a3c654dc44877d52a926eb89ab3ef30b5Richard Smith  // FIXME: Should we keep going if hasExternalVisibleStorage?
1241c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!LookupPtr.getInt())
1242c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return LookupPtr.getPointer();
1243c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1244cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<DeclContext *, 2> Contexts;
1245c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  collectAllContexts(Contexts);
1246c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1247e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    buildLookupImpl<&DeclContext::decls_begin,
1248e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith                    &DeclContext::decls_end>(Contexts[I]);
1249c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1250c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // We no longer have any lazy decls.
1251c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  LookupPtr.setInt(false);
1252c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return LookupPtr.getPointer();
1253c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1254c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1255c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// buildLookupImpl - Build part of the lookup data structure for the
1256c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// declarations contained within DCtx, which will either be this
1257c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// DeclContext, a DeclContext linked to it, or a transparent context
1258c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// nested within it.
1259e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smithtemplate<DeclContext::decl_iterator (DeclContext::*Begin)() const,
1260e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith         DeclContext::decl_iterator (DeclContext::*End)() const>
1261c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::buildLookupImpl(DeclContext *DCtx) {
1262e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  for (decl_iterator I = (DCtx->*Begin)(), E = (DCtx->*End)();
1263c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith       I != E; ++I) {
1264c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Decl *D = *I;
1265c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1266c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // Insert this declaration into the lookup structure, but only if
1267c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // it's semantically within its decl context. Any other decls which
1268c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // should be found in this context are added eagerly.
1269096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    //
1270096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    // If it's from an AST file, don't add it now. It'll get handled by
1271096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1272096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    // in C++, we do not track external visible decls for the TU, so in
1273096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    // that case we need to collect them all here.
1274c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1275096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith      if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1276096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith          (!ND->isFromASTFile() ||
1277096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith           (isTranslationUnit() &&
1278096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith            !getParentASTContext().getLangOpts().CPlusPlus)))
1279c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextImpl(ND, false);
1280c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1281c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // If this declaration is itself a transparent declaration context
1282c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // or inline namespace, add the members of this declaration of that
1283c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // context (recursively).
1284c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1285c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1286e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith        buildLookupImpl<Begin, End>(InnerCtx);
1287c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
12889faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
12899faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
12901eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::lookup_result
129117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::lookup(DeclarationName Name) {
129265daef179790a02eab1b5a989f53984375a06483Nick Lewycky  assert(DeclKind != Decl::LinkageSpec &&
129365daef179790a02eab1b5a989f53984375a06483Nick Lewycky         "Should not perform lookups into linkage specs!");
129465daef179790a02eab1b5a989f53984375a06483Nick Lewycky
12950701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
129644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this)
129717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    return PrimaryContext->lookup(Name);
129844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
12991b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  if (hasExternalVisibleStorage()) {
1300651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (NeedToReconcileExternalVisibleStorage)
1301651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      reconcileExternalVisibleStorage();
1302651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1303bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    StoredDeclsMap *Map = LookupPtr.getPointer();
1304651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1305bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    if (LookupPtr.getInt())
1306bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith      Map = buildLookup();
1307bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith
13082bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith    if (!Map)
13092bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith      Map = CreateStoredDeclsMap(getParentASTContext());
13102bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith
1311b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    // If we have a lookup result with no external decls, we are done.
13122bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith    std::pair<StoredDeclsMap::iterator, bool> R =
13132bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith        Map->insert(std::make_pair(Name, StoredDeclsList()));
1314b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    if (!R.second && !R.first->second.hasExternalDecls())
13152bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith      return R.first->second.getLookupResult();
1316c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
131776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    ExternalASTSource *Source = getParentASTContext().getExternalSource();
1318651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
13193646c68676c3c46a026b23d52188ef6e0d856178Richard Smith      if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
13203646c68676c3c46a026b23d52188ef6e0d856178Richard Smith        StoredDeclsMap::iterator I = Map->find(Name);
13213646c68676c3c46a026b23d52188ef6e0d856178Richard Smith        if (I != Map->end())
13223646c68676c3c46a026b23d52188ef6e0d856178Richard Smith          return I->second.getLookupResult();
13233646c68676c3c46a026b23d52188ef6e0d856178Richard Smith      }
13243646c68676c3c46a026b23d52188ef6e0d856178Richard Smith    }
13253646c68676c3c46a026b23d52188ef6e0d856178Richard Smith
13266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return lookup_result(lookup_iterator(nullptr), lookup_iterator(nullptr));
132776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  }
13282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1329c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap *Map = LookupPtr.getPointer();
1330c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (LookupPtr.getInt())
1331c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Map = buildLookup();
1332c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1333c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Map)
13346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return lookup_result(lookup_iterator(nullptr), lookup_iterator(nullptr));
1335c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1336c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap::iterator I = Map->find(Name);
1337c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (I == Map->end())
13386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return lookup_result(lookup_iterator(nullptr), lookup_iterator(nullptr));
1339c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1340c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return I->second.getLookupResult();
134144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
134244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1343e7bae1597f4a7088f5048695c14a8f1013a86108Richard SmithDeclContext::lookup_result
1344e7bae1597f4a7088f5048695c14a8f1013a86108Richard SmithDeclContext::noload_lookup(DeclarationName Name) {
1345e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  assert(DeclKind != Decl::LinkageSpec &&
1346e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith         "Should not perform lookups into linkage specs!");
1347e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  if (!hasExternalVisibleStorage())
1348e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    return lookup(Name);
1349e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1350e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  DeclContext *PrimaryContext = getPrimaryContext();
1351e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  if (PrimaryContext != this)
1352e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    return PrimaryContext->noload_lookup(Name);
1353e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1354e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  StoredDeclsMap *Map = LookupPtr.getPointer();
1355e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  if (LookupPtr.getInt()) {
1356e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    // Carefully build the lookup map, without deserializing anything.
1357e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    SmallVector<DeclContext *, 2> Contexts;
1358e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    collectAllContexts(Contexts);
1359e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1360e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith      buildLookupImpl<&DeclContext::noload_decls_begin,
1361e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith                      &DeclContext::noload_decls_end>(Contexts[I]);
1362e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1363e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    // We no longer have any lazy decls.
1364e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    LookupPtr.setInt(false);
1365e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1366e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    // There may now be names for which we have local decls but are
1367b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    // missing the external decls. FIXME: Just set the hasExternalDecls
1368b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    // flag on those names that have external decls.
1369e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    NeedToReconcileExternalVisibleStorage = true;
1370e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1371e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    Map = LookupPtr.getPointer();
1372e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  }
1373e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1374e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  if (!Map)
13756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return lookup_result(lookup_iterator(nullptr), lookup_iterator(nullptr));
1376e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1377e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  StoredDeclsMap::iterator I = Map->find(Name);
13786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return I != Map->end() ? I->second.getLookupResult()
13796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                         : lookup_result(lookup_iterator(nullptr),
13806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                         lookup_iterator(nullptr));
1381e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith}
1382e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1383cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenkovoid DeclContext::localUncachedLookup(DeclarationName Name,
1384cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                      SmallVectorImpl<NamedDecl *> &Results) {
1385b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  Results.clear();
1386b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1387b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // If there's no external storage, just perform a normal lookup and copy
1388b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // the results.
138993ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor  if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
1390b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    lookup_result LookupResults = lookup(Name);
13913bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1392b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    return;
1393b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1394b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1395b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // If we have a lookup table, check there first. Maybe we'll get lucky.
1396bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  if (Name && !LookupPtr.getInt()) {
139793ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor    if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
139893ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      StoredDeclsMap::iterator Pos = Map->find(Name);
139993ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      if (Pos != Map->end()) {
140093ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor        Results.insert(Results.end(),
14013bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie                       Pos->second.getLookupResult().begin(),
14023bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie                       Pos->second.getLookupResult().end());
140393ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor        return;
140493ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      }
1405b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    }
1406b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
140793ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor
1408b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // Slow case: grovel through the declarations in our chain looking for
1409b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // matches.
1410b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1411b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1412b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor      if (ND->getDeclName() == Name)
1413b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor        Results.push_back(ND);
1414b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1415b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor}
1416b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
14177a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian RedlDeclContext *DeclContext::getRedeclContext() {
14180cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *Ctx = this;
1419410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  // Skip through transparent contexts.
1420410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  while (Ctx->isTransparentContext())
1421ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor    Ctx = Ctx->getParent();
1422ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  return Ctx;
1423ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor}
1424ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor
142588b7094185b9d4fe9820c731b6936d8d37f6143eDouglas GregorDeclContext *DeclContext::getEnclosingNamespaceContext() {
142688b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *Ctx = this;
142788b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  // Skip through non-namespace, non-translation-unit contexts.
142851a8a378012b5d6a1306fdd75bd135fea3e23b7bSebastian Redl  while (!Ctx->isFileContext())
142988b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    Ctx = Ctx->getParent();
143088b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  return Ctx->getPrimaryContext();
143188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor}
143288b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
14337a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redlbool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
14347a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  // For non-file contexts, this is equivalent to Equals.
14357a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  if (!isFileContext())
14367a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    return O->Equals(this);
14377a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
14387a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  do {
14397a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (O->Equals(this))
14407a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl      return true;
14417a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
14427a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
14437a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (!NS || !NS->isInline())
14447a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl      break;
14457a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    O = NS->getParent();
14467a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  } while (O);
14477a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
14487a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  return false;
14497a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl}
14507a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
1451c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1452c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclContext *PrimaryDC = this->getPrimaryContext();
1453c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1454c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If the decl is being added outside of its semantic decl context, we
1455c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // need to ensure that we eagerly build the lookup information for it.
1456c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
14579faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
14589faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
1459c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1460c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith                                                    bool Recoverable) {
1461c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(this == getPrimaryContext() && "expected a primary DC");
146244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
14631b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // Skip declarations within functions.
14644e9686b1f9947f1747b4f4316deb2087a7f56282Richard Smith  if (isFunctionOrMethod())
1465cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
1466cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
1467c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip declarations which should be invisible to name lookup.
1468c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (shouldBeHidden(D))
1469c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
1470c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1471c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If we already have a lookup data structure, perform the insertion into
1472c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // it. If we might have externally-stored decls with this name, look them
1473c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // up and perform the insertion. If this decl was declared outside its
1474c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // semantic context, buildLookup won't add it, so add it now.
1475c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  //
1476c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // FIXME: As a performance hack, don't add such decls into the translation
1477c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // unit unless we're in C++, since qualified lookup into the TU is never
1478c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // performed.
1479c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1480c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1481c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith       (getParentASTContext().getLangOpts().CPlusPlus ||
1482c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        !isTranslationUnit()))) {
1483c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // If we have lazily omitted any decls, they might have the same name as
1484c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // the decl which we are adding, so build a full lookup table before adding
1485c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // this decl.
1486c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    buildLookup();
1487c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    makeDeclVisibleInContextImpl(D, Internal);
1488c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  } else {
1489c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    LookupPtr.setInt(true);
1490c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
1491c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1492c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If we are a transparent context or inline namespace, insert into our
1493c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // parent context, too. This operation is recursive.
1494c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (isTransparentContext() || isInlineNamespace())
1495c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    getParent()->getPrimaryContext()->
1496c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1497c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1498c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  Decl *DCAsDecl = cast<Decl>(this);
1499c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Notify that a decl was made visible unless we are a Tag being defined.
1500c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1501c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1502c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      L->AddedVisibleDecl(this, D);
1503c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1504c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1505c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1506c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Find or create the stored declaration map.
1507c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap *Map = LookupPtr.getPointer();
1508c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Map) {
1509c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ASTContext *C = &getParentASTContext();
1510c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Map = CreateStoredDeclsMap(*C);
15115586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  }
15125586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis
1513074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If there is an external AST source, load any declarations it knows about
1514074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // with this declaration's name.
1515074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If the lookup table contains an entry about this name it means that we
1516074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // have already checked the external source.
15179faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  if (!Internal)
15189faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
15199faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan      if (hasExternalVisibleStorage() &&
1520c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith          Map->find(D->getDeclName()) == Map->end())
15219faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan        Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1522074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
152344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // Insert this declaration into the map.
1524c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
1525b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith
1526b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  if (Internal) {
1527b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    // If this is being added as part of loading an external declaration,
1528b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    // this may not be the only external declaration with this name.
1529b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    // In this case, we never try to replace an existing declaration; we'll
1530b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    // handle that when we finalize the list of declarations for this name.
1531b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    DeclNameEntries.setHasExternalDecls();
1532b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    DeclNameEntries.AddSubsequentDecl(D);
1533b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith    return;
1534b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  }
1535b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith
1536b7165589b2eafc4b48d09a5914e21604ae580256Richard Smith  else if (DeclNameEntries.isNull()) {
153767762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    DeclNameEntries.setOnlyValue(D);
1538c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
1539c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
1540c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1541c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (DeclNameEntries.HandleRedeclaration(D)) {
15421b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    // This declaration has replaced an existing one for which
15431b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    // declarationReplaces returns true.
1544c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
154544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
154691942501b6f71a41d3a09bedec19be479832c718Chris Lattner
1547c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Put this declaration into the appropriate slot.
1548c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclNameEntries.AddSubsequentDecl(D);
154944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
15502a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
15512a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
15522a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// this context.
1553651f13cea278ec967336033dd032faef0e9fc2ecStephen HinesDeclContext::udir_range DeclContext::using_directives() const {
15541b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // FIXME: Use something more efficient than normal lookup for using
15551b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // directives. In C++, using directives are looked up more than anything else.
155617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
1557651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return udir_range(
1558651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      reinterpret_cast<UsingDirectiveDecl *const *>(Result.begin()),
1559651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      reinterpret_cast<UsingDirectiveDecl *const *>(Result.end()));
15602a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor}
15612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
15623478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
15633478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek// Creation and Destruction of StoredDeclsMaps.                               //
15643478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
15653478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
15660c01d18094100db92d38daa923c95661512db203John McCallStoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1567c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(!LookupPtr.getPointer() && "context already has a decls map");
15680c01d18094100db92d38daa923c95661512db203John McCall  assert(getPrimaryContext() == this &&
15690c01d18094100db92d38daa923c95661512db203John McCall         "creating decls map on non-primary context");
15700c01d18094100db92d38daa923c95661512db203John McCall
15710c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap *M;
15720c01d18094100db92d38daa923c95661512db203John McCall  bool Dependent = isDependentContext();
15730c01d18094100db92d38daa923c95661512db203John McCall  if (Dependent)
15740c01d18094100db92d38daa923c95661512db203John McCall    M = new DependentStoredDeclsMap();
15750c01d18094100db92d38daa923c95661512db203John McCall  else
15760c01d18094100db92d38daa923c95661512db203John McCall    M = new StoredDeclsMap();
15770c01d18094100db92d38daa923c95661512db203John McCall  M->Previous = C.LastSDM;
15780c01d18094100db92d38daa923c95661512db203John McCall  C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1579c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  LookupPtr.setPointer(M);
15803478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  return M;
15813478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
15823478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
15833478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenekvoid ASTContext::ReleaseDeclContextMaps() {
15840c01d18094100db92d38daa923c95661512db203John McCall  // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
15850c01d18094100db92d38daa923c95661512db203John McCall  // pointer because the subclass doesn't add anything that needs to
15860c01d18094100db92d38daa923c95661512db203John McCall  // be deleted.
15870c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
15880c01d18094100db92d38daa923c95661512db203John McCall}
15890c01d18094100db92d38daa923c95661512db203John McCall
15900c01d18094100db92d38daa923c95661512db203John McCallvoid StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
15910c01d18094100db92d38daa923c95661512db203John McCall  while (Map) {
15920c01d18094100db92d38daa923c95661512db203John McCall    // Advance the iteration before we invalidate memory.
15930c01d18094100db92d38daa923c95661512db203John McCall    llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
15940c01d18094100db92d38daa923c95661512db203John McCall
15950c01d18094100db92d38daa923c95661512db203John McCall    if (Dependent)
15960c01d18094100db92d38daa923c95661512db203John McCall      delete static_cast<DependentStoredDeclsMap*>(Map);
15970c01d18094100db92d38daa923c95661512db203John McCall    else
15980c01d18094100db92d38daa923c95661512db203John McCall      delete Map;
15990c01d18094100db92d38daa923c95661512db203John McCall
16000c01d18094100db92d38daa923c95661512db203John McCall    Map = Next.getPointer();
16010c01d18094100db92d38daa923c95661512db203John McCall    Dependent = Next.getInt();
16020c01d18094100db92d38daa923c95661512db203John McCall  }
16030c01d18094100db92d38daa923c95661512db203John McCall}
16040c01d18094100db92d38daa923c95661512db203John McCall
16050c01d18094100db92d38daa923c95661512db203John McCallDependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
16060c01d18094100db92d38daa923c95661512db203John McCall                                                 DeclContext *Parent,
16070c01d18094100db92d38daa923c95661512db203John McCall                                           const PartialDiagnostic &PDiag) {
16080c01d18094100db92d38daa923c95661512db203John McCall  assert(Parent->isDependentContext()
16090c01d18094100db92d38daa923c95661512db203John McCall         && "cannot iterate dependent diagnostics of non-dependent context");
16100c01d18094100db92d38daa923c95661512db203John McCall  Parent = Parent->getPrimaryContext();
1611c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Parent->LookupPtr.getPointer())
16120c01d18094100db92d38daa923c95661512db203John McCall    Parent->CreateStoredDeclsMap(C);
16130c01d18094100db92d38daa923c95661512db203John McCall
16140c01d18094100db92d38daa923c95661512db203John McCall  DependentStoredDeclsMap *Map
1615c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
16160c01d18094100db92d38daa923c95661512db203John McCall
1617b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  // Allocate the copy of the PartialDiagnostic via the ASTContext's
1618fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  // BumpPtrAllocator, rather than the ASTContext itself.
16196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  PartialDiagnostic::Storage *DiagStorage = nullptr;
1620b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  if (PDiag.hasStorage())
1621b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor    DiagStorage = new (C) PartialDiagnostic::Storage;
1622b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor
1623b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
16240c01d18094100db92d38daa923c95661512db203John McCall
16250c01d18094100db92d38daa923c95661512db203John McCall  // TODO: Maybe we shouldn't reverse the order during insertion.
16260c01d18094100db92d38daa923c95661512db203John McCall  DD->NextDiagnostic = Map->FirstDiagnostic;
16270c01d18094100db92d38daa923c95661512db203John McCall  Map->FirstDiagnostic = DD;
16280c01d18094100db92d38daa923c95661512db203John McCall
16290c01d18094100db92d38daa923c95661512db203John McCall  return DD;
16303478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
1631