[C++ PATCH] Add __is_aggregate trait builtin (PR libstdc++/80251)

Jakub Jelinek jakub@redhat.com
Thu Mar 30 20:34:00 GMT 2017


Hi!

As discussed with Jon, libstdc++ needs a GCC builtin in order to implement
this easily.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2017-03-30  Jakub Jelinek  <jakub@redhat.com>

	PR libstdc++/80251
c-family/
	* c-common.h (enum rid): Add RID_IS_AGGREGATE.
	* c-common.c (c_common_reswords): Add __is_aggregate trait.
	* cp-tree.h (enum cp_trait_kind): Add CPTK_IS_AGGREGATE.
	* cxx-pretty-print.c (pp_cxx_trait_expression): Handle
	CPTK_IS_AGGREGATE.
	* semantics.c (trait_expr_value): Handle CPTK_IS_AGGREGATE.
	Remove extraneous parens.
	(finish_trait_expr): Handle CPTK_IS_AGGREGATE.
	* parser.c (cp_parser_primary_expression): Handle RID_IS_AGGREGATE.
	(cp_parser_trait_expr): Likewise.
testsuite/
	* g++.dg/ext/is_aggregate.C: New test.

--- gcc/c-family/c-common.h.jj	2017-02-24 21:39:13.000000000 +0100
+++ gcc/c-family/c-common.h	2017-03-30 11:54:35.289989887 +0200
@@ -162,8 +162,8 @@ enum rid
   RID_HAS_TRIVIAL_CONSTRUCTOR, RID_HAS_TRIVIAL_COPY,
   RID_HAS_TRIVIAL_DESTRUCTOR,  RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
   RID_HAS_VIRTUAL_DESTRUCTOR,
-  RID_IS_ABSTRACT,             RID_IS_BASE_OF,
-  RID_IS_CLASS,
+  RID_IS_ABSTRACT,             RID_IS_AGGREGATE,
+  RID_IS_BASE_OF,              RID_IS_CLASS,
   RID_IS_EMPTY,                RID_IS_ENUM,
   RID_IS_FINAL,                RID_IS_LITERAL_TYPE,
   RID_IS_POD,                  RID_IS_POLYMORPHIC,
--- gcc/c-family/c-common.c.jj	2017-03-29 07:11:13.000000000 +0200
+++ gcc/c-family/c-common.c	2017-03-30 11:54:06.551365199 +0200
@@ -403,6 +403,7 @@ const struct c_common_resword c_common_r
   { "__inline",		RID_INLINE,	0 },
   { "__inline__",	RID_INLINE,	0 },
   { "__is_abstract",	RID_IS_ABSTRACT, D_CXXONLY },
+  { "__is_aggregate",	RID_IS_AGGREGATE, D_CXXONLY },
   { "__is_base_of",	RID_IS_BASE_OF, D_CXXONLY },
   { "__is_class",	RID_IS_CLASS,	D_CXXONLY },
   { "__is_empty",	RID_IS_EMPTY,	D_CXXONLY },
--- gcc/cp/cp-tree.h.jj	2017-03-29 07:11:18.000000000 +0200
+++ gcc/cp/cp-tree.h	2017-03-30 11:56:39.036370331 +0200
@@ -728,6 +728,7 @@ enum cp_trait_kind
   CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS,
   CPTK_HAS_VIRTUAL_DESTRUCTOR,
   CPTK_IS_ABSTRACT,
+  CPTK_IS_AGGREGATE,
   CPTK_IS_BASE_OF,
   CPTK_IS_CLASS,
   CPTK_IS_EMPTY,
--- gcc/cp/cxx-pretty-print.c.jj	2017-01-01 12:45:44.000000000 +0100
+++ gcc/cp/cxx-pretty-print.c	2017-03-30 11:57:10.924952663 +0200
@@ -2585,6 +2585,9 @@ pp_cxx_trait_expression (cxx_pretty_prin
     case CPTK_IS_ABSTRACT:
       pp_cxx_ws_string (pp, "__is_abstract");
       break;
+    case CPTK_IS_AGGREGATE:
+      pp_cxx_ws_string (pp, "__is_aggregate");
+      break;
     case CPTK_IS_BASE_OF:
       pp_cxx_ws_string (pp, "__is_base_of");
       break;
--- gcc/cp/semantics.c.jj	2017-03-22 19:53:09.000000000 +0100
+++ gcc/cp/semantics.c	2017-03-30 11:59:24.240206534 +0200
@@ -9144,7 +9144,10 @@ trait_expr_value (cp_trait_kind kind, tr
       return type_has_unique_obj_representations (type1);
 
     case CPTK_IS_ABSTRACT:
-      return (ABSTRACT_CLASS_TYPE_P (type1));
+      return ABSTRACT_CLASS_TYPE_P (type1);
+
+    case CPTK_IS_AGGREGATE:
+      return CP_AGGREGATE_TYPE_P (type1);
 
     case CPTK_IS_BASE_OF:
       return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
@@ -9152,34 +9155,34 @@ trait_expr_value (cp_trait_kind kind, tr
 		  || DERIVED_FROM_P (type1, type2)));
 
     case CPTK_IS_CLASS:
-      return (NON_UNION_CLASS_TYPE_P (type1));
+      return NON_UNION_CLASS_TYPE_P (type1);
 
     case CPTK_IS_EMPTY:
-      return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
+      return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
 
     case CPTK_IS_ENUM:
-      return (type_code1 == ENUMERAL_TYPE);
+      return type_code1 == ENUMERAL_TYPE;
 
     case CPTK_IS_FINAL:
-      return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
+      return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
 
     case CPTK_IS_LITERAL_TYPE:
-      return (literal_type_p (type1));
+      return literal_type_p (type1);
 
     case CPTK_IS_POD:
-      return (pod_type_p (type1));
+      return pod_type_p (type1);
 
     case CPTK_IS_POLYMORPHIC:
-      return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
+      return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
 
     case CPTK_IS_SAME_AS:
       return same_type_p (type1, type2);
 
     case CPTK_IS_STD_LAYOUT:
-      return (std_layout_type_p (type1));
+      return std_layout_type_p (type1);
 
     case CPTK_IS_TRIVIAL:
-      return (trivial_type_p (type1));
+      return trivial_type_p (type1);
 
     case CPTK_IS_TRIVIALLY_ASSIGNABLE:
       return is_trivially_xible (MODIFY_EXPR, type1, type2);
@@ -9188,10 +9191,10 @@ trait_expr_value (cp_trait_kind kind, tr
       return is_trivially_xible (INIT_EXPR, type1, type2);
 
     case CPTK_IS_TRIVIALLY_COPYABLE:
-      return (trivially_copyable_p (type1));
+      return trivially_copyable_p (type1);
 
     case CPTK_IS_UNION:
-      return (type_code1 == UNION_TYPE);
+      return type_code1 == UNION_TYPE;
 
     default:
       gcc_unreachable ();
@@ -9253,6 +9256,7 @@ finish_trait_expr (cp_trait_kind kind, t
     case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
     case CPTK_HAS_VIRTUAL_DESTRUCTOR:
     case CPTK_IS_ABSTRACT:
+    case CPTK_IS_AGGREGATE:
     case CPTK_IS_EMPTY:
     case CPTK_IS_FINAL:
     case CPTK_IS_LITERAL_TYPE:
--- gcc/cp/parser.c.jj	2017-03-07 20:06:35.000000000 +0100
+++ gcc/cp/parser.c	2017-03-30 11:56:13.699702184 +0200
@@ -5121,6 +5121,7 @@ cp_parser_primary_expression (cp_parser
 	case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
 	case RID_HAS_VIRTUAL_DESTRUCTOR:
 	case RID_IS_ABSTRACT:
+	case RID_IS_AGGREGATE:
 	case RID_IS_BASE_OF:
 	case RID_IS_CLASS:
 	case RID_IS_EMPTY:
@@ -9611,6 +9612,9 @@ cp_parser_trait_expr (cp_parser* parser,
     case RID_IS_ABSTRACT:
       kind = CPTK_IS_ABSTRACT;
       break;
+    case RID_IS_AGGREGATE:
+      kind = CPTK_IS_AGGREGATE;
+      break;
     case RID_IS_BASE_OF:
       kind = CPTK_IS_BASE_OF;
       binary = true;
--- gcc/testsuite/g++.dg/ext/is_aggregate.C.jj	2017-03-30 12:01:48.154321583 +0200
+++ gcc/testsuite/g++.dg/ext/is_aggregate.C	2017-03-30 13:37:17.413831900 +0200
@@ -0,0 +1,117 @@
+// { dg-do run { target c++11 } }
+#include <cassert>
+
+template<typename T>
+  bool
+  f()
+  { return __is_aggregate(T); } 
+
+template<typename T>
+  class My
+  {
+  public:
+    bool
+    f()
+    { return !!__is_aggregate(T); }
+  };
+
+template<typename T>
+  class My2
+  {
+  public:
+    static const bool trait = __is_aggregate(T);
+  };
+
+template<typename T>
+  const bool My2<T>::trait;
+
+template<typename T, bool b = __is_aggregate(T)>
+  struct My3_help
+  { static const bool trait = b; };
+
+template<typename T, bool b>
+  const bool My3_help<T, b>::trait;
+
+template<typename T>
+  class My3
+  {
+  public:
+    bool
+    f()
+    { return My3_help<T>::trait; }
+  };
+
+#define PTEST(T) (__is_aggregate(T) && f<T>() \
+		  && My<T>().f() && My2<T>::trait && My3<T>().f())
+
+#define NTEST(T) (!__is_aggregate(T) && !f<T>() \
+		  && !My<T>().f() && !My2<T>::trait && !My3<T>().f())
+
+struct A { int a, b, c; };
+class B { static int a; private: static int b; public: int c; };
+struct C { C () {} int a, b, c; };
+struct D { explicit D (int) {} int a, b, c; };
+struct E : public A { int d, e, f; };
+struct F : public C { using C::C; int d, e, f; };
+class G { int a, b; };
+struct H { private: int a, b; };
+struct I { protected: int a, b; };
+struct J { int a, b; void foo (); };
+struct K { int a, b; virtual void foo (); };
+struct L : virtual public A { int d, e; };
+struct M : protected A { int d, e; };
+struct N : private A { int d, e; };
+typedef int T;
+typedef float U;
+typedef int V __attribute__((vector_size (4 * sizeof (int))));
+typedef double W __attribute__((vector_size (8 * sizeof (double))));
+
+int
+main ()
+{
+  assert (NTEST (void));
+  assert (NTEST (int));
+  assert (NTEST (double));
+  assert (NTEST (T));
+  assert (NTEST (U));
+  assert (PTEST (V));
+  assert (PTEST (W));
+  assert (PTEST (A));
+  assert (PTEST (B));
+  assert (NTEST (C));
+  assert (NTEST (D));
+#if __cplusplus >= 201703L
+  assert (PTEST (E));
+#else
+  assert (NTEST (E));
+#endif
+  assert (NTEST (F));
+  assert (NTEST (G));
+  assert (NTEST (H));
+  assert (NTEST (I));
+  assert (PTEST (J));
+  assert (NTEST (K));
+  assert (NTEST (L));
+  assert (NTEST (M));
+  assert (NTEST (N));
+  assert (PTEST (int[]));
+  assert (PTEST (double[]));
+  assert (PTEST (T[2]));
+  assert (PTEST (U[]));
+  assert (PTEST (V[]));
+  assert (PTEST (W[]));
+  assert (PTEST (A[19]));
+  assert (PTEST (B[]));
+  assert (PTEST (C[]));
+  assert (PTEST (D[]));
+  assert (PTEST (E[]));
+  assert (PTEST (F[]));
+  assert (PTEST (G[]));
+  assert (PTEST (H[]));
+  assert (PTEST (I[]));
+  assert (PTEST (J[24]));
+  assert (PTEST (K[]));
+  assert (PTEST (L[]));
+  assert (PTEST (M[6]));
+  assert (PTEST (N[]));
+}

	Jakub



More information about the Libstdc++ mailing list