diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index b70ecf99a30..0b91e878563 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3316,6 +3316,9 @@ diagnose_trait_expr (location_t loc, tree expr, tree args) case CPTK_IS_VOLATILE: inform (loc, "%qT is not a volatile type", t1); break; + case CPTK_IS_CONSTEVAL_ONLY: + inform (decl_loc, "%qT is not consteval-only", t1); + break; case CPTK_RANK: inform (loc, "%qT cannot yield a rank", t1); break; diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index c7fc40d0544..395cadc5767 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -69,6 +69,7 @@ DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2) DEFTRAIT_EXPR (IS_BOUNDED_ARRAY, "__is_bounded_array", 1) DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1) DEFTRAIT_EXPR (IS_CONST, "__is_const", 1) +DEFTRAIT_EXPR (IS_CONSTEVAL_ONLY, "__builtin_is_consteval_only", 1) DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1) DEFTRAIT_EXPR (IS_CONVERTIBLE, "__is_convertible", 2) DEFTRAIT_EXPR (IS_DESTRUCTIBLE, "__is_destructible", 1) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 0b6cb7ce200..f4f9bc924d0 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -13914,6 +13914,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_DEDUCIBLE: return type_targs_deducible_from (type1, type2); + case CPTK_IS_CONSTEVAL_ONLY: + return consteval_only_p (type1); + /* __array_rank, __builtin_type_order and __builtin_structured_binding_size are handled in finish_trait_expr. */ case CPTK_RANK: @@ -14094,6 +14097,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_STD_LAYOUT: case CPTK_IS_TRIVIAL: case CPTK_IS_TRIVIALLY_COPYABLE: + case CPTK_IS_CONSTEVAL_ONLY: case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS: if (!check_trait_type (type1, /* kind = */ 2)) return error_mark_node; diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index c8f58842754..1e7af3ea885 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -3889,6 +3889,24 @@ template # endif #endif +#if __cpp_impl_reflection >= 202500L \ + && _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_is_consteval_only) // C++ >= 26 + /// is_consteval_only - true if the type is consteval-only. + /// @since C++26 + template + struct is_consteval_only + : bool_constant<__builtin_is_consteval_only(_Tp)> + { }; + + /** is_consteval_only_v - true if the type is consteval-only. + * @ingroup variable_templates + * @since C++26 + */ + template + inline constexpr bool is_consteval_only_v + = __builtin_is_consteval_only(_Tp); +#endif + /** * Remove references and cv-qualifiers. * @since C++20 * @{ diff --git a/libstdc++-v3/testsuite/20_util/is_consteval_only/requirements/explicit_instantiation.cc b/libstdc++-v3/testsuite/20_util/is_consteval_only/requirements/explicit_instantiation.cc new file mode 100644 index 00000000000..3680730bd45 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/is_consteval_only/requirements/explicit_instantiation.cc @@ -0,0 +1,29 @@ +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +// Copyright (C) 2025 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 +// . + +// NB: This file is for testing type_traits with NO OTHER INCLUDES. + +#include + +namespace std +{ + typedef short test_type; + template struct is_consteval_only; +} diff --git a/libstdc++-v3/testsuite/20_util/is_consteval_only/requirements/typedefs.cc b/libstdc++-v3/testsuite/20_util/is_consteval_only/requirements/typedefs.cc new file mode 100644 index 00000000000..39a55baeb3f --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/is_consteval_only/requirements/typedefs.cc @@ -0,0 +1,34 @@ +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +// Copyright (C) 2025 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 +// . + +// +// NB: This file is for testing type_traits with NO OTHER INCLUDES. + +#include + +void test01() +{ + // Check for required typedefs + typedef std::is_consteval_only test_type; + typedef test_type::value_type value_type; + typedef test_type::type type; + typedef test_type::type::value_type type_value_type; + typedef test_type::type::type type_type; +} diff --git a/libstdc++-v3/testsuite/20_util/is_consteval_only/value.cc b/libstdc++-v3/testsuite/20_util/is_consteval_only/value.cc new file mode 100644 index 00000000000..d391ac79c34 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/is_consteval_only/value.cc @@ -0,0 +1,47 @@ +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +// Copyright (C) 2025 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 +// . + +#include +#include + +void test01() +{ + using std::is_consteval_only; + using namespace __gnu_test; + int v = 1; + struct S1 { decltype(^^long) a; }; + union U2 { int a; decltype(^^test01) b; }; + struct S3 { const decltype(^^__gnu_test) *c; }; + struct S4 : public S3 {}; + struct S5 { int a; long *b; }; + + static_assert(test_category(true), ""); + static_assert(test_category(true), ""); + static_assert(test_category(true), ""); + static_assert(test_category(true), ""); + static_assert(test_category(true), ""); + static_assert(test_category(true), ""); + static_assert(test_category(true), ""); + static_assert(test_category(true), ""); + + // Sanity check. + static_assert(test_category(false), ""); + static_assert(test_category(false), ""); +}