DeclBase.cpp revision 8fc463adf0116fdcbff86e9cca11955aad1649fe
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"
2356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman#include "llvm/ADT/DenseMap.h"
2449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner#include "llvm/Support/raw_ostream.h"
256ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor#include <algorithm>
263daed52a57d03765223021f5f921bdc280c8f3ccChris Lattner#include <cstdio>
273fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor#include <vector>
2856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanusing namespace clang;
2956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//  Statistics
3256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
3356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) static int n##Derived##s = 0;
3564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
3656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanstatic bool StatSwitch = false;
3856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// This keeps track of all decl attributes. Since so few decls have attrs, we
4056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// keep them in a hash map instead of wasting space in the Decl class.
4156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmantypedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
4256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
4356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanstatic DeclAttrMapTy *DeclAttrs = 0;
4456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
4556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanconst char *Decl::getDeclKindName() const {
4656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (DeclKind) {
4764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration not in DeclNodes.def!");
4864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) case Derived: return #Derived;
4964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
5056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
5156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
5256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
530a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroffconst char *DeclContext::getDeclKindName() const {
540a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  switch (DeclKind) {
5564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration context not in DeclNodes.def!");
561ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis#define DECL(Derived, Base) case Decl::Derived: return #Derived;
5764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
580a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  }
590a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff}
600a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff
6156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanbool Decl::CollectingStats(bool Enable) {
6256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (Enable)
6356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    StatSwitch = true;
6456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  return StatSwitch;
6556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
6656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
6756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::PrintStats() {
6856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  fprintf(stderr, "*** Decl Stats:\n");
6956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
7064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalDecls = 0;
7164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) totalDecls += n##Derived##s;
7264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
7364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  fprintf(stderr, "  %d decls total.\n", totalDecls);
7464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor
7564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  int totalBytes = 0;
7664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base)                                             \
7764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  if (n##Derived##s > 0) {                                              \
7864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl));         \
7964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    fprintf(stderr, "    %d " #Derived " decls, %d each (%d bytes)\n",  \
8064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor            n##Derived##s, (int)sizeof(Derived##Decl),                  \
8164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor            (int)(n##Derived##s * sizeof(Derived##Decl)));              \
8264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  }
8364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
8456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
8564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", totalBytes);
8656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
8756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
8856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::addDeclKind(Kind k) {
8956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  switch (k) {
9064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  default: assert(0 && "Declaration not in DeclNodes.def!");
9164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
9264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
9356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
9456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
9556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
9656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
9749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner// PrettyStackTraceDecl Implementation
9849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
9949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
10049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattnervoid PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
10149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation TheLoc = Loc;
10249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isInvalid() && TheDecl)
10349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc = TheDecl->getLocation();
10449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
10549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (TheLoc.isValid()) {
10649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    TheLoc.print(OS, SM);
10749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << ": ";
10849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  }
10949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
11049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << Message;
11149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
11249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
11349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner    OS << " '" << DN->getQualifiedNameAsString() << '\'';
11449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  OS << '\n';
11549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner}
11649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
11749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner//===----------------------------------------------------------------------===//
11856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// Decl Implementation
11956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
12056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
121769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner// Out-of-line virtual method providing a home for Decl.
122769dbdf467681f6020ff248b969c2d41a4fdccd3Chris LattnerDecl::~Decl() {
123769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  if (isOutOfSemaDC())
124769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    delete getMultipleDC();
125769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
126769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  assert(!HasAttrs && "attributes should have been freed by Destroy");
127769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner}
128769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
1294afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setDeclContext(DeclContext *DC) {
1304afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isOutOfSemaDC())
1314afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    delete getMultipleDC();
1324afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
133ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  DeclCtx = DC;
1344afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
1354afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1364afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorvoid Decl::setLexicalDeclContext(DeclContext *DC) {
1374afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (DC == getLexicalDeclContext())
1384afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return;
1394afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1404afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (isInSemaDC()) {
1414afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MultipleDC *MDC = new MultipleDC();
1424afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MDC->SemanticDC = getDeclContext();
1434afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    MDC->LexicalDC = DC;
144ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    DeclCtx = MDC;
1454afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  } else {
1464afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    getMultipleDC()->LexicalDC = DC;
1474afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
1484afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
1494afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
150769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattnerunsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
151769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  switch (DeclKind) {
152769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    default:
153769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
154769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner        return IDNS_Ordinary;
155769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      assert(0 && "Unknown decl kind!");
156769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case OverloadedFunction:
157769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Typedef:
158769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case EnumConstant:
159769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Var:
160769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ImplicitParam:
161769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ParmVar:
162769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case OriginalParmVar:
163769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case NonTypeTemplateParm:
164769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCMethod:
165769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCContainer:
166769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCCategory:
167769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCInterface:
168769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProperty:
169769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCCompatibleAlias:
170769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Ordinary;
171769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
172769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCProtocol:
1738fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCProtocol;
174769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
1758fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    case ObjCImplementation:
1768fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCImplementation;
1778fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor
1788fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    case ObjCCategoryImpl:
1798fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor      return IDNS_ObjCCategoryImpl;
1808fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor
181769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Field:
182769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCAtDefsField:
183769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCIvar:
184769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Member;
185769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
186769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Record:
187769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case CXXRecord:
188769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Enum:
189769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTypeParm:
190769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Tag;
191769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
192769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Namespace:
193769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Template:
194769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FunctionTemplate:
195769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplate:
196769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TemplateTemplateParm:
197faf0e872f3409ecafbc458eabb22be76f79cb050Anders Carlsson    case NamespaceAlias:
198769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return IDNS_Tag | IDNS_Ordinary;
199769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
200769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Never have names.
201769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case LinkageSpec:
202769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case FileScopeAsm:
203769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case StaticAssert:
204769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCClass:
205769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCPropertyImpl:
206769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ObjCForwardProtocol:
207769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case Block:
208769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case TranslationUnit:
209769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
210769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    // Aren't looked up?
211769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case UsingDirective:
212769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    case ClassTemplateSpecialization:
213769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      return 0;
214769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  }
21556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
21656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
21756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::addAttr(Attr *NewAttr) {
21856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!DeclAttrs)
21956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    DeclAttrs = new DeclAttrMapTy();
22056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
22156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  Attr *&ExistingAttr = (*DeclAttrs)[this];
22256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
22356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  NewAttr->setNext(ExistingAttr);
22456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  ExistingAttr = NewAttr;
22556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
22656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = true;
22756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
22856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
22956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::invalidateAttrs() {
23056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasAttrs) return;
23156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
23256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  HasAttrs = false;
23356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  (*DeclAttrs)[this] = 0;
23456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  DeclAttrs->erase(this);
23556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
23656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (DeclAttrs->empty()) {
23756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    delete DeclAttrs;
23856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    DeclAttrs = 0;
23956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
24056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
24156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
24281abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattnerconst Attr *Decl::getAttrsImpl() const {
24381abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattner  assert(HasAttrs && "getAttrs() should verify this!");
24456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  return (*DeclAttrs)[this];
24556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
24656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
24756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedmanvoid Decl::swapAttrs(Decl *RHS) {
24856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasLHSAttr = this->HasAttrs;
24956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  bool HasRHSAttr = RHS->HasAttrs;
25056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
25156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Usually, neither decl has attrs, nothing to do.
25256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr && !HasRHSAttr) return;
25356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
25456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // If 'this' has no attrs, swap the other way.
25556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (!HasLHSAttr)
25656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    return RHS->swapAttrs(this);
25756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
25856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Handle the case when both decls have attrs.
25956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  if (HasRHSAttr) {
26056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
26156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman    return;
26256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
26356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
26456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  // Otherwise, LHS has an attr and RHS doesn't.
26556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
26656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  (*DeclAttrs).erase(this);
26756d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  this->HasAttrs = false;
26856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  RHS->HasAttrs = true;
26956d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
27056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
27156d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
272cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattnervoid Decl::Destroy(ASTContext &C) {
273cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  // Free attributes for this decl.
274cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  if (HasAttrs) {
275cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner    DeclAttrMapTy::iterator it = DeclAttrs->find(this);
276cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner    assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
277cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner
278cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner    // release attributes.
279cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner    it->second->Destroy(C);
280cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner    invalidateAttrs();
281cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner    HasAttrs = false;
282cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner  }
283cc5814732edc0c382d0136ab57ec6149566043e2Chris Lattner
284a0fc55f3e9d7d7aa8761d0a9726033947d0d6bc0Douglas Gregor#if 0
28500ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor  // FIXME: Once ownership is fully understood, we can enable this code
28600ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor  if (DeclContext *DC = dyn_cast<DeclContext>(this))
28700ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor    DC->decls_begin()->Destroy(C);
28856d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
289244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  // Observe the unrolled recursion.  By setting N->NextDeclInContext = 0x0
2904afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // within the loop, only the Destroy method for the first Decl
2914afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // will deallocate all of the Decls in a chain.
2924afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
293244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  Decl* N = getNextDeclInContext();
2944afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2954afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  while (N) {
296244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    Decl* Tmp = N->getNextDeclInContext();
297244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    N->NextDeclInContext = 0;
2984afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    N->Destroy(C);
2994afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    N = Tmp;
30056d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  }
301a0fc55f3e9d7d7aa8761d0a9726033947d0d6bc0Douglas Gregor
30256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman  this->~Decl();
3033e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate((void *)this);
30400ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor#endif
30556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman}
30656d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
30742220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDecl *Decl::castFromDeclContext (const DeclContext *D) {
3083d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getDeclKind();
3093d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
3103d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) \
3113d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    case Decl::Name:     \
3123d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
3133d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
3143d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3153d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
3163d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                                   \
3173d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (DK >= Decl::Name##First && DK <= Decl::Name##Last)    \
3183d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
3193d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3203d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      assert(false && "a decl that inherits DeclContext isn't handled");
3213d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return 0;
3223d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
32342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
32442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
32542220c5432c141d47cc8ce786e472b49dc907378Argyrios KyrtzidisDeclContext *Decl::castToDeclContext(const Decl *D) {
3263d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  Decl::Kind DK = D->getKind();
3273d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch(DK) {
3283d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) \
3293d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    case Decl::Name:     \
3303d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return static_cast<Name##Decl*>(const_cast<Decl*>(D));
3313d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
3323d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3333d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
3343d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                                   \
3353d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (DK >= Decl::Name##First && DK <= Decl::Name##Last)    \
3363d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return static_cast<Name##Decl*>(const_cast<Decl*>(D));
3373d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3383d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      assert(false && "a decl that inherits DeclContext isn't handled");
3393d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return 0;
3403d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
34142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis}
34242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
3431329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#ifndef NDEBUG
3441329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlssonvoid Decl::CheckAccessDeclContext() const {
3455c27f2bfd2e19429dd9206f9b019e11d4a570acfDouglas Gregor  assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
3465c27f2bfd2e19429dd9206f9b019e11d4a570acfDouglas Gregor          !isa<CXXRecordDecl>(getDeclContext())) &&
3471329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson         "Access specifier is AS_none inside a record decl");
3481329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson}
3491329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
3501329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#endif
3511329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
35256d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
35356d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman// DeclContext Implementation
35456d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman//===----------------------------------------------------------------------===//
35556d29376459f88dcdbcbf6c9a83c2f77e433f1e2Eli Friedman
3563d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidisbool DeclContext::classof(const Decl *D) {
3573d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  switch (D->getKind()) {
3583d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT(Name) case Decl::Name:
3593d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)
3603d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3613d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return true;
3623d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis    default:
3633d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#define DECL_CONTEXT_BASE(Name)                   \
3643d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      if (D->getKind() >= Decl::Name##First &&  \
3653d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis          D->getKind() <= Decl::Name##Last)     \
3663d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis        return true;
3673d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis#include "clang/AST/DeclNodes.def"
3683d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis      return false;
3693d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  }
3703d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis}
3713d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis
37244b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext::~DeclContext() {
373c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  delete static_cast<StoredDeclsMap*>(LookupPtr);
37444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
37544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
37644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorvoid DeclContext::DestroyDecls(ASTContext &C) {
3776ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  for (decl_iterator D = decls_begin(C); D != decls_end(C); )
37800ad0ef8369ee65337ff29c8db3c1841a01102c4Douglas Gregor    (*D++)->Destroy(C);
37944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
38044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
381074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregorbool DeclContext::isTransparentContext() const {
382074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (DeclKind == Decl::Enum)
383074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true; // FIXME: Check for C++0x scoped enums
384074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::LinkageSpec)
385074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return true;
38665100792a69a16895bd80f1d639b99e7ad903386Douglas Gregor  else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
387bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor    return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
388074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  else if (DeclKind == Decl::Namespace)
389074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return false; // FIXME: Check for C++0x inline namespaces
390074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
391074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  return false;
392074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
393074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
3940701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve NaroffDeclContext *DeclContext::getPrimaryContext() {
39544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
39644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::TranslationUnit:
397074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::LinkageSpec:
398074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  case Decl::Block:
39944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // There is only one DeclContext for these entities.
40044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
40144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
40244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
40344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // The original namespace is our primary context.
40444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
40544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
40644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCMethod:
40744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
40844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
40944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::ObjCInterface:
4100701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCProtocol:
4110701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategory:
41244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // FIXME: Can Objective-C interfaces be forward-declared?
41344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
41444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4150701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCImplementation:
4160701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  case Decl::ObjCCategoryImpl:
4170701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff    return this;
4180701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff
41944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
420cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
421cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // If this is a tag type that has a definition or is currently
422cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      // being defined, that definition is our primary context.
423244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner      if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
424cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor        if (TagT->isBeingDefined() ||
425cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor            (TagT->getDecl() && TagT->getDecl()->isDefinition()))
426cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor          return TagT->getDecl();
427cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor      return this;
428cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    }
429cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
43044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
43144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          "Unknown DeclContext kind");
43244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return this;
43344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
43444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
43544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
43644b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext *DeclContext::getNextContext() {
43744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  switch (DeclKind) {
43844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  case Decl::Namespace:
43944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Return the next namespace
44044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return static_cast<NamespaceDecl*>(this)->getNextNamespace();
44144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
44244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  default:
44344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return 0;
44444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
44544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
44644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Load the declarations within this lexical storage from an
4482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// external source.
4492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid
4502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorDeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
4512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalASTSource *Source = Context.getExternalSource();
4522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalLexicalStorage() && Source && "No external storage?");
4532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  llvm::SmallVector<unsigned, 64> Decls;
4552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
4562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                          Decls))
4572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
4582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // There is no longer any lexical storage in this context
4602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalLexicalStorage = false;
4612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Decls.empty())
4632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
4642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Resolve all of the declaration IDs into declarations, building up
4662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // a chain of declarations via the Decl::NextDeclInContext field.
4672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Decl *FirstNewDecl = 0;
4682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Decl *PrevDecl = 0;
4692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
4702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Decl *D = Source->GetDecl(Decls[I]);
4712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (PrevDecl)
4722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      PrevDecl->NextDeclInContext = D;
4732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    else
4742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      FirstNewDecl = D;
4752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    PrevDecl = D;
4772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
4782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Splice the newly-read declarations into the beginning of the list
4802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // of declarations.
4812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  PrevDecl->NextDeclInContext = FirstDecl;
4822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  FirstDecl = FirstNewDecl;
4832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!LastDecl)
4842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LastDecl = PrevDecl;
4852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
4862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid
4882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorDeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
4892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  DeclContext *This = const_cast<DeclContext *>(this);
4902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalASTSource *Source = Context.getExternalSource();
4912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(hasExternalVisibleStorage() && Source && "No external storage?");
4922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  llvm::SmallVector<VisibleDeclaration, 64> Decls;
4942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (Source->ReadDeclsVisibleInContext(This, Decls))
4952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
4962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // There is no longer any visible storage in this context
4982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalVisibleStorage = false;
4992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Load the declaration IDs for all of the names visible in this
5012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // context.
5022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(!LookupPtr && "Have a lookup map before de-serialization?");
5032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  StoredDeclsMap *Map = new StoredDeclsMap;
5042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  LookupPtr = Map;
5052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
5062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
5072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
5082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
5092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5106ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
5112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
5122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LoadLexicalDeclsFromExternalStorage(Context);
5132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // FIXME: Check whether we need to load some declarations from
5152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // external storage.
5166ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  return decl_iterator(FirstDecl);
5176ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
5186ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
5196ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
5202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalLexicalStorage())
5212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LoadLexicalDeclsFromExternalStorage(Context);
5222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5236ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  return decl_iterator();
5246ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor}
5256ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
5268038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregorbool DeclContext::decls_empty(ASTContext &Context) const {
5278038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  if (hasExternalLexicalStorage())
5288038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor    LoadLexicalDeclsFromExternalStorage(Context);
5298038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
5308038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  return !FirstDecl;
5318038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor}
5328038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor
5336ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregorvoid DeclContext::addDecl(ASTContext &Context, Decl *D) {
5347f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner  assert(D->getLexicalDeclContext() == this &&
5357f0be13b435ad110f99af83a24a50f43225f3083Chris Lattner         "Decl inserted into wrong lexical context");
536244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  assert(!D->getNextDeclInContext() && D != LastDecl &&
5376037fcba3431b47de1a994c9b286feac17894effDouglas Gregor         "Decl already inserted into a DeclContext");
5386037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5396037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  if (FirstDecl) {
540244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner    LastDecl->NextDeclInContext = D;
5416037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    LastDecl = D;
5426037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  } else {
5436037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    FirstDecl = LastDecl = D;
5446037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  }
5454afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
5464afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
5476ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
54844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
54944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
550074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// buildLookup - Build the lookup data structure with all of the
551074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// declarations in DCtx (and any other contexts linked to it or
552074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor/// transparent contexts nested within it).
5536ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregorvoid DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
554074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  for (; DCtx; DCtx = DCtx->getNextContext()) {
5556ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    for (decl_iterator D = DCtx->decls_begin(Context),
5566ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                    DEnd = DCtx->decls_end(Context);
5574f3b8f8ac2f8c89028a2f8793df0a7887df809d4Douglas Gregor         D != DEnd; ++D) {
558074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      // Insert this declaration into the lookup structure
5594afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
5606ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor        makeDeclVisibleInContextImpl(Context, ND);
561074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
562074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      // If this declaration is itself a transparent declaration context,
563074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      // add its members (recursively).
564074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
565074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor        if (InnerCtx->isTransparentContext())
5666ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor          buildLookup(Context, InnerCtx->getPrimaryContext());
567074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    }
568074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  }
569074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor}
570074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
57144b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext::lookup_result
5726ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::lookup(ASTContext &Context, DeclarationName Name) {
5730701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
57444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this)
5756ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    return PrimaryContext->lookup(Context, Name);
57644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
5772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (hasExternalVisibleStorage())
5782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LoadVisibleDeclsFromExternalStorage(Context);
5792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5803fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor  /// If there is no lookup data structure, build one now by walking
58144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// all of the linked DeclContexts (in declaration order!) and
58244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// inserting their values.
583c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (!LookupPtr) {
5846ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    buildLookup(Context, this);
58544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
586c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    if (!LookupPtr)
58791942501b6f71a41d3a09bedec19be479832c718Chris Lattner      return lookup_result(0, 0);
588c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  }
58944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
590c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
591c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  StoredDeclsMap::iterator Pos = Map->find(Name);
592c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (Pos == Map->end())
593c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    return lookup_result(0, 0);
5946ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  return Pos->second.getLookupResult(Context);
59544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
59644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
59744b4321feab46299d3f5cfd404680884752a0fcfDouglas GregorDeclContext::lookup_const_result
5986ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
5996ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  return const_cast<DeclContext*>(this)->lookup(Context, Name);
60044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
60144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
6020cf2b1990c82121d03a004dafe498ba43bf4b42aChris LattnerDeclContext *DeclContext::getLookupContext() {
6030cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *Ctx = this;
60472de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor  // Skip through transparent contexts.
605ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  while (Ctx->isTransparentContext())
606ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor    Ctx = Ctx->getParent();
607ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  return Ctx;
608ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor}
609ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor
61088b7094185b9d4fe9820c731b6936d8d37f6143eDouglas GregorDeclContext *DeclContext::getEnclosingNamespaceContext() {
61188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *Ctx = this;
61288b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  // Skip through non-namespace, non-translation-unit contexts.
61388b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  while (!Ctx->isFileContext() || Ctx->isTransparentContext())
61488b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    Ctx = Ctx->getParent();
61588b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  return Ctx->getPrimaryContext();
61688b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor}
61788b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
6186ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregorvoid DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
619cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // FIXME: This feels like a hack. Should DeclarationName support
620cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // template-ids, or is there a better way to keep specializations
621cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // from being visible?
622cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  if (isa<ClassTemplateSpecializationDecl>(D))
623cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
624cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
6250701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *PrimaryContext = getPrimaryContext();
62644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  if (PrimaryContext != this) {
6276ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    PrimaryContext->makeDeclVisibleInContext(Context, D);
62844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return;
62944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
63044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
63144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // If we already have a lookup data structure, perform the insertion
63244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // into it. Otherwise, be lazy and don't build that structure until
63344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // someone asks for it.
634c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (LookupPtr)
6356ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    makeDeclVisibleInContextImpl(Context, D);
636074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
637074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // If we are a transparent context, insert into our parent context,
638074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // too. This operation is recursive.
639074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (isTransparentContext())
6406ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    getParent()->makeDeclVisibleInContext(Context, D);
64144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
64244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
6436ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregorvoid DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
6446ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                               NamedDecl *D) {
645074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  // Skip unnamed declarations.
646074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  if (!D->getDeclName())
647074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor    return;
648074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
649cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // FIXME: This feels like a hack. Should DeclarationName support
650cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // template-ids, or is there a better way to keep specializations
651cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  // from being visible?
652cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  if (isa<ClassTemplateSpecializationDecl>(D))
653cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return;
654cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
655c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  if (!LookupPtr)
656c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor    LookupPtr = new StoredDeclsMap;
65744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
65844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  // Insert this declaration into the map.
659c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
66067762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
66167762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  if (DeclNameEntries.isNull()) {
66267762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    DeclNameEntries.setOnlyValue(D);
663bd6c80037626a37ce3936a36d9ae287f475845b7Chris Lattner    return;
66444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
66591942501b6f71a41d3a09bedec19be479832c718Chris Lattner
666bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // If it is possible that this is a redeclaration, check to see if there is
667bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // already a decl for which declarationReplaces returns true.  If there is
668bdc3d0034d5b637b211abd01a936267df27118ccChris Lattner  // one, just replace it and return.
6696ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  if (DeclNameEntries.HandleRedeclaration(Context, D))
67067762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner    return;
67191942501b6f71a41d3a09bedec19be479832c718Chris Lattner
672bd6c80037626a37ce3936a36d9ae287f475845b7Chris Lattner  // Put this declaration into the appropriate slot.
67367762a35dca6202d2272db02d0b8740728e3aa8fChris Lattner  DeclNameEntries.AddSubsequentDecl(D);
67444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor}
6752a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
6762a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
6772a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor/// this context.
6786ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::udir_iterator_range
6796ab3524f72a6e64aa04973fa9433b5559abb3525Douglas GregorDeclContext::getUsingDirectives(ASTContext &Context) const {
6806ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
6812a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
6822a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor                             reinterpret_cast<udir_iterator>(Result.second));
6832a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor}
6842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid StoredDeclsList::materializeDecls(ASTContext &Context) {
6862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (isNull())
6872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
6882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  switch ((DataKind)(Data & 0x03)) {
6902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_Decl:
6912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_Decl_Vector:
6922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
6932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_DeclID: {
6952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // Resolve this declaration ID to an actual declaration by
6962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // querying the external AST source.
6972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    unsigned DeclID = Data >> 2;
6982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
6992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalASTSource *Source = Context.getExternalSource();
7002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    assert(Source && "No external AST source available!");
7012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
7032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
7042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
7052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DK_ID_Vector: {
7072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // We have a vector of declaration IDs. Resolve all of them to
7082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // actual declarations.
7092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    VectorTy &Vector = *getAsVector();
7102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalASTSource *Source = Context.getExternalSource();
7112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    assert(Source && "No external AST source available!");
7122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    for (unsigned I = 0, N = Vector.size(); I != N; ++I)
7142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
7152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Data = (Data & ~0x03) | DK_Decl_Vector;
7172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
7182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
7192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
7202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
721