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
481e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregorvoid *Decl::AllocateDeserializedDecl(const ASTContext &Context,
491e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                     unsigned ID,
501e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                     unsigned Size) {
515d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  // Allocate an extra 8 bytes worth of storage, which ensures that the
52c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // resulting pointer will still be 8-byte aligned.
535d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  void *Start = Context.Allocate(Size + 8);
545d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  void *Result = (char*)Start + 8;
55b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor
56c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  unsigned *PrefixPtr = (unsigned *)Result - 2;
57c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor
58c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // Zero out the first 4 bytes; this is used to store the owning module ID.
59c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  PrefixPtr[0] = 0;
60c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor
61c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // Store the global declaration ID in the second 4 bytes.
62c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  PrefixPtr[1] = ID;
63b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor
64b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  return Result;
651e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
661e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
67ca2ab45341c448284cf93770018c717810575f86Douglas GregorModule *Decl::getOwningModuleSlow() const {
68ca2ab45341c448284cf93770018c717810575f86Douglas Gregor  assert(isFromASTFile() && "Not from AST file?");
69ca2ab45341c448284cf93770018c717810575f86Douglas Gregor  return getASTContext().getExternalSource()->getModule(getOwningModuleID());
70ca2ab45341c448284cf93770018c717810575f86Douglas Gregor}
71ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
7256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanconst char *Decl::getDeclKindName() const {
7356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (DeclKind) {
74b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration not in DeclNodes.inc!");
759a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
769a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
779a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
7856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
7956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
8056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
8142738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregorvoid Decl::setInvalidDecl(bool Invalid) {
8242738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  InvalidDecl = Invalid;
83ba50b3e8bc68bace2f6715111bbbb8510965be01Argyrios Kyrtzidis  if (Invalid && !isa<ParmVarDecl>(this)) {
8442738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // Defensive maneuver for ill-formed code: we're likely not to make it to
8542738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // a point where we set the access specifier, so default it to "public"
8642738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // to avoid triggering asserts elsewhere in the front end.
8742738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    setAccess(AS_public);
8842738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  }
8942738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor}
9042738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor
910a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroffconst char *DeclContext::getDeclKindName() const {
920a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  switch (DeclKind) {
93b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
949a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
959a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
969a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
970a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  }
980a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff}
990a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff
10002892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbarbool Decl::StatisticsEnabled = false;
10102892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbarvoid Decl::EnableStatistics() {
10202892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  StatisticsEnabled = true;
10356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
10456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
10556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::PrintStats() {
106b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "\n*** Decl Stats:\n";
1071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalDecls = 0;
1099a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
1109a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1119a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
112b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "  " << totalDecls << " decls total.\n";
1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalBytes = 0;
1159a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE)                                             \
1169a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt  if (n##DERIVED##s > 0) {                                              \
1179a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl));         \
118b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth    llvm::errs() << "    " << n##DERIVED##s << " " #DERIVED " decls, "  \
119b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << sizeof(DERIVED##Decl) << " each ("                  \
120b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << n##DERIVED##s * sizeof(DERIVED##Decl)               \
121b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << " bytes)\n";                                        \
12264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  }
1239a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1249a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
126b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "Total bytes = " << totalBytes << "\n";
12756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
12856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
1299a55591af3e5506b95a9718e15380129fbfc5ebcSean Huntvoid Decl::add(Kind k) {
13056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (k) {
1319a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
1329a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1339a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
13456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
13556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
13656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
13767e332009c6e349dc34700f539747afcff990336Anders Carlssonbool Decl::isTemplateParameterPack() const {
13867e332009c6e349dc34700f539747afcff990336Anders Carlsson  if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
13967e332009c6e349dc34700f539747afcff990336Anders Carlsson    return TTP->isParameterPack();
14010738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  if (const NonTypeTemplateParmDecl *NTTP
14161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                = dyn_cast<NonTypeTemplateParmDecl>(this))
14210738d36b150aa65206890c1c845cdba076e4200Douglas Gregor    return NTTP->isParameterPack();
14361c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  if (const TemplateTemplateParmDecl *TTP
14461c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(this))
14561c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor    return TTP->isParameterPack();
14667e332009c6e349dc34700f539747afcff990336Anders Carlsson  return false;
14767e332009c6e349dc34700f539747afcff990336Anders Carlsson}
14867e332009c6e349dc34700f539747afcff990336Anders Carlsson
1491fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool Decl::isParameterPack() const {
1501fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
1511fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor    return Parm->isParameterPack();
1521fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
1531fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isTemplateParameterPack();
1541fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
1551fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
156e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool Decl::isFunctionOrFunctionTemplate() const {
1579488ea120e093068021f944176c3d610dd540914John McCall  if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
15858badb7a655d021fc67bb7ed0af95d6ea0c63eb1Anders Carlsson    return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
160e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
161e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
162e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
163ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowskibool Decl::isTemplateDecl() const {
164ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski  return isa<TemplateDecl>(this);
165ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski}
166ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski
167c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidisconst DeclContext *Decl::getParentFunctionOrMethod() const {
168c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  for (const DeclContext *DC = getDeclContext();
169c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis       DC && !DC->isTranslationUnit() && !DC->isNamespace();
17079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor       DC = DC->getParent())
17179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    if (DC->isFunctionOrMethod())
172c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis      return DC;
17379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
174c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  return 0;
17579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor}
17679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
1774c3e0ee8e6208eb42c4adb78a7d35b641fd85ae9Douglas Gregor
17856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
17949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner// PrettyStackTraceDecl Implementation
18049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyStackTraceDecl::print(raw_ostream &OS) const {
18349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation TheLoc = Loc;
18449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isInvalid() && TheDecl)
18549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc = TheDecl->getLocation();
1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isValid()) {
18849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc.print(OS, SM);
18949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << ": ";
19049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  }
19149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
19249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << Message;
19349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
194b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer  if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
195b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    OS << " '";
196b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    DN->printQualifiedName(OS);
197b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer    OS << '\'';
198b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer  }
19949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << '\n';
20049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner}
2011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
20356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// Decl Implementation
20456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
20556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
206da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor// Out-of-line virtual method providing a home for Decl.
207da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas GregorDecl::~Decl() { }
208f4a03cc2b022fab0ffac6c65449555c52036deceDouglas Gregor
2094afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setDeclContext(DeclContext *DC) {
210ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  DeclCtx = DC;
2114afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
2124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2134afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setLexicalDeclContext(DeclContext *DC) {
2144afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (DC == getLexicalDeclContext())
2154afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return;
2164afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2174afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isInSemaDC()) {
2184bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    setDeclContextsImpl(getDeclContext(), DC, getASTContext());
2194afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  } else {
2204afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    getMultipleDC()->LexicalDC = DC;
2214afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
2224afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
2234afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2244bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidisvoid Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
2254bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis                               ASTContext &Ctx) {
2264bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  if (SemaDC == LexicalDC) {
2274bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    DeclCtx = SemaDC;
2284bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  } else {
2294bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
2304bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    MDC->SemanticDC = SemaDC;
2314bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    MDC->LexicalDC = LexicalDC;
2324bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    DeclCtx = MDC;
2334bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  }
2344bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis}
2354bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis
2369aeed32282fe8a775c24c01c923717ca86695685John McCallbool Decl::isInAnonymousNamespace() const {
2379aeed32282fe8a775c24c01c923717ca86695685John McCall  const DeclContext *DC = getDeclContext();
2389aeed32282fe8a775c24c01c923717ca86695685John McCall  do {
2399aeed32282fe8a775c24c01c923717ca86695685John McCall    if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2409aeed32282fe8a775c24c01c923717ca86695685John McCall      if (ND->isAnonymousNamespace())
2419aeed32282fe8a775c24c01c923717ca86695685John McCall        return true;
2429aeed32282fe8a775c24c01c923717ca86695685John McCall  } while ((DC = DC->getParent()));
2439aeed32282fe8a775c24c01c923717ca86695685John McCall
2449aeed32282fe8a775c24c01c923717ca86695685John McCall  return false;
2459aeed32282fe8a775c24c01c923717ca86695685John McCall}
2469aeed32282fe8a775c24c01c923717ca86695685John McCall
2473708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisTranslationUnitDecl *Decl::getTranslationUnitDecl() {
2489b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis  if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
2499b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis    return TUD;
2509b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis
2513708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  DeclContext *DC = getDeclContext();
2523708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  assert(DC && "This decl is not contained in a translation unit!");
2531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2543708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  while (!DC->isTranslationUnit()) {
2553708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    DC = DC->getParent();
2563708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    assert(DC && "This decl is not contained in a translation unit!");
2573708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  }
2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2593708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  return cast<TranslationUnitDecl>(DC);
2603708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2613708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2623708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisASTContext &Decl::getASTContext() const {
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return getTranslationUnitDecl()->getASTContext();
2643708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2653708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2667b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener *Decl::getASTMutationListener() const {
2677b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  return getASTContext().getASTMutationListener();
2687b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis}
2697b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
2702fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramerunsigned Decl::getMaxAlignment() const {
2712fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  if (!hasAttrs())
2722fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer    return 0;
2732fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer
2742fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  unsigned Align = 0;
2752fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  const AttrVec &V = getAttrs();
2762fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  ASTContext &Ctx = getASTContext();
2772fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
2782fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  for (; I != E; ++I)
2792fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer    Align = std::max(Align, I->getAlignment(Ctx));
2802fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  return Align;
2812fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer}
2822fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer
283c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregorbool Decl::isUsed(bool CheckUsedAttr) const {
28412ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  if (Used)
28512ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
28612ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
28712ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  // Check for used attribute.
288c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (CheckUsedAttr && hasAttr<UsedAttr>())
28912ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
290919b7e69781daf68766bb9af11ca1b9f246ce935Rafael Espindola
29112ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  return false;
29212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner}
29312ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
2946b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidisbool Decl::isReferenced() const {
2956b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (Referenced)
2966b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    return true;
2976b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
2986b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  // Check redeclarations.
2996b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
3006b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    if (I->Referenced)
3016b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis      return true;
3026b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
3036b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  return false;
3046b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis}
3056b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
3060a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// \brief Determine the availability of the given declaration based on
3070a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// the target platform.
3080a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor///
3090a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// When it returns an availability result other than \c AR_Available,
3100a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// if the \p Message parameter is non-NULL, it will be set to a
3110a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// string describing why the entity is unavailable.
3120a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor///
3130a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// FIXME: Make these strings localizable, since they end up in
3140a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// diagnostics.
3150a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorstatic AvailabilityResult CheckAvailability(ASTContext &Context,
3160a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                            const AvailabilityAttr *A,
3170a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                            std::string *Message) {
318bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
3195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef PrettyPlatformName
3200a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
3210a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (PrettyPlatformName.empty())
3220a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    PrettyPlatformName = TargetPlatform;
3230a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
324bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
3250a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (TargetMinVersion.empty())
3260a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Available;
3270a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Match the platform name.
3290a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (A->getPlatform()->getName() != TargetPlatform)
3300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Available;
331006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian
332006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  std::string HintMessage;
333006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  if (!A->getMessage().empty()) {
334006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian    HintMessage = " - ";
335006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian    HintMessage += A->getMessage();
336006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  }
337006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian
338b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  // Make sure that this declaration has not been marked 'unavailable'.
339b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  if (A->getUnavailable()) {
340b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    if (Message) {
341b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor      Message->clear();
342b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor      llvm::raw_string_ostream Out(*Message);
343006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian      Out << "not available on " << PrettyPlatformName
344006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << HintMessage;
345b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    }
346b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor
347b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    return AR_Unavailable;
348b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  }
349b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor
3500a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration has already been introduced.
3510a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getIntroduced().empty() &&
3520a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      TargetMinVersion < A->getIntroduced()) {
3530a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3540a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3550a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3560a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "introduced in " << PrettyPlatformName << ' '
357006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getIntroduced() << HintMessage;
3580a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3590a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3600a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_NotYetIntroduced;
3610a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3620a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3630a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration hasn't been obsoleted.
3640a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
3650a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3660a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3670a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3680a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "obsoleted in " << PrettyPlatformName << ' '
369006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getObsoleted() << HintMessage;
3700a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3710a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3720a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Unavailable;
3730a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3740a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3750a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration hasn't been deprecated.
3760a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
3770a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3780a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "first deprecated in " << PrettyPlatformName << ' '
381006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getDeprecated() << HintMessage;
3820a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3830a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Deprecated;
3850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return AR_Available;
3880a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
3890a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3900a0d2b179085a52c10402feebeb6db8b4d96a140Douglas GregorAvailabilityResult Decl::getAvailability(std::string *Message) const {
3910a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  AvailabilityResult Result = AR_Available;
3920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  std::string ResultMessage;
3930a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3940a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
3950a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
3960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Result >= AR_Deprecated)
3970a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        continue;
3980a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Message)
4000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        ResultMessage = Deprecated->getMessage();
4010a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4020a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Result = AR_Deprecated;
4030a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      continue;
4040a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4050a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4060a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
4070a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Message)
4080a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        *Message = Unavailable->getMessage();
4090a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return AR_Unavailable;
4100a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4110a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4120a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
4130a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
4140a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                Message);
4150a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4160a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (AR == AR_Unavailable)
4170a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        return AR_Unavailable;
4180a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4190a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (AR > Result) {
4200a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        Result = AR;
4210a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        if (Message)
4220a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor          ResultMessage.swap(*Message);
4230a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      }
4240a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      continue;
4250a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4260a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
4270a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (Message)
4290a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    Message->swap(ResultMessage);
4300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return Result;
4310a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4320a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4330a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorbool Decl::canBeWeakImported(bool &IsDefinition) const {
4340a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  IsDefinition = false;
435260611a32535c851237926bfcf78869b13c07d5bJohn McCall
436260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Variables, if they aren't definitions.
4370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
438d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola    if (Var->isThisDeclarationADefinition()) {
4390a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      IsDefinition = true;
4400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return false;
4410a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
442260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
443260611a32535c851237926bfcf78869b13c07d5bJohn McCall
444260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Functions, if they aren't definitions.
4450a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
4460a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (FD->hasBody()) {
4470a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      IsDefinition = true;
4480a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return false;
4490a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
450260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
4510a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
452260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Objective-C classes, if this is the non-fragile runtime.
453260611a32535c851237926bfcf78869b13c07d5bJohn McCall  } else if (isa<ObjCInterfaceDecl>(this) &&
4540b92fcb1353d2d8b31b6c485e6caa14568aca43bJohn McCall             getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
455260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
456260611a32535c851237926bfcf78869b13c07d5bJohn McCall
457260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Nothing else.
458260611a32535c851237926bfcf78869b13c07d5bJohn McCall  } else {
459260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return false;
460260611a32535c851237926bfcf78869b13c07d5bJohn McCall  }
4610a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4620a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4630a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorbool Decl::isWeakImported() const {
4640a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  bool IsDefinition;
4650a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!canBeWeakImported(IsDefinition))
4660a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return false;
4670a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4680a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
4690a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (isa<WeakImportAttr>(*A))
4700a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return true;
4710a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4720a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
4730a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (CheckAvailability(getASTContext(), Availability, 0)
4740a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                         == AR_NotYetIntroduced)
4750a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        return true;
4760a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4770a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
4780a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return false;
4800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
48112ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
482769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattnerunsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
483769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  switch (DeclKind) {
4849488ea120e093068021f944176c3d610dd540914John McCall    case Function:
4859488ea120e093068021f944176c3d610dd540914John McCall    case CXXMethod:
4869488ea120e093068021f944176c3d610dd540914John McCall    case CXXConstructor:
4879488ea120e093068021f944176c3d610dd540914John McCall    case CXXDestructor:
4889488ea120e093068021f944176c3d610dd540914John McCall    case CXXConversion:
489769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case EnumConstant:
490769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Var:
491769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ImplicitParam:
492769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ParmVar:
493769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case NonTypeTemplateParm:
494769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCMethod:
495769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProperty:
49676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    case MSProperty:
49700b40d3f2fb8b2f9043daf3dd4558bff98346b3cDaniel Dunbar      return IDNS_Ordinary;
498ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    case Label:
499ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      return IDNS_Label;
50087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    case IndirectField:
50187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return IDNS_Ordinary | IDNS_Member;
50287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
5030d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case ObjCCompatibleAlias:
5040d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case ObjCInterface:
5050d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Type;
5060d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
5070d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case Typedef:
508162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case TypeAlias:
5093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case TypeAliasTemplate:
5100d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case UnresolvedUsingTypename:
5110d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case TemplateTypeParm:
5120d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Type;
5130d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
5149488ea120e093068021f944176c3d610dd540914John McCall    case UsingShadow:
5159488ea120e093068021f944176c3d610dd540914John McCall      return 0; // we'll actually overwrite this later
5169488ea120e093068021f944176c3d610dd540914John McCall
5177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    case UnresolvedUsingValue:
5187ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      return IDNS_Ordinary | IDNS_Using;
5199488ea120e093068021f944176c3d610dd540914John McCall
5209488ea120e093068021f944176c3d610dd540914John McCall    case Using:
5219488ea120e093068021f944176c3d610dd540914John McCall      return IDNS_Using;
5229488ea120e093068021f944176c3d610dd540914John McCall
523769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProtocol:
5248fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCProtocol;
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
526769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Field:
527769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCAtDefsField:
528769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCIvar:
529769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Member;
5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
531769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Record:
532769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case CXXRecord:
533769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Enum:
5340d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Tag | IDNS_Type;
5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
536769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Namespace:
5370d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case NamespaceAlias:
5380d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Namespace;
5390d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
540769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FunctionTemplate:
541ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    case VarTemplate:
5420d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary;
5430d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
544769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplate:
545769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTemplateParm:
5460d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
548769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Never have names.
54902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    case Friend:
550dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    case FriendTemplate:
5516206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    case AccessSpec:
552769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case LinkageSpec:
553769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FileScopeAsm:
554769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case StaticAssert:
555769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCPropertyImpl:
556769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Block:
5576afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    case Captured:
558769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TranslationUnit:
559769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
560769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case UsingDirective:
561769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplateSpecialization:
562c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    case ClassTemplatePartialSpecialization:
563af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    case ClassScopeFunctionSpecialization:
564ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    case VarTemplateSpecialization:
565ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    case VarTemplatePartialSpecialization:
566bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCImplementation:
567bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCCategory:
568bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCCategoryImpl:
56915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    case Import:
570c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    case OMPThreadPrivate:
571684aa73192d92850a926870be62a1787eb5b7ed9Michael Han    case Empty:
572bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor      // Never looked up by name.
573769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return 0;
574769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  }
5759488ea120e093068021f944176c3d610dd540914John McCall
5763026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid DeclKind!");
57756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
57856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
5794bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidisvoid Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
5801715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  assert(!HasAttrs && "Decl already contains attrs.");
5811715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
5824bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
583cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  assert(AttrBlank.empty() && "HasAttrs was wrong?");
5841715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
5851715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  AttrBlank = attrs;
5861715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  HasAttrs = true;
5871715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis}
5881715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
589cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntvoid Decl::dropAttrs() {
59056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasAttrs) return;
5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = false;
59340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  getASTContext().eraseDeclAttrs(this);
59456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
59556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
596cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntconst AttrVec &Decl::getAttrs() const {
597cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  assert(HasAttrs && "No attrs to get!");
59840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  return getASTContext().getDeclAttrs(this);
59956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
60056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
60142220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDecl *Decl::castFromDeclContext (const DeclContext *D) {
6023d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getDeclKind();
6033d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
6049a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6059a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) \
6069a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    case Decl::NAME:       \
6079a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
6089a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
6099a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6103d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
6119a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6129a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                  \
6139a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (DK >= first##NAME && DK <= last##NAME) \
6149a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
6159a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
616b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("a decl that inherits DeclContext isn't handled");
6173d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
61842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
61942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
62042220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDeclContext *Decl::castToDeclContext(const Decl *D) {
6213d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getKind();
6223d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
6239a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6249a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) \
6259a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    case Decl::NAME:       \
6269a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
6279a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
6289a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6293d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
6309a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6319a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                                   \
6329a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (DK >= first##NAME && DK <= last##NAME)                  \
6339a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
6349a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
635b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("a decl that inherits DeclContext isn't handled");
6363d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
63742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
63842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
6396fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisSourceLocation Decl::getBodyRBrace() const {
64006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
64106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // FunctionDecl stores EndRangeLoc for this purpose.
64206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
64306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    const FunctionDecl *Definition;
64406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
64506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return Definition->getSourceRange().getEnd();
64606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    return SourceLocation();
64706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
64806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
6496717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis  if (Stmt *Body = getBody())
6506717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis    return Body->getSourceRange().getEnd();
6516717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis
6526717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis  return SourceLocation();
653d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl}
654d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
6551329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlssonvoid Decl::CheckAccessDeclContext() const {
6563a1c36c099df4dcc94d1d24516a8a2c809c764d4Douglas Gregor#ifndef NDEBUG
65746460a68f6508775e98c19b4bb8454bb471aac24John McCall  // Suppress this check if any of the following hold:
65846460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 1. this is the translation unit (and thus has no parent)
65946460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 2. this is a template parameter (and thus doesn't belong to its context)
660d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 3. this is a non-type template parameter
661d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 4. the context is not a record
662d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 5. it's invalid
663d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 6. it's a C++0x static_assert.
66435eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson  if (isa<TranslationUnitDecl>(this) ||
66504aed0edee1fe0bad807afb69f484c5e807800afArgyrios Kyrtzidis      isa<TemplateTypeParmDecl>(this) ||
666d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      isa<NonTypeTemplateParmDecl>(this) ||
667fdd8ab11bc40c7f206c8f1e892faa002cc6536b1Douglas Gregor      !isa<CXXRecordDecl>(getDeclContext()) ||
66865b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      isInvalidDecl() ||
66965b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      isa<StaticAssertDecl>(this) ||
67065b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
67165b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      // as DeclContext (?).
672d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      isa<ParmVarDecl>(this) ||
673d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
674d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      // AS_none as access specifier.
675bc84532e762a41141bd94037cd5d1133f234088eFrancois Pichet      isa<CXXRecordDecl>(this) ||
676bc84532e762a41141bd94037cd5d1133f234088eFrancois Pichet      isa<ClassScopeFunctionSpecializationDecl>(this))
67735eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson    return;
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(Access != AS_none &&
6801329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson         "Access specifier is AS_none inside a record decl");
6813a1c36c099df4dcc94d1d24516a8a2c809c764d4Douglas Gregor#endif
6821329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson}
6831329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
684f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallstatic Decl::Kind getKind(const Decl *D) { return D->getKind(); }
685f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallstatic Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
686f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall
687f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall/// Starting at a given context (a Decl or DeclContext), look for a
688f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall/// code context that is not a closure (a lambda, block, etc.).
689f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCalltemplate <class T> static Decl *getNonClosureContext(T *D) {
690f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  if (getKind(D) == Decl::CXXMethod) {
691f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
692000817b3b2ab95c5b9cbb76950cd01b79fe93f55John McCall    if (MD->getOverloadedOperator() == OO_Call &&
693000817b3b2ab95c5b9cbb76950cd01b79fe93f55John McCall        MD->getParent()->isLambda())
694f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall      return getNonClosureContext(MD->getParent()->getParent());
695f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return MD;
696f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
697f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return FD;
698f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
699f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return MD;
700f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
701f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return getNonClosureContext(BD->getParent());
702f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
703f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return getNonClosureContext(CD->getParent());
704f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  } else {
705f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall    return 0;
706f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  }
7074b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
7084b9c2d235fb9449e249d74f48ecfec601650de93John McCall
709f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallDecl *Decl::getNonClosureContext() {
710f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  return ::getNonClosureContext(this);
711f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall}
712aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall
713f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallDecl *DeclContext::getNonClosureAncestor() {
714f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  return ::getNonClosureContext(this);
715aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall}
7161329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
71756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
71856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// DeclContext Implementation
71956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
72056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
7213d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidisbool DeclContext::classof(const Decl *D) {
7223d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch (D->getKind()) {
7239a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
7249a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) case Decl::NAME:
7259a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
7269a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
7273d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return true;
7283d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
7299a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
7309a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                 \
7319a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (D->getKind() >= Decl::first##NAME &&  \
7329a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt          D->getKind() <= Decl::last##NAME)     \
7333d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return true;
7349a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
7353d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return false;
7363d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
7373d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis}
7383d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis
739a2da780b325e78c6c6bbbb766459a73243c3cf9eDouglas GregorDeclContext::~DeclContext() { }
74044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
741e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// \brief Find the parent context of this context that will be
742e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// used for unqualified name lookup.
743e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor///
744e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// Generally, the parent lookup context is the semantic context. However, for
745e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// a friend function the parent lookup context is the lexical context, which
746e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// is the class in which the friend is declared.
747e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas GregorDeclContext *DeclContext::getLookupParent() {
748e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  // FIXME: Find a better way to identify friends
749e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  if (isa<FunctionDecl>(this))
7507a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (getParent()->getRedeclContext()->isFileContext() &&
7517a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        getLexicalParent()->getRedeclContext()->isRecord())
752e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor      return getLexicalParent();
753e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
754e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  return getParent();
755e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor}
756e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
757410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redlbool DeclContext::isInlineNamespace() const {
758410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  return isNamespace() &&
759410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl         cast<NamespaceDecl>(this)->isInline();
760410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl}
761410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl
762bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregorbool DeclContext::isDependentContext() const {
763bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (isFileContext())
764bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    return false;
765c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
766c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  if (isa<ClassTemplatePartialSpecializationDecl>(this))
767c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return true;
768bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
769f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor  if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
770bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Record->getDescribedClassTemplate())
771bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
772f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor
773f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor    if (Record->isDependentLambda())
774f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor      return true;
775f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor  }
776f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor
7770c01d18094100db92d38daa923c95661512db203John McCall  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
778bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Function->getDescribedFunctionTemplate())
779bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
7801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7810c01d18094100db92d38daa923c95661512db203John McCall    // Friend function declarations are dependent if their *lexical*
7820c01d18094100db92d38daa923c95661512db203John McCall    // context is dependent.
7830c01d18094100db92d38daa923c95661512db203John McCall    if (cast<Decl>(this)->getFriendObjectKind())
7840c01d18094100db92d38daa923c95661512db203John McCall      return getLexicalParent()->isDependentContext();
7850c01d18094100db92d38daa923c95661512db203John McCall  }
7860c01d18094100db92d38daa923c95661512db203John McCall
787bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  return getParent() && getParent()->isDependentContext();
788bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor}
789bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
790074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregorbool DeclContext::isTransparentContext() const {
791074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (DeclKind == Decl::Enum)
7921274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return !cast<EnumDecl>(this)->isScoped();
793074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::LinkageSpec)
794074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true;
795074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
796074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  return false;
797074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
798074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
7997a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redlbool DeclContext::Encloses(const DeclContext *DC) const {
8006dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  if (getPrimaryContext() != this)
8016dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    return getPrimaryContext()->Encloses(DC);
8021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8036dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  for (; DC; DC = DC->getParent())
8046dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    if (DC->getPrimaryContext() == this)
8056dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor      return true;
8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
8076dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor}
8086dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor
8090701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve NaroffDeclContext *DeclContext::getPrimaryContext() {
81044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
81144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::TranslationUnit:
812074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::LinkageSpec:
8131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Decl::Block:
8146afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  case Decl::Captured:
81544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // There is only one DeclContext for these entities.
81644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
81744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
81844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
81944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // The original namespace is our primary context.
82044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
82144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
82244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCMethod:
82344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
82444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
82544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCInterface:
82653df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor    if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
82753df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor      return Def;
82853df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
82953df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor    return this;
83053df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
8310701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCProtocol:
8321d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
8331d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor      return Def;
8341d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor
8351d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    return this;
83653df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
8370701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategory:
83844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
83944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
8400701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCImplementation:
8410701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategoryImpl:
8420701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff    return this;
8430701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff
84444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
8459a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
846cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // If this is a tag type that has a definition or is currently
847cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // being defined, that definition is our primary context.
8483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      TagDecl *Tag = cast<TagDecl>(this);
8493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TagType>(Tag->TypeForDecl) ||
8503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             isa<InjectedClassNameType>(Tag->TypeForDecl));
8513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (TagDecl *Def = Tag->getDefinition())
8533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        return Def;
8543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
8563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
8573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (TagTy->isBeingDefined())
8583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          // FIXME: is it necessarily being defined in the decl
8593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          // that owns the type?
8603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return TagTy->getDecl();
8613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      }
8623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return Tag;
864cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    }
865cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
8669a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
86744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          "Unknown DeclContext kind");
86844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
86944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
87044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
87144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
872f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregorvoid
873cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri GribenkoDeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
874f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  Contexts.clear();
875f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
876f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  if (DeclKind != Decl::Namespace) {
877f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    Contexts.push_back(this);
878f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    return;
87944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
880f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
881f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
882ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
883ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor       N = N->getPreviousDecl())
884f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    Contexts.push_back(N);
885f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
886f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  std::reverse(Contexts.begin(), Contexts.end());
88744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
88844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
889eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisstd::pair<Decl *, Decl *>
890341785ec52f87c0803ba52dc88faac4e136f8593Bill WendlingDeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
891ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                            bool FieldsAlreadyLoaded) {
89246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  // Build up a chain of declarations via the Decl::NextInContextAndBits field.
893eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *FirstNewDecl = 0;
894eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *PrevDecl = 0;
895eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
896ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis    if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
897ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis      continue;
898ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis
899eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    Decl *D = Decls[I];
900eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    if (PrevDecl)
90146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      PrevDecl->NextInContextAndBits.setPointer(D);
902eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    else
903eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis      FirstNewDecl = D;
904eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
905eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    PrevDecl = D;
906eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  }
907eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
908eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return std::make_pair(FirstNewDecl, PrevDecl);
909eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
910eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
911bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith/// \brief We have just acquired external visible storage, and we already have
912bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith/// built a lookup map. For every name in the map, pull in the new names from
913bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith/// the external storage.
914bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smithvoid DeclContext::reconcileExternalVisibleStorage() {
9158896339a3c654dc44877d52a926eb89ab3ef30b5Richard Smith  assert(NeedToReconcileExternalVisibleStorage && LookupPtr.getPointer());
916bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  NeedToReconcileExternalVisibleStorage = false;
917bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith
918bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  StoredDeclsMap &Map = *LookupPtr.getPointer();
919bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  ExternalASTSource *Source = getParentASTContext().getExternalSource();
920bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  for (StoredDeclsMap::iterator I = Map.begin(); I != Map.end(); ++I) {
921bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    I->second.removeExternalDecls();
922bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    Source->FindExternalVisibleDeclsByName(this, I->first);
923bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  }
924bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith}
925bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith
9262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Load the declarations within this lexical storage from an
9272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// external source.
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
92917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::LoadLexicalDeclsFromExternalStorage() const {
93017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  ExternalASTSource *Source = getParentASTContext().getExternalSource();
9312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalLexicalStorage() && Source && "No external storage?");
9322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9330dbbc042518e6ba903dd6f815cbb8704595cc937Argyrios Kyrtzidis  // Notify that we have a DeclContext that is initializing.
9340dbbc042518e6ba903dd6f815cbb8704595cc937Argyrios Kyrtzidis  ExternalASTSource::Deserializing ADeclContext(Source);
9359fc18c97991b1267221ee71d13d8fb2f036b387bDouglas Gregor
936ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  // Load the external declarations, if any.
9375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 64> Decls;
9382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalLexicalStorage = false;
939ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  switch (Source->FindExternalLexicalDecls(this, Decls)) {
940ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Success:
941ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    break;
942ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor
943ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Failure:
944ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_AlreadyLoaded:
945ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    return;
946ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  }
9472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Decls.empty())
9492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
9502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
951ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  // We may have already loaded just the fields of this record, in which case
952ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  // we need to ignore them.
953ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  bool FieldsAlreadyLoaded = false;
954ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
955ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis    FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
956ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis
9572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Splice the newly-read declarations into the beginning of the list
9582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // of declarations.
959eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *ExternalFirst, *ExternalLast;
960ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
961ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                                                          FieldsAlreadyLoaded);
96246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
963eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  FirstDecl = ExternalFirst;
9642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!LastDecl)
965eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LastDecl = ExternalLast;
9662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
9672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
96876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallDeclContext::lookup_result
96976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
97076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall                                                    DeclarationName Name) {
97176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  ASTContext &Context = DC->getParentASTContext();
97276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsMap *Map;
973c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(Map = DC->LookupPtr.getPointer()))
97476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    Map = DC->CreateStoredDeclsMap(Context);
97576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
976bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  // Add an entry to the map for this name, if it's not already present.
977bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  (*Map)[Name];
97876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
97976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  return DeclContext::lookup_result();
98076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall}
9812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
98276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallDeclContext::lookup_result
98376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
98476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall                                                  DeclarationName Name,
98545df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis                                                  ArrayRef<NamedDecl*> Decls) {
9861ad23d62007162df82b58bca31b4aa277a5f6586Dmitri Gribenko  ASTContext &Context = DC->getParentASTContext();
98776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsMap *Map;
988c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(Map = DC->LookupPtr.getPointer()))
98976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    Map = DC->CreateStoredDeclsMap(Context);
9902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
99176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsList &List = (*Map)[Name];
992ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith
993ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  // Clear out any old external visible declarations, to avoid quadratic
994ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  // performance in the redeclaration checks below.
995ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  List.removeExternalDecls();
996ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith
997ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  if (!List.isNull()) {
998ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // We have both existing declarations and new declarations for this name.
999ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // Some of the declarations may simply replace existing ones. Handle those
1000ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // first.
1001ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    llvm::SmallVector<unsigned, 8> Skip;
1002ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    for (unsigned I = 0, N = Decls.size(); I != N; ++I)
1003ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      if (List.HandleRedeclaration(Decls[I]))
1004ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        Skip.push_back(I);
1005ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    Skip.push_back(Decls.size());
1006ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith
1007ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // Add in any new declarations.
1008ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    unsigned SkipPos = 0;
1009ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1010ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      if (I == Skip[SkipPos])
1011ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        ++SkipPos;
1012ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      else
1013ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        List.AddSubsequentDecl(Decls[I]);
1014ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    }
1015ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith  } else {
1016ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    // Convert the array to a StoredDeclsList.
1017ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    for (ArrayRef<NamedDecl*>::iterator
1018ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith           I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1019ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      if (List.isNull())
1020ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        List.setOnlyValue(*I);
1021ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith      else
1022ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith        List.AddSubsequentDecl(*I);
1023ddb10f767604d8efa5e491076d6fdd23a19db86cRichard Smith    }
102476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  }
102576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
1026074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  return List.getLookupResult();
102776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall}
102876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
1029681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian RedlDeclContext::decl_iterator DeclContext::noload_decls_begin() const {
1030681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  return decl_iterator(FirstDecl);
1031681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl}
1032681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl
103317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::decl_iterator DeclContext::decls_begin() const {
10342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
103517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
10362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return decl_iterator(FirstDecl);
10386ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
10396ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
104017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidisbool DeclContext::decls_empty() const {
10418038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  if (hasExternalLexicalStorage())
104217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
10438038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
10448038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  return !FirstDecl;
10458038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor}
10468038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
1047cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callananbool DeclContext::containsDecl(Decl *D) const {
1048cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan  return (D->getLexicalDeclContext() == this &&
1049cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan          (D->NextInContextAndBits.getPointer() || D == LastDecl));
1050cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan}
1051cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan
10529f54ad4381370c6b771424b53d219e661d6d6706John McCallvoid DeclContext::removeDecl(Decl *D) {
10539f54ad4381370c6b771424b53d219e661d6d6706John McCall  assert(D->getLexicalDeclContext() == this &&
10549f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl being removed from non-lexical context");
105546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
10569f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl is not in decls list");
10579f54ad4381370c6b771424b53d219e661d6d6706John McCall
10589f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the decl chain.  This is O(n) but hopefully rare.
10599f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (D == FirstDecl) {
10609f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (D == LastDecl)
10619f54ad4381370c6b771424b53d219e661d6d6706John McCall      FirstDecl = LastDecl = 0;
10629f54ad4381370c6b771424b53d219e661d6d6706John McCall    else
106346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      FirstDecl = D->NextInContextAndBits.getPointer();
10649f54ad4381370c6b771424b53d219e661d6d6706John McCall  } else {
106546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
10669f54ad4381370c6b771424b53d219e661d6d6706John McCall      assert(I && "decl not found in linked list");
106746cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      if (I->NextInContextAndBits.getPointer() == D) {
106846cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor        I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
10699f54ad4381370c6b771424b53d219e661d6d6706John McCall        if (D == LastDecl) LastDecl = I;
10709f54ad4381370c6b771424b53d219e661d6d6706John McCall        break;
10719f54ad4381370c6b771424b53d219e661d6d6706John McCall      }
10729f54ad4381370c6b771424b53d219e661d6d6706John McCall    }
10739f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
10749f54ad4381370c6b771424b53d219e661d6d6706John McCall
10759f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Mark that D is no longer in the decl chain.
107646cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  D->NextInContextAndBits.setPointer(0);
10779f54ad4381370c6b771424b53d219e661d6d6706John McCall
10789f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the lookup table if necessary.
10799f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (isa<NamedDecl>(D)) {
10809f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *ND = cast<NamedDecl>(D);
10819f54ad4381370c6b771424b53d219e661d6d6706John McCall
108202368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann    // Remove only decls that have a name
108302368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann    if (!ND->getDeclName()) return;
108402368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann
1085c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
10860c01d18094100db92d38daa923c95661512db203John McCall    if (!Map) return;
10879f54ad4381370c6b771424b53d219e661d6d6706John McCall
10889f54ad4381370c6b771424b53d219e661d6d6706John McCall    StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
10899f54ad4381370c6b771424b53d219e661d6d6706John McCall    assert(Pos != Map->end() && "no lookup entry for decl");
1090d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann    if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1091d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann      Pos->second.remove(ND);
10929f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
10939f54ad4381370c6b771424b53d219e661d6d6706John McCall}
10949f54ad4381370c6b771424b53d219e661d6d6706John McCall
10953f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addHiddenDecl(Decl *D) {
10967f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner  assert(D->getLexicalDeclContext() == this &&
10977f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner         "Decl inserted into wrong lexical context");
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!D->getNextDeclInContext() && D != LastDecl &&
10996037fcba3431b47de1a994c9b286feac17894effDouglas Gregor         "Decl already inserted into a DeclContext");
11006037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
11016037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  if (FirstDecl) {
110246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    LastDecl->NextInContextAndBits.setPointer(D);
11036037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    LastDecl = D;
11046037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  } else {
11056037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    FirstDecl = LastDecl = D;
11066037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  }
110727c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor
110827c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  // Notify a C++ record declaration that we've added a member, so it can
110927c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  // update it's class-specific state.
111027c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
111127c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor    Record->addedMember(D);
1112e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
1113e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  // If this is a newly-created (not de-serialized) import declaration, wire
1114e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  // it in to the list of local import declarations.
1115e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!D->isFromASTFile()) {
1116e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1117e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor      D->getASTContext().addedLocalImportDecl(Import);
1118e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
11193f9a8a60614b763785d54ad08821745d03a4af70John McCall}
11203f9a8a60614b763785d54ad08821745d03a4af70John McCall
11213f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addDecl(Decl *D) {
11223f9a8a60614b763785d54ad08821745d03a4af70John McCall  addHiddenDecl(D);
11234afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
11244afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1125c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ND->getDeclContext()->getPrimaryContext()->
1126c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(ND, false, true);
112744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
112844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
11299faf810f08132aabb34a478297dfeea89c3bbe17Sean Callananvoid DeclContext::addDeclInternal(Decl *D) {
11309faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  addHiddenDecl(D);
11319faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
11329faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1133c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ND->getDeclContext()->getPrimaryContext()->
1134c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(ND, true, true);
1135c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1136c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1137c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// shouldBeHidden - Determine whether a declaration which was declared
1138c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// within its semantic context should be invisible to qualified name lookup.
1139c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithstatic bool shouldBeHidden(NamedDecl *D) {
1140c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip unnamed declarations.
1141c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!D->getDeclName())
1142c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1143c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1144c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip entities that can't be found by name lookup into a particular
1145c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // context.
1146c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1147c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      D->isTemplateParameter())
1148c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1149c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1150c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip template specializations.
1151c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // FIXME: This feels like a hack. Should DeclarationName support
1152c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // template-ids, or is there a better way to keep specializations
1153c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // from being visible?
1154c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (isa<ClassTemplateSpecializationDecl>(D))
1155c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1156c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1157c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (FD->isFunctionTemplateSpecialization())
1158c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      return true;
1159c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1160c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return false;
1161c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1162c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1163c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// buildLookup - Build the lookup data structure with all of the
1164c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// declarations in this DeclContext (and any other contexts linked
1165c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// to it or transparent contexts nested within it) and return it.
1166c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard SmithStoredDeclsMap *DeclContext::buildLookup() {
1167c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1168c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
11698896339a3c654dc44877d52a926eb89ab3ef30b5Richard Smith  // FIXME: Should we keep going if hasExternalVisibleStorage?
1170c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!LookupPtr.getInt())
1171c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return LookupPtr.getPointer();
1172c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1173cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<DeclContext *, 2> Contexts;
1174c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  collectAllContexts(Contexts);
1175c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1176e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    buildLookupImpl<&DeclContext::decls_begin,
1177e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith                    &DeclContext::decls_end>(Contexts[I]);
1178c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1179c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // We no longer have any lazy decls.
1180c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  LookupPtr.setInt(false);
11818896339a3c654dc44877d52a926eb89ab3ef30b5Richard Smith  NeedToReconcileExternalVisibleStorage = false;
1182c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return LookupPtr.getPointer();
1183c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1184c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1185c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// buildLookupImpl - Build part of the lookup data structure for the
1186c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// declarations contained within DCtx, which will either be this
1187c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// DeclContext, a DeclContext linked to it, or a transparent context
1188c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// nested within it.
1189e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smithtemplate<DeclContext::decl_iterator (DeclContext::*Begin)() const,
1190e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith         DeclContext::decl_iterator (DeclContext::*End)() const>
1191c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::buildLookupImpl(DeclContext *DCtx) {
1192e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  for (decl_iterator I = (DCtx->*Begin)(), E = (DCtx->*End)();
1193c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith       I != E; ++I) {
1194c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Decl *D = *I;
1195c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1196c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // Insert this declaration into the lookup structure, but only if
1197c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // it's semantically within its decl context. Any other decls which
1198c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // should be found in this context are added eagerly.
1199096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    //
1200096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    // If it's from an AST file, don't add it now. It'll get handled by
1201096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1202096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    // in C++, we do not track external visible decls for the TU, so in
1203096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith    // that case we need to collect them all here.
1204c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1205096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith      if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1206096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith          (!ND->isFromASTFile() ||
1207096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith           (isTranslationUnit() &&
1208096a394f3d5b4938640e1ac92d9b439a2bad0a23Richard Smith            !getParentASTContext().getLangOpts().CPlusPlus)))
1209c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextImpl(ND, false);
1210c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1211c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // If this declaration is itself a transparent declaration context
1212c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // or inline namespace, add the members of this declaration of that
1213c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // context (recursively).
1214c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1215c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1216e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith        buildLookupImpl<Begin, End>(InnerCtx);
1217c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
12189faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
12199faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::lookup_result
122117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::lookup(DeclarationName Name) {
122265daef179790a02eab1b5a989f53984375a06483Nick Lewycky  assert(DeclKind != Decl::LinkageSpec &&
122365daef179790a02eab1b5a989f53984375a06483Nick Lewycky         "Should not perform lookups into linkage specs!");
122465daef179790a02eab1b5a989f53984375a06483Nick Lewycky
12250701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
122644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this)
122717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    return PrimaryContext->lookup(Name);
122844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
12291b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  if (hasExternalVisibleStorage()) {
1230bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    StoredDeclsMap *Map = LookupPtr.getPointer();
1231bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    if (LookupPtr.getInt())
1232bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith      Map = buildLookup();
12338896339a3c654dc44877d52a926eb89ab3ef30b5Richard Smith    else if (NeedToReconcileExternalVisibleStorage)
12348896339a3c654dc44877d52a926eb89ab3ef30b5Richard Smith      reconcileExternalVisibleStorage();
1235bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith
12362bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith    if (!Map)
12372bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith      Map = CreateStoredDeclsMap(getParentASTContext());
12382bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith
1239bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    // If a PCH/module has a result for this name, and we have a local
1240bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    // declaration, we will have imported the PCH/module result when adding the
1241bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith    // local declaration or when reconciling the module.
12422bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith    std::pair<StoredDeclsMap::iterator, bool> R =
12432bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith        Map->insert(std::make_pair(Name, StoredDeclsList()));
12442bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith    if (!R.second)
12452bb07c1dfeda50d7edcee512932b86a1a65c6e69Richard Smith      return R.first->second.getLookupResult();
1246c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
124776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    ExternalASTSource *Source = getParentASTContext().getExternalSource();
12483646c68676c3c46a026b23d52188ef6e0d856178Richard Smith    if (Source->FindExternalVisibleDeclsByName(this, Name)) {
12493646c68676c3c46a026b23d52188ef6e0d856178Richard Smith      if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
12503646c68676c3c46a026b23d52188ef6e0d856178Richard Smith        StoredDeclsMap::iterator I = Map->find(Name);
12513646c68676c3c46a026b23d52188ef6e0d856178Richard Smith        if (I != Map->end())
12523646c68676c3c46a026b23d52188ef6e0d856178Richard Smith          return I->second.getLookupResult();
12533646c68676c3c46a026b23d52188ef6e0d856178Richard Smith      }
12543646c68676c3c46a026b23d52188ef6e0d856178Richard Smith    }
12553646c68676c3c46a026b23d52188ef6e0d856178Richard Smith
12563646c68676c3c46a026b23d52188ef6e0d856178Richard Smith    return lookup_result(lookup_iterator(0), lookup_iterator(0));
125776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  }
12582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1259c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap *Map = LookupPtr.getPointer();
1260c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (LookupPtr.getInt())
1261c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Map = buildLookup();
1262c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1263c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Map)
1264c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return lookup_result(lookup_iterator(0), lookup_iterator(0));
1265c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1266c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap::iterator I = Map->find(Name);
1267c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (I == Map->end())
1268c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return lookup_result(lookup_iterator(0), lookup_iterator(0));
1269c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1270c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return I->second.getLookupResult();
127144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
127244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1273e7bae1597f4a7088f5048695c14a8f1013a86108Richard SmithDeclContext::lookup_result
1274e7bae1597f4a7088f5048695c14a8f1013a86108Richard SmithDeclContext::noload_lookup(DeclarationName Name) {
1275e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  assert(DeclKind != Decl::LinkageSpec &&
1276e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith         "Should not perform lookups into linkage specs!");
1277e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  if (!hasExternalVisibleStorage())
1278e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    return lookup(Name);
1279e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1280e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  DeclContext *PrimaryContext = getPrimaryContext();
1281e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  if (PrimaryContext != this)
1282e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    return PrimaryContext->noload_lookup(Name);
1283e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1284e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  StoredDeclsMap *Map = LookupPtr.getPointer();
1285e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  if (LookupPtr.getInt()) {
1286e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    // Carefully build the lookup map, without deserializing anything.
1287e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    SmallVector<DeclContext *, 2> Contexts;
1288e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    collectAllContexts(Contexts);
1289e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1290e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith      buildLookupImpl<&DeclContext::noload_decls_begin,
1291e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith                      &DeclContext::noload_decls_end>(Contexts[I]);
1292e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1293e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    // We no longer have any lazy decls.
1294e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    LookupPtr.setInt(false);
1295e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1296e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    // There may now be names for which we have local decls but are
1297e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    // missing the external decls.
1298e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    NeedToReconcileExternalVisibleStorage = true;
1299e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1300e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    Map = LookupPtr.getPointer();
1301e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  }
1302e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1303e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  if (!Map)
1304e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith    return lookup_result(lookup_iterator(0), lookup_iterator(0));
1305e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1306e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  StoredDeclsMap::iterator I = Map->find(Name);
1307e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  return I != Map->end()
1308e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith             ? I->second.getLookupResult()
1309e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith             : lookup_result(lookup_iterator(0), lookup_iterator(0));
1310e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith}
1311e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1312cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenkovoid DeclContext::localUncachedLookup(DeclarationName Name,
1313cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                      SmallVectorImpl<NamedDecl *> &Results) {
1314b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  Results.clear();
1315b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1316b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // If there's no external storage, just perform a normal lookup and copy
1317b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // the results.
131893ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor  if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
1319b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    lookup_result LookupResults = lookup(Name);
13203bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1321b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    return;
1322b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1323b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1324b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // If we have a lookup table, check there first. Maybe we'll get lucky.
1325bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  if (Name && !LookupPtr.getInt()) {
132693ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor    if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
132793ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      StoredDeclsMap::iterator Pos = Map->find(Name);
132893ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      if (Pos != Map->end()) {
132993ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor        Results.insert(Results.end(),
13303bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie                       Pos->second.getLookupResult().begin(),
13313bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie                       Pos->second.getLookupResult().end());
133293ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor        return;
133393ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      }
1334b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    }
1335b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
133693ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor
1337b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // Slow case: grovel through the declarations in our chain looking for
1338b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // matches.
1339b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1340b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1341b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor      if (ND->getDeclName() == Name)
1342b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor        Results.push_back(ND);
1343b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1344b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor}
1345b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
13467a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian RedlDeclContext *DeclContext::getRedeclContext() {
13470cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *Ctx = this;
1348410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  // Skip through transparent contexts.
1349410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  while (Ctx->isTransparentContext())
1350ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor    Ctx = Ctx->getParent();
1351ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  return Ctx;
1352ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor}
1353ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor
135488b7094185b9d4fe9820c731b6936d8d37f6143eDouglas GregorDeclContext *DeclContext::getEnclosingNamespaceContext() {
135588b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *Ctx = this;
135688b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  // Skip through non-namespace, non-translation-unit contexts.
135751a8a378012b5d6a1306fdd75bd135fea3e23b7bSebastian Redl  while (!Ctx->isFileContext())
135888b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    Ctx = Ctx->getParent();
135988b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  return Ctx->getPrimaryContext();
136088b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor}
136188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
13627a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redlbool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
13637a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  // For non-file contexts, this is equivalent to Equals.
13647a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  if (!isFileContext())
13657a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    return O->Equals(this);
13667a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
13677a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  do {
13687a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (O->Equals(this))
13697a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl      return true;
13707a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
13717a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
13727a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (!NS || !NS->isInline())
13737a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl      break;
13747a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    O = NS->getParent();
13757a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  } while (O);
13767a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
13777a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  return false;
13787a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl}
13797a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
1380c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1381c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclContext *PrimaryDC = this->getPrimaryContext();
1382c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1383c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If the decl is being added outside of its semantic decl context, we
1384c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // need to ensure that we eagerly build the lookup information for it.
1385c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
13869faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
13879faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
1388c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1389c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith                                                    bool Recoverable) {
1390c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(this == getPrimaryContext() && "expected a primary DC");
139144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
13921b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // Skip declarations within functions.
13931b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // FIXME: We shouldn't need to build lookup tables for function declarations
13941b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // ever, and we can't do so correctly because we can't model the nesting of
13951b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // scopes which occurs within functions. We use "qualified" lookup into
13961b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // function declarations when handling friend declarations inside nested
13971b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // classes, and consequently accept the following invalid code:
13981b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  //
13991b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  //   void f() { void g(); { int g; struct S { friend void g(); }; } }
14001b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1401cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
1402cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
1403c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip declarations which should be invisible to name lookup.
1404c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (shouldBeHidden(D))
1405c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
1406c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1407c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If we already have a lookup data structure, perform the insertion into
1408c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // it. If we might have externally-stored decls with this name, look them
1409c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // up and perform the insertion. If this decl was declared outside its
1410c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // semantic context, buildLookup won't add it, so add it now.
1411c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  //
1412c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // FIXME: As a performance hack, don't add such decls into the translation
1413c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // unit unless we're in C++, since qualified lookup into the TU is never
1414c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // performed.
1415c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1416c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1417c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith       (getParentASTContext().getLangOpts().CPlusPlus ||
1418c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        !isTranslationUnit()))) {
1419c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // If we have lazily omitted any decls, they might have the same name as
1420c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // the decl which we are adding, so build a full lookup table before adding
1421c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // this decl.
1422c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    buildLookup();
1423c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    makeDeclVisibleInContextImpl(D, Internal);
1424c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  } else {
1425c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    LookupPtr.setInt(true);
1426c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
1427c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1428c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If we are a transparent context or inline namespace, insert into our
1429c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // parent context, too. This operation is recursive.
1430c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (isTransparentContext() || isInlineNamespace())
1431c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    getParent()->getPrimaryContext()->
1432c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1433c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1434c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  Decl *DCAsDecl = cast<Decl>(this);
1435c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Notify that a decl was made visible unless we are a Tag being defined.
1436c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1437c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1438c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      L->AddedVisibleDecl(this, D);
1439c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1440c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1441c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1442c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Find or create the stored declaration map.
1443c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap *Map = LookupPtr.getPointer();
1444c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Map) {
1445c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ASTContext *C = &getParentASTContext();
1446c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Map = CreateStoredDeclsMap(*C);
14475586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  }
14485586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis
1449074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If there is an external AST source, load any declarations it knows about
1450074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // with this declaration's name.
1451074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If the lookup table contains an entry about this name it means that we
1452074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // have already checked the external source.
14539faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  if (!Internal)
14549faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
14559faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan      if (hasExternalVisibleStorage() &&
1456c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith          Map->find(D->getDeclName()) == Map->end())
14579faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan        Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1458074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
145944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // Insert this declaration into the map.
1460c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
146167762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  if (DeclNameEntries.isNull()) {
146267762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    DeclNameEntries.setOnlyValue(D);
1463c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
1464c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
1465c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1466c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (DeclNameEntries.HandleRedeclaration(D)) {
14671b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    // This declaration has replaced an existing one for which
14681b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    // declarationReplaces returns true.
1469c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
147044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
147191942501b6f71a41d3a09bedec19be479832c718Chris Lattner
1472c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Put this declaration into the appropriate slot.
1473c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclNameEntries.AddSubsequentDecl(D);
147444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
14752a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
14762a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
14772a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// this context.
14781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::udir_iterator_range
147917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::getUsingDirectives() const {
14801b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // FIXME: Use something more efficient than normal lookup for using
14811b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // directives. In C++, using directives are looked up more than anything else.
148217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
14833bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie  return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.begin()),
14843bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie                             reinterpret_cast<udir_iterator>(Result.end()));
14852a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor}
14862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
14873478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
14883478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek// Creation and Destruction of StoredDeclsMaps.                               //
14893478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
14903478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
14910c01d18094100db92d38daa923c95661512db203John McCallStoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1492c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(!LookupPtr.getPointer() && "context already has a decls map");
14930c01d18094100db92d38daa923c95661512db203John McCall  assert(getPrimaryContext() == this &&
14940c01d18094100db92d38daa923c95661512db203John McCall         "creating decls map on non-primary context");
14950c01d18094100db92d38daa923c95661512db203John McCall
14960c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap *M;
14970c01d18094100db92d38daa923c95661512db203John McCall  bool Dependent = isDependentContext();
14980c01d18094100db92d38daa923c95661512db203John McCall  if (Dependent)
14990c01d18094100db92d38daa923c95661512db203John McCall    M = new DependentStoredDeclsMap();
15000c01d18094100db92d38daa923c95661512db203John McCall  else
15010c01d18094100db92d38daa923c95661512db203John McCall    M = new StoredDeclsMap();
15020c01d18094100db92d38daa923c95661512db203John McCall  M->Previous = C.LastSDM;
15030c01d18094100db92d38daa923c95661512db203John McCall  C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1504c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  LookupPtr.setPointer(M);
15053478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  return M;
15063478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
15073478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
15083478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenekvoid ASTContext::ReleaseDeclContextMaps() {
15090c01d18094100db92d38daa923c95661512db203John McCall  // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
15100c01d18094100db92d38daa923c95661512db203John McCall  // pointer because the subclass doesn't add anything that needs to
15110c01d18094100db92d38daa923c95661512db203John McCall  // be deleted.
15120c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
15130c01d18094100db92d38daa923c95661512db203John McCall}
15140c01d18094100db92d38daa923c95661512db203John McCall
15150c01d18094100db92d38daa923c95661512db203John McCallvoid StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
15160c01d18094100db92d38daa923c95661512db203John McCall  while (Map) {
15170c01d18094100db92d38daa923c95661512db203John McCall    // Advance the iteration before we invalidate memory.
15180c01d18094100db92d38daa923c95661512db203John McCall    llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
15190c01d18094100db92d38daa923c95661512db203John McCall
15200c01d18094100db92d38daa923c95661512db203John McCall    if (Dependent)
15210c01d18094100db92d38daa923c95661512db203John McCall      delete static_cast<DependentStoredDeclsMap*>(Map);
15220c01d18094100db92d38daa923c95661512db203John McCall    else
15230c01d18094100db92d38daa923c95661512db203John McCall      delete Map;
15240c01d18094100db92d38daa923c95661512db203John McCall
15250c01d18094100db92d38daa923c95661512db203John McCall    Map = Next.getPointer();
15260c01d18094100db92d38daa923c95661512db203John McCall    Dependent = Next.getInt();
15270c01d18094100db92d38daa923c95661512db203John McCall  }
15280c01d18094100db92d38daa923c95661512db203John McCall}
15290c01d18094100db92d38daa923c95661512db203John McCall
15300c01d18094100db92d38daa923c95661512db203John McCallDependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
15310c01d18094100db92d38daa923c95661512db203John McCall                                                 DeclContext *Parent,
15320c01d18094100db92d38daa923c95661512db203John McCall                                           const PartialDiagnostic &PDiag) {
15330c01d18094100db92d38daa923c95661512db203John McCall  assert(Parent->isDependentContext()
15340c01d18094100db92d38daa923c95661512db203John McCall         && "cannot iterate dependent diagnostics of non-dependent context");
15350c01d18094100db92d38daa923c95661512db203John McCall  Parent = Parent->getPrimaryContext();
1536c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Parent->LookupPtr.getPointer())
15370c01d18094100db92d38daa923c95661512db203John McCall    Parent->CreateStoredDeclsMap(C);
15380c01d18094100db92d38daa923c95661512db203John McCall
15390c01d18094100db92d38daa923c95661512db203John McCall  DependentStoredDeclsMap *Map
1540c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
15410c01d18094100db92d38daa923c95661512db203John McCall
1542b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  // Allocate the copy of the PartialDiagnostic via the ASTContext's
1543fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  // BumpPtrAllocator, rather than the ASTContext itself.
1544b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  PartialDiagnostic::Storage *DiagStorage = 0;
1545b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  if (PDiag.hasStorage())
1546b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor    DiagStorage = new (C) PartialDiagnostic::Storage;
1547b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor
1548b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
15490c01d18094100db92d38daa923c95661512db203John McCall
15500c01d18094100db92d38daa923c95661512db203John McCall  // TODO: Maybe we shouldn't reverse the order during insertion.
15510c01d18094100db92d38daa923c95661512db203John McCall  DD->NextDiagnostic = Map->FirstDiagnostic;
15520c01d18094100db92d38daa923c95661512db203John McCall  Map->FirstDiagnostic = DD;
15530c01d18094100db92d38daa923c95661512db203John McCall
15540c01d18094100db92d38daa923c95661512db203John McCall  return DD;
15553478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
1556