[cfe-commits] r80391 - in /cfe/trunk: lib/Sema/SemaTemplate.cpp lib/Sema/SemaTemplate.h lib/Sema/SemaTemplateDeduction.cpp lib/Sema/SemaType.cpp test/SemaTemplate/instantiate-member-template.cpp

Douglas Gregor dgregor at apple.com
Fri Aug 28 13:50:46 PDT 2009


Author: dgregor
Date: Fri Aug 28 15:50:45 2009
New Revision: 80391

URL: http://llvm.org/viewvc/llvm-project?rev=80391&view=rev
Log:
Tighten up the conversion from a single-level template argument list
to a multi-level template argument list by making it explicit. The
forced auditing of callers found a bug in the instantiation of member
classes inside member templates.

I *love* static type systems.


Modified:
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/lib/Sema/SemaTemplate.h
    cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/SemaTemplate/instantiate-member-template.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=80391&r1=80390&r2=80391&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Fri Aug 28 15:50:45 2009
@@ -1319,7 +1319,8 @@
 
         TemplateArgumentList TemplateArgs(Context, Converted,
                                           /*TakeArgs=*/false);
-        NTTPType = SubstType(NTTPType, TemplateArgs,
+        NTTPType = SubstType(NTTPType, 
+                             MultiLevelTemplateArgumentList(TemplateArgs),
                              NTTP->getLocation(),
                              NTTP->getDeclName());
         // If that worked, check the non-type template parameter type

Modified: cfe/trunk/lib/Sema/SemaTemplate.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.h?rev=80391&r1=80390&r2=80391&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.h (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.h Fri Aug 28 15:50:45 2009
@@ -38,7 +38,7 @@
   /// template argument list (17) at depth 1.
   struct MultiLevelTemplateArgumentList {
     /// \brief The template argument lists, stored from the innermost template
-    /// argument list (first) to the outermost template argument list (last)
+    /// argument list (first) to the outermost template argument list (last).
     llvm::SmallVector<const TemplateArgumentList *, 4> TemplateArgumentLists;
     
   public:
@@ -46,6 +46,7 @@
     MultiLevelTemplateArgumentList() { }
     
     /// \brief Construct a single-level template argument list.
+    explicit 
     MultiLevelTemplateArgumentList(const TemplateArgumentList &TemplateArgs) {
       TemplateArgumentLists.push_back(&TemplateArgs);
     }

Modified: cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp?rev=80391&r1=80390&r2=80391&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp Fri Aug 28 15:50:45 2009
@@ -967,8 +967,9 @@
   for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
     Decl *Param = const_cast<Decl *>(
                     ClassTemplate->getTemplateParameters()->getParam(I));
-    TemplateArgument InstArg = Subst(PartialTemplateArgs[I],
-                                     *DeducedArgumentList);
+    TemplateArgument InstArg 
+      = Subst(PartialTemplateArgs[I],
+              MultiLevelTemplateArgumentList(*DeducedArgumentList));
     if (InstArg.isNull()) {
       Info.Param = makeTemplateParameter(Param);
       Info.FirstArg = PartialTemplateArgs[I];
@@ -1118,10 +1119,10 @@
                                 PEnd = Function->param_end();
        P != PEnd;
        ++P) {
-    QualType ParamType = SubstType((*P)->getType(), 
-                                   *ExplicitArgumentList, 
-                                   (*P)->getLocation(), 
-                                   (*P)->getDeclName());
+    QualType ParamType 
+      = SubstType((*P)->getType(), 
+                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
+                  (*P)->getLocation(), (*P)->getDeclName());
     if (ParamType.isNull() || Trap.hasErrorOccurred())
       return TDK_SubstitutionFailure;
     
@@ -1136,10 +1137,11 @@
       = Function->getType()->getAsFunctionProtoType();
     assert(Proto && "Function template does not have a prototype?");
     
-    QualType ResultType = SubstType(Proto->getResultType(),
-                                    *ExplicitArgumentList,
-                                    Function->getTypeSpecStartLoc(),
-                                    Function->getDeclName());
+    QualType ResultType 
+      = SubstType(Proto->getResultType(),
+                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
+                  Function->getTypeSpecStartLoc(),
+                  Function->getDeclName());
     if (ResultType.isNull() || Trap.hasErrorOccurred())
       return TDK_SubstitutionFailure;
     
@@ -1215,7 +1217,7 @@
   Specialization = cast_or_null<FunctionDecl>(
                       SubstDecl(FunctionTemplate->getTemplatedDecl(),
                                 FunctionTemplate->getDeclContext(),
-                                *DeducedArgumentList));
+                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
   if (!Specialization)
     return TDK_SubstitutionFailure;
   

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=80391&r1=80390&r2=80391&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Fri Aug 28 15:50:45 2009
@@ -1787,14 +1787,9 @@
     } else if (CXXRecordDecl *Rec 
                  = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
       if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
-        // Find the class template specialization that surrounds this
-        // member class.
-        ClassTemplateSpecializationDecl *Spec = 0;
-        for (DeclContext *Parent = Rec->getDeclContext(); 
-             Parent && !Spec; Parent = Parent->getParent())
-          Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
-        assert(Spec && "Not a member of a class template specialization?");
-        return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
+        // This record was instantiated from a class within a template.
+        return InstantiateClass(Loc, Rec, Pattern, 
+                                getTemplateInstantiationArgs(Rec),
                                 /*ExplicitInstantiation=*/false,
                                 /*Complain=*/diag != 0);
       }

Modified: cfe/trunk/test/SemaTemplate/instantiate-member-template.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-member-template.cpp?rev=80391&r1=80390&r2=80391&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-member-template.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-member-template.cpp Fri Aug 28 15:50:45 2009
@@ -32,7 +32,14 @@
   template<typename U>
   struct Inner1 {
     U x; // expected-error{{void}}
-    T y; 
+    T y;
+  };
+  
+  template<typename U>
+  struct Inner2 {
+    struct SuperInner {
+      U z; // expected-error{{void}}
+    };
   };
 };
 
@@ -42,4 +49,7 @@
   
   X1<int>::Inner1<void> *xivp; // okay
   X1<int>::Inner1<void> xiv; // expected-note{{instantiation}}
+  
+  X1<int>::Inner2<void>::SuperInner *xisivp; // okay
+  X1<int>::Inner2<void>::SuperInner xisiv; // expected-note{{instantiation}}
 }





More information about the cfe-commits mailing list