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/Sema/IdentifierResolver.cpp b/lib/Sema/IdentifierResolver.cpp
index 762885e..4e9d085 100644
--- a/lib/Sema/IdentifierResolver.cpp
+++ b/lib/Sema/IdentifierResolver.cpp
@@ -46,19 +46,10 @@
 // LookupContext Implementation
 //===----------------------------------------------------------------------===//
 
-/// getContext - Returns translation unit context for non ScopedDecls and
+/// getContext - Returns translation unit context for non Decls and
 /// for EnumConstantDecls returns the parent context of their EnumDecl.
 DeclContext *IdentifierResolver::LookupContext::getContext(Decl *D) {
-  DeclContext *Ctx;
-
-  if (EnumConstantDecl *EnumD = dyn_cast<EnumConstantDecl>(D)) {
-    Ctx = EnumD->getDeclContext()->getParent();
-  } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D))
-    Ctx = SD->getDeclContext(); 
-  else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D))
-    Ctx = Ovl->getDeclContext();
-  else
-    return TUCtx();
+  DeclContext *Ctx = D->getDeclContext();
 
   if (!Ctx) // FIXME: HACK! We shouldn't end up with a NULL context here.
     return TUCtx();
diff --git a/lib/Sema/IdentifierResolver.h b/lib/Sema/IdentifierResolver.h
index c29fffb..a44323e 100644
--- a/lib/Sema/IdentifierResolver.h
+++ b/lib/Sema/IdentifierResolver.h
@@ -29,8 +29,8 @@
 class IdentifierResolver {
 
   /// LookupContext - A wrapper for DeclContext. DeclContext is only part of
-  /// ScopedDecls, LookupContext can be used with all decls (assumes
-  /// translation unit context for non ScopedDecls).
+  /// Decls, LookupContext can be used with all decls (assumes
+  /// translation unit context for non Decls).
   class LookupContext {
     const DeclContext *Ctx;
 
@@ -42,7 +42,7 @@
       return reinterpret_cast<DeclContext*>(-1);
     }
 
-    /// getContext - Returns translation unit context for non ScopedDecls and
+    /// getContext - Returns translation unit context for non Decls and
     /// for EnumConstantDecls returns the parent context of their EnumDecl.
     static DeclContext *getContext(Decl *D);
 
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index ea42dbf..423b049 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -79,7 +79,7 @@
   TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
                                                 SourceLocation(),
                                                 &Context.Idents.get("SEL"),
-                                                SelT, 0);
+                                                SelT);
   PushOnScopeChains(SelTypedef, TUScope);
   Context.setObjCSelType(SelTypedef);
 
@@ -88,7 +88,7 @@
   QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
   TypedefDecl *ClassTypedef = 
     TypedefDecl::Create(Context, CurContext, SourceLocation(),
-                        &Context.Idents.get("Class"), ClassT, 0);
+                        &Context.Idents.get("Class"), ClassT);
   PushOnScopeChains(ClassTag, TUScope);
   PushOnScopeChains(ClassTypedef, TUScope);
   Context.setObjCClassType(ClassTypedef);
@@ -108,7 +108,7 @@
   TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
                                                SourceLocation(),
                                                &Context.Idents.get("id"),
-                                               ObjT, 0);
+                                               ObjT);
   PushOnScopeChains(IdTypedef, TUScope);
   Context.setObjCIdType(IdTypedef);
 }
@@ -223,7 +223,7 @@
   while (isa<BlockDecl>(DC))
     DC = DC->getParent();
   if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
-    return cast<ScopedDecl>(DC);
+    return cast<NamedDecl>(DC);
   return 0;
 }
 
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 053a323..8016854 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -40,7 +40,6 @@
   class DeclContext;
   class DeclSpec;
   class NamedDecl;
-  class ScopedDecl;
   class Stmt;
   class Expr;
   class InitListExpr;
@@ -285,16 +284,16 @@
   }
   DeclTy *ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup,
                           bool IsFunctionDefinition);
-  ScopedDecl* ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC,
-                                     QualType R, ScopedDecl* LastDeclarator,
-                                     Decl* PrevDecl, bool& InvalidDecl);
-  ScopedDecl* ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
-                                      QualType R, ScopedDecl* LastDeclarator,
+  NamedDecl* ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC,
+                                    QualType R, Decl* LastDeclarator,
+                                    Decl* PrevDecl, bool& InvalidDecl);
+  NamedDecl* ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
+                                      QualType R, Decl* LastDeclarator,
                                       Decl* PrevDecl, bool& InvalidDecl);
-  ScopedDecl* ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
-                                      QualType R, ScopedDecl *LastDeclarator,
-                                      Decl* PrevDecl, bool IsFunctionDefinition,
-                                      bool& InvalidDecl);
+  NamedDecl* ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
+                                     QualType R, Decl *LastDeclarator,
+                                     Decl* PrevDecl, bool IsFunctionDefinition,
+                                     bool& InvalidDecl);
   virtual DeclTy *ActOnParamDeclarator(Scope *S, Declarator &D);
   virtual void ActOnParamDefaultArgument(DeclTy *param, 
                                          SourceLocation EqualLoc,
@@ -399,7 +398,7 @@
 
   /// Subroutines of ActOnDeclarator().
   TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
-                                ScopedDecl *LastDecl);
+                                Decl *LastDecl);
   TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, Decl *Old);
   FunctionDecl *MergeFunctionDecl(FunctionDecl *New, Decl *Old, 
                                   bool &Redeclaration);
@@ -786,10 +785,10 @@
   //@}
   
   ObjCInterfaceDecl *getObjCInterfaceDecl(IdentifierInfo *Id);
-  ScopedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, 
-                                  Scope *S);
-  ScopedDecl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
+  NamedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, 
                                  Scope *S);
+  NamedDecl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
+                                      Scope *S);
 
   // More parsing and symbol table subroutines.
 
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index fe60496..ec769a1 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -88,8 +88,8 @@
   if (isa<ObjCMethodDecl>(DC))
     return Context.getTranslationUnitDecl();
 
-  if (ScopedDecl *SD = dyn_cast<ScopedDecl>(DC))
-    return SD->getLexicalDeclContext();
+  if (Decl *D = dyn_cast<Decl>(DC))
+    return D->getLexicalDeclContext();
 
   return DC->getLexicalParent();
 }
@@ -121,8 +121,7 @@
   // Add scoped declarations into their context, so that they can be
   // found later. Declarations without a context won't be inserted
   // into any context.
-  if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D))
-    CurContext->addDecl(SD);
+  CurContext->addDecl(D);
 
   // C++ [basic.scope]p4:
   //   -- exactly one declaration shall declare a class name or
@@ -178,7 +177,7 @@
       = std::find_if(IdResolver.begin(FD->getDeclName(), DC, 
                                       false/*LookInParentCtx*/),
                      IdResolver.end(),
-                     std::bind1st(std::mem_fun(&ScopedDecl::declarationReplaces),
+                     std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
                                   FD));
     if (Redecl != IdResolver.end()) {
       // There is already a declaration of a function on our
@@ -303,8 +302,8 @@
 
 /// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
 /// lazily create a decl for it.
-ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
-                                      Scope *S) {
+NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
+                                     Scope *S) {
   Builtin::ID BID = (Builtin::ID)bid;
 
   if (Context.BuiltinInfo.hasVAListUse(BID))
@@ -314,7 +313,7 @@
   FunctionDecl *New = FunctionDecl::Create(Context,
                                            Context.getTranslationUnitDecl(),
                                            SourceLocation(), II, R,
-                                           FunctionDecl::Extern, false, 0);
+                                           FunctionDecl::Extern, false);
   
   // Create Decl objects for each parameter, adding them to the
   // FunctionDecl.
@@ -322,8 +321,7 @@
     llvm::SmallVector<ParmVarDecl*, 16> Params;
     for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
       Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0,
-                                           FT->getArgType(i), VarDecl::None, 0,
-                                           0));
+                                           FT->getArgType(i), VarDecl::None, 0));
     New->setParams(Context, &Params[0], Params.size());
   }
   
@@ -921,13 +919,12 @@
   }
 
   // Create a declaration for this anonymous struct/union. 
-  ScopedDecl *Anon = 0;
+  NamedDecl *Anon = 0;
   if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) {
     Anon = FieldDecl::Create(Context, OwningClass, Record->getLocation(),
                              /*IdentifierInfo=*/0, 
                              Context.getTypeDeclType(Record),
-                             /*BitWidth=*/0, /*Mutable=*/false,
-                             /*PrevDecl=*/0);
+                             /*BitWidth=*/0, /*Mutable=*/false);
     Anon->setAccess(AS_public);
     if (getLangOptions().CPlusPlus)
       FieldCollector->Add(cast<FieldDecl>(Anon));
@@ -953,8 +950,7 @@
     Anon = VarDecl::Create(Context, Owner, Record->getLocation(),
                            /*IdentifierInfo=*/0, 
                            Context.getTypeDeclType(Record),
-                           SC, /*FIXME:LastDeclarator=*/0,
-                           DS.getSourceRange().getBegin());
+                           SC, DS.getSourceRange().getBegin());
   }
   Anon->setImplicit();
 
@@ -1206,7 +1202,7 @@
 Sema::DeclTy *
 Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl,
                       bool IsFunctionDefinition) {
-  ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl);
+  NamedDecl *LastDeclarator = dyn_cast_or_null<NamedDecl>((Decl *)lastDecl);
   DeclarationName Name = GetNameForDeclarator(D);
 
   // All of these full declarators require an identifier.  If it doesn't have
@@ -1227,7 +1223,7 @@
   
   DeclContext *DC;
   Decl *PrevDecl;
-  ScopedDecl *New;
+  NamedDecl *New;
   bool InvalidDecl = false;
  
   // See if this is a redefinition of a variable in the same scope.
@@ -1320,9 +1316,9 @@
   return New;
 }
 
-ScopedDecl*
+NamedDecl*
 Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC,
-                             QualType R, ScopedDecl* LastDeclarator,
+                             QualType R, Decl* LastDeclarator,
                              Decl* PrevDecl, bool& InvalidDecl) {
   // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1).
   if (D.getCXXScopeSpec().isSet()) {
@@ -1364,9 +1360,9 @@
   return NewTD;
 }
 
-ScopedDecl*
+NamedDecl*
 Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
-                              QualType R, ScopedDecl* LastDeclarator,
+                              QualType R, Decl* LastDeclarator,
                               Decl* PrevDecl, bool& InvalidDecl) {
   DeclarationName Name = GetNameForDeclarator(D);
 
@@ -1410,7 +1406,7 @@
     // This is a static data member for a C++ class.
     NewVD = CXXClassVarDecl::Create(Context, cast<CXXRecordDecl>(DC),
                                     D.getIdentifierLoc(), II,
-                                    R, LastDeclarator);
+                                    R);
   } else {
     bool ThreadSpecified = D.getDeclSpec().isThreadSpecified();
     if (S->getFnParent() == 0) {
@@ -1422,11 +1418,13 @@
       }
     }
     NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(), 
-                            II, R, SC, LastDeclarator,
+                            II, R, SC, 
                             // FIXME: Move to DeclGroup...
                             D.getDeclSpec().getSourceRange().getBegin());
     NewVD->setThreadSpecified(ThreadSpecified);
   }
+  NewVD->setNextDeclarator(LastDeclarator);
+
   // Handle attributes prior to checking for duplicates in MergeVarDecl
   ProcessDeclAttributes(NewVD, D);
 
@@ -1471,9 +1469,9 @@
   return NewVD;
 }
 
-ScopedDecl* 
+NamedDecl* 
 Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
-                              QualType R, ScopedDecl *LastDeclarator,
+                              QualType R, Decl *LastDeclarator,
                               Decl* PrevDecl, bool IsFunctionDefinition,
                               bool& InvalidDecl) {
   assert(R.getTypePtr()->isFunctionType());
@@ -1534,7 +1532,7 @@
       // Create a FunctionDecl to satisfy the function definition parsing
       // code path.
       NewFD = FunctionDecl::Create(Context, DC, D.getIdentifierLoc(),
-                                   Name, R, SC, isInline, LastDeclarator,
+                                   Name, R, SC, isInline, 
                                    // FIXME: Move to DeclGroup...
                                    D.getDeclSpec().getSourceRange().getBegin());
       InvalidDecl = true;
@@ -1559,15 +1557,15 @@
     // This is a C++ method declaration.
     NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(DC),
                                   D.getIdentifierLoc(), Name, R,
-                                  (SC == FunctionDecl::Static), isInline,
-                                  LastDeclarator);
+                                  (SC == FunctionDecl::Static), isInline);
   } else {
     NewFD = FunctionDecl::Create(Context, DC,
                                  D.getIdentifierLoc(),
-                                 Name, R, SC, isInline, LastDeclarator,
+                                 Name, R, SC, isInline, 
                                  // FIXME: Move to DeclGroup...
                                  D.getDeclSpec().getSourceRange().getBegin());
   }
+  NewFD->setNextDeclarator(LastDeclarator);
 
   // Set the lexical context. If the declarator has a C++
   // scope specifier, the lexical context will be different
@@ -1639,7 +1637,7 @@
         Params.push_back(ParmVarDecl::Create(Context, DC,
                                              SourceLocation(), 0,
                                              *ArgType, VarDecl::None,
-                                             0, 0));
+                                             0));
       }
 
       NewFD->setParams(Context, &Params[0], Params.size());
@@ -2299,8 +2297,7 @@
   
   VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
   if (!VDecl) {
-    Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(), 
-         diag::err_illegal_initializer);
+    Diag(RealDecl->getLocation(), diag::err_illegal_initializer);
     RealDecl->setInvalidDecl();
     return;
   }  
@@ -2442,13 +2439,13 @@
   if (GroupDecl == 0)
     return 0;
   
-  ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl);
-  ScopedDecl *NewGroup = 0;
+  Decl *Group = dyn_cast<Decl>(GroupDecl);
+  Decl *NewGroup = 0;
   if (Group->getNextDeclarator() == 0) 
     NewGroup = Group;
   else { // reverse the list.
     while (Group) {
-      ScopedDecl *Next = Group->getNextDeclarator();
+      Decl *Next = Group->getNextDeclarator();
       Group->setNextDeclarator(NewGroup);
       NewGroup = Group;
       Group = Next;
@@ -2456,7 +2453,7 @@
   }
   // Perform semantic analysis that depends on having fully processed both
   // the declarator and initializer.
-  for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
+  for (Decl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
     VarDecl *IDecl = dyn_cast<VarDecl>(ID);
     if (!IDecl)
       continue;
@@ -2608,7 +2605,7 @@
   ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext, 
                                          D.getIdentifierLoc(), II,
                                          parmDeclType, StorageClass, 
-                                         0, 0);
+                                         0);
   
   if (D.getInvalidType())
     New->setInvalidDecl();
@@ -2756,8 +2753,8 @@
 
 /// ImplicitlyDefineFunction - An undeclared identifier was used in a function
 /// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
-ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, 
-                                           IdentifierInfo &II, Scope *S) {
+NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, 
+                                          IdentifierInfo &II, Scope *S) {
   // Extension in C99.  Legal in C90, but warn about it.
   if (getLangOptions().C99)
     Diag(Loc, diag::ext_implicit_function_decl) << &II;
@@ -2794,7 +2791,7 @@
 
 
 TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
-                                    ScopedDecl *LastDeclarator) {
+                                    Decl *LastDeclarator) {
   assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
   assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
   
@@ -2802,7 +2799,8 @@
   TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext,
                                            D.getIdentifierLoc(),
                                            D.getIdentifier(), 
-                                           T, LastDeclarator);
+                                           T);
+  NewTD->setNextDeclarator(LastDeclarator);
   if (D.getInvalidType())
     NewTD->setInvalidDecl();
   return NewTD;
@@ -2833,7 +2831,7 @@
   DeclContext *SearchDC = CurContext;
   DeclContext *DC = CurContext;
   DeclContext *LexicalContext = CurContext;
-  ScopedDecl *PrevDecl = 0;
+  Decl *PrevDecl = 0;
 
   bool Invalid = false;
 
@@ -2860,9 +2858,8 @@
   } else {
     // If this is a named struct, check to see if there was a previous forward
     // declaration or definition.
-    // Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up.
-    PrevDecl = dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S)
-                                              .getAsDecl());
+    PrevDecl = dyn_cast_or_null<NamedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S)
+                                             .getAsDecl());
 
     if (!getLangOptions().CPlusPlus && TK != TK_Reference) {
       // FIXME: This makes sure that we ignore the contexts associated
@@ -3222,8 +3219,7 @@
   NewFD = FieldDecl::Create(Context, Record,
                             Loc, II, T, BitWidth,
                             D.getDeclSpec().getStorageClassSpec() ==
-                              DeclSpec::SCS_mutable,
-                            /*PrevDecl=*/0);
+                              DeclSpec::SCS_mutable);
 
   if (II) {
     Decl *PrevDecl 
@@ -3556,8 +3552,7 @@
   
   EnumConstantDecl *New = 
     EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy,
-                             Val, EnumVal,
-                             LastEnumConst);
+                             Val, EnumVal);
   
   // Register this decl in the current scope stack.
   PushOnScopeChains(New, S);
@@ -3739,7 +3734,7 @@
                                           ExprArg expr) {
   StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr.release());
 
-  return FileScopeAsmDecl::Create(Context, Loc, AsmString);
+  return FileScopeAsmDecl::Create(Context, CurContext, Loc, AsmString);
 }
 
 
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index 95f9200..a582540 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -421,13 +421,6 @@
 /// bitfield width if there is one and 'InitExpr' specifies the initializer if
 /// any. 'LastInGroup' is non-null for cases where one declspec has multiple
 /// declarators on it.
-///
-/// FIXME: The note below is out-of-date.
-/// NOTE: Because of CXXFieldDecl's inability to be chained like ScopedDecls, if
-/// an instance field is declared, a new CXXFieldDecl is created but the method
-/// does *not* return it; it returns LastInGroup instead. The other C++ members
-/// (which are all ScopedDecls) are returned after appending them to
-/// LastInGroup.
 Sema::DeclTy *
 Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
                                ExprTy *BW, ExprTy *InitExpr,
@@ -876,7 +869,7 @@
     ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor,
                                                  ClassDecl->getLocation(),
                                                  /*IdentifierInfo=*/0,
-                                                 ArgType, VarDecl::None, 0, 0);
+                                                 ArgType, VarDecl::None, 0);
     CopyConstructor->setParams(Context, &FromParam, 1);
 
     ClassDecl->addedConstructor(Context, CopyConstructor);
@@ -945,7 +938,7 @@
       CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name,
                             Context.getFunctionType(RetType, &ArgType, 1,
                                                     false, 0),
-                            /*isStatic=*/false, /*isInline=*/true, 0);
+                            /*isStatic=*/false, /*isInline=*/true);
     CopyAssignment->setAccess(AS_public);
     CopyAssignment->setImplicit();
 
@@ -953,7 +946,7 @@
     ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment,
                                                  ClassDecl->getLocation(),
                                                  /*IdentifierInfo=*/0,
-                                                 ArgType, VarDecl::None, 0, 0);
+                                                 ArgType, VarDecl::None, 0);
     CopyAssignment->setParams(Context, &FromParam, 1);
 
     // Don't call addedAssignmentOperator. There is no way to distinguish an
@@ -2211,7 +2204,7 @@
   }
 
   VarDecl *ExDecl = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
-                                    II, ExDeclType, VarDecl::None, 0, Begin);
+                                    II, ExDeclType, VarDecl::None, Begin);
   if (D.getInvalidType() || Invalid)
     ExDecl->setInvalidDecl();
 
diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp
index c4fccc7..2653887 100644
--- a/lib/Sema/SemaDeclObjC.cpp
+++ b/lib/Sema/SemaDeclObjC.cpp
@@ -592,7 +592,7 @@
   
   ObjCImplementationDecl* IMPDecl = 
     ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, 
-                                   ClassName, IDecl, SDecl);
+                                   IDecl, SDecl);
   
   // FIXME: PushOnScopeChains?
   CurContext->addDecl(IMPDecl);
@@ -613,7 +613,7 @@
                                     ObjCIvarDecl **ivars, unsigned numIvars,
                                     SourceLocation RBrace) {
   assert(ImpDecl && "missing implementation decl");
-  ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
+  ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
   if (!IDecl)
     return;
   /// Check case of non-existing @interface decl.
@@ -1143,7 +1143,7 @@
                                                   property->getIdentifier(),
                                                   property->getType(),
                                                   VarDecl::None,
-                                                  0, 0);
+                                                  0);
       SetterMethod->setMethodParams(&Argument, 1);
       CD->addDecl(SetterMethod);
     } else
@@ -1258,7 +1258,7 @@
   }
   if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
     IC->setLocEnd(AtEndLoc);
-    if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
+    if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
       ImplMethodsVsClassMethods(IC, IDecl);
   } else if (ObjCCategoryImplDecl* CatImplClass = 
                                    dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
@@ -1361,12 +1361,12 @@
       Param = ParmVarDecl::Create(Context, ObjCMethod,
                                   SourceLocation(/*FIXME*/),
                                   ArgNames[i], argType,
-                                  VarDecl::None, 0, 0);
+                                  VarDecl::None, 0);
     else
       Param = ParmVarWithOriginalTypeDecl::Create(Context, ObjCMethod,
                                   SourceLocation(/*FIXME*/),
                                   ArgNames[i], argType, originalArgType,
-                                  VarDecl::None, 0, 0);
+                                  VarDecl::None, 0);
     
     Param->setObjCDeclQualifier(
       CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
@@ -1544,7 +1544,7 @@
                                                         FD.D.getIdentifier(),
                                                         T,
                                                         VarDecl::None,
-                                                        0, 0);
+                                                        0);
             SetterDecl->setMethodParams(&Argument, 1);
             PIDecl->setSetterMethodDecl(SetterDecl);
           }
@@ -1634,7 +1634,7 @@
   ObjCImplementationDecl *IC = 0;
   ObjCCategoryImplDecl* CatImplClass = 0;
   if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
-    IDecl = getObjCInterfaceDecl(IC->getIdentifier());
+    IDecl = IC->getClassInterface();
     // We always synthesize an interface for an implementation
     // without an interface decl. So, IDecl is always non-zero.
     assert(IDecl && 
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index f6623b0..5b6b00c 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -383,11 +383,11 @@
 /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or
 /// variable corresponding to the anonymous union or struct whose type
 /// is Record.
-static ScopedDecl *getObjectForAnonymousRecordDecl(RecordDecl *Record) {
+static Decl *getObjectForAnonymousRecordDecl(RecordDecl *Record) {
   assert(Record->isAnonymousStructOrUnion() && 
          "Record must be an anonymous struct or union!");
   
-  // FIXME: Once ScopedDecls are directly linked together, this will
+  // FIXME: Once Decls are directly linked together, this will
   // be an O(1) operation rather than a slow walk through DeclContext's
   // vector (which itself will be eliminated). DeclGroups might make
   // this even better.
@@ -400,7 +400,7 @@
       // follows its type in the list of declarations.
       ++D;
       assert(D != DEnd && "Missing object for anonymous record");
-      assert(!cast<ScopedDecl>(*D)->getDeclName() && "Decl should be unnamed");
+      assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed");
       return *D;
     }
   }
@@ -429,7 +429,7 @@
   DeclContext *Ctx = Field->getDeclContext();
   do {
     RecordDecl *Record = cast<RecordDecl>(Ctx);
-    ScopedDecl *AnonObject = getObjectForAnonymousRecordDecl(Record);
+    Decl *AnonObject = getObjectForAnonymousRecordDecl(Record);
     if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject))
       AnonFields.push_back(AnonField);
     else {
@@ -576,13 +576,12 @@
   // well.
   IdentifierInfo *II = Name.getAsIdentifierInfo();
   if (II && getCurMethodDecl()) {
-    ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
     // There are two cases to handle here.  1) scoped lookup could have failed,
     // in which case we should look for an ivar.  2) scoped lookup could have
     // found a decl, but that decl is outside the current method (i.e. a global
     // variable).  In these two cases, we do a lookup for an ivar with this
     // name, if the lookup suceeds, we replace it our current decl.
-    if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
+    if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) {
       ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
       if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II)) {
         // FIXME: This should use a new expr for a direct reference, don't turn
@@ -597,7 +596,7 @@
       }
     }
     // Needed to implement property "super.method" notation.
-    if (SD == 0 && II->isStr("super")) {
+    if (D == 0 && II->isStr("super")) {
       QualType T = Context.getPointerType(Context.getObjCInterfaceType(
                      getCurMethodDecl()->getClassInterface()));
       return Owned(new ObjCSuperExpr(Loc, T));
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index 8958221..1266d32 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -540,11 +540,11 @@
   QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0);
   FunctionDecl *Alloc =
     FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name,
-                         FnType, FunctionDecl::None, false, 0,
+                         FnType, FunctionDecl::None, false,
                          SourceLocation());
   Alloc->setImplicit();
   ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
-                                           0, Argument, VarDecl::None, 0, 0);
+                                           0, Argument, VarDecl::None, 0);
   Alloc->setParams(Context, &Param, 1);
 
   // FIXME: Also add this declaration to the IdentifierResolver, but
diff --git a/lib/Sema/SemaLookup.cpp b/lib/Sema/SemaLookup.cpp
index 5f8f3f5..7353bc3 100644
--- a/lib/Sema/SemaLookup.cpp
+++ b/lib/Sema/SemaLookup.cpp
@@ -53,8 +53,7 @@
         // FIXME: We leak this overload set. Eventually, we want to
         // stop building the declarations for these overload sets, so
         // there will be nothing to leak.
-        Ovl = OverloadedFunctionDecl::Create(Context, 
-                                         cast<ScopedDecl>(*I)->getDeclContext(),
+        Ovl = OverloadedFunctionDecl::Create(Context, (*I)->getDeclContext(),
                                              (*I)->getDeclName());
         Ovl->addOverload(cast<FunctionDecl>(*I));
       }
@@ -491,7 +490,7 @@
       //   A static member, a nested type or an enumerator defined in
       //   a base class T can unambiguously be found even if an object
       //   has more than one base class subobject of type T. 
-      ScopedDecl *FirstDecl = *Path->Decls.first;
+      Decl *FirstDecl = *Path->Decls.first;
       if (isa<VarDecl>(FirstDecl) ||
           isa<TypeDecl>(FirstDecl) ||
           isa<EnumConstantDecl>(FirstDecl))
@@ -610,10 +609,10 @@
   Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
     << Name << LookupRange;
 
-  std::set<ScopedDecl *> DeclsPrinted;
+  std::set<Decl *> DeclsPrinted;
   for (BasePaths::paths_iterator Path = Paths->begin(), PathEnd = Paths->end();
        Path != PathEnd; ++Path) {
-    ScopedDecl *D = *Path->Decls.first;
+    Decl *D = *Path->Decls.first;
     if (DeclsPrinted.insert(D).second)
       Diag(D->getLocation(), diag::note_ambiguous_member_found);
   }
diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp
index 61c22d4..9441ea5 100644
--- a/lib/Sema/SemaOverload.cpp
+++ b/lib/Sema/SemaOverload.cpp
@@ -2193,20 +2193,17 @@
       // operator names can only be ordinary identifiers.
 
       // Ignore member functions. 
-      if (ScopedDecl *SD = dyn_cast<ScopedDecl>(*I)) {
-        if (SD->getDeclContext()->isRecord())
-          continue;
-      } 
+      if ((*I)->getDeclContext()->isRecord())
+        continue;
 
       // We found something with this name. We're done.
       break;
     }
 
-    if (I != IEnd && isa<ScopedDecl>(*I)) {
-      ScopedDecl *FirstDecl = cast<ScopedDecl>(*I);
+    if (I != IEnd) {
+      Decl *FirstDecl = *I;
       for (; I != IEnd; ++I) {
-        ScopedDecl *SD = cast<ScopedDecl>(*I);
-        if (FirstDecl->getDeclContext() != SD->getDeclContext())
+        if (FirstDecl->getDeclContext() != (*I)->getDeclContext())
           break;
 
         if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index 8b72514..9e15c21 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -43,14 +43,14 @@
   if (decl == 0)
     return StmtError();
 
-  ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl));
+  Decl *D = static_cast<Decl *>(decl);
 
   // This is a temporary hack until we are always passing around
   // DeclGroupRefs.
   llvm::SmallVector<Decl*, 10> decls;
-  while (SD) { 
-    ScopedDecl* d = SD;
-    SD = SD->getNextDeclarator();
+  while (D) { 
+    Decl* d = D;
+    D = D->getNextDeclarator();
     d->setNextDeclarator(0);
     decls.push_back(d);
   }
@@ -86,7 +86,7 @@
       /*empty*/;
     
     if (i != NumElts) {
-      ScopedDecl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
+      Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
       Diag(D->getLocation(), diag::ext_mixed_decls_code);
     }
   }
@@ -633,7 +633,7 @@
         return StmtError(Diag((*DS->decl_begin())->getLocation(),
                          diag::err_toomany_element_decls));
 
-      ScopedDecl *D = DS->getSolitaryDecl();
+      Decl *D = DS->getSolitaryDecl();
       FirstType = cast<ValueDecl>(D)->getType();
       // C99 6.8.5p3: The declaration part of a 'for' statement shall only
       // declare identifiers for objects having storage class 'auto' or