DeclBase.cpp revision 40b598eea1310ec9ed554d56ce3e25b34c585458
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"
18aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclObjC.h"
19aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "clang/AST/ASTContext.h"
2244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor#include "clang/AST/Type.h"
23d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl#include "clang/AST/Stmt.h"
24d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl#include "clang/AST/StmtCXX.h"
2556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "llvm/ADT/DenseMap.h"
2649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner#include "llvm/Support/raw_ostream.h"
276ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor#include <algorithm>
283daed52a57d03765223021f5f921bdc280c8f3ccChris Lattner#include <cstdio>
293fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor#include <vector>
3056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanusing namespace clang;
3156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//  Statistics
3456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) static int n##Derived##s = 0;
3764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
3856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanstatic bool StatSwitch = false;
4056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
4156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanconst char *Decl::getDeclKindName() const {
4256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (DeclKind) {
4364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration not in DeclNodes.def!");
4464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) case Derived: return #Derived;
4564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
4656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
4756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
4856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
490a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroffconst char *DeclContext::getDeclKindName() const {
500a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  switch (DeclKind) {
5164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration context not in DeclNodes.def!");
521ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis#define DECL(Derived, Base) case Decl::Derived: return #Derived;
5364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
540a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  }
550a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff}
560a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff
5756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanbool Decl::CollectingStats(bool Enable) {
5856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (Enable)
5956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    StatSwitch = true;
6056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  return StatSwitch;
6156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
6256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
6356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::PrintStats() {
6456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  fprintf(stderr, "*** Decl Stats:\n");
6556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
6664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalDecls = 0;
6764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) totalDecls += n##Derived##s;
6864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
6964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  fprintf(stderr, "  %d decls total.\n", totalDecls);
7064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor
7164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalBytes = 0;
7264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base)                                             \
7364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  if (n##Derived##s > 0) {                                              \
7464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl));         \
7564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    fprintf(stderr, "    %d " #Derived " decls, %d each (%d bytes)\n",  \
7664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor            n##Derived##s, (int)sizeof(Derived##Decl),                  \
7764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor            (int)(n##Derived##s * sizeof(Derived##Decl)));              \
7864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  }
7964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
8056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
8164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", totalBytes);
8256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
8356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
8456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::addDeclKind(Kind k) {
8556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (k) {
8664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration not in DeclNodes.def!");
8764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
8864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
8956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
9056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
9156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
9267e332009c6e349dc34700f539747afcff990336Anders Carlssonbool Decl::isTemplateParameterPack() const {
9367e332009c6e349dc34700f539747afcff990336Anders Carlsson  if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
9467e332009c6e349dc34700f539747afcff990336Anders Carlsson    return TTP->isParameterPack();
9567e332009c6e349dc34700f539747afcff990336Anders Carlsson
9667e332009c6e349dc34700f539747afcff990336Anders Carlsson  return false;
9767e332009c6e349dc34700f539747afcff990336Anders Carlsson}
9867e332009c6e349dc34700f539747afcff990336Anders Carlsson
99e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool Decl::isFunctionOrFunctionTemplate() const {
10058badb7a655d021fc67bb7ed0af95d6ea0c63eb1Anders Carlsson  if (const UsingDecl *UD = dyn_cast<UsingDecl>(this))
10158badb7a655d021fc67bb7ed0af95d6ea0c63eb1Anders Carlsson    return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
10258badb7a655d021fc67bb7ed0af95d6ea0c63eb1Anders Carlsson
103e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
104e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
105e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
10656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
10749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner// PrettyStackTraceDecl Implementation
10849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
10949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
11049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattnervoid PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
11149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation TheLoc = Loc;
11249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isInvalid() && TheDecl)
11349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc = TheDecl->getLocation();
11449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
11549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isValid()) {
11649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc.print(OS, SM);
11749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << ": ";
11849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  }
11949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
12049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << Message;
12149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
12249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
12349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << " '" << DN->getQualifiedNameAsString() << '\'';
12449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << '\n';
12549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner}
12649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
12749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
12856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// Decl Implementation
12956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
13056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
131769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner// Out-of-line virtual method providing a home for Decl.
132769dbdf467681f6020ff248b969c2d41a4fdccd3Chris LattnerDecl::~Decl() {
133769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  if (isOutOfSemaDC())
134769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    delete getMultipleDC();
135769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
136769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  assert(!HasAttrs && "attributes should have been freed by Destroy");
137769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner}
138769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
1394afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setDeclContext(DeclContext *DC) {
1404afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isOutOfSemaDC())
1414afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    delete getMultipleDC();
1424afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
143ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  DeclCtx = DC;
1444afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
1454afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1464afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setLexicalDeclContext(DeclContext *DC) {
1474afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (DC == getLexicalDeclContext())
1484afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return;
1494afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1504afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isInSemaDC()) {
1514afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MultipleDC *MDC = new MultipleDC();
1524afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MDC->SemanticDC = getDeclContext();
1534afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MDC->LexicalDC = DC;
154ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    DeclCtx = MDC;
1554afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  } else {
1564afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    getMultipleDC()->LexicalDC = DC;
1574afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
1584afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
1594afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1603708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisTranslationUnitDecl *Decl::getTranslationUnitDecl() {
1613708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  DeclContext *DC = getDeclContext();
1623708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  assert(DC && "This decl is not contained in a translation unit!");
1633708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
1643708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  while (!DC->isTranslationUnit()) {
1653708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    DC = DC->getParent();
1663708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    assert(DC && "This decl is not contained in a translation unit!");
1673708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  }
1683708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
1693708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  return cast<TranslationUnitDecl>(DC);
1703708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
1713708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
1723708b3df2e86998dca4c006939014ea1174da834Argyrios KyrtzidisASTContext &Decl::getASTContext() const {
1733708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  return getTranslationUnitDecl()->getASTContext();
1743708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis}
1753708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
176769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattnerunsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
177769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  switch (DeclKind) {
178769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    default:
179769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
180769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner        return IDNS_Ordinary;
181769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      assert(0 && "Unknown decl kind!");
182769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case OverloadedFunction:
183769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Typedef:
184769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case EnumConstant:
185769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Var:
186769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ImplicitParam:
187769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ParmVar:
188769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case OriginalParmVar:
189769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case NonTypeTemplateParm:
1909cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor    case Using:
191769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCMethod:
192769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCContainer:
193769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCCategory:
194769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCInterface:
195769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProperty:
196769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCCompatibleAlias:
197769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Ordinary;
198769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
199769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProtocol:
2008fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCProtocol;
201769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
2028fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    case ObjCImplementation:
2038fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCImplementation;
2048fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor
2058fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    case ObjCCategoryImpl:
2068fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCCategoryImpl;
2078fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor
208769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Field:
209769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCAtDefsField:
210769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCIvar:
211769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Member;
212769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
213769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Record:
214769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case CXXRecord:
215769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Enum:
216769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTypeParm:
217769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Tag;
218769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
219769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Namespace:
220769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Template:
221769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FunctionTemplate:
222769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplate:
223769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTemplateParm:
224faf0e872f3409ecafbc458eabb22be76f79cb050Anders Carlsson    case NamespaceAlias:
225769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Tag | IDNS_Ordinary;
226769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
227769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Never have names.
228769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case LinkageSpec:
229769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FileScopeAsm:
230769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case StaticAssert:
231769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCClass:
232769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCPropertyImpl:
233769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCForwardProtocol:
234769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Block:
235769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TranslationUnit:
236769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
237769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Aren't looked up?
238769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case UsingDirective:
239769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplateSpecialization:
240c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    case ClassTemplatePartialSpecialization:
241769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return 0;
242769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  }
24356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
24456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
24540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisvoid Decl::addAttr(Attr *NewAttr) {
24640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
24756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
24856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  NewAttr->setNext(ExistingAttr);
24956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  ExistingAttr = NewAttr;
25056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
25156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = true;
25256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
25356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
25440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisvoid Decl::invalidateAttrs() {
25556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasAttrs) return;
25668584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor
25756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = false;
25840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  getASTContext().eraseDeclAttrs(this);
25956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
26056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
26140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisconst Attr *Decl::getAttrsImpl() const {
26281abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattner  assert(HasAttrs && "getAttrs() should verify this!");
26340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  return getASTContext().getDeclAttrs(this);
26456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
26556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
26640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisvoid Decl::swapAttrs(Decl *RHS) {
26756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasLHSAttr = this->HasAttrs;
26856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasRHSAttr = RHS->HasAttrs;
26956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
27056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Usually, neither decl has attrs, nothing to do.
27156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr && !HasRHSAttr) return;
27256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
27356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // If 'this' has no attrs, swap the other way.
27456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr)
27540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    return RHS->swapAttrs(this);
27640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis
27740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  ASTContext &Context = getASTContext();
27856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
27956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Handle the case when both decls have attrs.
28056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (HasRHSAttr) {
28168584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
28256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    return;
28356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
28456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
28556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Otherwise, LHS has an attr and RHS doesn't.
28668584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
28768584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  Context.eraseDeclAttrs(this);
28856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  this->HasAttrs = false;
28956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  RHS->HasAttrs = true;
29056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
29156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
29256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
293cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattnervoid Decl::Destroy(ASTContext &C) {
294cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  // Free attributes for this decl.
295cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  if (HasAttrs) {
29668584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    C.getDeclAttrs(this)->Destroy(C);
29740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    invalidateAttrs();
298cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner    HasAttrs = false;
299cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  }
300cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner
301a0fc55f3e9d7d7aa8761d0a9726033947d0d6bc0Douglas Gregor#if 0
30200ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor  // FIXME: Once ownership is fully understood, we can enable this code
30300ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor  if (DeclContext *DC = dyn_cast<DeclContext>(this))
30400ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor    DC->decls_begin()->Destroy(C);
30556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
306244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  // Observe the unrolled recursion.  By setting N->NextDeclInContext = 0x0
3074afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // within the loop, only the Destroy method for the first Decl
3084afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // will deallocate all of the Decls in a chain.
3094afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
310244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  Decl* N = getNextDeclInContext();
3114afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
3124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  while (N) {
313244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    Decl* Tmp = N->getNextDeclInContext();
314244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    N->NextDeclInContext = 0;
3154afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    N->Destroy(C);
3164afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    N = Tmp;
31756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
318a0fc55f3e9d7d7aa8761d0a9726033947d0d6bc0Douglas Gregor
31956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  this->~Decl();
3203e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate((void *)this);
32100ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor#endif
32256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
32356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
32442220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDecl *Decl::castFromDeclContext (const DeclContext *D) {
3253d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getDeclKind();
3263d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
3273d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) \
3283d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    case Decl::Name:     \
3293d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
3303d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
3313d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3323d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
3333d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                                   \
3343d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (DK >= Decl::Name##First && DK <= Decl::Name##Last)    \
3353d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
3363d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3373d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      assert(false && "a decl that inherits DeclContext isn't handled");
3383d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return 0;
3393d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
34042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
34142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
34242220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDeclContext *Decl::castToDeclContext(const Decl *D) {
3433d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getKind();
3443d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
3453d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) \
3463d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    case Decl::Name:     \
3473d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return static_cast<Name##Decl*>(const_cast<Decl*>(D));
3483d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
3493d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3503d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
3513d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                                   \
3523d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (DK >= Decl::Name##First && DK <= Decl::Name##Last)    \
3533d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return static_cast<Name##Decl*>(const_cast<Decl*>(D));
3543d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3553d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      assert(false && "a decl that inherits DeclContext isn't handled");
3563d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return 0;
3573d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
35842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
35942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
360d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian RedlCompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
361d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  return dyn_cast_or_null<CompoundStmt>(getBody(Context));
362d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl}
363d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
364d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian RedlSourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
365d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  Stmt *Body = getBody(Context);
366d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (!Body)
367d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    return SourceLocation();
368d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
369d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    return CS->getRBracLoc();
370d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  assert(isa<CXXTryStmt>(Body) &&
371d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl         "Body can only be CompoundStmt or CXXTryStmt");
372d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
373d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl}
374d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
3751329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#ifndef NDEBUG
3761329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlssonvoid Decl::CheckAccessDeclContext() const {
3775c27f2bfd2e19429dd9206f9b019e11d4a570acfDouglas Gregor  assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
3785c27f2bfd2e19429dd9206f9b019e11d4a570acfDouglas Gregor          !isa<CXXRecordDecl>(getDeclContext())) &&
3791329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson         "Access specifier is AS_none inside a record decl");
3801329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson}
3811329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
3821329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#endif
3831329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
38456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
38556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// DeclContext Implementation
38656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
38756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3883d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidisbool DeclContext::classof(const Decl *D) {
3893d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch (D->getKind()) {
3903d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) case Decl::Name:
3913d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
3923d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3933d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return true;
3943d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
3953d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                   \
3963d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (D->getKind() >= Decl::Name##First &&  \
3973d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis          D->getKind() <= Decl::Name##Last)     \
3983d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return true;
3993d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
4003d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return false;
4013d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
4023d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis}
4033d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis
40444b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext::~DeclContext() {
405c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  delete static_cast<StoredDeclsMap*>(LookupPtr);
40644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
40744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
40844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorvoid DeclContext::DestroyDecls(ASTContext &C) {
4096ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  for (decl_iterator D = decls_begin(C); D != decls_end(C); )
41000ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor    (*D++)->Destroy(C);
41144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
41244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
413bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregorbool DeclContext::isDependentContext() const {
414bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (isFileContext())
415bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    return false;
416c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
417c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  if (isa<ClassTemplatePartialSpecializationDecl>(this))
418c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return true;
419bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
420bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
421bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Record->getDescribedClassTemplate())
422bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
423bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
424bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
425bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor    if (Function->getDescribedFunctionTemplate())
426bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor      return true;
427bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
428bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  return getParent() && getParent()->isDependentContext();
429bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor}
430bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
431074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregorbool DeclContext::isTransparentContext() const {
432074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (DeclKind == Decl::Enum)
433074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true; // FIXME: Check for C++0x scoped enums
434074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::LinkageSpec)
435074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true;
43665100792a69a16895bd80f1d639b99e7ad903386Douglas Gregor  else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
437bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor    return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
438074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::Namespace)
439074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return false; // FIXME: Check for C++0x inline namespaces
440074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
441074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  return false;
442074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
443074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
4440701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve NaroffDeclContext *DeclContext::getPrimaryContext() {
44544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
44644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::TranslationUnit:
447074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::LinkageSpec:
448074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::Block:
44944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // There is only one DeclContext for these entities.
45044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
45144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
45244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
45344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // The original namespace is our primary context.
45444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
45544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
45644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCMethod:
45744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
45844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
45944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCInterface:
4600701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCProtocol:
4610701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategory:
46244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // FIXME: Can Objective-C interfaces be forward-declared?
46344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
46444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4650701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCImplementation:
4660701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategoryImpl:
4670701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff    return this;
4680701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff
46944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
470cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
471cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // If this is a tag type that has a definition or is currently
472cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // being defined, that definition is our primary context.
473244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner      if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
474cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor        if (TagT->isBeingDefined() ||
475cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor            (TagT->getDecl() && TagT->getDecl()->isDefinition()))
476cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor          return TagT->getDecl();
477cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      return this;
478cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    }
479cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
48044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
48144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          "Unknown DeclContext kind");
48244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
48344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
48444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
48544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
48644b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext *DeclContext::getNextContext() {
48744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
48844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
48944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Return the next namespace
49044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getNextNamespace();
49144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
49244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
49344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return 0;
49444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
49544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
49644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Load the declarations within this lexical storage from an
4982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// external source.
4992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid
5002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorDeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
5012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalASTSource *Source = Context.getExternalSource();
5022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalLexicalStorage() && Source && "No external storage?");
5032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
504b0156ea412df1c2eb12d620054a404da71784cf5Eli Friedman  llvm::SmallVector<uint32_t, 64> Decls;
5052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
5062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                          Decls))
5072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
5082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // There is no longer any lexical storage in this context
5102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalLexicalStorage = false;
5112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Decls.empty())
5132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
5142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Resolve all of the declaration IDs into declarations, building up
5162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // a chain of declarations via the Decl::NextDeclInContext field.
5172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Decl *FirstNewDecl = 0;
5182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Decl *PrevDecl = 0;
5192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
5202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Decl *D = Source->GetDecl(Decls[I]);
5212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (PrevDecl)
5222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      PrevDecl->NextDeclInContext = D;
5232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    else
5242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      FirstNewDecl = D;
5252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    PrevDecl = D;
5272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
5282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Splice the newly-read declarations into the beginning of the list
5302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // of declarations.
5312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  PrevDecl->NextDeclInContext = FirstDecl;
5322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  FirstDecl = FirstNewDecl;
5332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!LastDecl)
5342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LastDecl = PrevDecl;
5352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
5362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid
5382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorDeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
5392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  DeclContext *This = const_cast<DeclContext *>(this);
5402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalASTSource *Source = Context.getExternalSource();
5412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalVisibleStorage() && Source && "No external storage?");
5422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  llvm::SmallVector<VisibleDeclaration, 64> Decls;
5442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Source->ReadDeclsVisibleInContext(This, Decls))
5452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
5462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // There is no longer any visible storage in this context
5482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalVisibleStorage = false;
5492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Load the declaration IDs for all of the names visible in this
5512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // context.
5522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(!LookupPtr && "Have a lookup map before de-serialization?");
5532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  StoredDeclsMap *Map = new StoredDeclsMap;
5542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  LookupPtr = Map;
5552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
5562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
5572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
5582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
5592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5606ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
5612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
5622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LoadLexicalDeclsFromExternalStorage(Context);
5632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // FIXME: Check whether we need to load some declarations from
5652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // external storage.
5666ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  return decl_iterator(FirstDecl);
5676ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
5686ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
5696ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
5702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
5712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LoadLexicalDeclsFromExternalStorage(Context);
5722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5736ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  return decl_iterator();
5746ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
5756ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
5768038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregorbool DeclContext::decls_empty(ASTContext &Context) const {
5778038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  if (hasExternalLexicalStorage())
5788038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor    LoadLexicalDeclsFromExternalStorage(Context);
5798038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
5808038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  return !FirstDecl;
5818038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor}
5828038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
5836ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregorvoid DeclContext::addDecl(ASTContext &Context, Decl *D) {
5847f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner  assert(D->getLexicalDeclContext() == this &&
5857f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner         "Decl inserted into wrong lexical context");
586244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  assert(!D->getNextDeclInContext() && D != LastDecl &&
5876037fcba3431b47de1a994c9b286feac17894effDouglas Gregor         "Decl already inserted into a DeclContext");
5886037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5896037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  if (FirstDecl) {
590244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    LastDecl->NextDeclInContext = D;
5916037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    LastDecl = D;
5926037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  } else {
5936037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    FirstDecl = LastDecl = D;
5946037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  }
5954afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
5964afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
5976ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
59844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
59944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
600074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// buildLookup - Build the lookup data structure with all of the
601074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// declarations in DCtx (and any other contexts linked to it or
602074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// transparent contexts nested within it).
6036ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregorvoid DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
604074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  for (; DCtx; DCtx = DCtx->getNextContext()) {
6056ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    for (decl_iterator D = DCtx->decls_begin(Context),
6066ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                    DEnd = DCtx->decls_end(Context);
6074f3b8f8ac2f8c89028a2f8793df0a7887df809d4Douglas Gregor         D != DEnd; ++D) {
608074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      // Insert this declaration into the lookup structure
6094afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
6106ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor        makeDeclVisibleInContextImpl(Context, ND);
611074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
612074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      // If this declaration is itself a transparent declaration context,
613074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      // add its members (recursively).
614074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
615074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor        if (InnerCtx->isTransparentContext())
6166ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor          buildLookup(Context, InnerCtx->getPrimaryContext());
617074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    }
618074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  }
619074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
620074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
62144b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext::lookup_result
6226ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::lookup(ASTContext &Context, DeclarationName Name) {
6230701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
62444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this)
6256ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    return PrimaryContext->lookup(Context, Name);
62644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
6272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalVisibleStorage())
6282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LoadVisibleDeclsFromExternalStorage(Context);
6292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6303fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor  /// If there is no lookup data structure, build one now by walking
63144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// all of the linked DeclContexts (in declaration order!) and
63244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// inserting their values.
633c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (!LookupPtr) {
6346ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    buildLookup(Context, this);
63544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
636c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    if (!LookupPtr)
63791942501b6f71a41d3a09bedec19be479832c718Chris Lattner      return lookup_result(0, 0);
638c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  }
63944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
640c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
641c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  StoredDeclsMap::iterator Pos = Map->find(Name);
642c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (Pos == Map->end())
643c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    return lookup_result(0, 0);
6446ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  return Pos->second.getLookupResult(Context);
64544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
64644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
64744b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext::lookup_const_result
6486ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
6496ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  return const_cast<DeclContext*>(this)->lookup(Context, Name);
65044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
65144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
6520cf2b1990c82121d03a004dafe498ba43bf4b42aChris LattnerDeclContext *DeclContext::getLookupContext() {
6530cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *Ctx = this;
65472de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor  // Skip through transparent contexts.
655ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  while (Ctx->isTransparentContext())
656ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor    Ctx = Ctx->getParent();
657ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  return Ctx;
658ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor}
659ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor
66088b7094185b9d4fe9820c731b6936d8d37f6143eDouglas GregorDeclContext *DeclContext::getEnclosingNamespaceContext() {
66188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *Ctx = this;
66288b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  // Skip through non-namespace, non-translation-unit contexts.
66388b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  while (!Ctx->isFileContext() || Ctx->isTransparentContext())
66488b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    Ctx = Ctx->getParent();
66588b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  return Ctx->getPrimaryContext();
66688b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor}
66788b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
6686ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregorvoid DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
669cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // FIXME: This feels like a hack. Should DeclarationName support
670cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // template-ids, or is there a better way to keep specializations
671cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // from being visible?
672cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  if (isa<ClassTemplateSpecializationDecl>(D))
673cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
674cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
6750701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
67644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this) {
6776ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    PrimaryContext->makeDeclVisibleInContext(Context, D);
67844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return;
67944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
68044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
68144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // If we already have a lookup data structure, perform the insertion
68244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // into it. Otherwise, be lazy and don't build that structure until
68344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // someone asks for it.
684c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (LookupPtr)
6856ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    makeDeclVisibleInContextImpl(Context, D);
686074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
687074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // If we are a transparent context, insert into our parent context,
688074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // too. This operation is recursive.
689074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (isTransparentContext())
6906ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    getParent()->makeDeclVisibleInContext(Context, D);
69144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
69244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
6936ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregorvoid DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
6946ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                               NamedDecl *D) {
695074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // Skip unnamed declarations.
696074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (!D->getDeclName())
697074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return;
698074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
699cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // FIXME: This feels like a hack. Should DeclarationName support
700cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // template-ids, or is there a better way to keep specializations
701cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // from being visible?
702cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  if (isa<ClassTemplateSpecializationDecl>(D))
703cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
704cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
705c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (!LookupPtr)
706c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    LookupPtr = new StoredDeclsMap;
70744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
70844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // Insert this declaration into the map.
709c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
71067762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
71167762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  if (DeclNameEntries.isNull()) {
71267762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    DeclNameEntries.setOnlyValue(D);
713bd6c80037626a37ce3936a36d9ae287f475845b7Chris Lattner    return;
71444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
71591942501b6f71a41d3a09bedec19be479832c718Chris Lattner
716bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // If it is possible that this is a redeclaration, check to see if there is
717bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // already a decl for which declarationReplaces returns true.  If there is
718bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // one, just replace it and return.
7196ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  if (DeclNameEntries.HandleRedeclaration(Context, D))
72067762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    return;
72191942501b6f71a41d3a09bedec19be479832c718Chris Lattner
722bd6c80037626a37ce3936a36d9ae287f475845b7Chris Lattner  // Put this declaration into the appropriate slot.
72367762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  DeclNameEntries.AddSubsequentDecl(D);
72444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
7252a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7262a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
7272a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// this context.
7286ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::udir_iterator_range
7296ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::getUsingDirectives(ASTContext &Context) const {
7306ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
7312a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
7322a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor                             reinterpret_cast<udir_iterator>(Result.second));
7332a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor}
7342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid StoredDeclsList::materializeDecls(ASTContext &Context) {
7362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (isNull())
7372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
7382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  switch ((DataKind)(Data & 0x03)) {
7402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_Decl:
7412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_Decl_Vector:
7422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
7432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_DeclID: {
7452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // Resolve this declaration ID to an actual declaration by
7462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // querying the external AST source.
7472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    unsigned DeclID = Data >> 2;
7482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalASTSource *Source = Context.getExternalSource();
7502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    assert(Source && "No external AST source available!");
7512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
7532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
7542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
7552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_ID_Vector: {
7572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // We have a vector of declaration IDs. Resolve all of them to
7582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // actual declarations.
7592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    VectorTy &Vector = *getAsVector();
7602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalASTSource *Source = Context.getExternalSource();
7612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    assert(Source && "No external AST source available!");
7622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    for (unsigned I = 0, N = Vector.size(); I != N; ++I)
7642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
7652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Data = (Data & ~0x03) | DK_Decl_Vector;
7672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
7682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
7692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
7702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
771