Remove ScopedDecl, collapsing all of its functionality into Decl, so
that every declaration lives inside a DeclContext.

Moved several things that don't have names but were ScopedDecls (and,
therefore, NamedDecls) to inherit from Decl rather than NamedDecl,
including ObjCImplementationDecl and LinkageSpecDecl. Now, we don't
store empty DeclarationNames for these things, nor do we try to insert
them into DeclContext's lookup structure.

The serialization tests are temporarily disabled. We'll re-enable them
once we've sorted out the remaining ownership/serialiazation issues
between DeclContexts and TranslationUnion, DeclGroups, etc.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62562 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index 2865687..70792c4 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -262,8 +262,32 @@
 // Decl Implementation
 //===----------------------------------------------------------------------===//
 
+void Decl::setDeclContext(DeclContext *DC) {
+  if (isOutOfSemaDC())
+    delete getMultipleDC();
+  
+  DeclCtx = reinterpret_cast<uintptr_t>(DC);
+}
+
+void Decl::setLexicalDeclContext(DeclContext *DC) {
+  if (DC == getLexicalDeclContext())
+    return;
+
+  if (isInSemaDC()) {
+    MultipleDC *MDC = new MultipleDC();
+    MDC->SemanticDC = getDeclContext();
+    MDC->LexicalDC = DC;
+    DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
+  } else {
+    getMultipleDC()->LexicalDC = DC;
+  }
+}
+
 // Out-of-line virtual method providing a home for Decl.
 Decl::~Decl() {
+  if (isOutOfSemaDC())
+    delete getMultipleDC();
+
   if (!HasAttrs)
     return;
   
@@ -336,20 +360,18 @@
 #if 0
   // FIXME: This causes double-destroys in some cases, so it is
   // disabled at the moment.
-  if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {    
 
-    // Observe the unrolled recursion.  By setting N->NextDeclarator = 0x0
-    // within the loop, only the Destroy method for the first ScopedDecl
-    // will deallocate all of the ScopedDecls in a chain.
-    
-    ScopedDecl* N = SD->getNextDeclarator();
-    
-    while (N) {
-      ScopedDecl* Tmp = N->getNextDeclarator();
-      N->NextDeclarator = 0x0;
-      N->Destroy(C);
-      N = Tmp;
-    }
+  // Observe the unrolled recursion.  By setting N->NextDeclarator = 0x0
+  // within the loop, only the Destroy method for the first Decl
+  // will deallocate all of the Decls in a chain.
+  
+  Decl* N = SD->getNextDeclarator();
+  
+  while (N) {
+    Decl* Tmp = N->getNextDeclarator();
+    N->NextDeclarator = 0x0;
+    N->Destroy(C);
+    N = Tmp;
   }  
 #endif
 
@@ -370,17 +392,16 @@
 //===----------------------------------------------------------------------===//
 
 const DeclContext *DeclContext::getParent() const {
-  if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
-    return SD->getDeclContext();
-  else if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
-    return BD->getParentContext();
-  else
-    return NULL;
+  if (const Decl *D = dyn_cast<Decl>(this))
+    return D->getDeclContext();
+
+  return NULL;
 }
 
 const DeclContext *DeclContext::getLexicalParent() const {
-  if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
-    return SD->getLexicalDeclContext();
+  if (const Decl *D = dyn_cast<Decl>(this))
+    return D->getLexicalDeclContext();
+
   return getParent();
 }
 
@@ -391,7 +412,7 @@
 // implemented in terms of DenseMap anyway. However, this data
 // structure is really space-inefficient, so we'll have to do
 // something.
-typedef llvm::DenseMap<DeclarationName, std::vector<ScopedDecl*> >
+typedef llvm::DenseMap<DeclarationName, std::vector<NamedDecl*> >
   StoredDeclsMap;
 
 DeclContext::~DeclContext() {
@@ -400,7 +421,7 @@
     StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
     delete Map;
   } else {
-    ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
+    NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
     delete [] Array;
   }
 }
@@ -500,7 +521,7 @@
   }
 }
 
-void DeclContext::addDecl(ScopedDecl *D) {
+void DeclContext::addDecl(Decl *D) {
   assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
   assert(!D->NextDeclInScope && D != LastDecl && 
          "Decl already inserted into a DeclContext");
@@ -511,7 +532,9 @@
   } else {
     FirstDecl = LastDecl = D;
   }
-  D->getDeclContext()->insert(D);
+
+  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
+    ND->getDeclContext()->insert(ND);
 }
 
 /// buildLookup - Build the lookup data structure with all of the
@@ -522,7 +545,8 @@
     for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end(); 
          D != DEnd; ++D) {
       // Insert this declaration into the lookup structure
-      insertImpl(*D);
+      if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
+        insertImpl(ND);
 
       // If this declaration is itself a transparent declaration context,
       // add its members (recursively).
@@ -556,7 +580,7 @@
 
   // We have a small array. Look into it.
   unsigned Size = LookupPtr.getInt();
-  ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
+  NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
   for (unsigned Idx = 0; Idx != Size; ++Idx)
     if (Array[Idx]->getDeclName() == Name) {
       unsigned Last = Idx + 1;
@@ -581,7 +605,7 @@
   return Ctx;
 }
 
-void DeclContext::insert(ScopedDecl *D) {
+void DeclContext::insert(NamedDecl *D) {
   DeclContext *PrimaryContext = getPrimaryContext();
   if (PrimaryContext != this) {
     PrimaryContext->insert(D);
@@ -600,7 +624,7 @@
     getParent()->insert(D);
 }
 
-void DeclContext::insertImpl(ScopedDecl *D) {
+void DeclContext::insertImpl(NamedDecl *D) {
   // Skip unnamed declarations.
   if (!D->getDeclName())
     return;
@@ -612,12 +636,12 @@
 
     // The lookup data is stored as an array. Search through the array
     // to find the insertion location.
-    ScopedDecl **Array;
+    NamedDecl **Array;
     if (Size == 0) {
-      Array = new ScopedDecl*[LookupIsMap - 1];
+      Array = new NamedDecl*[LookupIsMap - 1];
       LookupPtr.setPointer(Array);
     } else {
-      Array = static_cast<ScopedDecl **>(LookupPtr.getPointer());
+      Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
     }
 
     // We always keep declarations of the same name next to each other
@@ -688,9 +712,9 @@
   if (Pos != Map->end()) {
     if (MayBeRedeclaration) {
       // Determine if this declaration is actually a redeclaration.
-      std::vector<ScopedDecl *>::iterator Redecl
+      std::vector<NamedDecl *>::iterator Redecl
         = std::find_if(Pos->second.begin(), Pos->second.end(),
-                     std::bind1st(std::mem_fun(&ScopedDecl::declarationReplaces),
+                     std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
                                   D));
       if (Redecl != Pos->second.end()) {
         *Redecl = D;
@@ -702,7 +726,7 @@
     if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
       Pos->second.push_back(D);
     else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
-      ScopedDecl *TagD = Pos->second.back();
+      NamedDecl *TagD = Pos->second.back();
       Pos->second.back() = D;
       Pos->second.push_back(TagD);
     } else