]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Define built-in for std::tuple_element [PR100157]
authorPatrick Palka <ppalka@redhat.com>
Wed, 19 Apr 2023 19:36:34 +0000 (15:36 -0400)
committerPatrick Palka <ppalka@redhat.com>
Wed, 19 Apr 2023 19:36:34 +0000 (15:36 -0400)
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 <jwakely@redhat.com>
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) <case TRAIT_TYPE>: 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) <case TRAIT_TYPE>: 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.

13 files changed:
gcc/cp/cp-trait.def
gcc/cp/cp-tree.h
gcc/cp/cxx-pretty-print.cc
gcc/cp/parser.cc
gcc/cp/pt.cc
gcc/cp/semantics.cc
gcc/cp/tree.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/ext/type_pack_element1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/type_pack_element2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/type_pack_element3.C [new file with mode: 0644]
libstdc++-v3/include/bits/utility.h
libstdc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc

index bac593c00945799acdcd358955d747f4111e6f3f..8b7fece0cc86e5a666c8b1104837a452ccaa02e3 100644 (file)
@@ -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 (TYPE_PACK_ELEMENT, "__type_pack_element", -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.  */
index a14eb8d0b9a538e49c3c53220a3df0bc6557091b..6c5920dcbe07dd923407ea38c69f92aa48af6f8f 100644 (file)
@@ -7761,7 +7761,7 @@ extern tree finish_decltype_type                (tree, bool, tsubst_flags_t);
 extern tree fold_builtin_is_corresponding_member (location_t, int, tree *);
 extern tree fold_builtin_is_pointer_inverconvertible_with_class (location_t, int, tree *);
 extern tree finish_trait_expr                  (location_t, enum cp_trait_kind, tree, tree);
-extern tree finish_trait_type                  (enum cp_trait_kind, tree, tree);
+extern tree finish_trait_type                  (enum cp_trait_kind, tree, tree, tsubst_flags_t);
 extern tree build_lambda_expr                   (void);
 extern tree build_lambda_object                        (tree);
 extern tree begin_lambda_type                   (tree);
index 7f4556d0da2b2ac827d3777d08bdee94dc7632bd..c33919873f1c23ce40439779b4e72d4cd3e27f42 100644 (file)
@@ -2625,11 +2625,19 @@ pp_cxx_trait (cxx_pretty_printer *pp, tree t)
 #undef DEFTRAIT
     }
 
-  pp_cxx_left_paren (pp);
-  if (TYPE_P (type1))
-    pp->type_id (type1);
+  if (kind == CPTK_TYPE_PACK_ELEMENT)
+    {
+      pp_cxx_begin_template_argument_list (pp);
+      pp->expression (type1);
+    }
   else
-    pp->expression (type1);
+    {
+      pp_cxx_left_paren (pp);
+      if (TYPE_P (type1))
+       pp->type_id (type1);
+      else
+       pp->expression (type1);
+    }
   if (type2)
     {
       if (TREE_CODE (type2) != TREE_LIST)
@@ -2644,7 +2652,10 @@ pp_cxx_trait (cxx_pretty_printer *pp, tree t)
            pp->type_id (TREE_VALUE (arg));
          }
     }
-  pp_cxx_right_paren (pp);
+  if (kind == CPTK_TYPE_PACK_ELEMENT)
+    pp_cxx_end_template_argument_list (pp);
+  else
+    pp_cxx_right_paren (pp);
 }
 
 // requires-clause:
index a6341b98af21aabc047e3faf59fe22559b7aed6f..ee1497b7071a724c2377419bde1e86640da32096 100644 (file)
@@ -10730,9 +10730,9 @@ cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
 
 static cp_expr
 cp_parser_constant_expression (cp_parser* parser,
-                              int allow_non_constant_p,
-                              bool *non_constant_p,
-                              bool strict_p)
+                              int allow_non_constant_p /* = 0 */,
+                              bool *non_constant_p /* = NULL */,
+                              bool strict_p /* = false */)
 {
   bool saved_integral_constant_expression_p;
   bool saved_allow_non_integral_constant_expression_p;
@@ -10959,7 +10959,10 @@ cp_parser_trait (cp_parser* parser, enum rid keyword)
   cp_lexer_consume_token (parser->lexer);
 
   matching_parens parens;
-  parens.require_open (parser);
+  if (kind == CPTK_TYPE_PACK_ELEMENT)
+    cp_parser_require (parser, CPP_LESS, RT_LESS);
+  else
+    parens.require_open (parser);
 
   if (kind == CPTK_IS_DEDUCIBLE)
     {
@@ -10972,6 +10975,12 @@ cp_parser_trait (cp_parser* parser, enum rid keyword)
                                       /*optional_p=*/false);
       type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
     }
+  else if (kind == CPTK_TYPE_PACK_ELEMENT)
+    /* __type_pack_element takes an expression as its first argument and uses
+       template-id syntax instead of function call syntax (for consistency
+       with Clang).  We special case these properties of __type_pack_element
+       here and elsewhere.  */
+    type1 = cp_parser_constant_expression (parser);
   else
     {
       type_id_in_expr_sentinel s (parser);
@@ -10981,7 +10990,24 @@ cp_parser_trait (cp_parser* parser, enum rid keyword)
   if (type1 == error_mark_node)
     return error_mark_node;
 
-  if (binary)
+  if (kind == CPTK_TYPE_PACK_ELEMENT)
+    {
+      cp_parser_require (parser, CPP_COMMA, RT_COMMA);
+      tree rest = cp_parser_enclosed_template_argument_list (parser);
+      for (tree elt : tree_vec_range (rest))
+       {
+         if (!TYPE_P (elt))
+           {
+             error_at (cp_expr_loc_or_input_loc (elt),
+                       "trailing argument to %<__type_pack_element%> "
+                       "is not a type");
+             return error_mark_node;
+           }
+         type2 = tree_cons (NULL_TREE, elt, type2);
+       }
+      type2 = nreverse (type2);
+    }
+  else if (binary)
     {
       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
 
@@ -11012,7 +11038,11 @@ cp_parser_trait (cp_parser* parser, enum rid keyword)
     }
 
   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
-  parens.require_close (parser);
+  if (kind == CPTK_TYPE_PACK_ELEMENT)
+    /* cp_parser_enclosed_template_argument_list above already took care
+       of parsing the closing '>'.  */;
+  else
+    parens.require_close (parser);
 
   /* Construct a location of the form:
        __is_trivially_copyable(_Tp)
@@ -11030,7 +11060,7 @@ cp_parser_trait (cp_parser* parser, enum rid keyword)
       return cp_expr (finish_bases (type1, true), trait_loc);
     default:
       if (type)
-       return finish_trait_type (kind, type1, type2);
+       return finish_trait_type (kind, type1, type2, tf_warning_or_error);
       else
        return finish_trait_expr (trait_loc, kind, type1, type2);
     }
index 68a056acf8b6014325b259258d298e4abc74b701..f65f2d58b28b4d98b6a5b2c8fa830a2278c559ce 100644 (file)
@@ -16696,9 +16696,13 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 
     case TRAIT_TYPE:
       {
-       tree type1 = tsubst (TRAIT_TYPE_TYPE1 (t), args, complain, in_decl);
+       tree type1 = TRAIT_TYPE_TYPE1 (t);
+       if (TYPE_P (type1))
+         type1 = tsubst (type1, args, complain, in_decl);
+       else
+         type1 = tsubst_copy_and_build (type1, args, complain, in_decl);
        tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
-       type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2);
+       type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
        return cp_build_qualified_type (type,
                                        cp_type_quals (t) | cp_type_quals (type),
                                        complain | tf_ignore_bad_quals);
index 45e0b0e81d30a6c389af2beed03d5c105d824b08..a4f30fdac113f630e2d5fc847c0b65d86d74e1f9 100644 (file)
@@ -4470,6 +4470,36 @@ finish_underlying_type (tree type)
   return underlying_type;
 }
 
+/* Implement the __type_pack_element keyword: Return the type
+   at index IDX within TYPES.  */
+
+static tree
+finish_type_pack_element (tree idx, tree types, tsubst_flags_t complain)
+{
+  idx = maybe_constant_value (idx);
+  if (TREE_CODE (idx) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (idx)))
+    {
+      if (complain & tf_error)
+       error ("%<__type_pack_element%> index is not an integral constant");
+      return error_mark_node;
+    }
+  HOST_WIDE_INT val = tree_to_shwi (idx);
+  if (val < 0)
+    {
+      if (complain & tf_error)
+       error ("%<__type_pack_element%> index is negative");
+      return error_mark_node;
+    }
+  tree result = chain_index (val, types);
+  if (!result)
+    {
+      if (complain & tf_error)
+       error ("%<__type_pack_element%> index is out of range");
+      return error_mark_node;
+    }
+  return TREE_VALUE (result);
+}
+
 /* Implement the __direct_bases keyword: Return the direct base classes
    of type.  */
 
@@ -12245,7 +12275,8 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
 /* Process a trait type.  */
 
 tree
-finish_trait_type (cp_trait_kind kind, tree type1, tree type2)
+finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
+                  tsubst_flags_t complain)
 {
   if (type1 == error_mark_node
       || type2 == error_mark_node)
@@ -12269,17 +12300,23 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2)
     {
     case CPTK_UNDERLYING_TYPE:
       return finish_underlying_type (type1);
+
     case CPTK_REMOVE_CV:
       return cv_unqualified (type1);
+
     case CPTK_REMOVE_REFERENCE:
       if (TYPE_REF_P (type1))
        type1 = TREE_TYPE (type1);
       return type1;
+
     case CPTK_REMOVE_CVREF:
       if (TYPE_REF_P (type1))
        type1 = TREE_TYPE (type1);
       return cv_unqualified (type1);
 
+    case CPTK_TYPE_PACK_ELEMENT:
+      return finish_type_pack_element (type1, type2, complain);
+
 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
     case CPTK_##CODE:
 #include "cp-trait.def"
index 16b8fcb7d57518635e74340758347ae4fcbfae60..2c22fac17ee8c6b9981f216624f0fa77f658b063 100644 (file)
@@ -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:
index 8b60cbbc167077a141729c5992f76849464bd6a2..53ac925a09241914ff4ed94dbc4fbb00f2dd6c55 100644 (file)
@@ -1632,7 +1632,7 @@ structural_comptypes (tree t1, tree t2, int strict)
     case TRAIT_TYPE:
       if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
        return false;
-      if (!same_type_p (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
+      if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
          || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
        return false;
       break;
diff --git a/gcc/testsuite/g++.dg/ext/type_pack_element1.C b/gcc/testsuite/g++.dg/ext/type_pack_element1.C
new file mode 100644 (file)
index 0000000..4685855
--- /dev/null
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+
+using ty0 = __type_pack_element<0, int>;
+using ty0 = __type_pack_element<0, int, char>;
+using ty0 = int;
+
+using ty1 = __type_pack_element<1, int, char>;
+using ty1 = __type_pack_element<(6 - 5) * 1, int, char>;
+using ty1 = char;
+
+template<int N, class... Ts>
+using __const_type_pack_element_t = const __type_pack_element<N, Ts...>;
+
+using ty2 = __const_type_pack_element_t<2, int, char, long>;
+using ty2 = const long;
+
+template<class T> struct A { };
+using ty3 = __type_pack_element<3, int, int, int, A<int>>;
+using ty3 = A<int>;
diff --git a/gcc/testsuite/g++.dg/ext/type_pack_element2.C b/gcc/testsuite/g++.dg/ext/type_pack_element2.C
new file mode 100644 (file)
index 0000000..1bf7753
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-do compile { target c++11 } }
+
+int p;
+
+using type = __type_pack_element<&p, int>;      // { dg-error "not an integral constant" }
+using type = __type_pack_element<1, int>;       // { dg-error "out of range" }
+using type = __type_pack_element<2, int, char>; // { dg-error "out of range" }
+using type = __type_pack_element<-1, int>;      // { dg-error "negative" }
+
+template<int N, class... Ts>
+using __type_pack_element_t = __type_pack_element<N, Ts...>;
+// { dg-error "out of range" "" { target *-*-* } .-1 }
+
+using type = __type_pack_element_t<3, int, char, long>; // { dg-message "here" }
diff --git a/gcc/testsuite/g++.dg/ext/type_pack_element3.C b/gcc/testsuite/g++.dg/ext/type_pack_element3.C
new file mode 100644 (file)
index 0000000..269f84f
--- /dev/null
@@ -0,0 +1,22 @@
+// { dg-do compile { target c++11 } }
+
+template<class T, T N, class... Ts, class = __type_pack_element<N, Ts...>>
+constexpr int f(int) { return 1; }
+
+template<class T, T N, class... Ts>
+constexpr int f(...) { return 2; };
+
+int p;
+
+static_assert(f<int, 0, void, char>(0) == 1, "");
+static_assert(f<int, 1, void, char>(0) == 1, "");
+static_assert(f<int, 2, void, char>(0) == 2, "");
+static_assert(f<int*, &p, void, char>(0) == 2, "");
+
+template<class T, class U> struct A;
+template<class T> struct A<T, __type_pack_element<sizeof(T), void, long>> { };
+template struct A<char, long>;
+
+template<class T, class U> struct B;
+template<class T> struct B<T, __type_pack_element<0, T, short>> { };
+template struct B<int, int>;
index abaaae2dd3384a28bf82add9d6800348bab1cca4..4692aa0c9b01423da7f8e5449283bbb209909445 100644 (file)
@@ -224,6 +224,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif // C++17
 #endif // C++14
 
+#if __has_builtin(__type_pack_element)
+  template<size_t _Np, typename... _Types>
+    struct _Nth_type
+    { using type = __type_pack_element<_Np, _Types...>; };
+#else
   template<size_t _Np, typename... _Types>
     struct _Nth_type
     { };
@@ -262,6 +267,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct _Nth_type<1, _Tp0, _Tp1, _Tp2, _Rest...>
     { using type = _Tp1; };
 #endif
+#endif
 
 #if __cplusplus > 202002L
 #define __cpp_lib_ranges_zip 202110L // for <tuple> and <utility>
index 9bce0b1caa2abb34066f90c9f9b4ac1c3a3d9b23..dd0df9be1ea3cbc6684524b3860dabc0b7c1ecea 100644 (file)
@@ -61,3 +61,4 @@ test03()
 
 // { dg-error "tuple index must be in range" "" { target *-*-* } 0 }
 // { dg-prune-output "no type named 'type' in .*_Nth_type" }
+// { dg-prune-output "'__type_pack_element' index is out of range" }