Revert r293455, which breaks v8 with a spurious error. Testcase added.

Summary: Revert r293455, which breaks v8 with a spurious error. Testcase added.

Reviewers: klimek

Subscribers: cfe-commits, rsmith

Differential Revision: https://reviews.llvm.org/D29271

llvm-svn: 293473
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 60daae0..ce843a5 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -60,6 +60,11 @@
   return DeclGroupPtrTy::make(DeclGroupRef(Ptr));
 }
 
+static bool isTypeTemplate(NamedDecl *ND) {
+  return isa<ClassTemplateDecl>(ND) || isa<TypeAliasTemplateDecl>(ND) ||
+         isa<TemplateTemplateParmDecl>(ND);
+}
+
 namespace {
 
 class TypeNameValidatorCCC : public CorrectionCandidateCallback {
@@ -76,7 +81,7 @@
   bool ValidateCandidate(const TypoCorrection &candidate) override {
     if (NamedDecl *ND = candidate.getCorrectionDecl()) {
       bool IsType = isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND);
-      bool AllowedTemplate = AllowTemplates && getAsTypeTemplateDecl(ND);
+      bool AllowedTemplate = AllowTemplates && isTypeTemplate(ND);
       return (IsType || AllowedTemplate) &&
              (AllowInvalidDecl || !ND->isInvalidDecl());
     }
@@ -400,7 +405,7 @@
     for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end();
          Res != ResEnd; ++Res) {
       if (isa<TypeDecl>(*Res) || isa<ObjCInterfaceDecl>(*Res) ||
-          (AllowDeducedTemplate && getAsTypeTemplateDecl(*Res))) {
+          (AllowDeducedTemplate && isTypeTemplate(*Res))) {
         if (!IIDecl ||
             (*Res)->getLocation().getRawEncoding() <
               IIDecl->getLocation().getRawEncoding())
@@ -453,10 +458,9 @@
     (void)DiagnoseUseOfDecl(IDecl, NameLoc);
     if (!HasTrailingDot)
       T = Context.getObjCInterfaceType(IDecl);
-  } else if (AllowDeducedTemplate) {
-    if (auto *TD = getAsTypeTemplateDecl(IIDecl))
-      T = Context.getDeducedTemplateSpecializationType(TemplateName(TD),
-                                                       QualType(), false);
+  } else if (AllowDeducedTemplate && isTypeTemplate(IIDecl)) {
+    T = Context.getDeducedTemplateSpecializationType(
+        TemplateName(cast<TemplateDecl>(IIDecl)), QualType(), false);
   }
 
   if (T.isNull()) {
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index d3b47f1..8f46ad0 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8792,18 +8792,8 @@
                                        Context.getTypeDeclType(Type));
     }
 
-    // C++ [dcl.type.simple]p2:
-    //   A type-specifier of the form
-    //     typename[opt] nested-name-specifier[opt] template-name
-    //   is a placeholder for a deduced class type [...].
-    if (getLangOpts().CPlusPlus1z) {
-      if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) {
-        return Context.getElaboratedType(
-            Keyword, QualifierLoc.getNestedNameSpecifier(),
-            Context.getDeducedTemplateSpecializationType(TemplateName(TD),
-                                                         QualType(), false));
-      }
-    }
+    // FIXME: Form a deduced template specialization type if we get a template
+    // declaration here.
 
     DiagID = diag::err_typename_nested_not_type;
     Referenced = Result.getFoundDecl();
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 723a6c0..aff19ec 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1490,16 +1490,12 @@
 /// a cast expression) or that the entity has no name (e.g., an
 /// unnamed function parameter).
 ///
-/// \param AllowDeducedTST Whether a DeducedTemplateSpecializationType is
-/// acceptable as the top level type of the result.
-///
 /// \returns If the instantiation succeeds, the instantiated
 /// type. Otherwise, produces diagnostics and returns a NULL type.
 TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
                                 const MultiLevelTemplateArgumentList &Args,
                                 SourceLocation Loc,
-                                DeclarationName Entity,
-                                bool AllowDeducedTST) {
+                                DeclarationName Entity) {
   assert(!ActiveTemplateInstantiations.empty() &&
          "Cannot perform an instantiation without some context on the "
          "instantiation stack");
@@ -1509,8 +1505,7 @@
     return T;
 
   TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
-  return AllowDeducedTST ? Instantiator.TransformTypeWithDeducedTST(T)
-                         : Instantiator.TransformType(T);
+  return Instantiator.TransformType(T);
 }
 
 TypeSourceInfo *Sema::SubstType(TypeLoc TL,
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 46de4a1..2310e99 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -657,9 +657,10 @@
                                              ArrayRef<BindingDecl*> *Bindings) {
 
   // Do substitution on the type of the declaration
-  TypeSourceInfo *DI = SemaRef.SubstType(
-      D->getTypeSourceInfo(), TemplateArgs, D->getTypeSpecStartLoc(),
-      D->getDeclName(), /*AllowDeducedTST*/true);
+  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
+                                         TemplateArgs,
+                                         D->getTypeSpecStartLoc(),
+                                         D->getDeclName());
   if (!DI)
     return nullptr;
 
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 7198fce..d27cd77 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -307,17 +307,6 @@
   ///
   QualType TransformType(TypeLocBuilder &TLB, TypeLoc TL);
 
-  /// \brief Transform a type that is permitted to produce a
-  /// DeducedTemplateSpecializationType.
-  ///
-  /// This is used in the (relatively rare) contexts where it is acceptable
-  /// for transformation to produce a class template type with deduced
-  /// template arguments.
-  /// @{
-  QualType TransformTypeWithDeducedTST(QualType T);
-  TypeSourceInfo *TransformTypeWithDeducedTST(TypeSourceInfo *DI);
-  /// @}
-
   /// \brief Transform the given statement.
   ///
   /// By default, this routine transforms a statement by delegating to the
@@ -909,7 +898,7 @@
   /// By default, builds a new ParenType type from the inner type.
   /// Subclasses may override this routine to provide different behavior.
   QualType RebuildParenType(QualType InnerType) {
-    return SemaRef.BuildParenType(InnerType);
+    return SemaRef.Context.getParenType(InnerType);
   }
 
   /// \brief Build a new qualified name type.
@@ -979,8 +968,7 @@
                                     SourceLocation KeywordLoc,
                                     NestedNameSpecifierLoc QualifierLoc,
                                     const IdentifierInfo *Id,
-                                    SourceLocation IdLoc,
-                                    bool DeducedTSTContext) {
+                                    SourceLocation IdLoc) {
     CXXScopeSpec SS;
     SS.Adopt(QualifierLoc);
 
@@ -992,25 +980,9 @@
                                                     Id);
     }
 
-    if (Keyword == ETK_None || Keyword == ETK_Typename) {
-      QualType T = SemaRef.CheckTypenameType(Keyword, KeywordLoc, QualifierLoc,
-                                             *Id, IdLoc);
-      // If a dependent name resolves to a deduced template specialization type,
-      // check that we're in one of the syntactic contexts permitting it.
-      if (!DeducedTSTContext) {
-        if (auto *Deduced = dyn_cast_or_null<DeducedTemplateSpecializationType>(
-                T.isNull() ? nullptr : T->getContainedDeducedType())) {
-          SemaRef.Diag(IdLoc, diag::err_dependent_deduced_tst)
-            << (int)SemaRef.getTemplateNameKindForDiagnostics(
-                   Deduced->getTemplateName())
-            << QualType(QualifierLoc.getNestedNameSpecifier()->getAsType(), 0);
-          if (auto *TD = Deduced->getTemplateName().getAsTemplateDecl())
-            SemaRef.Diag(TD->getLocation(), diag::note_template_decl_here);
-          return QualType();
-        }
-      }
-      return T;
-    }
+    if (Keyword == ETK_None || Keyword == ETK_Typename)
+      return SemaRef.CheckTypenameType(Keyword, KeywordLoc, QualifierLoc,
+                                       *Id, IdLoc);
 
     TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
 
@@ -3185,10 +3157,6 @@
   TypeSourceInfo *TransformTSIInObjectScope(TypeLoc TL, QualType ObjectType,
                                             NamedDecl *FirstQualifierInScope,
                                             CXXScopeSpec &SS);
-
-  QualType TransformDependentNameType(TypeLocBuilder &TLB,
-                                      DependentNameTypeLoc TL,
-                                      bool DeducibleTSTContext);
 };
 
 template<typename Derived>
@@ -4080,52 +4048,6 @@
   llvm_unreachable("unhandled type loc!");
 }
 
-template<typename Derived>
-QualType TreeTransform<Derived>::TransformTypeWithDeducedTST(QualType T) {
-  if (!isa<DependentNameType>(T))
-    return TransformType(T);
-
-  if (getDerived().AlreadyTransformed(T))
-    return T;
-  TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo(T,
-                                                getDerived().getBaseLocation());
-  TypeSourceInfo *NewDI = getDerived().TransformTypeWithDeducedTST(DI);
-  return NewDI ? NewDI->getType() : QualType();
-}
-
-template<typename Derived>
-TypeSourceInfo *
-TreeTransform<Derived>::TransformTypeWithDeducedTST(TypeSourceInfo *DI) {
-  if (!isa<DependentNameType>(DI->getType()))
-    return TransformType(DI);
-
-  // Refine the base location to the type's location.
-  TemporaryBase Rebase(*this, DI->getTypeLoc().getBeginLoc(),
-                       getDerived().getBaseEntity());
-  if (getDerived().AlreadyTransformed(DI->getType()))
-    return DI;
-
-  TypeLocBuilder TLB;
-
-  TypeLoc TL = DI->getTypeLoc();
-  TLB.reserve(TL.getFullDataSize());
-
-  Qualifiers Quals;
-  if (auto QTL = TL.getAs<QualifiedTypeLoc>()) {
-    Quals = QTL.getType().getLocalQualifiers();
-    TL = QTL.getUnqualifiedLoc();
-  }
-
-  auto DNTL = TL.castAs<DependentNameTypeLoc>();
-
-  QualType Result = getDerived().TransformDependentNameType(
-      TLB, DNTL, /*DeducedTSTContext*/true);
-  if (Result.isNull())
-    return nullptr;
-
-  return TLB.getTypeSourceInfo(SemaRef.Context, Result);
-}
-
 /// FIXME: By default, this routine adds type qualifiers only to types
 /// that can have qualifiers, and silently suppresses those qualifiers
 /// that are not permitted (e.g., qualifiers on reference or function
@@ -5932,14 +5854,8 @@
 }
 
 template<typename Derived>
-QualType TreeTransform<Derived>::TransformDependentNameType(
-    TypeLocBuilder &TLB, DependentNameTypeLoc TL) {
-  return TransformDependentNameType(TLB, TL, false);
-}
-
-template<typename Derived>
-QualType TreeTransform<Derived>::TransformDependentNameType(
-    TypeLocBuilder &TLB, DependentNameTypeLoc TL, bool DeducedTSTContext) {
+QualType TreeTransform<Derived>::TransformDependentNameType(TypeLocBuilder &TLB,
+                                                      DependentNameTypeLoc TL) {
   const DependentNameType *T = TL.getTypePtr();
 
   NestedNameSpecifierLoc QualifierLoc
@@ -5952,8 +5868,7 @@
                                             TL.getElaboratedKeywordLoc(),
                                             QualifierLoc,
                                             T->getIdentifier(),
-                                            TL.getNameLoc(),
-                                            DeducedTSTContext);
+                                            TL.getNameLoc());
   if (Result.isNull())
     return QualType();
 
@@ -9559,8 +9474,7 @@
 ExprResult
 TreeTransform<Derived>::TransformCXXFunctionalCastExpr(
                                                      CXXFunctionalCastExpr *E) {
-  TypeSourceInfo *Type =
-      getDerived().TransformTypeWithDeducedTST(E->getTypeInfoAsWritten());
+  TypeSourceInfo *Type = getDerived().TransformType(E->getTypeInfoAsWritten());
   if (!Type)
     return ExprError();
 
@@ -9749,8 +9663,8 @@
 ExprResult
 TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
   // Transform the type that we're allocating
-  TypeSourceInfo *AllocTypeInfo =
-      getDerived().TransformTypeWithDeducedTST(E->getAllocatedTypeSourceInfo());
+  TypeSourceInfo *AllocTypeInfo
+    = getDerived().TransformType(E->getAllocatedTypeSourceInfo());
   if (!AllocTypeInfo)
     return ExprError();
 
@@ -10461,8 +10375,7 @@
 ExprResult
 TreeTransform<Derived>::TransformCXXTemporaryObjectExpr(
                                                     CXXTemporaryObjectExpr *E) {
-  TypeSourceInfo *T =
-      getDerived().TransformTypeWithDeducedTST(E->getTypeSourceInfo());
+  TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
   if (!T)
     return ExprError();
 
@@ -10759,8 +10672,7 @@
 ExprResult
 TreeTransform<Derived>::TransformCXXUnresolvedConstructExpr(
                                                   CXXUnresolvedConstructExpr *E) {
-  TypeSourceInfo *T =
-      getDerived().TransformTypeWithDeducedTST(E->getTypeSourceInfo());
+  TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo());
   if (!T)
     return ExprError();