With C++ templates and attribute ((aligned)), you can have TYPE_ALIGN
and TYPE_USER_ALIGN set on a type before you know its size, so
layout_type and kin need to respect them if they are already set.
Tested x86_64-pc-linux-gnu. OK for trunk?
commit 9b138ff6e829e73765e7bba75771751c6239e7cc
Author: Jason Merrill <ja...@redhat.com>
Date: Fri Apr 10 18:13:56 2015 -0400
PR c++/65734
gcc/
* stor-layout.c (finalize_type_size): Respect TYPE_USER_ALIGN.
(layout_type) [ARRAY_TYPE]: Likewise.
gcc/cp/
* class.c (fixup_attribute_variants): Respect TYPE_USER_ALIGN.
diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index 9f189fb..c08bd49 100644
--- a/gcc/cp/class.c
+++ b/gcc/cp/class.c
@@ -1989,14 +1989,23 @@ fixup_attribute_variants (tree t)
if (!t)
return;
+ tree attrs = TYPE_ATTRIBUTES (t);
+ unsigned align = TYPE_ALIGN (t);
+ bool user_align = TYPE_USER_ALIGN (t);
+
for (variants = TYPE_NEXT_VARIANT (t);
variants;
variants = TYPE_NEXT_VARIANT (variants))
{
/* These are the two fields that check_qualified_type looks at and
are affected by attributes. */
- TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
- TYPE_ALIGN (variants) = TYPE_ALIGN (t);
+ TYPE_ATTRIBUTES (variants) = attrs;
+ unsigned valign = align;
+ if (TYPE_USER_ALIGN (variants))
+ valign = MAX (valign, TYPE_ALIGN (variants));
+ else
+ TYPE_USER_ALIGN (variants) = user_align;
+ TYPE_ALIGN (variants) = valign;
}
}
diff --git a/gcc/stor-layout.c b/gcc/stor-layout.c
index f18f1ac..aac407d 100644
--- a/gcc/stor-layout.c
+++ b/gcc/stor-layout.c
@@ -1831,9 +1831,13 @@ finalize_type_size (tree type)
{
TYPE_SIZE (variant) = size;
TYPE_SIZE_UNIT (variant) = size_unit;
- TYPE_ALIGN (variant) = align;
+ unsigned valign = align;
+ if (TYPE_USER_ALIGN (variant))
+ valign = MAX (valign, TYPE_ALIGN (variant));
+ else
+ TYPE_USER_ALIGN (variant) = user_align;
+ TYPE_ALIGN (variant) = valign;
TYPE_PRECISION (variant) = precision;
- TYPE_USER_ALIGN (variant) = user_align;
SET_TYPE_MODE (variant, mode);
}
}
@@ -2350,13 +2354,17 @@ layout_type (tree type)
/* Now round the alignment and size,
using machine-dependent criteria if any. */
+ unsigned align = TYPE_ALIGN (element);
+ if (TYPE_USER_ALIGN (type))
+ align = MAX (align, TYPE_ALIGN (type));
+ else
+ TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
#ifdef ROUND_TYPE_ALIGN
- TYPE_ALIGN (type)
- = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
+ align = ROUND_TYPE_ALIGN (type, align, BITS_PER_UNIT);
#else
- TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
+ align = MAX (align, BITS_PER_UNIT);
#endif
- TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
+ TYPE_ALIGN (type) = align;
SET_TYPE_MODE (type, BLKmode);
if (TYPE_SIZE (type) != 0
&& ! targetm.member_type_forces_blk (type, VOIDmode)
diff --git a/gcc/testsuite/g++.dg/cpp0x/alignas1.C b/gcc/testsuite/g++.dg/cpp0x/alignas1.C
new file mode 100644
index 0000000..d73c875
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alignas1.C
@@ -0,0 +1,16 @@
+// PR c++/65734
+// { dg-do compile { target c++11 } }
+
+template <class T> struct A
+{
+ T t;
+};
+
+typedef A<int> T[4] alignas (2 * alignof (int));
+A<int> a[4];
+
+typedef A<char> T2[4] alignas (2 * alignof (int));
+
+#define SA(X) static_assert((X),#X)
+SA(alignof (T) == 2 * alignof(int));
+SA(alignof (T2) == 2 * alignof(int));
diff --git a/gcc/testsuite/g++.dg/cpp0x/alignas2.C b/gcc/testsuite/g++.dg/cpp0x/alignas2.C
new file mode 100644
index 0000000..2e7d051
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alignas2.C
@@ -0,0 +1,20 @@
+// PR c++/65734
+// { dg-do compile { target c++11 } }
+
+template <typename T>
+struct BVector
+{
+ T t;
+};
+BVector<int> m;
+
+template <template <class> class T>
+struct BV2
+{
+ typedef T<float> value_type alignas (16);
+ value_type v;
+};
+BV2<BVector> m2;
+
+#define SA(X) static_assert((X),#X)
+SA(alignof (BV2<BVector>::value_type) == 16);