From 58b7dbf865b146a4e65dbda9be6df78f212c03b6 Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Wed, 19 Apr 2023 15:36:34 -0400 Subject: c++: Define built-in for std::tuple_element [PR100157] This adds a new built-in to replace the recursive class template instantiations done by traits such as std::tuple_element and std::variant_alternative. The purpose is to select the Nth type from a list of types, e.g. __type_pack_element<1, char, int, float> is int. We implement it as a special kind of TRAIT_TYPE. For a pathological example tuple_element_t<1000, tuple<2000 types...>> the compilation time is reduced by more than 90% and the memory used by the compiler is reduced by 97%. In realistic examples the gains will be much smaller, but still relevant. Unlike the other built-in traits, __type_pack_element uses template-id syntax instead of call syntax and is SFINAE-enabled, matching Clang's implementation. And like the other built-in traits, it's not mangleable so we can't use it directly in function signatures. N.B. Clang seems to implement __type_pack_element as a first-class template that can e.g. be used as a template-template argument. For simplicity we implement it in a more ad-hoc way. Co-authored-by: Jonathan Wakely PR c++/100157 gcc/cp/ChangeLog: * cp-trait.def (TYPE_PACK_ELEMENT): Define. * cp-tree.h (finish_trait_type): Add complain parameter. * cxx-pretty-print.cc (pp_cxx_trait): Handle CPTK_TYPE_PACK_ELEMENT. * parser.cc (cp_parser_constant_expression): Document default arguments. (cp_parser_trait): Handle CPTK_TYPE_PACK_ELEMENT. Pass tf_warning_or_error to finish_trait_type. * pt.cc (tsubst) : Handle non-type first argument. Pass complain to finish_trait_type. * semantics.cc (finish_type_pack_element): Define. (finish_trait_type): Add complain parameter. Handle CPTK_TYPE_PACK_ELEMENT. * tree.cc (strip_typedefs): Handle non-type first argument. Pass tf_warning_or_error to finish_trait_type. * typeck.cc (structural_comptypes) : Use cp_tree_equal instead of same_type_p for the first argument. libstdc++-v3/ChangeLog: * include/bits/utility.h (_Nth_type): Conditionally define in terms of __type_pack_element if available. * testsuite/20_util/tuple/element_access/get_neg.cc: Prune additional errors from the new built-in. gcc/testsuite/ChangeLog: * g++.dg/ext/type_pack_element1.C: New test. * g++.dg/ext/type_pack_element2.C: New test. * g++.dg/ext/type_pack_element3.C: New test. --- gcc/cp/tree.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'gcc/cp/tree.cc') diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 16b8fcb..2c22fac 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -1792,14 +1792,18 @@ strip_typedefs (tree t, bool *remove_attributes /* = NULL */, break; case TRAIT_TYPE: { - tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t), - remove_attributes, flags); + tree type1 = TRAIT_TYPE_TYPE1 (t); + if (TYPE_P (type1)) + type1 = strip_typedefs (type1, remove_attributes, flags); + else + type1 = strip_typedefs_expr (type1, remove_attributes, flags); tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t), remove_attributes, flags); if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t)) result = NULL_TREE; else - result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2); + result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, + tf_warning_or_error); } break; case TYPE_PACK_EXPANSION: -- cgit v1.1