156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//                     The LLVM Compiler Infrastructure
456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// This file is distributed under the University of Illinois Open Source
656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// License. See LICENSE.TXT for details.
756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
1056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// This file implements the Decl and DeclContext classes.
1156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//
1256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
1356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
1456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "clang/AST/DeclBase.h"
1564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/Decl.h"
16c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor#include "clang/AST/DeclContextInternals.h"
17d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
1892b7f70c924cbf4514e9e434cea7def51ab49860John McCall#include "clang/AST/DeclFriend.h"
19aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclObjC.h"
20aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
210c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "clang/AST/ASTContext.h"
2444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor#include "clang/AST/Type.h"
25d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl#include "clang/AST/Stmt.h"
26d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl#include "clang/AST/StmtCXX.h"
27100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h"
280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor#include "clang/Basic/TargetInfo.h"
2956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "llvm/ADT/DenseMap.h"
3049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner#include "llvm/Support/raw_ostream.h"
316ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor#include <algorithm>
3256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanusing namespace clang;
3356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//  Statistics
3656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
389a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
399a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
409a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
4156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
421e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregorvoid *Decl::AllocateDeserializedDecl(const ASTContext &Context,
431e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                     unsigned ID,
441e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                     unsigned Size) {
455d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  // Allocate an extra 8 bytes worth of storage, which ensures that the
46c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // resulting pointer will still be 8-byte aligned.
475d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  void *Start = Context.Allocate(Size + 8);
485d1f496f86305b4738d465031a517b5be49f9ebdDouglas Gregor  void *Result = (char*)Start + 8;
49b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor
50c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  unsigned *PrefixPtr = (unsigned *)Result - 2;
51c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor
52c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // Zero out the first 4 bytes; this is used to store the owning module ID.
53c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  PrefixPtr[0] = 0;
54c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor
55c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  // Store the global declaration ID in the second 4 bytes.
56c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  PrefixPtr[1] = ID;
57b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor
58b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  return Result;
591e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
601e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
6156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanconst char *Decl::getDeclKindName() const {
6256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (DeclKind) {
63b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration not in DeclNodes.inc!");
649a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
659a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
669a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
6856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
6956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
7042738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregorvoid Decl::setInvalidDecl(bool Invalid) {
7142738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  InvalidDecl = Invalid;
72ba50b3e8bc68bace2f6715111bbbb8510965be01Argyrios Kyrtzidis  if (Invalid && !isa<ParmVarDecl>(this)) {
7342738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // Defensive maneuver for ill-formed code: we're likely not to make it to
7442738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // a point where we set the access specifier, so default it to "public"
7542738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // to avoid triggering asserts elsewhere in the front end.
7642738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    setAccess(AS_public);
7742738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  }
7842738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor}
7942738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor
800a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroffconst char *DeclContext::getDeclKindName() const {
810a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  switch (DeclKind) {
82b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
839a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
849a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
859a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
860a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  }
870a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff}
880a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff
8902892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbarbool Decl::StatisticsEnabled = false;
9002892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbarvoid Decl::EnableStatistics() {
9102892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  StatisticsEnabled = true;
9256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
9356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
9456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::PrintStats() {
95b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "\n*** Decl Stats:\n";
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalDecls = 0;
989a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
999a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1009a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
101b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "  " << totalDecls << " decls total.\n";
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalBytes = 0;
1049a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE)                                             \
1059a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt  if (n##DERIVED##s > 0) {                                              \
1069a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl));         \
107b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth    llvm::errs() << "    " << n##DERIVED##s << " " #DERIVED " decls, "  \
108b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << sizeof(DERIVED##Decl) << " each ("                  \
109b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << n##DERIVED##s * sizeof(DERIVED##Decl)               \
110b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth                 << " bytes)\n";                                        \
11164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  }
1129a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1139a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115b43c8ec359293df0c1fc250201930f4461c835f8Chandler Carruth  llvm::errs() << "Total bytes = " << totalBytes << "\n";
11656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
11756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
1189a55591af3e5506b95a9718e15380129fbfc5ebcSean Huntvoid Decl::add(Kind k) {
11956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (k) {
1209a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
1219a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
1229a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
12356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
12456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
12556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
12667e332009c6e349dc34700f539747afcff990336Anders Carlssonbool Decl::isTemplateParameterPack() const {
12767e332009c6e349dc34700f539747afcff990336Anders Carlsson  if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
12867e332009c6e349dc34700f539747afcff990336Anders Carlsson    return TTP->isParameterPack();
12910738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  if (const NonTypeTemplateParmDecl *NTTP
13061c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                = dyn_cast<NonTypeTemplateParmDecl>(this))
13110738d36b150aa65206890c1c845cdba076e4200Douglas Gregor    return NTTP->isParameterPack();
13261c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  if (const TemplateTemplateParmDecl *TTP
13361c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(this))
13461c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor    return TTP->isParameterPack();
13567e332009c6e349dc34700f539747afcff990336Anders Carlsson  return false;
13667e332009c6e349dc34700f539747afcff990336Anders Carlsson}
13767e332009c6e349dc34700f539747afcff990336Anders Carlsson
1381fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool Decl::isParameterPack() const {
1391fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
1401fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor    return Parm->isParameterPack();
1411fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
1421fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isTemplateParameterPack();
1431fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
1441fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
145e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool Decl::isFunctionOrFunctionTemplate() const {
1469488ea120e093068021f944176c3d610dd540914John McCall  if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
14758badb7a655d021fc67bb7ed0af95d6ea0c63eb1Anders Carlsson    return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
150e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
151e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
152ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowskibool Decl::isTemplateDecl() const {
153ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski  return isa<TemplateDecl>(this);
154ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski}
155ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski
156c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidisconst DeclContext *Decl::getParentFunctionOrMethod() const {
157c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  for (const DeclContext *DC = getDeclContext();
158c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis       DC && !DC->isTranslationUnit() && !DC->isNamespace();
15979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor       DC = DC->getParent())
16079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    if (DC->isFunctionOrMethod())
161c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis      return DC;
16279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
163c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  return 0;
16479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor}
16579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
1664c3e0ee8e6208eb42c4adb78a7d35b641fd85ae9Douglas Gregor
16756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
16849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner// PrettyStackTraceDecl Implementation
16949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyStackTraceDecl::print(raw_ostream &OS) const {
17249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation TheLoc = Loc;
17349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isInvalid() && TheDecl)
17449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc = TheDecl->getLocation();
1751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isValid()) {
17749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc.print(OS, SM);
17849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << ": ";
17949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  }
18049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
18149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << Message;
18249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
183c52365674b5b157a0486f75c12dd9f4cc41d8089Daniel Dunbar  if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
18449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << " '" << DN->getQualifiedNameAsString() << '\'';
18549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << '\n';
18649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner}
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
18956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// Decl Implementation
19056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
19156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
192da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor// Out-of-line virtual method providing a home for Decl.
193da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas GregorDecl::~Decl() { }
194f4a03cc2b022fab0ffac6c65449555c52036deceDouglas Gregor
1954afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setDeclContext(DeclContext *DC) {
196ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  DeclCtx = DC;
1974afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
1984afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1994afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setLexicalDeclContext(DeclContext *DC) {
2004afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (DC == getLexicalDeclContext())
2014afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return;
2024afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2034afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isInSemaDC()) {
2044bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    setDeclContextsImpl(getDeclContext(), DC, getASTContext());
2054afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  } else {
2064afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    getMultipleDC()->LexicalDC = DC;
2074afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
2084afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
2094afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2104bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidisvoid Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
2114bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis                               ASTContext &Ctx) {
2124bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  if (SemaDC == LexicalDC) {
2134bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    DeclCtx = SemaDC;
2144bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  } else {
2154bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
2164bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    MDC->SemanticDC = SemaDC;
2174bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    MDC->LexicalDC = LexicalDC;
2184bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    DeclCtx = MDC;
2194bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  }
2204bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis}
2214bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis
2229aeed32282fe8a775c24c01c923717ca86695685John McCallbool Decl::isInAnonymousNamespace() const {
2239aeed32282fe8a775c24c01c923717ca86695685John McCall  const DeclContext *DC = getDeclContext();
2249aeed32282fe8a775c24c01c923717ca86695685John McCall  do {
2259aeed32282fe8a775c24c01c923717ca86695685John McCall    if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2269aeed32282fe8a775c24c01c923717ca86695685John McCall      if (ND->isAnonymousNamespace())
2279aeed32282fe8a775c24c01c923717ca86695685John McCall        return true;
2289aeed32282fe8a775c24c01c923717ca86695685John McCall  } while ((DC = DC->getParent()));
2299aeed32282fe8a775c24c01c923717ca86695685John McCall
2309aeed32282fe8a775c24c01c923717ca86695685John McCall  return false;
2319aeed32282fe8a775c24c01c923717ca86695685John McCall}
2329aeed32282fe8a775c24c01c923717ca86695685John McCall
2333708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisTranslationUnitDecl *Decl::getTranslationUnitDecl() {
2349b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis  if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
2359b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis    return TUD;
2369b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis
2373708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  DeclContext *DC = getDeclContext();
2383708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  assert(DC && "This decl is not contained in a translation unit!");
2391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2403708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  while (!DC->isTranslationUnit()) {
2413708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    DC = DC->getParent();
2423708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    assert(DC && "This decl is not contained in a translation unit!");
2433708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  }
2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2453708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  return cast<TranslationUnitDecl>(DC);
2463708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2473708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2483708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisASTContext &Decl::getASTContext() const {
2491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return getTranslationUnitDecl()->getASTContext();
2503708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2513708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2527b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener *Decl::getASTMutationListener() const {
2537b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  return getASTContext().getASTMutationListener();
2547b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis}
2557b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
256c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregorbool Decl::isUsed(bool CheckUsedAttr) const {
25712ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  if (Used)
25812ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
25912ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
26012ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  // Check for used attribute.
261c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (CheckUsedAttr && hasAttr<UsedAttr>())
26212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
26312ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
26412ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  // Check redeclarations for used attribute.
26512ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
266c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
26712ead498c4bc279472b21d446bfccec0f654779eTanya Lattner      return true;
26812ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  }
26912ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
27012ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  return false;
27112ead498c4bc279472b21d446bfccec0f654779eTanya Lattner}
27212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
2736b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidisbool Decl::isReferenced() const {
2746b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (Referenced)
2756b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    return true;
2766b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
2776b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  // Check redeclarations.
2786b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2796b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    if (I->Referenced)
2806b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis      return true;
2816b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
2826b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  return false;
2836b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis}
2846b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
2850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// \brief Determine the availability of the given declaration based on
2860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// the target platform.
2870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor///
2880a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// When it returns an availability result other than \c AR_Available,
2890a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// if the \p Message parameter is non-NULL, it will be set to a
2900a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// string describing why the entity is unavailable.
2910a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor///
2920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// FIXME: Make these strings localizable, since they end up in
2930a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor/// diagnostics.
2940a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorstatic AvailabilityResult CheckAvailability(ASTContext &Context,
2950a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                            const AvailabilityAttr *A,
2960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                            std::string *Message) {
297bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
2985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef PrettyPlatformName
2990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
3000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (PrettyPlatformName.empty())
3010a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    PrettyPlatformName = TargetPlatform;
3020a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
303bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
3040a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (TargetMinVersion.empty())
3050a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Available;
3060a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3070a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Match the platform name.
3080a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (A->getPlatform()->getName() != TargetPlatform)
3090a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Available;
310006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian
311006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  std::string HintMessage;
312006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  if (!A->getMessage().empty()) {
313006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian    HintMessage = " - ";
314006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian    HintMessage += A->getMessage();
315006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian  }
316006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian
317b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  // Make sure that this declaration has not been marked 'unavailable'.
318b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  if (A->getUnavailable()) {
319b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    if (Message) {
320b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor      Message->clear();
321b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor      llvm::raw_string_ostream Out(*Message);
322006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian      Out << "not available on " << PrettyPlatformName
323006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << HintMessage;
324b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    }
325b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor
326b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor    return AR_Unavailable;
327b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor  }
328b53e417ba487f4193ef3b0485b420e0fdae643a2Douglas Gregor
3290a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration has already been introduced.
3300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getIntroduced().empty() &&
3310a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      TargetMinVersion < A->getIntroduced()) {
3320a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3330a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3340a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3350a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "introduced in " << PrettyPlatformName << ' '
336006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getIntroduced() << HintMessage;
3370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3380a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3390a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_NotYetIntroduced;
3400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3410a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3420a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration hasn't been obsoleted.
3430a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
3440a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3450a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3460a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3470a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "obsoleted in " << PrettyPlatformName << ' '
348006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getObsoleted() << HintMessage;
3490a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3500a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3510a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Unavailable;
3520a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3530a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3540a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  // Make sure that this declaration hasn't been deprecated.
3550a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
3560a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (Message) {
3570a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Message->clear();
3580a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      llvm::raw_string_ostream Out(*Message);
3590a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Out << "first deprecated in " << PrettyPlatformName << ' '
360006e42f0c8b65b783d565ef10b938a9e82fc02e3Fariborz Jahanian          << A->getDeprecated() << HintMessage;
3610a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3620a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3630a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return AR_Deprecated;
3640a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
3650a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3660a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return AR_Available;
3670a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
3680a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3690a0d2b179085a52c10402feebeb6db8b4d96a140Douglas GregorAvailabilityResult Decl::getAvailability(std::string *Message) const {
3700a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  AvailabilityResult Result = AR_Available;
3710a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  std::string ResultMessage;
3720a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3730a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
3740a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
3750a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Result >= AR_Deprecated)
3760a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        continue;
3770a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3780a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Message)
3790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        ResultMessage = Deprecated->getMessage();
3800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3810a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Result = AR_Deprecated;
3820a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      continue;
3830a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
3860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (Message)
3870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        *Message = Unavailable->getMessage();
3880a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return AR_Unavailable;
3890a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
3900a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3910a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
3920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
3930a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                Message);
3940a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3950a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (AR == AR_Unavailable)
3960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        return AR_Unavailable;
3970a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
3980a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (AR > Result) {
3990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        Result = AR;
4000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        if (Message)
4010a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor          ResultMessage.swap(*Message);
4020a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      }
4030a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      continue;
4040a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4050a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
4060a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4070a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (Message)
4080a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    Message->swap(ResultMessage);
4090a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return Result;
4100a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4110a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4120a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorbool Decl::canBeWeakImported(bool &IsDefinition) const {
4130a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  IsDefinition = false;
414260611a32535c851237926bfcf78869b13c07d5bJohn McCall
415260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Variables, if they aren't definitions.
4160a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
4170a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (!Var->hasExternalStorage() || Var->getInit()) {
4180a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      IsDefinition = true;
4190a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return false;
4200a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
421260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
422260611a32535c851237926bfcf78869b13c07d5bJohn McCall
423260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Functions, if they aren't definitions.
4240a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
4250a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (FD->hasBody()) {
4260a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      IsDefinition = true;
4270a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return false;
4280a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
429260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
4300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
431260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Objective-C classes, if this is the non-fragile runtime.
432260611a32535c851237926bfcf78869b13c07d5bJohn McCall  } else if (isa<ObjCInterfaceDecl>(this) &&
4330b92fcb1353d2d8b31b6c485e6caa14568aca43bJohn McCall             getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
434260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return true;
435260611a32535c851237926bfcf78869b13c07d5bJohn McCall
436260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // Nothing else.
437260611a32535c851237926bfcf78869b13c07d5bJohn McCall  } else {
438260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return false;
439260611a32535c851237926bfcf78869b13c07d5bJohn McCall  }
4400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
4410a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4420a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregorbool Decl::isWeakImported() const {
4430a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  bool IsDefinition;
4440a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (!canBeWeakImported(IsDefinition))
4450a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return false;
4460a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4470a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
4480a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (isa<WeakImportAttr>(*A))
4490a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return true;
4500a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4510a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
4520a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (CheckAvailability(getASTContext(), Availability, 0)
4530a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                         == AR_NotYetIntroduced)
4540a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        return true;
4550a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
4560a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
4570a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
4580a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  return false;
4590a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor}
46012ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
461769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattnerunsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
462769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  switch (DeclKind) {
4639488ea120e093068021f944176c3d610dd540914John McCall    case Function:
4649488ea120e093068021f944176c3d610dd540914John McCall    case CXXMethod:
4659488ea120e093068021f944176c3d610dd540914John McCall    case CXXConstructor:
4669488ea120e093068021f944176c3d610dd540914John McCall    case CXXDestructor:
4679488ea120e093068021f944176c3d610dd540914John McCall    case CXXConversion:
468769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case EnumConstant:
469769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Var:
470769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ImplicitParam:
471769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ParmVar:
472769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case NonTypeTemplateParm:
473769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCMethod:
474769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProperty:
47500b40d3f2fb8b2f9043daf3dd4558bff98346b3cDaniel Dunbar      return IDNS_Ordinary;
476ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    case Label:
477ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner      return IDNS_Label;
47887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    case IndirectField:
47987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return IDNS_Ordinary | IDNS_Member;
48087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
4810d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case ObjCCompatibleAlias:
4820d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case ObjCInterface:
4830d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Type;
4840d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
4850d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case Typedef:
486162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    case TypeAlias:
4873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    case TypeAliasTemplate:
4880d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case UnresolvedUsingTypename:
4890d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case TemplateTypeParm:
4900d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Type;
4910d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
4929488ea120e093068021f944176c3d610dd540914John McCall    case UsingShadow:
4939488ea120e093068021f944176c3d610dd540914John McCall      return 0; // we'll actually overwrite this later
4949488ea120e093068021f944176c3d610dd540914John McCall
4957ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    case UnresolvedUsingValue:
4967ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      return IDNS_Ordinary | IDNS_Using;
4979488ea120e093068021f944176c3d610dd540914John McCall
4989488ea120e093068021f944176c3d610dd540914John McCall    case Using:
4999488ea120e093068021f944176c3d610dd540914John McCall      return IDNS_Using;
5009488ea120e093068021f944176c3d610dd540914John McCall
501769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProtocol:
5028fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCProtocol;
5031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
504769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Field:
505769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCAtDefsField:
506769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCIvar:
507769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Member;
5081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
509769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Record:
510769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case CXXRecord:
511769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Enum:
5120d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Tag | IDNS_Type;
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
514769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Namespace:
5150d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    case NamespaceAlias:
5160d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Namespace;
5170d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
518769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FunctionTemplate:
5190d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary;
5200d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
521769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplate:
522769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTemplateParm:
5230d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
525769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Never have names.
52602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    case Friend:
527dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    case FriendTemplate:
5286206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    case AccessSpec:
529769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case LinkageSpec:
530769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FileScopeAsm:
531769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case StaticAssert:
532769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCPropertyImpl:
533769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Block:
534769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TranslationUnit:
535769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
536769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case UsingDirective:
537769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplateSpecialization:
538c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    case ClassTemplatePartialSpecialization:
539af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    case ClassScopeFunctionSpecialization:
540bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCImplementation:
541bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCCategory:
542bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor    case ObjCCategoryImpl:
54315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    case Import:
544bd4187bb6f4a0cfe7d6d2c8e8856b16bca2f0748Douglas Gregor      // Never looked up by name.
545769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return 0;
546769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  }
5479488ea120e093068021f944176c3d610dd540914John McCall
5483026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid DeclKind!");
54956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
55056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
5514bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidisvoid Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
5521715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  assert(!HasAttrs && "Decl already contains attrs.");
5531715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
5544bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
555cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  assert(AttrBlank.empty() && "HasAttrs was wrong?");
5561715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
5571715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  AttrBlank = attrs;
5581715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis  HasAttrs = true;
5591715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis}
5601715bf5ed87c792c63278e739bc492921d512a88Argyrios Kyrtzidis
561cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntvoid Decl::dropAttrs() {
56256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasAttrs) return;
5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = false;
56540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  getASTContext().eraseDeclAttrs(this);
56656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
56756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
568cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntconst AttrVec &Decl::getAttrs() const {
569cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  assert(HasAttrs && "No attrs to get!");
57040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  return getASTContext().getDeclAttrs(this);
57156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
57256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
57340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisvoid Decl::swapAttrs(Decl *RHS) {
57456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasLHSAttr = this->HasAttrs;
57556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasRHSAttr = RHS->HasAttrs;
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Usually, neither decl has attrs, nothing to do.
57856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr && !HasRHSAttr) return;
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // If 'this' has no attrs, swap the other way.
58156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr)
58240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    return RHS->swapAttrs(this);
5831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  ASTContext &Context = getASTContext();
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Handle the case when both decls have attrs.
58756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (HasRHSAttr) {
58868584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
58956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    return;
59056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Otherwise, LHS has an attr and RHS doesn't.
59368584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
59468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  Context.eraseDeclAttrs(this);
59556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  this->HasAttrs = false;
59656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  RHS->HasAttrs = true;
59756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
59856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
59942220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDecl *Decl::castFromDeclContext (const DeclContext *D) {
6003d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getDeclKind();
6013d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
6029a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6039a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) \
6049a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    case Decl::NAME:       \
6059a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
6069a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
6079a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6083d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
6099a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6109a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                  \
6119a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (DK >= first##NAME && DK <= last##NAME) \
6129a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
6139a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
614b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("a decl that inherits DeclContext isn't handled");
6153d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
61642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
61742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
61842220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDeclContext *Decl::castToDeclContext(const Decl *D) {
6193d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getKind();
6203d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
6219a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6229a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) \
6239a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    case Decl::NAME:       \
6249a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
6259a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
6269a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
6273d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
6289a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
6299a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                                   \
6309a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (DK >= first##NAME && DK <= last##NAME)                  \
6319a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
6329a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
633b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("a decl that inherits DeclContext isn't handled");
6343d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
63542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
63642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
6376fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisSourceLocation Decl::getBodyRBrace() const {
63806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
63906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // FunctionDecl stores EndRangeLoc for this purpose.
64006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
64106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    const FunctionDecl *Definition;
64206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
64306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return Definition->getSourceRange().getEnd();
64406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    return SourceLocation();
64506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
64606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
6476717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis  if (Stmt *Body = getBody())
6486717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis    return Body->getSourceRange().getEnd();
6496717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis
6506717ef4e695cb37b69dead5fae486c73f8a44a28Argyrios Kyrtzidis  return SourceLocation();
651d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl}
652d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
6531329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlssonvoid Decl::CheckAccessDeclContext() const {
6543a1c36c099df4dcc94d1d24516a8a2c809c764d4Douglas Gregor#ifndef NDEBUG
65546460a68f6508775e98c19b4bb8454bb471aac24John McCall  // Suppress this check if any of the following hold:
65646460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 1. this is the translation unit (and thus has no parent)
65746460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 2. this is a template parameter (and thus doesn't belong to its context)
658d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 3. this is a non-type template parameter
659d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 4. the context is not a record
660d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 5. it's invalid
661d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis  // 6. it's a C++0x static_assert.
66235eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson  if (isa<TranslationUnitDecl>(this) ||
66304aed0edee1fe0bad807afb69f484c5e807800afArgyrios Kyrtzidis      isa<TemplateTypeParmDecl>(this) ||
664d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      isa<NonTypeTemplateParmDecl>(this) ||
665fdd8ab11bc40c7f206c8f1e892faa002cc6536b1Douglas Gregor      !isa<CXXRecordDecl>(getDeclContext()) ||
66665b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      isInvalidDecl() ||
66765b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      isa<StaticAssertDecl>(this) ||
66865b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
66965b63ec1410f09e1f3cdb847018d678b8f8fc3f7Argyrios Kyrtzidis      // as DeclContext (?).
670d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      isa<ParmVarDecl>(this) ||
671d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
672d580e5636568cdc8db0584dd3b7a53323f981e48Argyrios Kyrtzidis      // AS_none as access specifier.
673bc84532e762a41141bd94037cd5d1133f234088eFrancois Pichet      isa<CXXRecordDecl>(this) ||
674bc84532e762a41141bd94037cd5d1133f234088eFrancois Pichet      isa<ClassScopeFunctionSpecializationDecl>(this))
67535eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson    return;
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(Access != AS_none &&
6781329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson         "Access specifier is AS_none inside a record decl");
6793a1c36c099df4dcc94d1d24516a8a2c809c764d4Douglas Gregor#endif
6801329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson}
6811329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
682aab9e315184d344bbd733f13b68915d02db7b32bJohn McCallDeclContext *Decl::getNonClosureContext() {
6834b9c2d235fb9449e249d74f48ecfec601650de93John McCall  return getDeclContext()->getNonClosureAncestor();
6844b9c2d235fb9449e249d74f48ecfec601650de93John McCall}
6854b9c2d235fb9449e249d74f48ecfec601650de93John McCall
6864b9c2d235fb9449e249d74f48ecfec601650de93John McCallDeclContext *DeclContext::getNonClosureAncestor() {
6874b9c2d235fb9449e249d74f48ecfec601650de93John McCall  DeclContext *DC = this;
688aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall
689aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  // This is basically "while (DC->isClosure()) DC = DC->getParent();"
690aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  // except that it's significantly more efficient to cast to a known
691aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  // decl type and call getDeclContext() than to call getParent().
6927b3f853c6f684c1df423e994482681844c396342John McCall  while (isa<BlockDecl>(DC))
6937b3f853c6f684c1df423e994482681844c396342John McCall    DC = cast<BlockDecl>(DC)->getDeclContext();
694aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall
695aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  assert(!DC->isClosure());
696aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  return DC;
697aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall}
6981329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
69956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
70056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// DeclContext Implementation
70156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
70256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
7033d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidisbool DeclContext::classof(const Decl *D) {
7043d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch (D->getKind()) {
7059a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
7069a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT(NAME) case Decl::NAME:
7079a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)
7089a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
7093d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return true;
7103d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
7119a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(NAME, BASE)
7129a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_CONTEXT_BASE(NAME)                 \
7139a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      if (D->getKind() >= Decl::first##NAME &&  \
7149a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt          D->getKind() <= Decl::last##NAME)     \
7153d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return true;
7169a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
7173d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return false;
7183d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
7193d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis}
7203d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis
721a2da780b325e78c6c6bbbb766459a73243c3cf9eDouglas GregorDeclContext::~DeclContext() { }
72244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
723e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// \brief Find the parent context of this context that will be
724e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// used for unqualified name lookup.
725e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor///
726e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// Generally, the parent lookup context is the semantic context. However, for
727e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// a friend function the parent lookup context is the lexical context, which
728e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// is the class in which the friend is declared.
729e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas GregorDeclContext *DeclContext::getLookupParent() {
730e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  // FIXME: Find a better way to identify friends
731e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  if (isa<FunctionDecl>(this))
7327a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (getParent()->getRedeclContext()->isFileContext() &&
7337a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        getLexicalParent()->getRedeclContext()->isRecord())
734e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor      return getLexicalParent();
735e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
736e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  return getParent();
737e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor}
738e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
739410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redlbool DeclContext::isInlineNamespace() const {
740410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  return isNamespace() &&
741410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl         cast<NamespaceDecl>(this)->isInline();
742410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl}
743410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl
744bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregorbool DeclContext::isDependentContext() const {
745bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (isFileContext())
746bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    return false;
747c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
748c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  if (isa<ClassTemplatePartialSpecializationDecl>(this))
749c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return true;
750bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
751f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor  if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
752bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Record->getDescribedClassTemplate())
753bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
754f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor
755f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor    if (Record->isDependentLambda())
756f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor      return true;
757f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor  }
758f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor
7590c01d18094100db92d38daa923c95661512db203John McCall  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
760bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Function->getDescribedFunctionTemplate())
761bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
7621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7630c01d18094100db92d38daa923c95661512db203John McCall    // Friend function declarations are dependent if their *lexical*
7640c01d18094100db92d38daa923c95661512db203John McCall    // context is dependent.
7650c01d18094100db92d38daa923c95661512db203John McCall    if (cast<Decl>(this)->getFriendObjectKind())
7660c01d18094100db92d38daa923c95661512db203John McCall      return getLexicalParent()->isDependentContext();
7670c01d18094100db92d38daa923c95661512db203John McCall  }
7680c01d18094100db92d38daa923c95661512db203John McCall
769bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  return getParent() && getParent()->isDependentContext();
770bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor}
771bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
772074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregorbool DeclContext::isTransparentContext() const {
773074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (DeclKind == Decl::Enum)
7741274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return !cast<EnumDecl>(this)->isScoped();
775074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::LinkageSpec)
776074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true;
777074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
778074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  return false;
779074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
780074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
781ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCallbool DeclContext::isExternCContext() const {
782ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall  const DeclContext *DC = this;
783ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall  while (DC->DeclKind != Decl::TranslationUnit) {
784ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    if (DC->DeclKind == Decl::LinkageSpec)
785ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall      return cast<LinkageSpecDecl>(DC)->getLanguage()
786ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        == LinkageSpecDecl::lang_c;
787ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    DC = DC->getParent();
788ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall  }
789ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall  return false;
790ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall}
791ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall
7927a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redlbool DeclContext::Encloses(const DeclContext *DC) const {
7936dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  if (getPrimaryContext() != this)
7946dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    return getPrimaryContext()->Encloses(DC);
7951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7966dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  for (; DC; DC = DC->getParent())
7976dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    if (DC->getPrimaryContext() == this)
7986dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor      return true;
7991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
8006dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor}
8016dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor
8020701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve NaroffDeclContext *DeclContext::getPrimaryContext() {
80344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
80444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::TranslationUnit:
805074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::LinkageSpec:
8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Decl::Block:
80744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // There is only one DeclContext for these entities.
80844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
80944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
81044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
81144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // The original namespace is our primary context.
81244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
81344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
81444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCMethod:
81544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
81644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
81744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCInterface:
81853df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor    if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
81953df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor      return Def;
82053df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
82153df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor    return this;
82253df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
8230701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCProtocol:
8241d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
8251d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor      return Def;
8261d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor
8271d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    return this;
82853df7a1d34f21d8f2309311d1067d463e9064c60Douglas Gregor
8290701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategory:
83044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
83144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
8320701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCImplementation:
8330701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategoryImpl:
8340701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff    return this;
8350701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff
83644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
8379a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
838cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // If this is a tag type that has a definition or is currently
839cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // being defined, that definition is our primary context.
8403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      TagDecl *Tag = cast<TagDecl>(this);
8413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TagType>(Tag->TypeForDecl) ||
8423cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             isa<InjectedClassNameType>(Tag->TypeForDecl));
8433cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (TagDecl *Def = Tag->getDefinition())
8453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        return Def;
8463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
8483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
8493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (TagTy->isBeingDefined())
8503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          // FIXME: is it necessarily being defined in the decl
8513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          // that owns the type?
8523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return TagTy->getDecl();
8533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      }
8543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return Tag;
856cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    }
857cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
8589a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
85944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          "Unknown DeclContext kind");
86044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
86144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
86244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
86344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
864f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregorvoid
865f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas GregorDeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
866f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  Contexts.clear();
867f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
868f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  if (DeclKind != Decl::Namespace) {
869f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    Contexts.push_back(this);
870f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    return;
87144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
872f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
873f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
874ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
875ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor       N = N->getPreviousDecl())
876f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor    Contexts.push_back(N);
877f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor
878f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  std::reverse(Contexts.begin(), Contexts.end());
87944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
88044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
881eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisstd::pair<Decl *, Decl *>
882341785ec52f87c0803ba52dc88faac4e136f8593Bill WendlingDeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
883ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                            bool FieldsAlreadyLoaded) {
88446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  // Build up a chain of declarations via the Decl::NextInContextAndBits field.
885eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *FirstNewDecl = 0;
886eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *PrevDecl = 0;
887eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
888ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis    if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
889ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis      continue;
890ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis
891eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    Decl *D = Decls[I];
892eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    if (PrevDecl)
89346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      PrevDecl->NextInContextAndBits.setPointer(D);
894eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    else
895eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis      FirstNewDecl = D;
896eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
897eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    PrevDecl = D;
898eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  }
899eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
900eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return std::make_pair(FirstNewDecl, PrevDecl);
901eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
902eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
9032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Load the declarations within this lexical storage from an
9042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// external source.
9051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
90617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::LoadLexicalDeclsFromExternalStorage() const {
90717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  ExternalASTSource *Source = getParentASTContext().getExternalSource();
9082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalLexicalStorage() && Source && "No external storage?");
9092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9100dbbc042518e6ba903dd6f815cbb8704595cc937Argyrios Kyrtzidis  // Notify that we have a DeclContext that is initializing.
9110dbbc042518e6ba903dd6f815cbb8704595cc937Argyrios Kyrtzidis  ExternalASTSource::Deserializing ADeclContext(Source);
9129fc18c97991b1267221ee71d13d8fb2f036b387bDouglas Gregor
913ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  // Load the external declarations, if any.
9145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 64> Decls;
9152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalLexicalStorage = false;
916ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  switch (Source->FindExternalLexicalDecls(this, Decls)) {
917ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Success:
918ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    break;
919ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor
920ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Failure:
921ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_AlreadyLoaded:
922ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    return;
923ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  }
9242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Decls.empty())
9262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
9272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
928ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  // We may have already loaded just the fields of this record, in which case
929ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  // we need to ignore them.
930ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  bool FieldsAlreadyLoaded = false;
931ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
932ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis    FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
933ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis
9342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Splice the newly-read declarations into the beginning of the list
9352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // of declarations.
936eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  Decl *ExternalFirst, *ExternalLast;
937ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
938ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                                                          FieldsAlreadyLoaded);
93946cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
940eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  FirstDecl = ExternalFirst;
9412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!LastDecl)
942eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LastDecl = ExternalLast;
9432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
9442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
94576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallDeclContext::lookup_result
94676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
94776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall                                                    DeclarationName Name) {
94876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  ASTContext &Context = DC->getParentASTContext();
94976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsMap *Map;
950c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(Map = DC->LookupPtr.getPointer()))
95176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    Map = DC->CreateStoredDeclsMap(Context);
95276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
95376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsList &List = (*Map)[Name];
95476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  assert(List.isNull());
95576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  (void) List;
95676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
95776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  return DeclContext::lookup_result();
95876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall}
9592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
96076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallDeclContext::lookup_result
96176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
96276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall                                                  DeclarationName Name,
96345df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis                                                  ArrayRef<NamedDecl*> Decls) {
9641ad23d62007162df82b58bca31b4aa277a5f6586Dmitri Gribenko  ASTContext &Context = DC->getParentASTContext();
96576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
96676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsMap *Map;
967c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(Map = DC->LookupPtr.getPointer()))
96876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    Map = DC->CreateStoredDeclsMap(Context);
9692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
97076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  StoredDeclsList &List = (*Map)[Name];
97145df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis  for (ArrayRef<NamedDecl*>::iterator
97245df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis         I = Decls.begin(), E = Decls.end(); I != E; ++I) {
97376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    if (List.isNull())
97445df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis      List.setOnlyValue(*I);
97576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    else
97645df9c68b4d8eb2206b884c38e89f56d41452c83Argyrios Kyrtzidis      List.AddSubsequentDecl(*I);
97776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  }
97876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
979074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  return List.getLookupResult();
98076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall}
98176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
982681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian RedlDeclContext::decl_iterator DeclContext::noload_decls_begin() const {
983681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  return decl_iterator(FirstDecl);
984681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl}
985681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl
98617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::decl_iterator DeclContext::decls_begin() const {
9872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
98817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
9892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return decl_iterator(FirstDecl);
9916ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
9926ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
99317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidisbool DeclContext::decls_empty() const {
9948038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  if (hasExternalLexicalStorage())
99517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
9968038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
9978038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  return !FirstDecl;
9988038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor}
9998038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
10009f54ad4381370c6b771424b53d219e661d6d6706John McCallvoid DeclContext::removeDecl(Decl *D) {
10019f54ad4381370c6b771424b53d219e661d6d6706John McCall  assert(D->getLexicalDeclContext() == this &&
10029f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl being removed from non-lexical context");
100346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
10049f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl is not in decls list");
10059f54ad4381370c6b771424b53d219e661d6d6706John McCall
10069f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the decl chain.  This is O(n) but hopefully rare.
10079f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (D == FirstDecl) {
10089f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (D == LastDecl)
10099f54ad4381370c6b771424b53d219e661d6d6706John McCall      FirstDecl = LastDecl = 0;
10109f54ad4381370c6b771424b53d219e661d6d6706John McCall    else
101146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      FirstDecl = D->NextInContextAndBits.getPointer();
10129f54ad4381370c6b771424b53d219e661d6d6706John McCall  } else {
101346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
10149f54ad4381370c6b771424b53d219e661d6d6706John McCall      assert(I && "decl not found in linked list");
101546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      if (I->NextInContextAndBits.getPointer() == D) {
101646cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor        I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
10179f54ad4381370c6b771424b53d219e661d6d6706John McCall        if (D == LastDecl) LastDecl = I;
10189f54ad4381370c6b771424b53d219e661d6d6706John McCall        break;
10199f54ad4381370c6b771424b53d219e661d6d6706John McCall      }
10209f54ad4381370c6b771424b53d219e661d6d6706John McCall    }
10219f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
10229f54ad4381370c6b771424b53d219e661d6d6706John McCall
10239f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Mark that D is no longer in the decl chain.
102446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  D->NextInContextAndBits.setPointer(0);
10259f54ad4381370c6b771424b53d219e661d6d6706John McCall
10269f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the lookup table if necessary.
10279f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (isa<NamedDecl>(D)) {
10289f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *ND = cast<NamedDecl>(D);
10299f54ad4381370c6b771424b53d219e661d6d6706John McCall
103002368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann    // Remove only decls that have a name
103102368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann    if (!ND->getDeclName()) return;
103202368d0d1a778b73fd73600496fcde4557b2973fAxel Naumann
1033c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
10340c01d18094100db92d38daa923c95661512db203John McCall    if (!Map) return;
10359f54ad4381370c6b771424b53d219e661d6d6706John McCall
10369f54ad4381370c6b771424b53d219e661d6d6706John McCall    StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
10379f54ad4381370c6b771424b53d219e661d6d6706John McCall    assert(Pos != Map->end() && "no lookup entry for decl");
1038d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann    if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1039d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann      Pos->second.remove(ND);
10409f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
10419f54ad4381370c6b771424b53d219e661d6d6706John McCall}
10429f54ad4381370c6b771424b53d219e661d6d6706John McCall
10433f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addHiddenDecl(Decl *D) {
10447f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner  assert(D->getLexicalDeclContext() == this &&
10457f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner         "Decl inserted into wrong lexical context");
10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!D->getNextDeclInContext() && D != LastDecl &&
10476037fcba3431b47de1a994c9b286feac17894effDouglas Gregor         "Decl already inserted into a DeclContext");
10486037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
10496037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  if (FirstDecl) {
105046cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    LastDecl->NextInContextAndBits.setPointer(D);
10516037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    LastDecl = D;
10526037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  } else {
10536037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    FirstDecl = LastDecl = D;
10546037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  }
105527c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor
105627c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  // Notify a C++ record declaration that we've added a member, so it can
105727c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  // update it's class-specific state.
105827c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
105927c08ab4859d071efa158a256f7e47e13d924443Douglas Gregor    Record->addedMember(D);
1060e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
1061e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  // If this is a newly-created (not de-serialized) import declaration, wire
1062e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  // it in to the list of local import declarations.
1063e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!D->isFromASTFile()) {
1064e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1065e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor      D->getASTContext().addedLocalImportDecl(Import);
1066e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
10673f9a8a60614b763785d54ad08821745d03a4af70John McCall}
10683f9a8a60614b763785d54ad08821745d03a4af70John McCall
10693f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addDecl(Decl *D) {
10703f9a8a60614b763785d54ad08821745d03a4af70John McCall  addHiddenDecl(D);
10714afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
10724afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1073c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ND->getDeclContext()->getPrimaryContext()->
1074c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(ND, false, true);
107544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
107644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10779faf810f08132aabb34a478297dfeea89c3bbe17Sean Callananvoid DeclContext::addDeclInternal(Decl *D) {
10789faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  addHiddenDecl(D);
10799faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
10809faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1081c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ND->getDeclContext()->getPrimaryContext()->
1082c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(ND, true, true);
1083c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1084c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1085c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// shouldBeHidden - Determine whether a declaration which was declared
1086c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// within its semantic context should be invisible to qualified name lookup.
1087c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithstatic bool shouldBeHidden(NamedDecl *D) {
1088c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip unnamed declarations.
1089c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!D->getDeclName())
1090c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1091c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1092c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip entities that can't be found by name lookup into a particular
1093c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // context.
1094c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1095c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      D->isTemplateParameter())
1096c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1097c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1098c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip template specializations.
1099c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // FIXME: This feels like a hack. Should DeclarationName support
1100c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // template-ids, or is there a better way to keep specializations
1101c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // from being visible?
1102c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (isa<ClassTemplateSpecializationDecl>(D))
1103c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return true;
1104c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1105c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (FD->isFunctionTemplateSpecialization())
1106c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      return true;
1107c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1108c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return false;
1109c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1110c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1111c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// buildLookup - Build the lookup data structure with all of the
1112c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// declarations in this DeclContext (and any other contexts linked
1113c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// to it or transparent contexts nested within it) and return it.
1114c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard SmithStoredDeclsMap *DeclContext::buildLookup() {
1115c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1116c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1117c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!LookupPtr.getInt())
1118c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return LookupPtr.getPointer();
1119c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1120c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  llvm::SmallVector<DeclContext *, 2> Contexts;
1121c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  collectAllContexts(Contexts);
1122c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1123c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    buildLookupImpl(Contexts[I]);
1124c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1125c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // We no longer have any lazy decls.
1126c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  LookupPtr.setInt(false);
1127c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return LookupPtr.getPointer();
1128c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1129c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1130c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// buildLookupImpl - Build part of the lookup data structure for the
1131c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// declarations contained within DCtx, which will either be this
1132c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// DeclContext, a DeclContext linked to it, or a transparent context
1133c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith/// nested within it.
1134c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::buildLookupImpl(DeclContext *DCtx) {
1135c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  for (decl_iterator I = DCtx->decls_begin(), E = DCtx->decls_end();
1136c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith       I != E; ++I) {
1137c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Decl *D = *I;
1138c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1139c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // Insert this declaration into the lookup structure, but only if
1140c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // it's semantically within its decl context. Any other decls which
1141c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // should be found in this context are added eagerly.
1142c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1143c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND))
1144c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextImpl(ND, false);
1145c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1146c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // If this declaration is itself a transparent declaration context
1147c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // or inline namespace, add the members of this declaration of that
1148c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // context (recursively).
1149c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1150c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1151c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        buildLookupImpl(InnerCtx);
1152c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
11539faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
11549faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
11551eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::lookup_result
115617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::lookup(DeclarationName Name) {
115765daef179790a02eab1b5a989f53984375a06483Nick Lewycky  assert(DeclKind != Decl::LinkageSpec &&
115865daef179790a02eab1b5a989f53984375a06483Nick Lewycky         "Should not perform lookups into linkage specs!");
115965daef179790a02eab1b5a989f53984375a06483Nick Lewycky
11600701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
116144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this)
116217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    return PrimaryContext->lookup(Name);
116344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
11641b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  if (hasExternalVisibleStorage()) {
1165c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // If a PCH has a result for this name, and we have a local declaration, we
1166c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // will have imported the PCH result when adding the local declaration.
1167c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // FIXME: For modules, we could have had more declarations added by module
1168c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // imoprts since we saw the declaration of the local name.
1169c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1170c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      StoredDeclsMap::iterator I = Map->find(Name);
1171c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      if (I != Map->end())
1172c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        return I->second.getLookupResult();
1173c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    }
1174c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
117576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    ExternalASTSource *Source = getParentASTContext().getExternalSource();
117676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall    return Source->FindExternalVisibleDeclsByName(this, Name);
117776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  }
11782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1179c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap *Map = LookupPtr.getPointer();
1180c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (LookupPtr.getInt())
1181c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Map = buildLookup();
1182c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1183c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Map)
1184c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return lookup_result(lookup_iterator(0), lookup_iterator(0));
1185c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1186c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap::iterator I = Map->find(Name);
1187c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (I == Map->end())
1188c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return lookup_result(lookup_iterator(0), lookup_iterator(0));
1189c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1190c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  return I->second.getLookupResult();
119144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
119244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1193b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregorvoid DeclContext::localUncachedLookup(DeclarationName Name,
1194b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor                                  llvm::SmallVectorImpl<NamedDecl *> &Results) {
1195b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  Results.clear();
1196b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1197b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // If there's no external storage, just perform a normal lookup and copy
1198b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // the results.
119993ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor  if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
1200b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    lookup_result LookupResults = lookup(Name);
1201b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1202b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    return;
1203b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1204b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
1205b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // If we have a lookup table, check there first. Maybe we'll get lucky.
120693ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor  if (Name) {
120793ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor    if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
120893ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      StoredDeclsMap::iterator Pos = Map->find(Name);
120993ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      if (Pos != Map->end()) {
121093ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor        Results.insert(Results.end(),
121193ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor                       Pos->second.getLookupResult().first,
121293ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor                       Pos->second.getLookupResult().second);
121393ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor        return;
121493ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor      }
1215b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    }
1216b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
121793ed7cf05f900b9150dcf59c0e0f37f3bd325f62Douglas Gregor
1218b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // Slow case: grovel through the declarations in our chain looking for
1219b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  // matches.
1220b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1221b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor    if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1222b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor      if (ND->getDeclName() == Name)
1223b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor        Results.push_back(ND);
1224b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  }
1225b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor}
1226b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor
12277a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian RedlDeclContext *DeclContext::getRedeclContext() {
12280cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *Ctx = this;
1229410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  // Skip through transparent contexts.
1230410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  while (Ctx->isTransparentContext())
1231ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor    Ctx = Ctx->getParent();
1232ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  return Ctx;
1233ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor}
1234ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor
123588b7094185b9d4fe9820c731b6936d8d37f6143eDouglas GregorDeclContext *DeclContext::getEnclosingNamespaceContext() {
123688b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *Ctx = this;
123788b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  // Skip through non-namespace, non-translation-unit contexts.
123851a8a378012b5d6a1306fdd75bd135fea3e23b7bSebastian Redl  while (!Ctx->isFileContext())
123988b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    Ctx = Ctx->getParent();
124088b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  return Ctx->getPrimaryContext();
124188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor}
124288b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
12437a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redlbool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
12447a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  // For non-file contexts, this is equivalent to Equals.
12457a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  if (!isFileContext())
12467a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    return O->Equals(this);
12477a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
12487a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  do {
12497a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (O->Equals(this))
12507a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl      return true;
12517a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
12527a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
12537a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (!NS || !NS->isInline())
12547a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl      break;
12557a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    O = NS->getParent();
12567a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  } while (O);
12577a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
12587a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  return false;
12597a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl}
12607a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
1261c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1262c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclContext *PrimaryDC = this->getPrimaryContext();
1263c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1264c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If the decl is being added outside of its semantic decl context, we
1265c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // need to ensure that we eagerly build the lookup information for it.
1266c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
12679faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan}
12689faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan
1269c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1270c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith                                                    bool Recoverable) {
1271c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(this == getPrimaryContext() && "expected a primary DC");
127244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
12731b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // Skip declarations within functions.
12741b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // FIXME: We shouldn't need to build lookup tables for function declarations
12751b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // ever, and we can't do so correctly because we can't model the nesting of
12761b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // scopes which occurs within functions. We use "qualified" lookup into
12771b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // function declarations when handling friend declarations inside nested
12781b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // classes, and consequently accept the following invalid code:
12791b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  //
12801b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  //   void f() { void g(); { int g; struct S { friend void g(); }; } }
12811b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1282cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
1283cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
1284c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Skip declarations which should be invisible to name lookup.
1285c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (shouldBeHidden(D))
1286c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
1287c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1288c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If we already have a lookup data structure, perform the insertion into
1289c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // it. If we might have externally-stored decls with this name, look them
1290c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // up and perform the insertion. If this decl was declared outside its
1291c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // semantic context, buildLookup won't add it, so add it now.
1292c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  //
1293c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // FIXME: As a performance hack, don't add such decls into the translation
1294c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // unit unless we're in C++, since qualified lookup into the TU is never
1295c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // performed.
1296c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1297c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1298c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith       (getParentASTContext().getLangOpts().CPlusPlus ||
1299c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        !isTranslationUnit()))) {
1300c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // If we have lazily omitted any decls, they might have the same name as
1301c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // the decl which we are adding, so build a full lookup table before adding
1302c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    // this decl.
1303c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    buildLookup();
1304c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    makeDeclVisibleInContextImpl(D, Internal);
1305c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  } else {
1306c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    LookupPtr.setInt(true);
1307c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
1308c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1309c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // If we are a transparent context or inline namespace, insert into our
1310c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // parent context, too. This operation is recursive.
1311c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (isTransparentContext() || isInlineNamespace())
1312c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    getParent()->getPrimaryContext()->
1313c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith        makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1314c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1315c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  Decl *DCAsDecl = cast<Decl>(this);
1316c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Notify that a decl was made visible unless we are a Tag being defined.
1317c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1318c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1319c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith      L->AddedVisibleDecl(this, D);
1320c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith}
1321c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1322c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smithvoid DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1323c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Find or create the stored declaration map.
1324c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap *Map = LookupPtr.getPointer();
1325c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Map) {
1326c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    ASTContext *C = &getParentASTContext();
1327c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    Map = CreateStoredDeclsMap(*C);
13285586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis  }
13295586b019c18024b2967d027a17d5a05584a8b181Argyrios Kyrtzidis
1330074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If there is an external AST source, load any declarations it knows about
1331074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // with this declaration's name.
1332074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If the lookup table contains an entry about this name it means that we
1333074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // have already checked the external source.
13349faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  if (!Internal)
13359faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan    if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
13369faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan      if (hasExternalVisibleStorage() &&
1337c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith          Map->find(D->getDeclName()) == Map->end())
13389faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan        Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1339074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
134044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // Insert this declaration into the map.
1341c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
134267762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  if (DeclNameEntries.isNull()) {
134367762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    DeclNameEntries.setOnlyValue(D);
1344c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
1345c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  }
1346c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1347c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (DeclNameEntries.HandleRedeclaration(D)) {
13481b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    // This declaration has replaced an existing one for which
13491b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    // declarationReplaces returns true.
1350c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    return;
135144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
135291942501b6f71a41d3a09bedec19be479832c718Chris Lattner
1353c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  // Put this declaration into the appropriate slot.
1354c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  DeclNameEntries.AddSubsequentDecl(D);
135544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
13562a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
13572a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
13582a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// this context.
13591eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::udir_iterator_range
136017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::getUsingDirectives() const {
13611b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // FIXME: Use something more efficient than normal lookup for using
13621b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  // directives. In C++, using directives are looked up more than anything else.
136317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
13642a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
13652a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor                             reinterpret_cast<udir_iterator>(Result.second));
13662a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor}
13672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
13683478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
13693478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek// Creation and Destruction of StoredDeclsMaps.                               //
13703478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
13713478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
13720c01d18094100db92d38daa923c95661512db203John McCallStoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1373c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  assert(!LookupPtr.getPointer() && "context already has a decls map");
13740c01d18094100db92d38daa923c95661512db203John McCall  assert(getPrimaryContext() == this &&
13750c01d18094100db92d38daa923c95661512db203John McCall         "creating decls map on non-primary context");
13760c01d18094100db92d38daa923c95661512db203John McCall
13770c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap *M;
13780c01d18094100db92d38daa923c95661512db203John McCall  bool Dependent = isDependentContext();
13790c01d18094100db92d38daa923c95661512db203John McCall  if (Dependent)
13800c01d18094100db92d38daa923c95661512db203John McCall    M = new DependentStoredDeclsMap();
13810c01d18094100db92d38daa923c95661512db203John McCall  else
13820c01d18094100db92d38daa923c95661512db203John McCall    M = new StoredDeclsMap();
13830c01d18094100db92d38daa923c95661512db203John McCall  M->Previous = C.LastSDM;
13840c01d18094100db92d38daa923c95661512db203John McCall  C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1385c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  LookupPtr.setPointer(M);
13863478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  return M;
13873478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
13883478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
13893478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenekvoid ASTContext::ReleaseDeclContextMaps() {
13900c01d18094100db92d38daa923c95661512db203John McCall  // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
13910c01d18094100db92d38daa923c95661512db203John McCall  // pointer because the subclass doesn't add anything that needs to
13920c01d18094100db92d38daa923c95661512db203John McCall  // be deleted.
13930c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
13940c01d18094100db92d38daa923c95661512db203John McCall}
13950c01d18094100db92d38daa923c95661512db203John McCall
13960c01d18094100db92d38daa923c95661512db203John McCallvoid StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
13970c01d18094100db92d38daa923c95661512db203John McCall  while (Map) {
13980c01d18094100db92d38daa923c95661512db203John McCall    // Advance the iteration before we invalidate memory.
13990c01d18094100db92d38daa923c95661512db203John McCall    llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
14000c01d18094100db92d38daa923c95661512db203John McCall
14010c01d18094100db92d38daa923c95661512db203John McCall    if (Dependent)
14020c01d18094100db92d38daa923c95661512db203John McCall      delete static_cast<DependentStoredDeclsMap*>(Map);
14030c01d18094100db92d38daa923c95661512db203John McCall    else
14040c01d18094100db92d38daa923c95661512db203John McCall      delete Map;
14050c01d18094100db92d38daa923c95661512db203John McCall
14060c01d18094100db92d38daa923c95661512db203John McCall    Map = Next.getPointer();
14070c01d18094100db92d38daa923c95661512db203John McCall    Dependent = Next.getInt();
14080c01d18094100db92d38daa923c95661512db203John McCall  }
14090c01d18094100db92d38daa923c95661512db203John McCall}
14100c01d18094100db92d38daa923c95661512db203John McCall
14110c01d18094100db92d38daa923c95661512db203John McCallDependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
14120c01d18094100db92d38daa923c95661512db203John McCall                                                 DeclContext *Parent,
14130c01d18094100db92d38daa923c95661512db203John McCall                                           const PartialDiagnostic &PDiag) {
14140c01d18094100db92d38daa923c95661512db203John McCall  assert(Parent->isDependentContext()
14150c01d18094100db92d38daa923c95661512db203John McCall         && "cannot iterate dependent diagnostics of non-dependent context");
14160c01d18094100db92d38daa923c95661512db203John McCall  Parent = Parent->getPrimaryContext();
1417c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  if (!Parent->LookupPtr.getPointer())
14180c01d18094100db92d38daa923c95661512db203John McCall    Parent->CreateStoredDeclsMap(C);
14190c01d18094100db92d38daa923c95661512db203John McCall
14200c01d18094100db92d38daa923c95661512db203John McCall  DependentStoredDeclsMap *Map
1421c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith    = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
14220c01d18094100db92d38daa923c95661512db203John McCall
1423b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  // Allocate the copy of the PartialDiagnostic via the ASTContext's
1424fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  // BumpPtrAllocator, rather than the ASTContext itself.
1425b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  PartialDiagnostic::Storage *DiagStorage = 0;
1426b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  if (PDiag.hasStorage())
1427b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor    DiagStorage = new (C) PartialDiagnostic::Storage;
1428b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor
1429b836518bfc0a2ad5e22a670c82fa070ed83ea909Douglas Gregor  DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
14300c01d18094100db92d38daa923c95661512db203John McCall
14310c01d18094100db92d38daa923c95661512db203John McCall  // TODO: Maybe we shouldn't reverse the order during insertion.
14320c01d18094100db92d38daa923c95661512db203John McCall  DD->NextDiagnostic = Map->FirstDiagnostic;
14330c01d18094100db92d38daa923c95661512db203John McCall  Map->FirstDiagnostic = DD;
14340c01d18094100db92d38daa923c95661512db203John McCall
14350c01d18094100db92d38daa923c95661512db203John McCall  return DD;
14363478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
1437