[v2,1/4] c++: Implement __add_pointer built-in trait
Checks
Commit Message
This patch implements built-in trait for std::add_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __add_pointer.
* semantics.cc (finish_trait_type): Handle CPTK_ADD_POINTER.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __add_pointer.
* g++.dg/ext/add_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 9 ++++++
gcc/testsuite/g++.dg/ext/add_pointer.C | 37 ++++++++++++++++++++++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
4 files changed, 50 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/add_pointer.C
Comments
On Wed, 14 Feb 2024, Ken Matsui wrote:
> This patch implements built-in trait for std::add_pointer.
>
> gcc/cp/ChangeLog:
>
> * cp-trait.def: Define __add_pointer.
> * semantics.cc (finish_trait_type): Handle CPTK_ADD_POINTER.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/ext/has-builtin-1.C: Test existence of __add_pointer.
> * g++.dg/ext/add_pointer.C: New test.
>
> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> ---
> gcc/cp/cp-trait.def | 1 +
> gcc/cp/semantics.cc | 9 ++++++
> gcc/testsuite/g++.dg/ext/add_pointer.C | 37 ++++++++++++++++++++++++
> gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
> 4 files changed, 50 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/ext/add_pointer.C
>
> diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> index 394f006f20f..cec385ee501 100644
> --- a/gcc/cp/cp-trait.def
> +++ b/gcc/cp/cp-trait.def
> @@ -48,6 +48,7 @@
> #define DEFTRAIT_TYPE_DEFAULTED
> #endif
>
> +DEFTRAIT_TYPE (ADD_POINTER, "__add_pointer", 1)
> DEFTRAIT_EXPR (HAS_NOTHROW_ASSIGN, "__has_nothrow_assign", 1)
> DEFTRAIT_EXPR (HAS_NOTHROW_CONSTRUCTOR, "__has_nothrow_constructor", 1)
> DEFTRAIT_EXPR (HAS_NOTHROW_COPY, "__has_nothrow_copy", 1)
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 57840176863..e23693ab57f 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -12760,6 +12760,15 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
>
> switch (kind)
> {
> + case CPTK_ADD_POINTER:
> + if (TREE_CODE (type1) == FUNCTION_TYPE
> + && ((TYPE_QUALS (type1) & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
> + || FUNCTION_REF_QUALIFIED (type1)))
In other parts of the front end, e.g. the POINTER_TYPE case of tsubst, in
build_trait_object, grokdeclarator and get_typeid, it seems we check for
an unqualified function type with
(type_memfn_quals (type) != TYPE_UNQUALIFIED
&& type_mem_rqual (type) != REF_QUAL_NONE)
which should be equivalent to your formulation except it also checks
for non-standard qualifiers such as __restrict.
I'm not sure what a __restrict-qualified function type means or if we
care about the semantics of __add_pointer(void () __restrict), but I
reckon we might as well be consistent and use the type_mem_quals/rqual
formulation in new code too?
> + return type1;
> + if (TYPE_REF_P (type1))
> + type1 = TREE_TYPE (type1);
> + return build_pointer_type (type1);
> +
> case CPTK_REMOVE_CV:
> return cv_unqualified (type1);
>
> diff --git a/gcc/testsuite/g++.dg/ext/add_pointer.C b/gcc/testsuite/g++.dg/ext/add_pointer.C
> new file mode 100644
> index 00000000000..3091510f3b5
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/add_pointer.C
> @@ -0,0 +1,37 @@
> +// { dg-do compile { target c++11 } }
> +
> +#define SA(X) static_assert((X),#X)
> +
> +class ClassType { };
> +
> +SA(__is_same(__add_pointer(int), int*));
> +SA(__is_same(__add_pointer(int*), int**));
> +SA(__is_same(__add_pointer(const int), const int*));
> +SA(__is_same(__add_pointer(int&), int*));
> +SA(__is_same(__add_pointer(ClassType*), ClassType**));
> +SA(__is_same(__add_pointer(ClassType), ClassType*));
> +SA(__is_same(__add_pointer(void), void*));
> +SA(__is_same(__add_pointer(const void), const void*));
> +SA(__is_same(__add_pointer(volatile void), volatile void*));
> +SA(__is_same(__add_pointer(const volatile void), const volatile void*));
> +
> +void f1();
> +using f1_type = decltype(f1);
> +using pf1_type = decltype(&f1);
> +SA(__is_same(__add_pointer(f1_type), pf1_type));
> +
> +void f2() noexcept; // PR libstdc++/78361
> +using f2_type = decltype(f2);
> +using pf2_type = decltype(&f2);
> +SA(__is_same(__add_pointer(f2_type), pf2_type));
> +
> +using fn_type = void();
> +using pfn_type = void(*)();
> +SA(__is_same(__add_pointer(fn_type), pfn_type));
> +
> +SA(__is_same(__add_pointer(void() &), void() &));
> +SA(__is_same(__add_pointer(void() & noexcept), void() & noexcept));
> +SA(__is_same(__add_pointer(void() const), void() const));
> +SA(__is_same(__add_pointer(void(...) &), void(...) &));
> +SA(__is_same(__add_pointer(void(...) & noexcept), void(...) & noexcept));
> +SA(__is_same(__add_pointer(void(...) const), void(...) const));
> diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> index 02b4b4d745d..56e8db7ac32 100644
> --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> @@ -2,6 +2,9 @@
> // { dg-do compile }
> // Verify that __has_builtin gives the correct answer for C++ built-ins.
>
> +#if !__has_builtin (__add_pointer)
> +# error "__has_builtin (__add_pointer) failed"
> +#endif
> #if !__has_builtin (__builtin_addressof)
> # error "__has_builtin (__builtin_addressof) failed"
> #endif
> --
> 2.43.0
>
>
On Wed, Feb 14, 2024 at 12:19 PM Patrick Palka <ppalka@redhat.com> wrote:
>
> On Wed, 14 Feb 2024, Ken Matsui wrote:
>
> > This patch implements built-in trait for std::add_pointer.
> >
> > gcc/cp/ChangeLog:
> >
> > * cp-trait.def: Define __add_pointer.
> > * semantics.cc (finish_trait_type): Handle CPTK_ADD_POINTER.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * g++.dg/ext/has-builtin-1.C: Test existence of __add_pointer.
> > * g++.dg/ext/add_pointer.C: New test.
> >
> > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> > ---
> > gcc/cp/cp-trait.def | 1 +
> > gcc/cp/semantics.cc | 9 ++++++
> > gcc/testsuite/g++.dg/ext/add_pointer.C | 37 ++++++++++++++++++++++++
> > gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
> > 4 files changed, 50 insertions(+)
> > create mode 100644 gcc/testsuite/g++.dg/ext/add_pointer.C
> >
> > diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> > index 394f006f20f..cec385ee501 100644
> > --- a/gcc/cp/cp-trait.def
> > +++ b/gcc/cp/cp-trait.def
> > @@ -48,6 +48,7 @@
> > #define DEFTRAIT_TYPE_DEFAULTED
> > #endif
> >
> > +DEFTRAIT_TYPE (ADD_POINTER, "__add_pointer", 1)
> > DEFTRAIT_EXPR (HAS_NOTHROW_ASSIGN, "__has_nothrow_assign", 1)
> > DEFTRAIT_EXPR (HAS_NOTHROW_CONSTRUCTOR, "__has_nothrow_constructor", 1)
> > DEFTRAIT_EXPR (HAS_NOTHROW_COPY, "__has_nothrow_copy", 1)
> > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> > index 57840176863..e23693ab57f 100644
> > --- a/gcc/cp/semantics.cc
> > +++ b/gcc/cp/semantics.cc
> > @@ -12760,6 +12760,15 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
> >
> > switch (kind)
> > {
> > + case CPTK_ADD_POINTER:
> > + if (TREE_CODE (type1) == FUNCTION_TYPE
> > + && ((TYPE_QUALS (type1) & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
> > + || FUNCTION_REF_QUALIFIED (type1)))
>
> In other parts of the front end, e.g. the POINTER_TYPE case of tsubst, in
> build_trait_object, grokdeclarator and get_typeid, it seems we check for
> an unqualified function type with
>
> (type_memfn_quals (type) != TYPE_UNQUALIFIED
> && type_mem_rqual (type) != REF_QUAL_NONE)
>
> which should be equivalent to your formulation except it also checks
> for non-standard qualifiers such as __restrict.
>
> I'm not sure what a __restrict-qualified function type means or if we
> care about the semantics of __add_pointer(void () __restrict), but I
> reckon we might as well be consistent and use the type_mem_quals/rqual
> formulation in new code too?
>
I see and agree. Thank you for your review! I will update this patch.
> > + return type1;
> > + if (TYPE_REF_P (type1))
> > + type1 = TREE_TYPE (type1);
> > + return build_pointer_type (type1);
> > +
> > case CPTK_REMOVE_CV:
> > return cv_unqualified (type1);
> >
> > diff --git a/gcc/testsuite/g++.dg/ext/add_pointer.C b/gcc/testsuite/g++.dg/ext/add_pointer.C
> > new file mode 100644
> > index 00000000000..3091510f3b5
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/ext/add_pointer.C
> > @@ -0,0 +1,37 @@
> > +// { dg-do compile { target c++11 } }
> > +
> > +#define SA(X) static_assert((X),#X)
> > +
> > +class ClassType { };
> > +
> > +SA(__is_same(__add_pointer(int), int*));
> > +SA(__is_same(__add_pointer(int*), int**));
> > +SA(__is_same(__add_pointer(const int), const int*));
> > +SA(__is_same(__add_pointer(int&), int*));
> > +SA(__is_same(__add_pointer(ClassType*), ClassType**));
> > +SA(__is_same(__add_pointer(ClassType), ClassType*));
> > +SA(__is_same(__add_pointer(void), void*));
> > +SA(__is_same(__add_pointer(const void), const void*));
> > +SA(__is_same(__add_pointer(volatile void), volatile void*));
> > +SA(__is_same(__add_pointer(const volatile void), const volatile void*));
> > +
> > +void f1();
> > +using f1_type = decltype(f1);
> > +using pf1_type = decltype(&f1);
> > +SA(__is_same(__add_pointer(f1_type), pf1_type));
> > +
> > +void f2() noexcept; // PR libstdc++/78361
> > +using f2_type = decltype(f2);
> > +using pf2_type = decltype(&f2);
> > +SA(__is_same(__add_pointer(f2_type), pf2_type));
> > +
> > +using fn_type = void();
> > +using pfn_type = void(*)();
> > +SA(__is_same(__add_pointer(fn_type), pfn_type));
> > +
> > +SA(__is_same(__add_pointer(void() &), void() &));
> > +SA(__is_same(__add_pointer(void() & noexcept), void() & noexcept));
> > +SA(__is_same(__add_pointer(void() const), void() const));
> > +SA(__is_same(__add_pointer(void(...) &), void(...) &));
> > +SA(__is_same(__add_pointer(void(...) & noexcept), void(...) & noexcept));
> > +SA(__is_same(__add_pointer(void(...) const), void(...) const));
> > diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > index 02b4b4d745d..56e8db7ac32 100644
> > --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > @@ -2,6 +2,9 @@
> > // { dg-do compile }
> > // Verify that __has_builtin gives the correct answer for C++ built-ins.
> >
> > +#if !__has_builtin (__add_pointer)
> > +# error "__has_builtin (__add_pointer) failed"
> > +#endif
> > #if !__has_builtin (__builtin_addressof)
> > # error "__has_builtin (__builtin_addressof) failed"
> > #endif
> > --
> > 2.43.0
> >
> >
>
@@ -48,6 +48,7 @@
#define DEFTRAIT_TYPE_DEFAULTED
#endif
+DEFTRAIT_TYPE (ADD_POINTER, "__add_pointer", 1)
DEFTRAIT_EXPR (HAS_NOTHROW_ASSIGN, "__has_nothrow_assign", 1)
DEFTRAIT_EXPR (HAS_NOTHROW_CONSTRUCTOR, "__has_nothrow_constructor", 1)
DEFTRAIT_EXPR (HAS_NOTHROW_COPY, "__has_nothrow_copy", 1)
@@ -12760,6 +12760,15 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
switch (kind)
{
+ case CPTK_ADD_POINTER:
+ if (TREE_CODE (type1) == FUNCTION_TYPE
+ && ((TYPE_QUALS (type1) & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
+ || FUNCTION_REF_QUALIFIED (type1)))
+ return type1;
+ if (TYPE_REF_P (type1))
+ type1 = TREE_TYPE (type1);
+ return build_pointer_type (type1);
+
case CPTK_REMOVE_CV:
return cv_unqualified (type1);
new file mode 100644
@@ -0,0 +1,37 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+class ClassType { };
+
+SA(__is_same(__add_pointer(int), int*));
+SA(__is_same(__add_pointer(int*), int**));
+SA(__is_same(__add_pointer(const int), const int*));
+SA(__is_same(__add_pointer(int&), int*));
+SA(__is_same(__add_pointer(ClassType*), ClassType**));
+SA(__is_same(__add_pointer(ClassType), ClassType*));
+SA(__is_same(__add_pointer(void), void*));
+SA(__is_same(__add_pointer(const void), const void*));
+SA(__is_same(__add_pointer(volatile void), volatile void*));
+SA(__is_same(__add_pointer(const volatile void), const volatile void*));
+
+void f1();
+using f1_type = decltype(f1);
+using pf1_type = decltype(&f1);
+SA(__is_same(__add_pointer(f1_type), pf1_type));
+
+void f2() noexcept; // PR libstdc++/78361
+using f2_type = decltype(f2);
+using pf2_type = decltype(&f2);
+SA(__is_same(__add_pointer(f2_type), pf2_type));
+
+using fn_type = void();
+using pfn_type = void(*)();
+SA(__is_same(__add_pointer(fn_type), pfn_type));
+
+SA(__is_same(__add_pointer(void() &), void() &));
+SA(__is_same(__add_pointer(void() & noexcept), void() & noexcept));
+SA(__is_same(__add_pointer(void() const), void() const));
+SA(__is_same(__add_pointer(void(...) &), void(...) &));
+SA(__is_same(__add_pointer(void(...) & noexcept), void(...) & noexcept));
+SA(__is_same(__add_pointer(void(...) const), void(...) const));
@@ -2,6 +2,9 @@
// { dg-do compile }
// Verify that __has_builtin gives the correct answer for C++ built-ins.
+#if !__has_builtin (__add_pointer)
+# error "__has_builtin (__add_pointer) failed"
+#endif
#if !__has_builtin (__builtin_addressof)
# error "__has_builtin (__builtin_addressof) failed"
#endif