From ba1b4e36d910905f01bd3c26d95a9c62b7a939c5 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Sat, 4 Jul 2020 05:45:01 -0400 Subject: c++: Support non-type template parms of union type. Another thing newly allowed by P1907R1. The ABI group has discussed representing unions with designated initializers, and has separately specified how to represent designators; this patch implements both. gcc/cp/ChangeLog: * tree.c (structural_type_p): Allow unions. * mangle.c (write_expression): Express unions with a designator. libiberty/ChangeLog: * cp-demangle.c (cplus_demangle_operators): Add di, dx, dX. (d_expression_1): Handle di and dX. (is_designated_init, d_maybe_print_designated_init): New. (d_print_comp_inner): Use d_maybe_print_designated_init. * testsuite/demangle-expected: Add designator tests. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/nontype-class-union1.C: New test. --- gcc/cp/mangle.c | 11 ++++++++-- gcc/cp/tree.c | 7 ------- gcc/testsuite/g++.dg/cpp2a/nontype-class-union1.C | 25 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class-union1.C (limited to 'gcc') diff --git a/gcc/cp/mangle.c b/gcc/cp/mangle.c index 090fb52..ab2d8ec 100644 --- a/gcc/cp/mangle.c +++ b/gcc/cp/mangle.c @@ -3189,6 +3189,7 @@ write_expression (tree expr) { vec *elts = CONSTRUCTOR_ELTS (expr); unsigned last_nonzero = UINT_MAX, i; + constructor_elt *ce; tree val; if (!nontriv) @@ -3197,12 +3198,18 @@ write_expression (tree expr) last_nonzero = i; if (nontriv || last_nonzero != UINT_MAX) - FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val) + for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) { if (i > last_nonzero) break; /* FIXME handle RANGE_EXPR */ - write_expression (val); + if (TREE_CODE (etype) == UNION_TYPE) + { + /* Express the active member as a designator. */ + write_string ("di"); + write_unqualified_name (ce->index); + } + write_expression (ce->value); } } else diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index 9effd27..1fcba55 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -4534,13 +4534,6 @@ structural_type_p (tree t, bool explain) structural types or (possibly multi-dimensional) array thereof. */ if (!CLASS_TYPE_P (t)) return false; - if (TREE_CODE (t) == UNION_TYPE) - { - /* FIXME allow (and mangle) unions. */ - if (explain) - inform (location_of (t), "%qT is a union", t); - return false; - } if (!literal_type_p (t)) { if (explain) diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class-union1.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class-union1.C new file mode 100644 index 0000000..038d46f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class-union1.C @@ -0,0 +1,25 @@ +// { dg-do compile { target c++20 } } + +template struct A {}; +template struct assert_same; +template struct assert_same {}; + +#define TEQ(X,Y) static_assert(__is_same(A<(X)>,A<(Y)>)) +#define TNEQ(X,Y) static_assert(!__is_same(A<(X)>,A<(Y)>)) + +union U { + int i; int j; + constexpr U(int i): i(i) {} + constexpr U(unsigned u): j(u) {} +}; + +TEQ(U(0),U(0)); + +// Calling the other constructor initializes a different member with the same +// value. We need to distinguish these. +TNEQ(U(0),U(0u)); + +// { dg-final { scan-assembler "_Z1f1AIXtl1Udi1iLi0EEEE" } } +void f(A) { } +// { dg-final { scan-assembler "_Z1g1AIXtl1Udi1jLi0EEEE" } } +void g(A) { } -- cgit v1.1