[cfe-commits] r64266 - in /cfe/trunk: lib/Sema/SemaOverload.cpp lib/Sema/SemaTemplate.cpp test/SemaTemplate/temp_arg_nontype.cpp test/SemaTemplate/temp_param.cpp

Douglas Gregor dgregor at apple.com
Tue Feb 10 16:19:33 PST 2009


Author: dgregor
Date: Tue Feb 10 18:19:33 2009
New Revision: 64266

URL: http://llvm.org/viewvc/llvm-project?rev=64266&view=rev
Log:
Add partial semantic checking of template arguments that are meant for
non-type template parameters of pointer-to-object and
pointer-to-function type. The most fun part of this is the use of
overload resolution to pick a function from the set of overloaded
functions that comes in as a template argument.

Also, fixed two minor bugs in this area:
  - We were allowing non-type template parameters of type pointer to
  void.
  - We weren't patching up an expression that refers to an overloaded
  function set via "&f" properly.

We're still not performing complete checking of the expression to be
sure that it is referring to an object or function with external
linkage (C++ [temp.arg.nontype]p1).


Modified:
    cfe/trunk/lib/Sema/SemaOverload.cpp
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/test/SemaTemplate/temp_arg_nontype.cpp
    cfe/trunk/test/SemaTemplate/temp_param.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Tue Feb 10 18:19:33 2009
@@ -3953,7 +3953,7 @@
     assert(UnOp->getOpcode() == UnaryOperator::AddrOf && 
            "Can only take the address of an overloaded function");
     FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn);
-    E->setType(Context.getPointerType(E->getType()));
+    E->setType(Context.getPointerType(UnOp->getSubExpr()->getType()));
   } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) {
     assert(isa<OverloadedFunctionDecl>(DR->getDecl()) && 
            "Expected overloaded function");

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Tue Feb 10 18:19:33 2009
@@ -192,7 +192,9 @@
   //       -- integral or enumeration type,
   if (T->isIntegralType() || T->isEnumeralType() ||
       //   -- pointer to object or pointer to function, 
-      T->isPointerType() ||
+      (T->isPointerType() && 
+       (T->getAsPointerType()->getPointeeType()->isObjectType() ||
+        T->getAsPointerType()->getPointeeType()->isFunctionType())) ||
       //   -- reference to object or reference to function, 
       T->isReferenceType() ||
       //   -- pointer to member.
@@ -844,9 +846,8 @@
   //        enumeration type, integral promotions (4.5) and integral
   //        conversions (4.7) are applied.
   QualType ParamType = Param->getType();
+  QualType ArgType = Arg->getType();
   if (ParamType->isIntegralType() || ParamType->isEnumeralType()) {
-    QualType ArgType = Arg->getType();
-
     // C++ [temp.arg.nontype]p1:
     //   A template-argument for a non-type, non-template
     //   template-parameter shall be one of:
@@ -894,6 +895,71 @@
 
     return false;
   }
+
+  if (const PointerType *ParamPtrType = ParamType->getAsPointerType()) {
+    //   -- for a non-type template-parameter of type pointer to
+    //      object, qualification conversions (4.4) and the
+    //      array-to-pointer conversion (4.2) are applied.
+    if (ParamPtrType->getPointeeType()->isObjectType()) {
+      if (ArgType->isArrayType()) {
+        ArgType = Context.getArrayDecayedType(ArgType);
+        ImpCastExprToType(Arg, ArgType);
+      }
+
+      if (IsQualificationConversion(ArgType, ParamType)) {
+        ArgType = ParamType;
+        ImpCastExprToType(Arg, ParamType);
+      }
+
+      if (!hasSameUnqualifiedType(ArgType, ParamType)) {
+        // We can't perform this conversion.
+        Diag(Arg->getSourceRange().getBegin(), 
+             diag::err_template_arg_not_convertible)
+          << Arg->getType() << Param->getType() << Arg->getSourceRange();
+        Diag(Param->getLocation(), diag::note_template_param_here);
+        return true;
+      }
+
+      // FIXME: Check the restrictions in p1!
+      return false;
+    }
+    
+    assert(ParamPtrType->getPointeeType()->isFunctionType() &&
+           "Only function pointers remain");
+    //   -- For a non-type template-parameter of type pointer to
+    //      function, only the function-to-pointer conversion (4.3) is
+    //      applied. If the template-argument represents a set of
+    //      overloaded functions (or a pointer to such), the matching
+    //      function is selected from the set (13.4).
+    if (hasSameUnqualifiedType(ArgType, ParamType)) {
+      // We don't have to do anything: the types already match.
+    }
+    else if (ArgType->isFunctionType()) {
+      ArgType = Context.getPointerType(ArgType);
+      ImpCastExprToType(Arg, ArgType);
+    } else if (FunctionDecl *Fn 
+                 = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) {
+      FixOverloadedFunctionReference(Arg, Fn);
+      ArgType = Arg->getType();
+      if (ArgType->isFunctionType()) {
+        ArgType = Context.getPointerType(Arg->getType());
+        ImpCastExprToType(Arg, ArgType);
+      }
+    }
+
+    if (!hasSameUnqualifiedType(ArgType, ParamType)) {
+      // We can't perform this conversion.
+      Diag(Arg->getSourceRange().getBegin(), 
+           diag::err_template_arg_not_convertible)
+        << Arg->getType() << Param->getType() << Arg->getSourceRange();
+      Diag(Param->getLocation(), diag::note_template_param_here);
+      return true;
+    }
+
+    // FIXME: Check the restrictions in p1!
+    return false;
+  }
+
   // FIXME: p5 has a lot more checks to perform!
 
   return false;

Modified: cfe/trunk/test/SemaTemplate/temp_arg_nontype.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/temp_arg_nontype.cpp?rev=64266&r1=64265&r2=64266&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/temp_arg_nontype.cpp (original)
+++ cfe/trunk/test/SemaTemplate/temp_arg_nontype.cpp Tue Feb 10 18:19:33 2009
@@ -33,8 +33,37 @@
 
 class X {
 public:
+  X();
   X(int, int);
   operator int() const;
 };
 A<X(17, 42)> *a11; // expected-error{{non-type template argument of type 'class X' must have an integral or enumeration type}} \
                    // FIXME:expected-error{{expected unqualified-id}}
+
+template<X const *Ptr> struct A2;
+
+X *X_ptr;
+X array_of_Xs[10];
+A2<X_ptr> *a12;
+A2<array_of_Xs> *a13;
+
+float f(float);
+
+float g(float);
+double g(double);
+
+int h(int);
+float h2(float);
+
+template<int fp(int)> struct A3; // expected-note 2{{template parameter is declared here}}
+A3<h> *a14_1;
+A3<&h> *a14_2;
+A3<f> *a14_3;
+A3<&f> *a14_4;
+A3<((&f))> *a14_5;
+A3<h2> *a14_6;  // expected-error{{non-type template argument of type 'float (*)(float)' cannot be converted to a value of type 'int (*)(int)'}} \
+// FIXME: expected-error{{expected unqualified-id}}
+A3<g> *a14_7; // expected-error{{non-type template argument of type '<overloaded function type>' cannot be converted to a value of type 'int (*)(int)'}}\
+// FIXME: expected-error{{expected unqualified-id}}
+// FIXME: the first error includes the string <overloaded function
+// type>, which makes Doug slightly unhappy.

Modified: cfe/trunk/test/SemaTemplate/temp_param.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/temp_param.cpp?rev=64266&r1=64265&r2=64266&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/temp_param.cpp (original)
+++ cfe/trunk/test/SemaTemplate/temp_param.cpp Tue Feb 10 18:19:33 2009
@@ -18,6 +18,7 @@
 
 template<float f> struct A11; // expected-error{{a non-type template parameter cannot have type 'float'}}
 
+template<void *Ptr> struct A12; // expected-error{{a non-type template parameter cannot have type 'void *'}}
 
 // C++ [temp.param]p8
 template<int X[10]> struct A5;





More information about the cfe-commits mailing list