[1/2] c++: implement __add_const built-in trait
Checks
Commit Message
This patch implements built-in trait for std::add_const.
gcc/cp/ChangeLog:
* cp-trait.def: Define __add_const.
* semantics.cc (finish_trait_type): Handle CPTK_ADD_CONST.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __add_const.
* g++.dg/ext/add_const.C: New test.
---
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 6 ++++
gcc/testsuite/g++.dg/ext/add_const.C | 39 ++++++++++++++++++++++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
4 files changed, 49 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/add_const.C
Comments
On Tue, 21 Mar 2023 at 11:12, Ken Matsui via Libstdc++ <
libstdc++@gcc.gnu.org> wrote:
> This patch implements built-in trait for std::add_const.
>
> gcc/cp/ChangeLog:
>
> * cp-trait.def: Define __add_const.
> * semantics.cc (finish_trait_type): Handle CPTK_ADD_CONST.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/ext/has-builtin-1.C: Test existence of __add_const.
> * g++.dg/ext/add_const.C: New test.
> ---
> gcc/cp/cp-trait.def | 1 +
> gcc/cp/semantics.cc | 6 ++++
> gcc/testsuite/g++.dg/ext/add_const.C | 39 ++++++++++++++++++++++++
> gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
> 4 files changed, 49 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/ext/add_const.C
>
> diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> index bac593c0094..e362c448c84 100644
> --- a/gcc/cp/cp-trait.def
> +++ b/gcc/cp/cp-trait.def
> @@ -91,6 +91,7 @@ DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
> DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
> DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
> DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
> +DEFTRAIT_TYPE (ADD_CONST, "__add_const", 1)
>
> /* These traits yield a type pack, not a type, and are represented by
> cp_parser_trait as a special BASES tree instead of a TRAIT_TYPE tree.
> */
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 87c2e8a7111..14e27a71a55 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -12273,6 +12273,12 @@ finish_trait_type (cp_trait_kind kind, tree
> type1, tree type2)
> if (TYPE_REF_P (type1))
> type1 = TREE_TYPE (type1);
> return cv_unqualified (type1);
> + case CPTK_ADD_CONST:
> + if (TYPE_REF_P (type1) || TYPE_PTRFN_P (type1))
> + return type1;
> + return cp_build_qualified_type (type1,
> + cp_type_quals (type1) |
> + TYPE_QUAL_CONST);
>
> #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
> case CPTK_##CODE:
> diff --git a/gcc/testsuite/g++.dg/ext/add_const.C
> b/gcc/testsuite/g++.dg/ext/add_const.C
> new file mode 100644
> index 00000000000..1c8618a8b00
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/add_const.C
> @@ -0,0 +1,39 @@
> +// { dg-do compile { target c++11 } }
> +
> +#define SA(X) static_assert((X),#X)
> +
> +SA(__is_same(__add_const(void), const void));
> +SA(__is_same(__add_const(int), const int));
> +
> +SA(__is_same(__add_const(const int), const int));
> +SA(__is_same(__add_const(volatile int), const volatile int));
> +SA(__is_same(__add_const(const volatile int), const volatile int));
> +
> +SA(__is_same(__add_const(int*), int* const));
> +SA(__is_same(__add_const(int* const), int* const));
> +SA(__is_same(__add_const(int* volatile), int* const volatile));
> +SA(__is_same(__add_const(int* const volatile), int* const volatile));
> +
> +SA(__is_same(__add_const(const int*), const int* const));
> +SA(__is_same(__add_const(volatile int*), volatile int* const));
> +SA(__is_same(__add_const(const volatile int*), const volatile int*
> const));
> +
> +SA(__is_same(__add_const(int&), int&));
> +SA(__is_same(__add_const(const int&), const int&));
> +SA(__is_same(__add_const(volatile int&), volatile int&));
> +SA(__is_same(__add_const(const volatile int&), const volatile int&));
> +
> +SA(__is_same(__add_const(int&&), int&&));
> +SA(__is_same(__add_const(const int&&), const int&&));
> +SA(__is_same(__add_const(volatile int&&), volatile int&&));
> +SA(__is_same(__add_const(const volatile int&&), const volatile int&&));
> +
> +SA(__is_same(__add_const(int[3]), const int[3]));
> +SA(__is_same(__add_const(const int[3]), const int[3]));
> +SA(__is_same(__add_const(volatile int[3]), const volatile int[3]));
> +SA(__is_same(__add_const(const volatile int[3]), const volatile int[3]));
> +
> +SA(__is_same(__add_const(int(int)), int(int)));
> +SA(__is_same(__add_const(int(*const)(int)), int(*const)(int)));
> +SA(__is_same(__add_const(int(*volatile)(int)), int(*volatile)(int)));
>
This looks wrong.
> +SA(__is_same(__add_const(int(*const volatile)(int)), int(*const
> volatile)(int)));
> diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> index f343e153e56..dd331ebbc9a 100644
> --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> @@ -146,3 +146,6 @@
> #if !__has_builtin (__remove_cvref)
> # error "__has_builtin (__remove_cvref) failed"
> #endif
> +#if !__has_builtin (__add_const)
> +# error "__has_builtin (__add_const) failed"
> +#endif
> --
> 2.40.0
>
>
On Tue, 21 Mar 2023 at 11:26, Jonathan Wakely wrote:
>
>
> On Tue, 21 Mar 2023 at 11:12, Ken Matsui via Libstdc++ <
> libstdc++@gcc.gnu.org> wrote:
>
>> This patch implements built-in trait for std::add_const.
>>
>> gcc/cp/ChangeLog:
>>
>> * cp-trait.def: Define __add_const.
>> * semantics.cc (finish_trait_type): Handle CPTK_ADD_CONST.
>>
>> gcc/testsuite/ChangeLog:
>>
>> * g++.dg/ext/has-builtin-1.C: Test existence of __add_const.
>> * g++.dg/ext/add_const.C: New test.
>> ---
>> gcc/cp/cp-trait.def | 1 +
>> gcc/cp/semantics.cc | 6 ++++
>> gcc/testsuite/g++.dg/ext/add_const.C | 39 ++++++++++++++++++++++++
>> gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
>> 4 files changed, 49 insertions(+)
>> create mode 100644 gcc/testsuite/g++.dg/ext/add_const.C
>>
>> diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
>> index bac593c0094..e362c448c84 100644
>> --- a/gcc/cp/cp-trait.def
>> +++ b/gcc/cp/cp-trait.def
>> @@ -91,6 +91,7 @@ DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
>> DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
>> DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
>> DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
>> +DEFTRAIT_TYPE (ADD_CONST, "__add_const", 1)
>>
>> /* These traits yield a type pack, not a type, and are represented by
>> cp_parser_trait as a special BASES tree instead of a TRAIT_TYPE
>> tree. */
>> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
>> index 87c2e8a7111..14e27a71a55 100644
>> --- a/gcc/cp/semantics.cc
>> +++ b/gcc/cp/semantics.cc
>> @@ -12273,6 +12273,12 @@ finish_trait_type (cp_trait_kind kind, tree
>> type1, tree type2)
>> if (TYPE_REF_P (type1))
>> type1 = TREE_TYPE (type1);
>> return cv_unqualified (type1);
>> + case CPTK_ADD_CONST:
>> + if (TYPE_REF_P (type1) || TYPE_PTRFN_P (type1))
>> + return type1;
>> + return cp_build_qualified_type (type1,
>> + cp_type_quals (type1) |
>> + TYPE_QUAL_CONST);
>>
>> #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
>> case CPTK_##CODE:
>> diff --git a/gcc/testsuite/g++.dg/ext/add_const.C
>> b/gcc/testsuite/g++.dg/ext/add_const.C
>> new file mode 100644
>> index 00000000000..1c8618a8b00
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/ext/add_const.C
>> @@ -0,0 +1,39 @@
>> +// { dg-do compile { target c++11 } }
>> +
>> +#define SA(X) static_assert((X),#X)
>> +
>> +SA(__is_same(__add_const(void), const void));
>> +SA(__is_same(__add_const(int), const int));
>> +
>> +SA(__is_same(__add_const(const int), const int));
>> +SA(__is_same(__add_const(volatile int), const volatile int));
>> +SA(__is_same(__add_const(const volatile int), const volatile int));
>> +
>> +SA(__is_same(__add_const(int*), int* const));
>> +SA(__is_same(__add_const(int* const), int* const));
>> +SA(__is_same(__add_const(int* volatile), int* const volatile));
>> +SA(__is_same(__add_const(int* const volatile), int* const volatile));
>> +
>> +SA(__is_same(__add_const(const int*), const int* const));
>> +SA(__is_same(__add_const(volatile int*), volatile int* const));
>> +SA(__is_same(__add_const(const volatile int*), const volatile int*
>> const));
>> +
>> +SA(__is_same(__add_const(int&), int&));
>> +SA(__is_same(__add_const(const int&), const int&));
>> +SA(__is_same(__add_const(volatile int&), volatile int&));
>> +SA(__is_same(__add_const(const volatile int&), const volatile int&));
>> +
>> +SA(__is_same(__add_const(int&&), int&&));
>> +SA(__is_same(__add_const(const int&&), const int&&));
>> +SA(__is_same(__add_const(volatile int&&), volatile int&&));
>> +SA(__is_same(__add_const(const volatile int&&), const volatile int&&));
>> +
>> +SA(__is_same(__add_const(int[3]), const int[3]));
>> +SA(__is_same(__add_const(const int[3]), const int[3]));
>> +SA(__is_same(__add_const(volatile int[3]), const volatile int[3]));
>> +SA(__is_same(__add_const(const volatile int[3]), const volatile int[3]));
>> +
>> +SA(__is_same(__add_const(int(int)), int(int)));
>> +SA(__is_same(__add_const(int(*const)(int)), int(*const)(int)));
>> +SA(__is_same(__add_const(int(*volatile)(int)), int(*volatile)(int)));
>>
>
> This looks wrong.
>
It might be useful to test pointer-to-member types too. And for
completeness, a class type.
>
>> +SA(__is_same(__add_const(int(*const volatile)(int)), int(*const
>> volatile)(int)));
>> diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
>> b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
>> index f343e153e56..dd331ebbc9a 100644
>> --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
>> +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
>> @@ -146,3 +146,6 @@
>> #if !__has_builtin (__remove_cvref)
>> # error "__has_builtin (__remove_cvref) failed"
>> #endif
>> +#if !__has_builtin (__add_const)
>> +# error "__has_builtin (__add_const) failed"
>> +#endif
>> --
>> 2.40.0
>>
>>
@@ -91,6 +91,7 @@ DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
+DEFTRAIT_TYPE (ADD_CONST, "__add_const", 1)
/* These traits yield a type pack, not a type, and are represented by
cp_parser_trait as a special BASES tree instead of a TRAIT_TYPE tree. */
@@ -12273,6 +12273,12 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2)
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
return cv_unqualified (type1);
+ case CPTK_ADD_CONST:
+ if (TYPE_REF_P (type1) || TYPE_PTRFN_P (type1))
+ return type1;
+ return cp_build_qualified_type (type1,
+ cp_type_quals (type1) |
+ TYPE_QUAL_CONST);
#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
case CPTK_##CODE:
new file mode 100644
@@ -0,0 +1,39 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(__is_same(__add_const(void), const void));
+SA(__is_same(__add_const(int), const int));
+
+SA(__is_same(__add_const(const int), const int));
+SA(__is_same(__add_const(volatile int), const volatile int));
+SA(__is_same(__add_const(const volatile int), const volatile int));
+
+SA(__is_same(__add_const(int*), int* const));
+SA(__is_same(__add_const(int* const), int* const));
+SA(__is_same(__add_const(int* volatile), int* const volatile));
+SA(__is_same(__add_const(int* const volatile), int* const volatile));
+
+SA(__is_same(__add_const(const int*), const int* const));
+SA(__is_same(__add_const(volatile int*), volatile int* const));
+SA(__is_same(__add_const(const volatile int*), const volatile int* const));
+
+SA(__is_same(__add_const(int&), int&));
+SA(__is_same(__add_const(const int&), const int&));
+SA(__is_same(__add_const(volatile int&), volatile int&));
+SA(__is_same(__add_const(const volatile int&), const volatile int&));
+
+SA(__is_same(__add_const(int&&), int&&));
+SA(__is_same(__add_const(const int&&), const int&&));
+SA(__is_same(__add_const(volatile int&&), volatile int&&));
+SA(__is_same(__add_const(const volatile int&&), const volatile int&&));
+
+SA(__is_same(__add_const(int[3]), const int[3]));
+SA(__is_same(__add_const(const int[3]), const int[3]));
+SA(__is_same(__add_const(volatile int[3]), const volatile int[3]));
+SA(__is_same(__add_const(const volatile int[3]), const volatile int[3]));
+
+SA(__is_same(__add_const(int(int)), int(int)));
+SA(__is_same(__add_const(int(*const)(int)), int(*const)(int)));
+SA(__is_same(__add_const(int(*volatile)(int)), int(*volatile)(int)));
+SA(__is_same(__add_const(int(*const volatile)(int)), int(*const volatile)(int)));
@@ -146,3 +146,6 @@
#if !__has_builtin (__remove_cvref)
# error "__has_builtin (__remove_cvref) failed"
#endif
+#if !__has_builtin (__add_const)
+# error "__has_builtin (__add_const) failed"
+#endif