[C++ PATCH] Stash bitfield width into DECL_BIT_FIELD_REPRESENTATIVE instead of DECL_INITIAL

Jakub Jelinek jakub@redhat.com
Thu Sep 28 18:49:00 GMT 2017


Hi!

On Wed, Sep 27, 2017 at 07:55:20AM -0700, Nathan Sidwell wrote:
> 	1) fix the parsing bug you found and move to (ab)using
> DECL_BIT_FIELD_REPRESENTATIVE

The following patch is the D_B_F_R part of the above.
Bootstrapped/regtested on top of the patch I've just posted, ok for trunk?

2017-09-28  Jakub Jelinek  <jakub@redhat.com>

c-family/
	* c-attribs.c (handle_packed_attribute): Test DECL_C_BIT_FIELD
	rather than DECL_INITIAL.
	(common_handle_aligned_attribute): Likewise.
c/
	* c-decl.c (grokfield): Use SET_DECL_C_BIT_FIELD here if
	with is non-NULL.
	(finish_struct): Test DECL_C_BIT_FIELD instead of DECL_INITIAL,
	don't SET_DECL_C_BIT_FIELD here.
cp/
	* class.c (check_bitfield_decl): Retrieve and clear width from
	DECL_BIT_FIELD_REPRESENTATIVE rather than DECL_INITIAL.
	(check_field_decls): Test DECL_BIT_FIELD_REPRESENTATIVE rather than
	DECL_INITIAL.
	(remove_zero_width_bit_fields): Adjust comment.
	* decl2.c (grokbitfield): Stash width into
	DECL_BIT_FIELD_REPRESENTATIVE rather than DECL_INITIAL.
	* pt.c (tsubst_decl): For DECL_C_BIT_FIELD, tsubst_expr
	DECL_BIT_FIELD_REPRESENTATIVE rather than DECL_INITIAL for width.
objc/
	* objc-act.c (check_ivars, gen_declaration): For OBJCPLUS look at
	DECL_BIT_FIELD_REPRESENTATIVE rather than DECL_INITIAL.

--- gcc/c-family/c-attribs.c.jj	2017-09-18 20:48:53.731871226 +0200
+++ gcc/c-family/c-attribs.c	2017-09-19 09:51:21.928612658 +0200
@@ -426,7 +426,7 @@ handle_packed_attribute (tree *node, tre
     {
       if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
 	  /* Still pack bitfields.  */
-	  && ! DECL_INITIAL (*node))
+	  && ! DECL_C_BIT_FIELD (*node))
 	warning (OPT_Wattributes,
 		 "%qE attribute ignored for field of type %qT",
 		 name, TREE_TYPE (*node));
@@ -1773,7 +1773,7 @@ common_handle_aligned_attribute (tree *n
     {
       if (warn_if_not_aligned_p)
 	{
-	  if (TREE_CODE (decl) == FIELD_DECL && !DECL_INITIAL (decl))
+	  if (TREE_CODE (decl) == FIELD_DECL && !DECL_C_BIT_FIELD (decl))
 	    {
 	      SET_DECL_WARN_IF_NOT_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
 	      warn_if_not_aligned_p = false;
--- gcc/c/c-decl.c.jj	2017-09-12 21:57:59.000000000 +0200
+++ gcc/c/c-decl.c	2017-09-19 10:43:30.898898784 +0200
@@ -7602,6 +7602,8 @@ grokfield (location_t loc,
 
   finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
   DECL_INITIAL (value) = width;
+  if (width)
+    SET_DECL_C_BIT_FIELD (value);
 
   if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
     {
@@ -7946,12 +7948,11 @@ finish_struct (location_t loc, tree t, t
       if (C_DECL_VARIABLE_SIZE (x))
 	C_TYPE_VARIABLE_SIZE (t) = 1;
 
-      if (DECL_INITIAL (x))
+      if (DECL_C_BIT_FIELD (x))
 	{
 	  unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
 	  DECL_SIZE (x) = bitsize_int (width);
 	  DECL_BIT_FIELD (x) = 1;
-	  SET_DECL_C_BIT_FIELD (x);
 	}
 
       if (TYPE_PACKED (t)
--- gcc/cp/class.c.jj	2017-09-18 20:48:53.509873991 +0200
+++ gcc/cp/class.c	2017-09-19 10:31:35.435961690 +0200
@@ -3231,12 +3231,12 @@ check_bitfield_decl (tree field)
   tree w;
 
   /* Extract the declared width of the bitfield, which has been
-     temporarily stashed in DECL_INITIAL.  */
-  w = DECL_INITIAL (field);
+     temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE.  */
+  w = DECL_BIT_FIELD_REPRESENTATIVE (field);
   gcc_assert (w != NULL_TREE);
   /* Remove the bit-field width indicator so that the rest of the
-     compiler does not treat that value as an initializer.  */
-  DECL_INITIAL (field) = NULL_TREE;
+     compiler does not treat that value as a qualifier.  */
+  DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
 
   /* Detect invalid bit-field type.  */
   if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
@@ -3571,7 +3571,8 @@ check_field_decls (tree t, tree *access_
 	    DECL_PACKED (x) = 1;
 	}
 
-      if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
+      if (DECL_C_BIT_FIELD (x)
+	  && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
 	/* We don't treat zero-width bitfields as making a class
 	   non-empty.  */
 	;
@@ -5268,9 +5269,9 @@ remove_zero_width_bit_fields (tree t)
     {
       if (TREE_CODE (*fieldsp) == FIELD_DECL
 	  && DECL_C_BIT_FIELD (*fieldsp)
-          /* We should not be confused by the fact that grokbitfield
+	  /* We should not be confused by the fact that grokbitfield
 	     temporarily sets the width of the bit field into
-	     DECL_INITIAL (*fieldsp).
+	     DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
 	     check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
 	     to that width.  */
 	  && (DECL_SIZE (*fieldsp) == NULL_TREE
--- gcc/cp/decl2.c.jj	2017-09-18 20:48:53.388875498 +0200
+++ gcc/cp/decl2.c	2017-09-19 10:31:45.066839694 +0200
@@ -1042,7 +1047,8 @@ grokbitfield (const cp_declarator *decla
 	       TREE_TYPE (width));
       else
 	{
-	  DECL_INITIAL (value) = width;
+	  /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.  */
+	  DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
 	  SET_DECL_C_BIT_FIELD (value);
 	}
     }
--- gcc/cp/pt.c.jj	2017-09-18 20:48:53.435874912 +0200
+++ gcc/cp/pt.c	2017-09-19 10:31:56.250698026 +0200
@@ -12809,14 +12809,13 @@ tsubst_decl (tree t, tree args, tsubst_f
 	    cp_apply_type_quals_to_decl (cp_type_quals (type), r);
 
 	    if (DECL_C_BIT_FIELD (r))
-	      /* For bit-fields, DECL_INITIAL gives the number of bits.  For
-		 non-bit-fields DECL_INITIAL is a non-static data member
-		 initializer, which gets deferred instantiation.  */
-	      DECL_INITIAL (r)
-		= tsubst_expr (DECL_INITIAL (t), args,
+	      /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
+		 number of bits.  */
+	      DECL_BIT_FIELD_REPRESENTATIVE (r)
+		= tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
 			       complain, in_decl,
 			       /*integral_constant_expression_p=*/true);
-	    else if (DECL_INITIAL (t))
+	    else if (DECL_INITIAL (t))
 	      {
 		/* Set up DECL_TEMPLATE_INFO so that we can get at the
 		   NSDMI in perform_member_init.  Still set DECL_INITIAL
--- gcc/objc/objc-act.c.jj	2017-01-01 12:45:46.000000000 +0100
+++ gcc/objc/objc-act.c	2017-09-19 13:01:06.917412755 +0200
@@ -4602,8 +4602,14 @@ check_ivars (tree inter, tree imp)
       t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
 
       if (!comptypes (t1, t2)
+#ifdef OBJCPLUS
+	  || !tree_int_cst_equal (DECL_BIT_FIELD_REPRESENTATIVE (intdecls),
+				  DECL_BIT_FIELD_REPRESENTATIVE (impdecls))
+#else
 	  || !tree_int_cst_equal (DECL_INITIAL (intdecls),
-				  DECL_INITIAL (impdecls)))
+				  DECL_INITIAL (impdecls))
+#endif
+	 )
 	{
 	  if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
 	    {
@@ -8895,10 +8901,14 @@ gen_declaration (tree decl)
 	  strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
 	}
 
-      if (DECL_INITIAL (decl)
-	  && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
+#ifdef OBJCPLUS
+      tree w = DECL_BIT_FIELD_REPRESENTATIVE (decl);
+#else
+      tree w = DECL_INITIAL (decl);
+#endif
+      if (w && TREE_CODE (w) == INTEGER_CST)
 	sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
-		 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
+		 TREE_INT_CST_LOW (w));
     }
 
   return errbuf;


	Jakub



More information about the Gcc-patches mailing list