This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] PR libstdc++/91371 make std::is_function handle other calling conventions


On 16/08/19 16:35 +0100, Jonathan Wakely wrote:
The x86 attributes such as ms_abi, stdcall, fastcall etc. alter the
function type, which means that functions with one of those attributes
do not match any of the partial specializations of std::is_function.

Rather than duplicating the list for every calling convention, this
adds a fallback to the std::is_function primary template which
identifies other function types. The fallback works by assuming that
all function types fall into one of two categories: referenceable and
abominable. The former can be detected by testing for
function-to-pointer decay, and the latter are non-referenceable types
that are not cv void.

In order to detect referenceable types it's necessary to replace the
current definition of __is_referenceable with one that doesn't depend on
std::is_function, to avoid a cycle. The definition of std::decay can
also be modified to only act on referenceable function types, because
abominable function types do not decay.

	PR libstdc++/91371
	* include/std/type_traits (__declval, declval, __void_t): Declare
	earlier in the file.
	(__is_referenceable): Rewrite to not depend on is_function.
	(__is_referenceable_function): New trait to identify non-abominable
	function types.
	(__is_qualified_function): New alias to identify abominable function
	types.
	(is_function): Make primary template use __is_referenceable_function
	and __is_qualified_function to detect function types not covered by
	the partial specializations.
	(__decay_selector): Use __is_referenceable_function instead of
	is_function.
	(__decay_selector<_Up, false, true>): Do not use add_pointer.
	* testsuite/20_util/bind/91371.cc: New test.
	* testsuite/20_util/is_function/91371.cc: New test.
	* testsuite/20_util/is_function/value.cc: Check more pointer types.
	* testsuite/20_util/is_member_function_pointer/91371.cc: New test.

Tested x86_64-linux. Not committed yet.

I'd like to hear Daniel's thoughts on this approach, as he wrote the
original __is_referenceable trait, and much of <type_traits>.

This new __is_referenceable simply uses void_t<T&> to detect whether
forming T& is valid.

The detection for function-to-pointer decay works by checking whether
static_cast<T*>(declval<T&>()) is well-formed. If T is not a class
type (which could have a conversion operator) and is not nullptr or cv
void*cv (which can convert to nullptr* and cv void*cv* repectively)
then it must be a function.

The detection for abominable function types assumes that all types are
referenceable except functions with cv- or ref-qualifiers and cv void
types. So if it's not referenceable and not void, it's an abominable
function type.

Alisdair pointed out a much simpler implementation of is_function:

 /// is_function
 template<typename _Tp>
   struct is_function
   : public __bool_constant<!is_const<const _Tp>::value> { };

 template<typename _Tp>
   struct is_function<_Tp&>
   : public false_type { };

 template<typename _Tp>
   struct is_function<_Tp&&>
   : public false_type { };

I'm testing the attached patch which uses the above trick and keeps my
earlier simplification for __is_referenceable. I plan to commit this,
probably tomorrow.


commit 310eae1386a8410682b86aef3fa5391018583577
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Aug 19 22:04:58 2019 +0100

    PR libstdc++/91371 make std::is_function handle other calling conventions
    
    The x86 attributes such as ms_abi, stdcall, fastcall etc. alter the
    function type, which means that functions with one of those attributes
    do not match any of the partial specializations of std::is_function.
    
    Rather than duplicating the list for every calling convention, use a
    much simpler definition of std::is_function.
    
    Also redefine __is_referenceable to not rely on partial specializations
    for each type of referenceable function.
    
            PR libstdc++/91371
            * include/std/type_traits (is_function): Simplify definition. Remove
            partial specializations for function types.
            (__is_referenceable): Simplify definition.
            * testsuite/20_util/bind/91371.cc: New test.
            * testsuite/20_util/is_function/91371.cc: New test.
            * testsuite/20_util/is_function/value.cc: Check more pointer types.
            * testsuite/20_util/is_member_function_pointer/91371.cc: New test.
            * testsuite/20_util/is_object/91371.cc: New test.

diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 44db2cade5d..443208813b1 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -223,11 +223,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   struct __failure_type
   { };
 
-  // Primary type categories.
-
   template<typename>
     struct remove_cv;
 
+  template<typename>
+    struct is_const;
+
+  // Primary type categories.
+
   template<typename>
     struct __is_void_helper
     : public false_type { };
@@ -481,105 +484,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     { };
 
   /// is_function
-  template<typename>
+  template<typename _Tp>
     struct is_function
+    : public __bool_constant<!is_const<const _Tp>::value> { };
+
+  template<typename _Tp>
+    struct is_function<_Tp&>
     : public false_type { };
 
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) & _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) && _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) & _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) && _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) const _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) const & _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) const && _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) const _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) const & _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) const && _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) volatile _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) volatile & _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) volatile && _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) volatile _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) volatile & _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) volatile && _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) const volatile _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) const volatile & _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes...) const volatile && _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) const volatile _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) const volatile & _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
-
-  template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
-    struct is_function<_Res(_ArgTypes......) const volatile && _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type { };
+  template<typename _Tp>
+    struct is_function<_Tp&&>
+    : public false_type { };
 
 #define __cpp_lib_is_null_pointer 201309
 
@@ -706,20 +621,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  >;
 
 
+  // __void_t (std::void_t for C++11)
+  template<typename...> using __void_t = void;
+
   // Utility to detect referenceable types ([defns.referenceable]).
 
-  template<typename _Tp>
+  template<typename _Tp, typename = void>
     struct __is_referenceable
-    : public __or_<is_object<_Tp>, is_reference<_Tp>>::type
+    : public false_type
     { };
 
-  template<typename _Res, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
-    struct __is_referenceable<_Res(_Args...) _GLIBCXX_NOEXCEPT_QUAL>
-    : public true_type
-    { };
-
-  template<typename _Res, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
-    struct __is_referenceable<_Res(_Args......) _GLIBCXX_NOEXCEPT_QUAL>
+  template<typename _Tp>
+    struct __is_referenceable<_Tp, __void_t<_Tp&>>
     : public true_type
     { };
 
@@ -2261,9 +2174,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct conditional<false, _Iftrue, _Iffalse>
     { typedef _Iffalse type; };
 
-  // __void_t (std::void_t for C++11)
-  template<typename...> using __void_t = void;
-
   /// common_type
   template<typename... _Tp>
     struct common_type;
diff --git a/libstdc++-v3/testsuite/20_util/bind/91371.cc b/libstdc++-v3/testsuite/20_util/bind/91371.cc
new file mode 100644
index 00000000000..1c6f55e9ece
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/bind/91371.cc
@@ -0,0 +1,37 @@
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target i?86-*-* x86_64-*-* } }
+// { dg-require-effective-target c++11 }
+
+#include <functional>
+
+int bar(int) __attribute__((ms_abi));
+int baz(int) __attribute__((sysv_abi));
+
+void
+test01()
+{
+  // PR libstdc++/91371
+  std::bind(bar, 5)();
+  std::bind(baz, 5)();
+
+  static_assert(std::is_function<decltype(bar)>::value, "");
+  static_assert(std::is_function<decltype(baz)>::value, "");
+  static_assert(std::is_pointer<std::decay_t<decltype(bar)>>::value, "");
+  static_assert(std::is_pointer<std::decay_t<decltype(baz)>>::value, "");
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_function/91371.cc b/libstdc++-v3/testsuite/20_util/is_function/91371.cc
new file mode 100644
index 00000000000..c51d373027b
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_function/91371.cc
@@ -0,0 +1,47 @@
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target i?86-*-* x86_64-*-* } }
+// { dg-require-effective-target c++11 }
+
+#include <type_traits>
+
+using std::is_function;
+
+#ifdef __i386__
+static_assert(is_function<void __attribute__((thiscall)) ()>::value, "");
+static_assert(is_function<void __attribute__((thiscall)) () const>::value, "");
+static_assert(is_function<void __attribute__((fastcall)) ()>::value, "");
+static_assert(is_function<void __attribute__((fastcall)) () const>::value, "");
+static_assert(is_function<void __attribute__((stdcall)) ()>::value, "");
+static_assert(is_function<void __attribute__((stdcall)) () const>::value, "");
+#endif
+static_assert(is_function<void __attribute__((ms_abi)) ()>::value, "");
+static_assert(is_function<void __attribute__((ms_abi)) () const>::value, "");
+static_assert(is_function<void __attribute__((ms_abi)) () const &>::value, "");
+static_assert(is_function<void __attribute__((ms_abi)) () &&>::value, "");
+static_assert(is_function<void __attribute__((sysv_abi)) ()>::value, "");
+static_assert(is_function<void __attribute__((sysv_abi)) () const>::value, "");
+
+struct X { operator X*(); };
+static_assert(!is_function<X>::value, "");
+static_assert(!is_function<X&>::value, "");
+static_assert(!is_function<X*>::value, "");
+union Y { operator Y*(); int i; long l;};
+static_assert(!is_function<Y>::value, "");
+static_assert(!is_function<Y&>::value, "");
+static_assert(!is_function<Y*>::value, "");
diff --git a/libstdc++-v3/testsuite/20_util/is_function/value.cc b/libstdc++-v3/testsuite/20_util/is_function/value.cc
index 7b94b58b6cb..4a3bb7c5740 100644
--- a/libstdc++-v3/testsuite/20_util/is_function/value.cc
+++ b/libstdc++-v3/testsuite/20_util/is_function/value.cc
@@ -37,12 +37,18 @@ void test01()
 		char (int, ClassType) const volatile &&>(true), "");
 
   // Negative tests.
+  static_assert(test_category<is_function, int*>(false), "");
   static_assert(test_category<is_function, int&>(false), "");
   static_assert(test_category<is_function, void>(false), "");
   static_assert(test_category<is_function, const void>(false), "");
+  static_assert(test_category<is_function, void*>(false), "");
+  static_assert(test_category<is_function, const void*>(false), "");
+  static_assert(test_category<is_function, void**>(false), "");
+  static_assert(test_category<is_function, std::nullptr_t>(false), "");
 
   static_assert(test_category<is_function, AbstractClass>(false), "");
   static_assert(test_category<is_function, int(&)(int)>(false), "");
+  static_assert(test_category<is_function, int(*)(int)>(false), "");
   
   // Sanity check.
   static_assert(test_category<is_function, ClassType>(false), "");
diff --git a/libstdc++-v3/testsuite/20_util/is_member_function_pointer/91371.cc b/libstdc++-v3/testsuite/20_util/is_member_function_pointer/91371.cc
new file mode 100644
index 00000000000..25fecc11cd8
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_member_function_pointer/91371.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target i?86-*-* x86_64-*-* } }
+// { dg-require-effective-target c++11 }
+
+#include <type_traits>
+
+struct Z
+{
+  void __attribute__((ms_abi)) f() const { }
+  void __attribute__((sysv_abi)) g() const { }
+#ifdef __i386__
+  void __attribute__((thiscall)) h() const { }
+#endif
+};
+static_assert( std::is_member_function_pointer<decltype(&Z::f)>::value, "" );
+static_assert( std::is_member_function_pointer<decltype(&Z::g)>::value, "" );
+#ifdef __i386__
+static_assert( std::is_member_function_pointer<decltype(&Z::h)>::value, "" );
+#endif
diff --git a/libstdc++-v3/testsuite/20_util/is_object/91371.cc b/libstdc++-v3/testsuite/20_util/is_object/91371.cc
new file mode 100644
index 00000000000..3520d7680d2
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_object/91371.cc
@@ -0,0 +1,38 @@
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target i?86-*-* x86_64-*-* } }
+// { dg-require-effective-target c++11 }
+
+#include <type_traits>
+
+using std::is_object;
+
+#ifdef __i386__
+static_assert(!is_object<void __attribute__((thiscall)) ()>::value, "");
+static_assert(!is_object<void __attribute__((thiscall)) () const>::value, "");
+static_assert(!is_object<void __attribute__((fastcall)) ()>::value, "");
+static_assert(!is_object<void __attribute__((fastcall)) () const>::value, "");
+static_assert(!is_object<void __attribute__((stdcall)) ()>::value, "");
+static_assert(!is_object<void __attribute__((stdcall)) () const>::value, "");
+#endif
+static_assert(!is_object<void __attribute__((ms_abi)) ()>::value, "");
+static_assert(!is_object<void __attribute__((ms_abi)) () const>::value, "");
+static_assert(!is_object<void __attribute__((ms_abi)) () const &>::value, "");
+static_assert(!is_object<void __attribute__((ms_abi)) () &&>::value, "");
+static_assert(!is_object<void __attribute__((sysv_abi)) ()>::value, "");
+static_assert(!is_object<void __attribute__((sysv_abi)) () const>::value, "");

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]