DeclBase.cpp revision fe6b2d481d91140923f4541f273b253291884214
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"
2756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "llvm/ADT/DenseMap.h"
2849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner#include "llvm/Support/raw_ostream.h"
296ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor#include <algorithm>
303daed52a57d03765223021f5f921bdc280c8f3ccChris Lattner#include <cstdio>
313fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor#include <vector>
3256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanusing namespace clang;
3356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//  Statistics
3656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) static int n##Derived##s = 0;
3964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
4056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
4156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanstatic bool StatSwitch = false;
4256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
4356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanconst char *Decl::getDeclKindName() const {
4456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (DeclKind) {
4564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration not in DeclNodes.def!");
4664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) case Derived: return #Derived;
4764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
4856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
4956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
5056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
5142738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregorvoid Decl::setInvalidDecl(bool Invalid) {
5242738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  InvalidDecl = Invalid;
5342738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  if (Invalid) {
5442738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // Defensive maneuver for ill-formed code: we're likely not to make it to
5542738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // a point where we set the access specifier, so default it to "public"
5642738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    // to avoid triggering asserts elsewhere in the front end.
5742738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor    setAccess(AS_public);
5842738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  }
5942738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor}
6042738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor
610a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroffconst char *DeclContext::getDeclKindName() const {
620a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  switch (DeclKind) {
6364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration context not in DeclNodes.def!");
641ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis#define DECL(Derived, Base) case Decl::Derived: return #Derived;
6564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
660a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  }
670a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff}
680a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff
6956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanbool Decl::CollectingStats(bool Enable) {
702024f4d4b0d57616f79ea742fa782d633d414462Kovarththanan Rajaratnam  if (Enable) StatSwitch = true;
7156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  return StatSwitch;
7256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
7356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
7456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::PrintStats() {
7556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  fprintf(stderr, "*** Decl Stats:\n");
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalDecls = 0;
7864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) totalDecls += n##Derived##s;
7964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
8064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  fprintf(stderr, "  %d decls total.\n", totalDecls);
811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalBytes = 0;
8364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base)                                             \
8464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  if (n##Derived##s > 0) {                                              \
8564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl));         \
8664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    fprintf(stderr, "    %d " #Derived " decls, %d each (%d bytes)\n",  \
8764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor            n##Derived##s, (int)sizeof(Derived##Decl),                  \
8864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor            (int)(n##Derived##s * sizeof(Derived##Decl)));              \
8964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  }
9064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", totalBytes);
9356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
9456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
9556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::addDeclKind(Kind k) {
9656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (k) {
9764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration not in DeclNodes.def!");
9864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
9964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
10056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
10156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
10256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
10367e332009c6e349dc34700f539747afcff990336Anders Carlssonbool Decl::isTemplateParameterPack() const {
10467e332009c6e349dc34700f539747afcff990336Anders Carlsson  if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
10567e332009c6e349dc34700f539747afcff990336Anders Carlsson    return TTP->isParameterPack();
1061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10767e332009c6e349dc34700f539747afcff990336Anders Carlsson  return false;
10867e332009c6e349dc34700f539747afcff990336Anders Carlsson}
10967e332009c6e349dc34700f539747afcff990336Anders Carlsson
110e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool Decl::isFunctionOrFunctionTemplate() const {
1119488ea120e093068021f944176c3d610dd540914John McCall  if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
11258badb7a655d021fc67bb7ed0af95d6ea0c63eb1Anders Carlsson    return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
115e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
116e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
11779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregorbool Decl::isDefinedOutsideFunctionOrMethod() const {
11879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  for (const DeclContext *DC = getDeclContext();
11979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor       DC && !DC->isTranslationUnit();
12079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor       DC = DC->getParent())
12179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    if (DC->isFunctionOrMethod())
12279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      return false;
12379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
12479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  return true;
12579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor}
12679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
12779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor
12856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
12949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner// PrettyStackTraceDecl Implementation
13049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
1311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattnervoid PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
13349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation TheLoc = Loc;
13449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isInvalid() && TheDecl)
13549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc = TheDecl->getLocation();
1361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isValid()) {
13849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc.print(OS, SM);
13949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << ": ";
14049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  }
14149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
14249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << Message;
14349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
144c52365674b5b157a0486f75c12dd9f4cc41d8089Daniel Dunbar  if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
14549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << " '" << DN->getQualifiedNameAsString() << '\'';
14649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << '\n';
14749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner}
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
15056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// Decl Implementation
15156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
15256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
153769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner// Out-of-line virtual method providing a home for Decl.
154769dbdf467681f6020ff248b969c2d41a4fdccd3Chris LattnerDecl::~Decl() {
155769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  assert(!HasAttrs && "attributes should have been freed by Destroy");
156769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner}
157769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
1584afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setDeclContext(DeclContext *DC) {
1594afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isOutOfSemaDC())
1604afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    delete getMultipleDC();
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
162ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  DeclCtx = DC;
1634afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
1644afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1654afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setLexicalDeclContext(DeclContext *DC) {
1664afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (DC == getLexicalDeclContext())
1674afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return;
1684afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1694afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isInSemaDC()) {
17094a39005e3733f2e498f2642be95507dda184ca1Ted Kremenek    MultipleDC *MDC = new (getASTContext()) MultipleDC();
1714afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MDC->SemanticDC = getDeclContext();
1724afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MDC->LexicalDC = DC;
173ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    DeclCtx = MDC;
1744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  } else {
1754afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    getMultipleDC()->LexicalDC = DC;
1764afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
1774afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
1784afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1799aeed32282fe8a775c24c01c923717ca86695685John McCallbool Decl::isInAnonymousNamespace() const {
1809aeed32282fe8a775c24c01c923717ca86695685John McCall  const DeclContext *DC = getDeclContext();
1819aeed32282fe8a775c24c01c923717ca86695685John McCall  do {
1829aeed32282fe8a775c24c01c923717ca86695685John McCall    if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
1839aeed32282fe8a775c24c01c923717ca86695685John McCall      if (ND->isAnonymousNamespace())
1849aeed32282fe8a775c24c01c923717ca86695685John McCall        return true;
1859aeed32282fe8a775c24c01c923717ca86695685John McCall  } while ((DC = DC->getParent()));
1869aeed32282fe8a775c24c01c923717ca86695685John McCall
1879aeed32282fe8a775c24c01c923717ca86695685John McCall  return false;
1889aeed32282fe8a775c24c01c923717ca86695685John McCall}
1899aeed32282fe8a775c24c01c923717ca86695685John McCall
1903708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisTranslationUnitDecl *Decl::getTranslationUnitDecl() {
1919b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis  if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
1929b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis    return TUD;
1939b34669c672e776a24616eb01cffcf7061356d26Argyrios Kyrtzidis
1943708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  DeclContext *DC = getDeclContext();
1953708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  assert(DC && "This decl is not contained in a translation unit!");
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1973708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  while (!DC->isTranslationUnit()) {
1983708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    DC = DC->getParent();
1993708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    assert(DC && "This decl is not contained in a translation unit!");
2003708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  }
2011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2023708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  return cast<TranslationUnitDecl>(DC);
2033708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2043708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
2053708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisASTContext &Decl::getASTContext() const {
2061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return getTranslationUnitDecl()->getASTContext();
2073708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
2083708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
20912ead498c4bc279472b21d446bfccec0f654779eTanya Lattnerbool Decl::isUsed() const {
21012ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  if (Used)
21112ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
21212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
21312ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  // Check for used attribute.
21412ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  if (hasAttr<UsedAttr>())
21512ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    return true;
21612ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
21712ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  // Check redeclarations for used attribute.
21812ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
21912ead498c4bc279472b21d446bfccec0f654779eTanya Lattner    if (I->hasAttr<UsedAttr>() || I->Used)
22012ead498c4bc279472b21d446bfccec0f654779eTanya Lattner      return true;
22112ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  }
22212ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
22312ead498c4bc279472b21d446bfccec0f654779eTanya Lattner  return false;
22412ead498c4bc279472b21d446bfccec0f654779eTanya Lattner}
22512ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
22612ead498c4bc279472b21d446bfccec0f654779eTanya Lattner
227769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattnerunsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
228769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  switch (DeclKind) {
2299488ea120e093068021f944176c3d610dd540914John McCall    case Function:
2309488ea120e093068021f944176c3d610dd540914John McCall    case CXXMethod:
2319488ea120e093068021f944176c3d610dd540914John McCall    case CXXConstructor:
2329488ea120e093068021f944176c3d610dd540914John McCall    case CXXDestructor:
2339488ea120e093068021f944176c3d610dd540914John McCall    case CXXConversion:
234769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Typedef:
235769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case EnumConstant:
236769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Var:
237769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ImplicitParam:
238769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ParmVar:
239769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case NonTypeTemplateParm:
240769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCMethod:
241769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCContainer:
242769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCInterface:
243769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProperty:
244769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCCompatibleAlias:
245769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Ordinary;
2463f9a8a60614b763785d54ad08821745d03a4af70John McCall
2479488ea120e093068021f944176c3d610dd540914John McCall    case UsingShadow:
2489488ea120e093068021f944176c3d610dd540914John McCall      return 0; // we'll actually overwrite this later
2499488ea120e093068021f944176c3d610dd540914John McCall
2507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    case UnresolvedUsingValue:
2517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    case UnresolvedUsingTypename:
2527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      return IDNS_Ordinary | IDNS_Using;
2539488ea120e093068021f944176c3d610dd540914John McCall
2549488ea120e093068021f944176c3d610dd540914John McCall    case Using:
2559488ea120e093068021f944176c3d610dd540914John McCall      return IDNS_Using;
2569488ea120e093068021f944176c3d610dd540914John McCall
257769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProtocol:
2588fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCProtocol;
2591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2608fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    case ObjCImplementation:
2618fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCImplementation;
2628fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor
263737061fc2948776f941e1854a9bc6ebd070d9151Fariborz Jahanian    case ObjCCategory:
2648fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    case ObjCCategoryImpl:
265737061fc2948776f941e1854a9bc6ebd070d9151Fariborz Jahanian      return IDNS_ObjCCategoryName;
2668fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor
267769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Field:
268769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCAtDefsField:
269769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCIvar:
270769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Member;
2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
272769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Record:
273769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case CXXRecord:
274769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Enum:
275769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTypeParm:
276769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Tag;
2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
278769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Namespace:
279769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Template:
280769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FunctionTemplate:
281769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplate:
282769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTemplateParm:
283faf0e872f3409ecafbc458eabb22be76f79cb050Anders Carlsson    case NamespaceAlias:
284769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Tag | IDNS_Ordinary;
2851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
286769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Never have names.
28702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    case Friend:
288dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    case FriendTemplate:
289769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case LinkageSpec:
290769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FileScopeAsm:
291769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case StaticAssert:
292769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCClass:
293769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCPropertyImpl:
294769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCForwardProtocol:
295769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Block:
296769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TranslationUnit:
297769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
298769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Aren't looked up?
299769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case UsingDirective:
300769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplateSpecialization:
301c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    case ClassTemplatePartialSpecialization:
302769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return 0;
303769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  }
3049488ea120e093068021f944176c3d610dd540914John McCall
3059488ea120e093068021f944176c3d610dd540914John McCall  return 0;
30656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
30756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
30840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisvoid Decl::addAttr(Attr *NewAttr) {
30940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
31056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
31156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  NewAttr->setNext(ExistingAttr);
31256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  ExistingAttr = NewAttr;
3131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = true;
31556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
31656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
31740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisvoid Decl::invalidateAttrs() {
31856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasAttrs) return;
3191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = false;
32140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  getASTContext().eraseDeclAttrs(this);
32256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
32356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
32440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisconst Attr *Decl::getAttrsImpl() const {
3251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(HasAttrs && "getAttrs() should verify this!");
32640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  return getASTContext().getDeclAttrs(this);
32756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
32856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
32940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisvoid Decl::swapAttrs(Decl *RHS) {
33056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasLHSAttr = this->HasAttrs;
33156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasRHSAttr = RHS->HasAttrs;
3321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Usually, neither decl has attrs, nothing to do.
33456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr && !HasRHSAttr) return;
3351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // If 'this' has no attrs, swap the other way.
33756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr)
33840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    return RHS->swapAttrs(this);
3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  ASTContext &Context = getASTContext();
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Handle the case when both decls have attrs.
34356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (HasRHSAttr) {
34468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
34556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    return;
34656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Otherwise, LHS has an attr and RHS doesn't.
34968584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
35068584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  Context.eraseDeclAttrs(this);
35156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  this->HasAttrs = false;
35256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  RHS->HasAttrs = true;
35356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
35456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
35556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
356cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattnervoid Decl::Destroy(ASTContext &C) {
357cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  // Free attributes for this decl.
358cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  if (HasAttrs) {
35968584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    C.getDeclAttrs(this)->Destroy(C);
36040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    invalidateAttrs();
361cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner    HasAttrs = false;
362cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  }
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
364a0fc55f3e9d7d7aa8761d0a9726033947d0d6bc0Douglas Gregor#if 0
36500ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor  // FIXME: Once ownership is fully understood, we can enable this code
36600ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor  if (DeclContext *DC = dyn_cast<DeclContext>(this))
36700ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor    DC->decls_begin()->Destroy(C);
36856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
369244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  // Observe the unrolled recursion.  By setting N->NextDeclInContext = 0x0
3704afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // within the loop, only the Destroy method for the first Decl
3714afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // will deallocate all of the Decls in a chain.
3721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
373244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  Decl* N = getNextDeclInContext();
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3754afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  while (N) {
376244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    Decl* Tmp = N->getNextDeclInContext();
377244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    N->NextDeclInContext = 0;
3784afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    N->Destroy(C);
3794afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    N = Tmp;
3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
381a0fc55f3e9d7d7aa8761d0a9726033947d0d6bc0Douglas Gregor
38294a39005e3733f2e498f2642be95507dda184ca1Ted Kremenek  if (isOutOfSemaDC())
38394a39005e3733f2e498f2642be95507dda184ca1Ted Kremenek    delete (C) getMultipleDC();
38494a39005e3733f2e498f2642be95507dda184ca1Ted Kremenek
38556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  this->~Decl();
3863e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate((void *)this);
38794a39005e3733f2e498f2642be95507dda184ca1Ted Kremenek#endif
38856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
38956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
39042220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDecl *Decl::castFromDeclContext (const DeclContext *D) {
3913d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getDeclKind();
3923d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
3933d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) \
3943d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    case Decl::Name:     \
3953d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
3963d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
3973d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3983d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
3993d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                                   \
4003d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (DK >= Decl::Name##First && DK <= Decl::Name##Last)    \
4013d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
4023d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
4033d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      assert(false && "a decl that inherits DeclContext isn't handled");
4043d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return 0;
4053d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
40642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
40742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
40842220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDeclContext *Decl::castToDeclContext(const Decl *D) {
4093d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getKind();
4103d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
4113d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) \
4123d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    case Decl::Name:     \
4133d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return static_cast<Name##Decl*>(const_cast<Decl*>(D));
4143d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
4153d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
4163d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
4173d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                                   \
4183d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (DK >= Decl::Name##First && DK <= Decl::Name##Last)    \
4193d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return static_cast<Name##Decl*>(const_cast<Decl*>(D));
4203d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
4213d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      assert(false && "a decl that inherits DeclContext isn't handled");
4223d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return 0;
4233d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
42442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
42542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
4266fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisCompoundStmt* Decl::getCompoundBody() const {
4276fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  return dyn_cast_or_null<CompoundStmt>(getBody());
428d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl}
429d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
4306fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisSourceLocation Decl::getBodyRBrace() const {
4316fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  Stmt *Body = getBody();
432d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (!Body)
433d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    return SourceLocation();
434d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
435d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    return CS->getRBracLoc();
436d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  assert(isa<CXXTryStmt>(Body) &&
437d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl         "Body can only be CompoundStmt or CXXTryStmt");
438d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
439d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl}
440d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
4411329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#ifndef NDEBUG
4421329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlssonvoid Decl::CheckAccessDeclContext() const {
44346460a68f6508775e98c19b4bb8454bb471aac24John McCall  // Suppress this check if any of the following hold:
44446460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 1. this is the translation unit (and thus has no parent)
44546460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 2. this is a template parameter (and thus doesn't belong to its context)
44646460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 3. this is a ParmVarDecl (which can be in a record context during
44746460a68f6508775e98c19b4bb8454bb471aac24John McCall  //    the brief period between its creation and the creation of the
44846460a68f6508775e98c19b4bb8454bb471aac24John McCall  //    FunctionDecl)
44946460a68f6508775e98c19b4bb8454bb471aac24John McCall  // 4. the context is not a record
45035eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson  if (isa<TranslationUnitDecl>(this) ||
451fdd8ab11bc40c7f206c8f1e892faa002cc6536b1Douglas Gregor      !isa<CXXRecordDecl>(getDeclContext()) ||
452fdd8ab11bc40c7f206c8f1e892faa002cc6536b1Douglas Gregor      isInvalidDecl())
45335eda446cdf5b4e95a80ffacbf6c7f7478c6d927Anders Carlsson    return;
4541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(Access != AS_none &&
4561329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson         "Access specifier is AS_none inside a record decl");
4571329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson}
4581329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
4591329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#endif
4601329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
46156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
46256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// DeclContext Implementation
46356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
46456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
4653d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidisbool DeclContext::classof(const Decl *D) {
4663d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch (D->getKind()) {
4673d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) case Decl::Name:
4683d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
4693d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
4703d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return true;
4713d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
4723d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                   \
4733d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (D->getKind() >= Decl::Name##First &&  \
4743d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis          D->getKind() <= Decl::Name##Last)     \
4753d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return true;
4763d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
4773d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return false;
4783d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
4793d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis}
4803d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis
48144b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext::~DeclContext() {
4823478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because
4833478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // ~DeclContext() is not guaranteed to be called when ASTContext uses
4843478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // a BumpPtrAllocator.
4850c01d18094100db92d38daa923c95661512db203John McCall  // delete LookupPtr;
48644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
48744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
48844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorvoid DeclContext::DestroyDecls(ASTContext &C) {
48917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (decl_iterator D = decls_begin(); D != decls_end(); )
49000ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor    (*D++)->Destroy(C);
49144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
49244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
493e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// \brief Find the parent context of this context that will be
494e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// used for unqualified name lookup.
495e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor///
496e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// Generally, the parent lookup context is the semantic context. However, for
497e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// a friend function the parent lookup context is the lexical context, which
498e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor/// is the class in which the friend is declared.
499e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas GregorDeclContext *DeclContext::getLookupParent() {
500e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  // FIXME: Find a better way to identify friends
501e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  if (isa<FunctionDecl>(this))
502e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor    if (getParent()->getLookupContext()->isFileContext() &&
503e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor        getLexicalParent()->getLookupContext()->isRecord())
504e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor      return getLexicalParent();
505e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
506e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  return getParent();
507e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor}
508e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor
509bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregorbool DeclContext::isDependentContext() const {
510bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (isFileContext())
511bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    return false;
512c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
513c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  if (isa<ClassTemplatePartialSpecializationDecl>(this))
514c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return true;
515bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
516bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
517bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Record->getDescribedClassTemplate())
518bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
519bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
5200c01d18094100db92d38daa923c95661512db203John McCall  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
521bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Function->getDescribedFunctionTemplate())
522bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
5231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5240c01d18094100db92d38daa923c95661512db203John McCall    // Friend function declarations are dependent if their *lexical*
5250c01d18094100db92d38daa923c95661512db203John McCall    // context is dependent.
5260c01d18094100db92d38daa923c95661512db203John McCall    if (cast<Decl>(this)->getFriendObjectKind())
5270c01d18094100db92d38daa923c95661512db203John McCall      return getLexicalParent()->isDependentContext();
5280c01d18094100db92d38daa923c95661512db203John McCall  }
5290c01d18094100db92d38daa923c95661512db203John McCall
530bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  return getParent() && getParent()->isDependentContext();
531bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor}
532bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
533074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregorbool DeclContext::isTransparentContext() const {
534074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (DeclKind == Decl::Enum)
535074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true; // FIXME: Check for C++0x scoped enums
536074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::LinkageSpec)
537074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true;
53865100792a69a16895bd80f1d639b99e7ad903386Douglas Gregor  else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
539bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor    return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
540074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::Namespace)
541074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return false; // FIXME: Check for C++0x inline namespaces
542074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
543074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  return false;
544074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
545074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
5466dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregorbool DeclContext::Encloses(DeclContext *DC) {
5476dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  if (getPrimaryContext() != this)
5486dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    return getPrimaryContext()->Encloses(DC);
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5506dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  for (; DC; DC = DC->getParent())
5516dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor    if (DC->getPrimaryContext() == this)
5526dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor      return true;
5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
5546dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor}
5556dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor
5560701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve NaroffDeclContext *DeclContext::getPrimaryContext() {
55744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
55844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::TranslationUnit:
559074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::LinkageSpec:
5601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Decl::Block:
56144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // There is only one DeclContext for these entities.
56244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
56344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
56444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
56544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // The original namespace is our primary context.
56644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
56744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
56844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCMethod:
56944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
57044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
57144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCInterface:
5720701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCProtocol:
5730701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategory:
57444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // FIXME: Can Objective-C interfaces be forward-declared?
57544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
57644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
5770701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCImplementation:
5780701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategoryImpl:
5790701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff    return this;
5800701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff
58144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
582cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
583cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // If this is a tag type that has a definition or is currently
584cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // being defined, that definition is our primary context.
5853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      TagDecl *Tag = cast<TagDecl>(this);
5863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TagType>(Tag->TypeForDecl) ||
5873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             isa<InjectedClassNameType>(Tag->TypeForDecl));
5883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
5893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (TagDecl *Def = Tag->getDefinition())
5903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        return Def;
5913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
5923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
5933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
5943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (TagTy->isBeingDefined())
5953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          // FIXME: is it necessarily being defined in the decl
5963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          // that owns the type?
5973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return TagTy->getDecl();
5983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      }
5993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
6003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return Tag;
601cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    }
602cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
60344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
60444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          "Unknown DeclContext kind");
60544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
60644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
60744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
60844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
60944b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext *DeclContext::getNextContext() {
61044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
61144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
61244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Return the next namespace
61344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getNextNamespace();
61444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
61544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
61644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return 0;
61744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
61844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
61944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
6202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Load the declarations within this lexical storage from an
6212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// external source.
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
62317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::LoadLexicalDeclsFromExternalStorage() const {
62417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  ExternalASTSource *Source = getParentASTContext().getExternalSource();
6252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalLexicalStorage() && Source && "No external storage?");
6262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
627b0156ea412df1c2eb12d620054a404da71784cf5Eli Friedman  llvm::SmallVector<uint32_t, 64> Decls;
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
6292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                          Decls))
6302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
6312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // There is no longer any lexical storage in this context
6332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalLexicalStorage = false;
6342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Decls.empty())
6362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
6372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Resolve all of the declaration IDs into declarations, building up
6392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // a chain of declarations via the Decl::NextDeclInContext field.
6402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Decl *FirstNewDecl = 0;
6412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Decl *PrevDecl = 0;
6422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
6432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Decl *D = Source->GetDecl(Decls[I]);
6442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (PrevDecl)
6452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      PrevDecl->NextDeclInContext = D;
6462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    else
6472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      FirstNewDecl = D;
6482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    PrevDecl = D;
6502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
6512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Splice the newly-read declarations into the beginning of the list
6532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // of declarations.
6542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  PrevDecl->NextDeclInContext = FirstDecl;
6552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  FirstDecl = FirstNewDecl;
6562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!LastDecl)
6572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LastDecl = PrevDecl;
6582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
6592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
66117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::LoadVisibleDeclsFromExternalStorage() const {
6622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  DeclContext *This = const_cast<DeclContext *>(this);
66317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  ExternalASTSource *Source = getParentASTContext().getExternalSource();
6642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalVisibleStorage() && Source && "No external storage?");
6652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  llvm::SmallVector<VisibleDeclaration, 64> Decls;
6672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Source->ReadDeclsVisibleInContext(This, Decls))
6682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
6692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // There is no longer any visible storage in this context
6712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalVisibleStorage = false;
6722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Load the declaration IDs for all of the names visible in this
6742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // context.
6752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(!LookupPtr && "Have a lookup map before de-serialization?");
6760c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap *Map = CreateStoredDeclsMap(getParentASTContext());
6772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
6782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
6792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
6802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
6812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
68217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::decl_iterator DeclContext::decls_begin() const {
6832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
68417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
6852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // FIXME: Check whether we need to load some declarations from
6872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // external storage.
6881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return decl_iterator(FirstDecl);
6896ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
6906ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
69117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::decl_iterator DeclContext::decls_end() const {
6922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
69317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
6942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return decl_iterator();
6966ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
6976ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
69817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidisbool DeclContext::decls_empty() const {
6998038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  if (hasExternalLexicalStorage())
70017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadLexicalDeclsFromExternalStorage();
7018038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
7028038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  return !FirstDecl;
7038038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor}
7048038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
7059f54ad4381370c6b771424b53d219e661d6d6706John McCallvoid DeclContext::removeDecl(Decl *D) {
7069f54ad4381370c6b771424b53d219e661d6d6706John McCall  assert(D->getLexicalDeclContext() == this &&
7079f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl being removed from non-lexical context");
7089f54ad4381370c6b771424b53d219e661d6d6706John McCall  assert((D->NextDeclInContext || D == LastDecl) &&
7099f54ad4381370c6b771424b53d219e661d6d6706John McCall         "decl is not in decls list");
7109f54ad4381370c6b771424b53d219e661d6d6706John McCall
7119f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the decl chain.  This is O(n) but hopefully rare.
7129f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (D == FirstDecl) {
7139f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (D == LastDecl)
7149f54ad4381370c6b771424b53d219e661d6d6706John McCall      FirstDecl = LastDecl = 0;
7159f54ad4381370c6b771424b53d219e661d6d6706John McCall    else
7169f54ad4381370c6b771424b53d219e661d6d6706John McCall      FirstDecl = D->NextDeclInContext;
7179f54ad4381370c6b771424b53d219e661d6d6706John McCall  } else {
7189f54ad4381370c6b771424b53d219e661d6d6706John McCall    for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
7199f54ad4381370c6b771424b53d219e661d6d6706John McCall      assert(I && "decl not found in linked list");
7209f54ad4381370c6b771424b53d219e661d6d6706John McCall      if (I->NextDeclInContext == D) {
7219f54ad4381370c6b771424b53d219e661d6d6706John McCall        I->NextDeclInContext = D->NextDeclInContext;
7229f54ad4381370c6b771424b53d219e661d6d6706John McCall        if (D == LastDecl) LastDecl = I;
7239f54ad4381370c6b771424b53d219e661d6d6706John McCall        break;
7249f54ad4381370c6b771424b53d219e661d6d6706John McCall      }
7259f54ad4381370c6b771424b53d219e661d6d6706John McCall    }
7269f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
7279f54ad4381370c6b771424b53d219e661d6d6706John McCall
7289f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Mark that D is no longer in the decl chain.
7299f54ad4381370c6b771424b53d219e661d6d6706John McCall  D->NextDeclInContext = 0;
7309f54ad4381370c6b771424b53d219e661d6d6706John McCall
7319f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Remove D from the lookup table if necessary.
7329f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (isa<NamedDecl>(D)) {
7339f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *ND = cast<NamedDecl>(D);
7349f54ad4381370c6b771424b53d219e661d6d6706John McCall
7350c01d18094100db92d38daa923c95661512db203John McCall    StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
7360c01d18094100db92d38daa923c95661512db203John McCall    if (!Map) return;
7379f54ad4381370c6b771424b53d219e661d6d6706John McCall
7389f54ad4381370c6b771424b53d219e661d6d6706John McCall    StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
7399f54ad4381370c6b771424b53d219e661d6d6706John McCall    assert(Pos != Map->end() && "no lookup entry for decl");
7409f54ad4381370c6b771424b53d219e661d6d6706John McCall    Pos->second.remove(ND);
7419f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
7429f54ad4381370c6b771424b53d219e661d6d6706John McCall}
7439f54ad4381370c6b771424b53d219e661d6d6706John McCall
7443f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addHiddenDecl(Decl *D) {
7457f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner  assert(D->getLexicalDeclContext() == this &&
7467f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner         "Decl inserted into wrong lexical context");
7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!D->getNextDeclInContext() && D != LastDecl &&
7486037fcba3431b47de1a994c9b286feac17894effDouglas Gregor         "Decl already inserted into a DeclContext");
7496037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
7506037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  if (FirstDecl) {
751244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    LastDecl->NextDeclInContext = D;
7526037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    LastDecl = D;
7536037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  } else {
7546037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    FirstDecl = LastDecl = D;
7556037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  }
7563f9a8a60614b763785d54ad08821745d03a4af70John McCall}
7573f9a8a60614b763785d54ad08821745d03a4af70John McCall
7583f9a8a60614b763785d54ad08821745d03a4af70John McCallvoid DeclContext::addDecl(Decl *D) {
7593f9a8a60614b763785d54ad08821745d03a4af70John McCall  addHiddenDecl(D);
7604afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
7614afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
76217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    ND->getDeclContext()->makeDeclVisibleInContext(ND);
76344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
76444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
765074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// buildLookup - Build the lookup data structure with all of the
766074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// declarations in DCtx (and any other contexts linked to it or
767074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// transparent contexts nested within it).
76817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidisvoid DeclContext::buildLookup(DeclContext *DCtx) {
769074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  for (; DCtx; DCtx = DCtx->getNextContext()) {
7701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    for (decl_iterator D = DCtx->decls_begin(),
7711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                    DEnd = DCtx->decls_end();
7724f3b8f8ac2f8c89028a2f8793df0a7887df809d4Douglas Gregor         D != DEnd; ++D) {
7733f9a8a60614b763785d54ad08821745d03a4af70John McCall      // Insert this declaration into the lookup structure, but only
7743f9a8a60614b763785d54ad08821745d03a4af70John McCall      // if it's semantically in its decl context.  During non-lazy
7753f9a8a60614b763785d54ad08821745d03a4af70John McCall      // lookup building, this is implicitly enforced by addDecl.
7764afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
7773f9a8a60614b763785d54ad08821745d03a4af70John McCall        if (D->getDeclContext() == DCtx)
7783f9a8a60614b763785d54ad08821745d03a4af70John McCall          makeDeclVisibleInContextImpl(ND);
779074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
780c32b1d82c1f6d0f0d6c615beb3b6bdfbfbea7098Ted Kremenek      // Insert any forward-declared Objective-C interfaces into the lookup
781c32b1d82c1f6d0f0d6c615beb3b6bdfbfbea7098Ted Kremenek      // data structure.
782c32b1d82c1f6d0f0d6c615beb3b6bdfbfbea7098Ted Kremenek      if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
783c32b1d82c1f6d0f0d6c615beb3b6bdfbfbea7098Ted Kremenek        for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
784c32b1d82c1f6d0f0d6c615beb3b6bdfbfbea7098Ted Kremenek             I != IEnd; ++I)
785321c22f1c4271c3d9a3d4d3fc18847f948ab595bTed Kremenek          makeDeclVisibleInContextImpl(I->getInterface());
786c32b1d82c1f6d0f0d6c615beb3b6bdfbfbea7098Ted Kremenek
787074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      // If this declaration is itself a transparent declaration context,
788074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      // add its members (recursively).
789074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
790074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor        if (InnerCtx->isTransparentContext())
79117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis          buildLookup(InnerCtx->getPrimaryContext());
792074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    }
793074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  }
794074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
795074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
7961eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::lookup_result
79717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::lookup(DeclarationName Name) {
7980701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
79944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this)
80017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    return PrimaryContext->lookup(Name);
80144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
8022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalVisibleStorage())
80317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    LoadVisibleDeclsFromExternalStorage();
8042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8053fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor  /// If there is no lookup data structure, build one now by walking
80644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// all of the linked DeclContexts (in declaration order!) and
80744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// inserting their values.
808c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (!LookupPtr) {
80917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    buildLookup(this);
81044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
811c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    if (!LookupPtr)
81291942501b6f71a41d3a09bedec19be479832c718Chris Lattner      return lookup_result(0, 0);
813c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  }
81444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
8150c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
8160c01d18094100db92d38daa923c95661512db203John McCall  if (Pos == LookupPtr->end())
817c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    return lookup_result(0, 0);
81817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  return Pos->second.getLookupResult(getParentASTContext());
81944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
82044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
8211eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::lookup_const_result
82217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::lookup(DeclarationName Name) const {
82317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  return const_cast<DeclContext*>(this)->lookup(Name);
82444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
82544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
8260cf2b1990c82121d03a004dafe498ba43bf4b42aChris LattnerDeclContext *DeclContext::getLookupContext() {
8270cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *Ctx = this;
82872de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor  // Skip through transparent contexts.
829ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  while (Ctx->isTransparentContext())
830ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor    Ctx = Ctx->getParent();
831ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  return Ctx;
832ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor}
833ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor
83488b7094185b9d4fe9820c731b6936d8d37f6143eDouglas GregorDeclContext *DeclContext::getEnclosingNamespaceContext() {
83588b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *Ctx = this;
83688b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  // Skip through non-namespace, non-translation-unit contexts.
83788b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  while (!Ctx->isFileContext() || Ctx->isTransparentContext())
83888b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    Ctx = Ctx->getParent();
83988b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  return Ctx->getPrimaryContext();
84088b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor}
84188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
842ab88d97734f1260402a0c6a8f6b77bed7ed4e295John McCallvoid DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
843cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // FIXME: This feels like a hack. Should DeclarationName support
844cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // template-ids, or is there a better way to keep specializations
845cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // from being visible?
846cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  if (isa<ClassTemplateSpecializationDecl>(D))
847cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
8486bc20135a2c46f97da15994095616a305be35c6aEli Friedman  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
8496bc20135a2c46f97da15994095616a305be35c6aEli Friedman    if (FD->isFunctionTemplateSpecialization())
8506bc20135a2c46f97da15994095616a305be35c6aEli Friedman      return;
851cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
8520701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
85344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this) {
854ab88d97734f1260402a0c6a8f6b77bed7ed4e295John McCall    PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
85544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return;
85644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
85744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
85844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // If we already have a lookup data structure, perform the insertion
85944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // into it. Otherwise, be lazy and don't build that structure until
86044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // someone asks for it.
861ab88d97734f1260402a0c6a8f6b77bed7ed4e295John McCall  if (LookupPtr || !Recoverable)
86217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    makeDeclVisibleInContextImpl(D);
863074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
864074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // If we are a transparent context, insert into our parent context,
865074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // too. This operation is recursive.
866074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (isTransparentContext())
867ab88d97734f1260402a0c6a8f6b77bed7ed4e295John McCall    getParent()->makeDeclVisibleInContext(D, Recoverable);
86844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
86944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
87017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidisvoid DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
871074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // Skip unnamed declarations.
872074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (!D->getDeclName())
873074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return;
874074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
875cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // FIXME: This feels like a hack. Should DeclarationName support
876cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // template-ids, or is there a better way to keep specializations
877cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // from being visible?
878cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  if (isa<ClassTemplateSpecializationDecl>(D))
879cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
880cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
8813478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ASTContext *C = 0;
8823478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  if (!LookupPtr) {
8833478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek    C = &getParentASTContext();
8840c01d18094100db92d38daa923c95661512db203John McCall    CreateStoredDeclsMap(*C);
8853478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  }
88644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
88744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // Insert this declaration into the map.
8880c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
88967762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  if (DeclNameEntries.isNull()) {
89067762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    DeclNameEntries.setOnlyValue(D);
891bd6c80037626a37ce3936a36d9ae287f475845b7Chris Lattner    return;
89244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
89391942501b6f71a41d3a09bedec19be479832c718Chris Lattner
894bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // If it is possible that this is a redeclaration, check to see if there is
895bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // already a decl for which declarationReplaces returns true.  If there is
896bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // one, just replace it and return.
8973478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  if (!C)
8983478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek    C = &getParentASTContext();
8993478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
9003478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  if (DeclNameEntries.HandleRedeclaration(*C, D))
90167762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    return;
9021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
903bd6c80037626a37ce3936a36d9ae287f475845b7Chris Lattner  // Put this declaration into the appropriate slot.
90467762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  DeclNameEntries.AddSubsequentDecl(D);
90544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
9062a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
9072a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
9082a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// this context.
9091eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeclContext::udir_iterator_range
91017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios KyrtzidisDeclContext::getUsingDirectives() const {
91117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
9122a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
9132a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor                             reinterpret_cast<udir_iterator>(Result.second));
9142a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor}
9152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid StoredDeclsList::materializeDecls(ASTContext &Context) {
9172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (isNull())
9182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
9192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  switch ((DataKind)(Data & 0x03)) {
9212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_Decl:
9222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_Decl_Vector:
9232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
9242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_DeclID: {
9262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // Resolve this declaration ID to an actual declaration by
9272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // querying the external AST source.
9282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    unsigned DeclID = Data >> 2;
9292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalASTSource *Source = Context.getExternalSource();
9312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    assert(Source && "No external AST source available!");
9322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
9342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
9352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
9362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_ID_Vector: {
9382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // We have a vector of declaration IDs. Resolve all of them to
9392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // actual declarations.
9402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    VectorTy &Vector = *getAsVector();
9412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalASTSource *Source = Context.getExternalSource();
9422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    assert(Source && "No external AST source available!");
9432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    for (unsigned I = 0, N = Vector.size(); I != N; ++I)
9452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
9462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
9472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Data = (Data & ~0x03) | DK_Decl_Vector;
9482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
9492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
9502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
9512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
9523478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
9533478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
9543478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek// Creation and Destruction of StoredDeclsMaps.                               //
9553478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek//===----------------------------------------------------------------------===//
9563478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
9570c01d18094100db92d38daa923c95661512db203John McCallStoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
9580c01d18094100db92d38daa923c95661512db203John McCall  assert(!LookupPtr && "context already has a decls map");
9590c01d18094100db92d38daa923c95661512db203John McCall  assert(getPrimaryContext() == this &&
9600c01d18094100db92d38daa923c95661512db203John McCall         "creating decls map on non-primary context");
9610c01d18094100db92d38daa923c95661512db203John McCall
9620c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap *M;
9630c01d18094100db92d38daa923c95661512db203John McCall  bool Dependent = isDependentContext();
9640c01d18094100db92d38daa923c95661512db203John McCall  if (Dependent)
9650c01d18094100db92d38daa923c95661512db203John McCall    M = new DependentStoredDeclsMap();
9660c01d18094100db92d38daa923c95661512db203John McCall  else
9670c01d18094100db92d38daa923c95661512db203John McCall    M = new StoredDeclsMap();
9680c01d18094100db92d38daa923c95661512db203John McCall  M->Previous = C.LastSDM;
9690c01d18094100db92d38daa923c95661512db203John McCall  C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
9700c01d18094100db92d38daa923c95661512db203John McCall  LookupPtr = M;
9713478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  return M;
9723478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
9733478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
9743478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenekvoid ASTContext::ReleaseDeclContextMaps() {
9750c01d18094100db92d38daa923c95661512db203John McCall  // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
9760c01d18094100db92d38daa923c95661512db203John McCall  // pointer because the subclass doesn't add anything that needs to
9770c01d18094100db92d38daa923c95661512db203John McCall  // be deleted.
9780c01d18094100db92d38daa923c95661512db203John McCall
9790c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
9800c01d18094100db92d38daa923c95661512db203John McCall}
9810c01d18094100db92d38daa923c95661512db203John McCall
9820c01d18094100db92d38daa923c95661512db203John McCallvoid StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
9830c01d18094100db92d38daa923c95661512db203John McCall  while (Map) {
9840c01d18094100db92d38daa923c95661512db203John McCall    // Advance the iteration before we invalidate memory.
9850c01d18094100db92d38daa923c95661512db203John McCall    llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
9860c01d18094100db92d38daa923c95661512db203John McCall
9870c01d18094100db92d38daa923c95661512db203John McCall    if (Dependent)
9880c01d18094100db92d38daa923c95661512db203John McCall      delete static_cast<DependentStoredDeclsMap*>(Map);
9890c01d18094100db92d38daa923c95661512db203John McCall    else
9900c01d18094100db92d38daa923c95661512db203John McCall      delete Map;
9910c01d18094100db92d38daa923c95661512db203John McCall
9920c01d18094100db92d38daa923c95661512db203John McCall    Map = Next.getPointer();
9930c01d18094100db92d38daa923c95661512db203John McCall    Dependent = Next.getInt();
9940c01d18094100db92d38daa923c95661512db203John McCall  }
9950c01d18094100db92d38daa923c95661512db203John McCall}
9960c01d18094100db92d38daa923c95661512db203John McCall
9970c01d18094100db92d38daa923c95661512db203John McCallDependentStoredDeclsMap::~DependentStoredDeclsMap() {
9980c01d18094100db92d38daa923c95661512db203John McCall  // Kill off the dependent diagnostics.  They don't need to be
9990c01d18094100db92d38daa923c95661512db203John McCall  // deleted, but they do need to be destructed.
10000c01d18094100db92d38daa923c95661512db203John McCall  DependentDiagnostic *CurD = FirstDiagnostic;
10010c01d18094100db92d38daa923c95661512db203John McCall  while (CurD) {
10020c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *NextD = CurD->NextDiagnostic;
10030c01d18094100db92d38daa923c95661512db203John McCall    CurD->~DependentDiagnostic();
10040c01d18094100db92d38daa923c95661512db203John McCall    CurD = NextD;
10050c01d18094100db92d38daa923c95661512db203John McCall  }
10060c01d18094100db92d38daa923c95661512db203John McCall}
10070c01d18094100db92d38daa923c95661512db203John McCall
10080c01d18094100db92d38daa923c95661512db203John McCallDependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
10090c01d18094100db92d38daa923c95661512db203John McCall                                                 DeclContext *Parent,
10100c01d18094100db92d38daa923c95661512db203John McCall                                           const PartialDiagnostic &PDiag) {
10110c01d18094100db92d38daa923c95661512db203John McCall  assert(Parent->isDependentContext()
10120c01d18094100db92d38daa923c95661512db203John McCall         && "cannot iterate dependent diagnostics of non-dependent context");
10130c01d18094100db92d38daa923c95661512db203John McCall  Parent = Parent->getPrimaryContext();
10140c01d18094100db92d38daa923c95661512db203John McCall  if (!Parent->LookupPtr)
10150c01d18094100db92d38daa923c95661512db203John McCall    Parent->CreateStoredDeclsMap(C);
10160c01d18094100db92d38daa923c95661512db203John McCall
10170c01d18094100db92d38daa923c95661512db203John McCall  DependentStoredDeclsMap *Map
10180c01d18094100db92d38daa923c95661512db203John McCall    = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
10190c01d18094100db92d38daa923c95661512db203John McCall
1020fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  // FIXME: Allocate the copy of the PartialDiagnostic via the ASTContext's
1021fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  // BumpPtrAllocator, rather than the ASTContext itself.
10220c01d18094100db92d38daa923c95661512db203John McCall  DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag);
10230c01d18094100db92d38daa923c95661512db203John McCall
10240c01d18094100db92d38daa923c95661512db203John McCall  // TODO: Maybe we shouldn't reverse the order during insertion.
10250c01d18094100db92d38daa923c95661512db203John McCall  DD->NextDiagnostic = Map->FirstDiagnostic;
10260c01d18094100db92d38daa923c95661512db203John McCall  Map->FirstDiagnostic = DD;
10270c01d18094100db92d38daa923c95661512db203John McCall
10280c01d18094100db92d38daa923c95661512db203John McCall  return DD;
10293478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek}
1030