]> granicus.if.org Git - clang/commitdiff
Template argument deduction of a non-type template parameter from a
authorDouglas Gregor <dgregor@apple.com>
Fri, 13 Nov 2009 23:45:44 +0000 (23:45 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 13 Nov 2009 23:45:44 +0000 (23:45 +0000)
template argument.

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

lib/Sema/SemaTemplateDeduction.cpp
test/SemaTemplate/instantiate-member-pointers.cpp

index 17b4e5fe82009e7fb47ce76d2cf13e646fa14c46..fcec654334e698635ee957184d30c349471e6ad6 100644 (file)
@@ -126,7 +126,6 @@ DeduceNonTypeTemplateArgument(ASTContext &Context,
 /// from the given type- or value-dependent expression.
 ///
 /// \returns true if deduction succeeded, false otherwise.
-
 static Sema::TemplateDeductionResult
 DeduceNonTypeTemplateArgument(ASTContext &Context,
                               NonTypeTemplateParmDecl *NTTP,
@@ -166,6 +165,43 @@ DeduceNonTypeTemplateArgument(ASTContext &Context,
   return Sema::TDK_Success;
 }
 
+/// \brief Deduce the value of the given non-type template parameter
+/// from the given declaration.
+///
+/// \returns true if deduction succeeded, false otherwise.
+static Sema::TemplateDeductionResult
+DeduceNonTypeTemplateArgument(ASTContext &Context,
+                              NonTypeTemplateParmDecl *NTTP,
+                              Decl *D,
+                              Sema::TemplateDeductionInfo &Info,
+                              llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
+  assert(NTTP->getDepth() == 0 &&
+         "Cannot deduce non-type template argument with depth > 0");
+  
+  if (Deduced[NTTP->getIndex()].isNull()) {
+    Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
+    return Sema::TDK_Success;
+  }
+  
+  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
+    // Okay, we deduced a declaration in one case and a dependent expression
+    // in another case.
+    return Sema::TDK_Success;
+  }
+  
+  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
+    // Compare the declarations for equality
+    if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
+          D->getCanonicalDecl())
+      return Sema::TDK_Success;
+    
+    // FIXME: Fill in argument mismatch information
+    return Sema::TDK_NonDeducedMismatch;
+  }
+  
+  return Sema::TDK_Success;
+}
+
 static Sema::TemplateDeductionResult
 DeduceTemplateArguments(ASTContext &Context,
                         TemplateParameterList *TemplateParams,
@@ -847,7 +883,10 @@ DeduceTemplateArguments(ASTContext &Context,
       if (Arg.getKind() == TemplateArgument::Expression)
         return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
                                              Info, Deduced);
-
+      if (Arg.getKind() == TemplateArgument::Declaration)
+        return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsDecl(),
+                                             Info, Deduced);
+      
       assert(false && "Type/value mismatch");
       Info.FirstArg = Param;
       Info.SecondArg = Arg;
index 826ea1e3eb5e083510c1fb811c288f3cee3a83da..d2c9e6b5ed82b6f4d9fc5b0a1b7a0b5ce723f81f 100644 (file)
@@ -46,3 +46,10 @@ struct X4 {
 int &get_X4(X4<&Y::x> x4, Y& y) { 
   return x4.getMember(y); 
 }
+
+template<IntMember Member>
+void accept_X4(X4<Member>);
+
+void test_accept_X4(X4<&Y::x> x4) {
+  accept_X4(x4);
+}