]> granicus.if.org Git - clang/commitdiff
Tighten up the conversion from a single-level template argument list
authorDouglas Gregor <dgregor@apple.com>
Fri, 28 Aug 2009 20:50:45 +0000 (20:50 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 28 Aug 2009 20:50:45 +0000 (20:50 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80391 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaTemplate.cpp
lib/Sema/SemaTemplate.h
lib/Sema/SemaTemplateDeduction.cpp
lib/Sema/SemaType.cpp
test/SemaTemplate/instantiate-member-template.cpp

index 4fa09d8fda9b1274c491efe8586720d4bb4c1b78..d72cea0907104362c2fbf9680ec66007921ac2be 100644 (file)
@@ -1319,7 +1319,8 @@ bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
 
         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
index b400cb3aba72f643906b4a72b75e078a8b22fad6..59bb54233e4f6b25bba4bea64e8ebbbcac7b89c6 100644 (file)
@@ -38,7 +38,7 @@ namespace clang {
   /// 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 @@ namespace clang {
     MultiLevelTemplateArgumentList() { }
     
     /// \brief Construct a single-level template argument list.
+    explicit 
     MultiLevelTemplateArgumentList(const TemplateArgumentList &TemplateArgs) {
       TemplateArgumentLists.push_back(&TemplateArgs);
     }
index ec198e0ffb1ee0a9b5db8c18161ba372a33dbace..2253a4e6a7e5f726e9b39984892320c2673f411d 100644 (file)
@@ -967,8 +967,9 @@ Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
   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 @@ Sema::SubstituteExplicitTemplateArguments(
                                 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 @@ Sema::SubstituteExplicitTemplateArguments(
       = 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 @@ Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
   Specialization = cast_or_null<FunctionDecl>(
                       SubstDecl(FunctionTemplate->getTemplatedDecl(),
                                 FunctionTemplate->getDeclContext(),
-                                *DeducedArgumentList));
+                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
   if (!Specialization)
     return TDK_SubstitutionFailure;
   
index 783795235349e9bd77b4199952cb06bd1661db54..284dea2b754dfb260c43f085a2a0b0850f51d6f6 100644 (file)
@@ -1787,14 +1787,9 @@ bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
     } 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);
       }
index eb6a4500306e78ab45862447d3dfb41191871d0a..be5665b9892ff3dc602bceb1a098c62c40b1f5d7 100644 (file)
@@ -32,7 +32,14 @@ struct X1 {
   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 @@ void test_X1() {
   
   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}}
 }